[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>