Merge branch 'bug-1467' into pmd/5.4.x

This commit is contained in:
Andreas Dangel
2016-04-01 21:30:45 +02:00
3 changed files with 35 additions and 4 deletions

View File

@ -32,10 +32,7 @@ public class UseUtilityClassRule extends AbstractJavaRule {
if (p.jjtGetNumChildren() == 0) {
continue;
}
Node n = p.jjtGetChild(0);
if (n instanceof ASTAnnotation) {
n = p.jjtGetChild(1);
}
Node n = skipAnnotations(p);
if (n instanceof ASTFieldDeclaration) {
if (!((ASTFieldDeclaration) n).isStatic()) {
isOK = true;
@ -75,6 +72,15 @@ public class UseUtilityClassRule extends AbstractJavaRule {
return super.visit(decl, data);
}
private Node skipAnnotations(Node p) {
int index = 0;
Node n = p.jjtGetChild(index++);
while (n instanceof ASTAnnotation && index < p.jjtGetNumChildren()) {
n = p.jjtGetChild(index++);
}
return n;
}
private boolean isExceptionType(ASTClassOrInterfaceDeclaration parent) {
ASTExtendsList extendsList = parent.getFirstChildOfType(ASTExtendsList.class);
if (extendsList != null) {

View File

@ -209,6 +209,29 @@ public class MyException extends RuntimeException {
protected static String foo() {
return "foo";
}
}
]]></code>
</test-code>
<test-code>
<description>#1467 UseUtilityClass can't correctly check functions with multiple annotations</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
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);
}
}
]]></code>
</test-code>