[java] Fix ClassCastException in AccessorClassGeneration

- Fixes #352
This commit is contained in:
Juan Martín Sotuyo Dodero
2017-04-15 10:07:49 -03:00
parent 24a648e032
commit 98b4b96c4c
3 changed files with 37 additions and 13 deletions

View File

@ -72,19 +72,22 @@ public class AccessorClassGenerationRule extends AbstractJavaRule {
final List<ASTConstructorDeclaration> constructors = privateConstructors.get(type.getImage());
if (constructors != null) {
final ASTArguments callArguments = (ASTArguments) node.jjtGetChild(1);
final ClassScope enclosingScope = node.getScope().getEnclosingScope(ClassScope.class);
for (final ASTConstructorDeclaration cd : constructors) {
// Are we within the same class scope?
if (cd.getScope().getEnclosingScope(ClassScope.class) == enclosingScope) {
break;
}
if (cd.getParameterCount() == callArguments.getArgumentCount()) {
// TODO : Check types
addViolation(data, node);
break;
final ASTArguments callArguments = node.getFirstChildOfType(ASTArguments.class);
// Is this really a constructor call and not an array?
if (callArguments != null) {
final ClassScope enclosingScope = node.getScope().getEnclosingScope(ClassScope.class);
for (final ASTConstructorDeclaration cd : constructors) {
// Are we within the same class scope?
if (cd.getScope().getEnclosingScope(ClassScope.class) == enclosingScope) {
break;
}
if (cd.getParameterCount() == callArguments.getArgumentCount()) {
// TODO : Check types
addViolation(data, node);
break;
}
}
}
}

View File

@ -128,6 +128,26 @@ public class Example extends View {
}
});
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Array initializer is not a class body
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
private class Bar {
private int size;
private Bar() {
}
void bar() {
new Bar[size];
}
}
}
]]></code>
</test-code>

View File

@ -453,6 +453,7 @@ You need to use this, if you have a large project with many files, and you hit t
* [#275](https://github.com/pmd/pmd/issues/275): \[java] FinalFieldCouldBeStatic: Constant in @interface incorrectly reported as "could be made static"
* [#282](https://github.com/pmd/pmd/issues/282): \[java] UnnecessaryLocalBeforeReturn false positive when cloning Maps
* [#291](https://github.com/pmd/pmd/issues/291): \[java] Improve quality of AccessorClassGeneration
* [#352](https://github.com/pmd/pmd/issues/352): \[java] AccessorClassGeneration throws ClassCastException when seeing array construction
* java-imports
* [#338](https://github.com/pmd/pmd/issues/338): \[java] False positive on DontImportJavaLang when importing java.lang.ProcessBuilder
* [#339](https://github.com/pmd/pmd/issues/339): \[java] False positive on DontImportJavaLang when importing Java 7's java.lang.invoke.MethodHandles