How to create a Structural Search Inspection in IntelliJ IDEA
for Method Name length greater than 75 characters?
What I have tried:
Add Structural Search Inspection:
Search template:
public void $MethodName$() throws Exception {}
Target: MethodName
Add Modifier : Script = MethodName.length() > 75
(Java)
My actual code line which I want to be flagged:
verify_14_10_CARSUWRecoRolesNSecFilt_CopyPasteProfileWOutOfScopeMsgCheckasdas() throws Exception { ... }
When I run the inspection, the result says:
Code inspection did not find anything to report
What am I missing?
I am using IntelliJ Community Edition
for Java.
How to create a Structural Search Inspection in IntelliJ IDEA
for Method Name length greater than 75 characters?
What I have tried:
Add Structural Search Inspection:
Search template:
public void $MethodName$() throws Exception {}
Target: MethodName
Add Modifier : Script = MethodName.length() > 75
(Java)
My actual code line which I want to be flagged:
verify_14_10_CARSUWRecoRolesNSecFilt_CopyPasteProfileWOutOfScopeMsgCheckasdas() throws Exception { ... }
When I run the inspection, the result says:
Code inspection did not find anything to report
What am I missing?
I am using IntelliJ Community Edition
for Java.
2 Answers
Reset to default 2In your case, since you need to simply check for the length of method names, you could define a Structural Search Inspection with Regex Matching.
After selecting Structural Search
in User Defined
inspections, click on the plus icon and select Add Structural Search Inspection
. Choose the search template All methods of a class (within hierarchy)
, and add a Text
modifier for $Method$
that checks whether the method name's length is greater than 75
according to the specs of Java Identifiers.
To add a modifier for a certain element, click on the desired element or select it, and then enter the modifier in the text area on the right.
Search Template
class $Class$ {
$ReturnType$ $Method$ ($ParameterType$ $Parameter$);
}
Regex
[a-zA-Z$_][a-zA-Z0-9$_]{75,}
Preview
Use the template
$ReturnType$ $Method$($BeforeType$ $BeforeParameter$);
with the following modifiers:
$ReturnType$
:Count[0, 1]
,$Method$
: Script modifierMethod.name.length() > 75
,$BeforeParameter$
:Count[0, Unlimited]
.
In the Script modifier you can refer to the variables of the template, where the variables correspond to nodes in the PSI tree. This means that all methods from PsiElement are available. In particular, the method name implements the PsiNamedElement interface, giving us access to the getName()
method. Since the script is Groovy, this can be used as .name
.