Trying to create a custom inspection where the regex of one variable includes another variable

Answered

I'm creating a custom inspection. I've found the dialog where I can create a Replace template and all that. My ultimate goal is to create an inspection that shows a warning when a getter or setter method includes the Class or Superclass name within the method name. I copied this existing template, 

class $_1$ {
$ReturnType$ $Method$ ($ParameterType$ $Parameter$);
}

I edited $Method$ so that it matches the regex get[\w]*$_1$[\w]*|get[\w]*$_1$[\w]* This does not match methods that begin with get or set and also include the class or superclass name, as I would like. I think the issue is in it not matching $_1$, which I guess I cannot use in a regex. Any suggestions? Thanks.

 

0
1 comment
Official comment

You can achieve your goal by using a Script constraint. To start I would base this on the "methods of the class" existing template:

    $ReturnType$ $Method$($ParameterType$ $Parameter$);

 

Now edit $method$ to match Text/regexp 

(get|set).*

And add a Script constraint like: 

if (Method.name.contains(Method.containingClass.name)) return true;
Method.containingClass.supers.find { Method.name.contains(it.name) } != null

This is a little complicated but should get you the result you want.

Bas

Please sign in to leave a comment.