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;
This will be fixed with PMD 5.3.7, 5.4.2 and 5.5.0.
Commit: https://github.com/pmd/pmd/commit/3d84e3af7453fc02721c7161872c7db1be8fb12b
Last edit: Andreas Dangel 2016-04-30