Search in sources :

Example 6 with ASTConstructorDeclaration

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;
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope)

Example 7 with ASTConstructorDeclaration

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);
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ASTResultType(net.sourceforge.pmd.lang.java.ast.ASTResultType) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 8 with ASTConstructorDeclaration

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));
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Test(org.junit.Test)

Example 9 with ASTConstructorDeclaration

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)));
        }
    }
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) JavaOperationSigMask(net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSigMask) ASTMethodOrConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration) Test(org.junit.Test)

Aggregations

ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)9 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)6 Node (net.sourceforge.pmd.lang.ast.Node)4 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)3 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)3 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Linker (net.sourceforge.pmd.lang.dfa.Linker)1 LinkerException (net.sourceforge.pmd.lang.dfa.LinkerException)1 SequenceException (net.sourceforge.pmd.lang.dfa.SequenceException)1 Structure (net.sourceforge.pmd.lang.dfa.Structure)1 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)1 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)1 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)1 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)1 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)1