Search in sources :

Example 6 with ASTMethodDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration in project pmd by pmd.

the class StructureTest method testAddResultsinDFANodeContainingAddedNode.

@Test
public void testAddResultsinDFANodeContainingAddedNode() {
    Structure s = new Structure(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler().getDataFlowHandler());
    Node n = new ASTMethodDeclaration(1);
    assertEquals(n, s.createNewNode(n).getNode());
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) Structure(net.sourceforge.pmd.lang.dfa.Structure) Test(org.junit.Test)

Example 7 with ASTMethodDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration in project pmd by pmd.

the class SignatureTest method testGetterDetection.

@Test
public void testGetterDetection() {
    ASTCompilationUnit compilationUnit = parseJava17(GetterDetection.class);
    compilationUnit.jjtAccept(new JavaParserVisitorAdapter() {

        @Override
        public Object visit(ASTMethodDeclaration node, Object data) {
            assertEquals(Role.GETTER_OR_SETTER, Role.get(node));
            return data;
        }
    }, null);
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) JavaParserVisitorAdapter(net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter) Test(org.junit.Test)

Example 8 with ASTMethodDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration in project pmd by pmd.

the class JavaMultifileVisitorTest method testOperationsAreThere.

@Test
public void testOperationsAreThere() {
    ASTCompilationUnit acu = parseAndVisitForClass(MultifileVisitorTestData2.class);
    final ProjectMirror toplevel = PackageStats.INSTANCE;
    final JavaOperationSigMask opMask = new JavaOperationSigMask();
    // We could parse qnames from string but probably simpler to do that
    acu.jjtAccept(new JavaParserVisitorAdapter() {

        @Override
        public Object visit(ASTMethodDeclaration node, Object data) {
            assertTrue(toplevel.hasMatchingSig(node.getQualifiedName(), opMask));
            return data;
        }
    }, null);
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) JavaOperationSigMask(net.sourceforge.pmd.lang.java.multifile.signature.JavaOperationSigMask) JavaParserVisitorAdapter(net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter) Test(org.junit.Test)

Example 9 with ASTMethodDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration in project pmd by pmd.

the class JUnitUseExpectedRule method visit.

@Override
public Object visit(ASTClassOrInterfaceBodyDeclaration node, Object data) {
    boolean inAnnotation = false;
    for (int i = 0; i < node.jjtGetNumChildren(); i++) {
        Node child = node.jjtGetChild(i);
        if (child instanceof ASTAnnotation) {
            ASTName annotationName = child.getFirstDescendantOfType(ASTName.class);
            if ("Test".equals(annotationName.getImage())) {
                inAnnotation = true;
                continue;
            }
        }
        if (child instanceof ASTMethodDeclaration) {
            boolean isJUnitMethod = isJUnitMethod((ASTMethodDeclaration) child, data);
            if (inAnnotation || isJUnitMethod) {
                List<Node> found = new ArrayList<>();
                found.addAll((List<Node>) visit((ASTMethodDeclaration) child, data));
                for (Node name : found) {
                    addViolation(data, name);
                }
            }
        }
        inAnnotation = false;
    }
    return super.visit(node, data);
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTAnnotation(net.sourceforge.pmd.lang.java.ast.ASTAnnotation) ArrayList(java.util.ArrayList)

Example 10 with ASTMethodDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration in project pmd by pmd.

the class UnusedPrivateMethodRule method calledFromOutsideItself.

private boolean calledFromOutsideItself(List<NameOccurrence> occs, NameDeclaration mnd) {
    int callsFromOutsideMethod = 0;
    for (NameOccurrence occ : occs) {
        Node occNode = occ.getLocation();
        ASTConstructorDeclaration enclosingConstructor = occNode.getFirstParentOfType(ASTConstructorDeclaration.class);
        if (enclosingConstructor != null) {
            callsFromOutsideMethod++;
            // Do we miss unused private constructors here?
            break;
        }
        ASTInitializer enclosingInitializer = occNode.getFirstParentOfType(ASTInitializer.class);
        if (enclosingInitializer != null) {
            callsFromOutsideMethod++;
            break;
        }
        ASTMethodDeclaration enclosingMethod = occNode.getFirstParentOfType(ASTMethodDeclaration.class);
        if (enclosingMethod == null || !mnd.getNode().jjtGetParent().equals(enclosingMethod)) {
            callsFromOutsideMethod++;
        }
    }
    return callsFromOutsideMethod == 0;
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ASTInitializer(net.sourceforge.pmd.lang.java.ast.ASTInitializer) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Aggregations

ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)31 Test (org.junit.Test)15 Node (net.sourceforge.pmd.lang.ast.Node)8 List (java.util.List)6 Map (java.util.Map)5 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)5 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)5 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)5 ArrayList (java.util.ArrayList)4 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)4 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)3 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)3 ASTFieldDeclaration (net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)3 JavaParserVisitorAdapter (net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter)3 RuleContext (net.sourceforge.pmd.RuleContext)2 Structure (net.sourceforge.pmd.lang.dfa.Structure)2 ASTPrimitiveType (net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType)2 ASTResultType (net.sourceforge.pmd.lang.java.ast.ASTResultType)2 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)2 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)2