Identifying Struts2 Setters with a Custom Annotation

Identifying Struts2 Setters with a Custom Annotation

As a user (and avid fan) of IntelliJ, I benefit from a lot of great features in my Java IDE. One of those features is that IntelliJ will highlight code that it detects is unused. This works great in most cases, but it can’t detect code that is called reflectively at runtime.

One area that IntelliJ gets confused often is with setter methods on my Struts2 actions.

While poking around in the Inspections options in IntelliJ, I found a pretty cool way to get around this. First, I created a custom annotation, which I called @RequestParameter.

 * A marker annotation that indicates that a particular setter
 * will be called automatically by Struts to set a request
 * parameter.
 * <p>This annotation is intended to automatically suppress unused
 * code checks, since the method will be invoked by reflection
 * at runtime.
 * <p>This annotation must be registered as an entry point in the
 * IntelliJ inspections settings.
 * @author Steven Benitez
public @interface RequestParameter {

Then, I annotate all of my Struts setters with the @RequestParameter annotation.

Finally, I needed to register the annotation with IntelliJ to let it know that the annotation indicates that the code is used. To do that, go to the IntelliJ Settings > Inspections > Declaration Redundancy > Unused symbol inspection and click “Configure annotations.” Then just add your new annotation.

IntelliJ will no longer mark methods annotated as @RequestParameter as unused.