Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何对具有不同常量的代码使用Extract方法?_Java_Eclipse_Refactoring - Fatal编程技术网

Java 如何对具有不同常量的代码使用Extract方法?

Java 如何对具有不同常量的代码使用Extract方法?,java,eclipse,refactoring,Java,Eclipse,Refactoring,假设我有如下代码: public void c(String str, int constant, boolean boolVal){ a(str, constant); b(str, boolVal); } void foo(){ String str=“hello”; a(str,1); b(str,真); a(str,2); b(str,假); } 我想提取一个新方法c如下: void foo(){ String str=“hello”; c(str,1,真); c(s

假设我有如下代码:

public void c(String str, int constant, boolean boolVal){
    a(str, constant);
    b(str, boolVal);
}
void foo(){
String str=“hello”;
a(str,1);
b(str,真);
a(str,2);
b(str,假);
}
我想提取一个新方法
c
如下:

void foo(){
String str=“hello”;
c(str,1,真);
c(str,2,假);
}
但是,自动提取方法重构将只提取
a
/
b
对中的一对。我的猜测是,它不喜欢不同的常数。我可以通过先提取一个局部变量,然后提取方法,然后内联以前提取的变量来解决这个问题,但我仍然需要手动查找所有实例。有了这么多的工作,当我看每一部分时,我不妨自己做完全的改变


是否有一个技巧我没有告诉Eclipse,要提取这种类型的代码,搜索得更难一些?

是的,您需要将常量提取到变量中,您可以将变量放在顶部,告诉您的工具将它们作为参数传递给提取的方法

void foo() {
  String str = "hello";
  int constant = 1;
  boolean boolValue = true;
  a(str, constant);
  b(str, boolValue);

  constant = 2;
  boolValue = false;
  a(str, constant);
  b(str, boolValue);
}
使用提取方法时应给出以下信息:

public void c(String str, int constant, boolean boolVal){
    a(str, constant);
    b(str, boolVal);
}

我不明白。Eclipse怎么可能知道您想要将第一个代码段转换为第二个代码段呢?我从来没有发现搜索和替换这样令人生畏的功能。@OliCharlesworth-我希望有一个类似的过程(首先突出显示
a
/
b
对,重构>提取方法,修改选项以指定不应提取
1
true
,而应将其视为参数)@BrianRoach-这是真的,如果这是正确的,我将使用Eclipse搜索/替换,在emacs中打开文件,或者使用一些命令行工具。然而,我试图给Eclipse一个公平的机会,并在这个过程中学习一些新的Eclipse技巧。:-)我继续并遵循了@BrianRoach的建议。我使用emacs来完成繁重的操作,然后跳回Eclipse来整理并确保所有Java片段都适合在一起。是的,我可以将常量提取到变量中,但我必须手动找到包含常量的所有方法调用。我试图在我的问题中解释这一点,但肯定不是很清楚。“我怎么能把我的问题改得更清楚呢?”谢普马斯特:哦,我明白了。恐怕您必须手动执行此操作,因为该工具无法区分方法
a
true
和方法
b
true
是-同样值得一提的是,如果您执行重构->提取局部变量,它将(如果选中此框)将指定范围内
true
的所有实例替换为任何变量。@PO中的WChargin没有提到此模式在他/她的代码基线上。我的回答适用于报告中提到的代码question@WChargin-打得好。然而,如果我沿着这条路走下去,我会在提取方法后立即重新内联提取的局部变量,因为这不是我真正的目标。这意味着,如果最初提取了更多的变量,这应该无关紧要。