I'm trying to figure out a Structural replacement template in IntelliJ IDEA (future inspection) for adding a missing annotation to a class/interface/enum without removing any other existing annotations.
So far I tried several varieties of following
Search template:
@$OtherPossibleAnnotation$ // min: 0, max: 0, text/regexp: AnnotationName, invert condition: true
@$MissingAnnotation$ // min: 0, max: 0, text/regexp: AnnotationName
class $C$ {
$Content$ // min: 0, max: unlimited
}
Replacement template:
@$OtherPossibleAnnotation$
@AnnotationName
class $C$ {
$Content$
}
But none of my attempts worked. I expect it to work
So for instance following
@ProductArea("Area A")
class A {
public void method1() {
System.out.println("Should remain");
}
}
should be replaced with
@ProductArea("Area A")
@AnnotationName
class A {
public void method1() {
System.out.println("Should remain");
}
}
Could someone please advise?
The following works for me in IntelliJ IDEA 2019.3 EAP.
Search Template:@$MissingAnnotation$ // count=[0,0], text=AnnotationName
class $C$ {
}
Replace Template
@AnnotationName
class $C$ {
}
This will keep existing annotations and class content, but only when using IntelliJ IDEA 2019.3 EAP
To save some manual editing, copy the following to your clipboard and use "Import Template from Clipboard" under the cog menu in the Structural Search dialog:
<replaceConfiguration name="method calls" text="@$MissingAnnotation$ class $C$ { }" recursive="false" caseInsensitive="true" type="JAVA" pattern_context="default" reformatAccordingToStyle="false" shortenFQN="true" replacement="@AnnotationName class $C$ { }">
<constraint name="__context__" within="" contains="" />
<constraint name="MissingAnnotation" regexp="AnnotationName" minCount="0" maxCount="0" within="" contains="" />
<constraint name="C" within="" contains="" />
</replaceConfiguration>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With