Intellij idea Intellij结构替换更改方法签名

Intellij idea Intellij结构替换更改方法签名,intellij-idea,structural-search,Intellij Idea,Structural Search,我想更改具有特定签名的方法: public OldReturnType get.*(Params) { //lots of code } 为此: public NewReturnTyp get.*(Params) { //lots of code } ->我想更换退货类型。我用SSR试过这个: 复制现有模板“类的方法”。这将生成模板: 类$class${ $ReturnType$$MethodName$($ParameterType$$Parameter$); } 我首先尝试分别

我想更改具有特定签名的方法:

public OldReturnType get.*(Params) {
  //lots of code
}
为此:

public NewReturnTyp get.*(Params) {
  //lots of code
}
->我想更换退货类型。我用SSR试过这个:

  • 复制现有模板“类的方法”。这将生成模板:

    类$class${ $ReturnType$$MethodName$($ParameterType$$Parameter$); }

我首先尝试分别替换$ReturnType$OldType和NewType:

搜索模板:

class $Class$ { 
  OldType $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  NewType $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  $OldType$ $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  NewType $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  OldType $MethodName$($ParameterType$ $Parameter$) {
    $statement$;
  }

  $other$;
}
替换模板:

class $Class$ { 
  OldType $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  NewType $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  $OldType$ $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  NewType $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  OldType $MethodName$($ParameterType$ $Parameter$) {
    $statement$;
  }

  $other$;
}
这给了我所有的方法,但是如果我替换它,这个方法就会被删除

然后,我尝试通过以下方式改变模式:

搜索模板:

class $Class$ { 
  OldType $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  NewType $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  $OldType$ $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  NewType $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  OldType $MethodName$($ParameterType$ $Parameter$) {
    $statement$;
  }

  $other$;
}
并指定,$OldType$应该是搜索的目标。我还为此变量指定了一个RegEx模式:OldType

替换模板:

class $Class$ { 
  OldType $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  NewType $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  $OldType$ $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  NewType $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  OldType $MethodName$($ParameterType$ $Parameter$) {
    $statement$;
  }

  $other$;
}

这也会找到所有的方法,但是如果我替换它们,它们就会被删除。我尝试了非常不同的方法,总是得到相同的结果:如果替换匹配项,该方法将被删除。如何指定搜索/替换模板,以便在签名中仅将OldType替换为NewType。

您在结构化搜索和替换中遇到了一个错误。可能是这个:

但有一个解决办法。使用以下搜索模板:

class $Class$ { 
  OldType $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  NewType $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  $OldType$ $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  NewType $MethodName$($ParameterType$ $Parameter$);
}
class $Class$ { 
  OldType $MethodName$($ParameterType$ $Parameter$) {
    $statement$;
  }

  $other$;
}
确保未设置$MethodName$的“此变量是搜索目标”,否则它将无法工作

$statement$
最小计数:0
最大计数:无限制

$other$
最小计数:0
最大计数:无限制

其余部分与类现有模板的方法相同

替换模板与搜索模板相同,只是NewType替换了OldType


让我知道它是否有效,或者您是否还有其他问题。

有点:问题是,现在一些参数失去了它们的最终修饰符,因此我在匿名内部类中遇到了一些编译器错误。但是我现在是手工做的,我想你提到的错误是我发誓的原因(-)。我会接受你的答案,因为我认为这对下一个想这样做的人来说是一个好的开始,但对我来说,问题已经解决了。我认为你的方法签名行中需要类似的东西:$accessModifier$$static$OldType$MethodName$($ParameterType$$Parameter$){并将最小/最大计数分别设置为0和1。@mish在SSR中不可能使用变量作为修饰符。模板始终需要是有效的可编译java代码(片段)。啊,好的,感谢您的澄清-但肯定是一个缺点。