#1467 [java] UseUtilityClass: can't correctly check functions with multiple annotations

PMD-5.3.7
closed
java (55)
PMD
3-Major
Bug
UseUtilityClass
2016-06-25
2016-03-24
No

The UseUtilityClass is being triggered on the following example:

public class AccountFragment extends Fragment {

  public static AccountFragment newInstance() {
    AccountFragment instance = new AccountFragment();
    //OTHER STUFF
    return instance;
  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    return inflater.inflate(R.layout.mylayout, container, false);
  }

}

According to PMD check UseUtilityClass: All methods are static. Consider using a utility class instead. Alternatively, you could add a private constructor or make the class abstract to silence this warning.

As you can see, this is not true,

After checking the rule source coude I've found this on https://github.com/pmd/pmd/blob/master/pmd-java%2Fsrc%2Fmain%2Fjava%2Fnet%2Fsourceforge%2Fpmd%2Flang%2Fjava%2Frule%2Fdesign%2FUseUtilityClassRule.java#L36 :

Node n = p.jjtGetChild(0);
                 if (n instanceof ASTAnnotation) {
                     n = p.jjtGetChild(1);
                 }

This code assumes that in case of having an annotation the next item will be the function, but this assumption fails in case of having more than one annotation. This if should be replace by a loop looking for the next non-annotation node;

Discussion

  • Andreas Dangel

    Andreas Dangel - 2016-04-01
    • status: open --> in-progress
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-5.3.7
     
  • Andreas Dangel

    Andreas Dangel - 2016-04-01
    • status: in-progress --> closed
     
  • Andreas Dangel

    Andreas Dangel - 2016-06-25
    • labels: annotations, false positive --> java
    • summary: UseUtilityClass can't correctly check functions with multiple annotations --> [java] UseUtilityClass: can't correctly check functions with multiple annotations
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks