use of net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration in project pmd by pmd.
the class AccessorClassGenerationRule method visit.
@Override
public Object visit(final ASTAllocationExpression node, final Object data) {
if (node.jjtGetChild(0) instanceof ASTClassOrInterfaceType) {
// Ignore primitives
final ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) node.jjtGetChild(0);
final List<ASTConstructorDeclaration> constructors = privateConstructors.get(type.getImage());
if (constructors != null) {
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;
}
}
}
}
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration in project pmd by pmd.
the class UseUtilityClassRule method visit.
@Override
public Object visit(ASTClassOrInterfaceBody decl, Object data) {
if (decl.jjtGetParent() instanceof ASTClassOrInterfaceDeclaration) {
ASTClassOrInterfaceDeclaration parent = (ASTClassOrInterfaceDeclaration) decl.jjtGetParent();
if (parent.isAbstract() || parent.isInterface() || isExceptionType(parent)) {
return super.visit(decl, data);
}
if (isOkUsingLombok(parent)) {
return super.visit(decl, data);
}
int i = decl.jjtGetNumChildren();
int methodCount = 0;
boolean isOK = false;
while (i > 0) {
Node p = decl.jjtGetChild(--i);
if (p.jjtGetNumChildren() == 0) {
continue;
}
Node n = skipAnnotations(p);
if (n instanceof ASTFieldDeclaration) {
if (!((ASTFieldDeclaration) n).isStatic()) {
isOK = true;
break;
}
} else if (n instanceof ASTConstructorDeclaration) {
if (((ASTConstructorDeclaration) n).isPrivate()) {
isOK = true;
break;
}
} else if (n instanceof ASTMethodDeclaration) {
ASTMethodDeclaration m = (ASTMethodDeclaration) n;
if (!m.isPrivate()) {
methodCount++;
}
if (!m.isStatic()) {
isOK = true;
break;
}
// TODO use symbol table
if (m.getMethodName().equals("suite")) {
ASTResultType res = m.getResultType();
ASTClassOrInterfaceType c = res.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (c != null && c.hasImageEqualTo("Test")) {
isOK = true;
break;
}
}
}
}
if (!isOK && methodCount > 0) {
addViolation(data, decl);
}
}
return super.visit(decl, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration in project pmd by pmd.
the class StatementAndBraceFinderTest method testOnlyWorksForMethodsAndConstructors.
@Test(expected = RuntimeException.class)
public void testOnlyWorksForMethodsAndConstructors() {
StatementAndBraceFinder sbf = new StatementAndBraceFinder(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler().getDataFlowHandler());
sbf.buildDataFlowFor(new ASTMethodDeclaration(1));
sbf.buildDataFlowFor(new ASTConstructorDeclaration(1));
sbf.buildDataFlowFor(new ASTCompilationUnit(1));
}
use of net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration in project pmd by pmd.
the class SigMaskTest method testOperationRoles.
@Test
public void testOperationRoles() {
List<ASTMethodOrConstructorDeclaration> nodes = getOrderedNodes(ASTMethodOrConstructorDeclaration.class, TEST_OPERATIONS);
JavaOperationSigMask mask = new JavaOperationSigMask();
mask.restrictRolesTo(Role.STATIC);
mask.coverAbstract();
for (ASTMethodOrConstructorDeclaration node : nodes) {
if (node.isStatic()) {
assertTrue(mask.covers(JavaOperationSignature.buildFor(node)));
} else {
assertFalse(mask.covers(JavaOperationSignature.buildFor(node)));
}
}
mask.restrictRolesTo(Role.CONSTRUCTOR);
for (ASTMethodOrConstructorDeclaration node : nodes) {
if (node instanceof ASTConstructorDeclaration) {
assertTrue(mask.covers(JavaOperationSignature.buildFor(node)));
} else {
assertFalse(mask.covers(JavaOperationSignature.buildFor(node)));
}
}
mask.restrictRolesTo(Role.GETTER_OR_SETTER);
for (ASTMethodOrConstructorDeclaration node : nodes) {
if (node instanceof ASTMethodDeclaration && ((ASTMethodDeclaration) node).getMethodName().matches("(get|set).*")) {
assertTrue(mask.covers(JavaOperationSignature.buildFor(node)));
} else {
assertFalse(mask.covers(JavaOperationSignature.buildFor(node)));
}
}
mask.restrictRolesTo(Role.METHOD);
for (ASTMethodOrConstructorDeclaration node : nodes) {
if (node instanceof ASTMethodDeclaration && !node.isStatic() && !((ASTMethodDeclaration) node).getMethodName().matches("(get|set).*")) {
assertTrue(mask.covers(JavaOperationSignature.buildFor(node)));
} else {
assertFalse(mask.covers(JavaOperationSignature.buildFor(node)));
}
}
}
Aggregations