diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/AccessorClassGenerationRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/AccessorClassGenerationRule.java index 8683a2ce4e..0395bba3fc 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/AccessorClassGenerationRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/AccessorClassGenerationRule.java @@ -9,6 +9,7 @@ import java.util.List; import java.util.ListIterator; import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression; +import net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTArguments; import net.sourceforge.pmd.lang.java.ast.ASTArrayDimsAndInits; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; @@ -16,6 +17,8 @@ import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType; import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit; import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration; +import net.sourceforge.pmd.lang.java.ast.AbstractJavaAccessTypeNode; +import net.sourceforge.pmd.lang.java.ast.JavaNode; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; import net.sourceforge.pmd.lang.java.symboltable.SourceFileScope; @@ -152,67 +155,46 @@ public class AccessorClassGenerationRule extends AbstractJavaRule { } } + private boolean isToplevelType(JavaNode node) { + return node.jjtGetParent().jjtGetParent() instanceof ASTCompilationUnit; + } + /** * Outer interface visitation */ + @Override public Object visit(ASTClassOrInterfaceDeclaration node, Object data) { if (node.isInterface()) { - if (!(node.jjtGetParent().jjtGetParent() instanceof ASTCompilationUnit)) { - // not a top level interface - String interfaceName = node.getImage(); - int formerID = getClassID(); - setClassID(classDataList.size()); - ClassData newClassData = new ClassData(interfaceName); - // store the names of any outer classes of this class in the - // classQualifyingName List - ClassData formerClassData = classDataList.get(formerID); - newClassData.addClassQualifyingName(formerClassData.getClassName()); - classDataList.add(getClassID(), newClassData); - Object o = super.visit(node, data); - setClassID(formerID); - return o; - } else { - String interfaceName = node.getImage(); - classDataList.clear(); - setClassID(0); - classDataList.add(getClassID(), new ClassData(interfaceName)); - Object o = super.visit(node, data); - if (o != null) { - processRule(o); - } else { - processRule(data); - } - setClassID(-1); - return o; - } - } else if (!(node.jjtGetParent().jjtGetParent() instanceof ASTCompilationUnit)) { - // not a top level class - int formerID = getClassID(); - setClassID(classDataList.size()); - // TODO - // this is a hack to bail out here - // but I'm not sure why this is happening - // TODO - if (formerID == -1 || formerID >= classDataList.size()) { - return null; - } - // store the names of any outer classes of this class in the - // classQualifyingName List - ClassData formerClassData = classDataList.get(formerID); - String className = node.getImage(); - ClassData newClassData = new ClassData(className); - newClassData.addClassQualifyingName(formerClassData.getClassName()); - classDataList.add(getClassID(), newClassData); - Object o = super.visit(node, data); - setClassID(formerID); - return o; + return visitInterface(node, data); } - // outer classes + + if (!isToplevelType(node)) { + return handleInnerType(node, data); + } + return handleToplevelType(node, data); + } + + private Object visitInterface(ASTClassOrInterfaceDeclaration node, Object data) { + if (!isToplevelType(node)) { + return handleInnerType(node, data); + } + return handleToplevelType(node, data); + } + + @Override + public Object visit(ASTAnnotationTypeDeclaration node, Object data) { + if (!isToplevelType(node)) { + return handleInnerType(node, data); + } + return handleToplevelType(node, data); + } + + private Object handleToplevelType(AbstractJavaAccessTypeNode node, Object data) { if (!node.isStatic()) { // See bug# 1807370 - String className = node.getImage(); + String typeName = node.getImage(); classDataList.clear(); setClassID(0);// first class - classDataList.add(getClassID(), new ClassData(className)); + classDataList.add(getClassID(), new ClassData(typeName)); } Object o = super.visit(node, data); if (o != null && !node.isStatic()) { // See bug# 1807370 @@ -224,6 +206,21 @@ public class AccessorClassGenerationRule extends AbstractJavaRule { return o; } + private Object handleInnerType(AbstractJavaAccessTypeNode node, Object data) { + String typeName = node.getImage(); + int formerID = getClassID(); + setClassID(classDataList.size()); + ClassData newClassData = new ClassData(typeName); + // store the names of any outer classes of this class in the + // classQualifyingName List + ClassData formerClassData = classDataList.get(formerID); + newClassData.addClassQualifyingName(formerClassData.getClassName()); + classDataList.add(getClassID(), newClassData); + Object o = super.visit(node, data); + setClassID(formerID); + return o; + } + /** * Store all target constructors */ diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/AccessorClassGeneration.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/AccessorClassGeneration.xml index ed0ac00ae4..38c02df0d5 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/AccessorClassGeneration.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/AccessorClassGeneration.xml @@ -120,4 +120,12 @@ public class Foo1 { } ]]> --> + + + #1452 ArrayIndexOutOfBoundsException with Annotations for AccessorClassGenerationRule + 0 + + diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index 287c4abd5b..d6a0a4e135 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -16,12 +16,15 @@ **Pull Requests:** * [#27](https://github.com/adangel/pmd/pull/27): Added support for Raw String Literals (C++11). -* [#29)(https://github.com/adangel/pmd/pull/29): Added support for files with UTF-8 BOM to JSP tokenizer. +* [#29](https://github.com/adangel/pmd/pull/29): Added support for files with UTF-8 BOM to JSP tokenizer. * [#79](https://github.com/pmd/pmd/pull/79): do not flag public static void main(String[]) as UseVarargs; ignore @Override for UseVarargs * [#80](https://github.com/pmd/pmd/pull/80): Update mvn-plugin.md * [#83](https://github.com/pmd/pmd/pull/83): Adds new Code Climate-compliant JSON renderer **Bugfixes:** +* java-design/AccessorClassGeneration: + * [#1452](https://sourceforge.net/p/pmd/bugs/1452/): ArrayIndexOutOfBoundsException with Annotations for AccessorClassGenerationRule + **API Changes:**