简体   繁体   English

在eclipse中提取方法似乎不起作用

[英]Extracting a method in eclipse doesn't seem to work

I try to do a simple refactoring (extract method) in eclipse. 我尝试在eclipse中进行简单的重构(提取方法)。 I select the following code block and do a alt+shift+M (shortcut for extracting). 我选择以下代码块并执行alt + shift + M(提取快捷方式)。

    Parser parser = Parser.createParser(comment.getText(), "UTF-8");
    NodeList htmlAnchorNodes = null;
    try {
        htmlAnchorNodes = parser
                .extractAllNodesThatMatch(new TagNameFilter("a"));
    } catch (ParserException e) {
        e.printStackTrace();
    }

    int size = htmlAnchorNodes.size();

Only the size variable is used afterwards. 之后只使用size变量。

I get the error message: 我收到错误消息:

Ambiguous return value: Selected block contains more than one assignment to local variables. 不明确的返回值:所选块包含多个局部变量赋值。 Affected variables are: 受影响的变量是:

NodeList htmlAnchorNodes NodeList htmlAnchorNodes

Parser parser 解析器解析器

int size int size

How can I get Eclipse to recognize the return value? 如何让Eclipse识别返回值?

In some cases Eclipse isn't sure which variable you intend to return. 在某些情况下,Eclipse不确定您要返回哪个变量。 It would be ideal if it would prompt you to select one, or do an analysis based on which value is actually used later, but I've worked around it by using braces to limit the scope of the temporary values before extracting the method. 如果它会提示您选择一个,或者根据以后实际使用的值进行分析,那将是理想的,但我在解压缩方法之前使用大括号来限制临时值的范围。

With your code, I would change from 有了你的代码,我会改变

Parser parser = Parser.createParser(comment.getText(), "UTF-8");
NodeList htmlAnchorNodes = null;
try {
    htmlAnchorNodes = parser
            .extractAllNodesThatMatch(new TagNameFilter("a"));
} catch (ParserException e) {
    e.printStackTrace();
}

int size = htmlAnchorNodes.size();

to

int size;

{
    Parser parser = Parser.createParser(comment.getText(), "UTF-8");
    NodeList htmlAnchorNodes = null;
    try {
        htmlAnchorNodes = parser
                .extractAllNodesThatMatch(new TagNameFilter("a"));
    } catch (ParserException e) {
        e.printStackTrace();
    }
    size = htmlAnchorNodes.size();
}

The only assignment which has an effect outside of the scope of the braces is the change to size , which should resolve the ambiguity for the refactoring tool. 在大括号范围之外产生影响的唯一赋值是size的改变,这应该解决重构工具的模糊性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM