Search in sources :

Example 11 with ASTMethodDeclaration

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

the class AccessorMethodGenerationRule method analyzeScope.

private void analyzeScope(final AbstractJavaScope file, final Object data) {
    for (final ClassNameDeclaration classDecl : file.getDeclarations(ClassNameDeclaration.class).keySet()) {
        final ClassScope classScope = (ClassScope) classDecl.getScope();
        // Check fields
        for (final Map.Entry<VariableNameDeclaration, List<NameOccurrence>> varDecl : classScope.getVariableDeclarations().entrySet()) {
            final ASTFieldDeclaration field = varDecl.getKey().getNode().getFirstParentOfType(ASTFieldDeclaration.class);
            analyzeMember(field, varDecl.getValue(), classScope, data);
        }
        // Check methods
        for (final Map.Entry<MethodNameDeclaration, List<NameOccurrence>> methodDecl : classScope.getMethodDeclarations().entrySet()) {
            final ASTMethodDeclaration method = methodDecl.getKey().getNode().getFirstParentOfType(ASTMethodDeclaration.class);
            analyzeMember(method, methodDecl.getValue(), classScope, data);
        }
        // Check inner classes
        analyzeScope(classScope, data);
    }
}
Also used : MethodNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ClassNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.ClassNameDeclaration) List(java.util.List) Map(java.util.Map) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope)

Example 12 with ASTMethodDeclaration

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

the class AbstractStructureInspectorPage method showMethodToViolation.

/**
 * Shows the method that belongs to a violation (to a line).
 *
 * @param violation
 *            RuleViolation
 */
protected void showMethodToViolation(RuleViolation violation) {
    final int beginLine = violation.getBeginLine();
    for (int i = 0; i < pmdMethodList.size(); i++) {
        ASTMethodDeclaration pmdMethod = pmdMethodList.get(i);
        if (beginLine >= pmdMethod.getBeginLine() && beginLine <= pmdMethod.getEndLine()) {
            showMethod(pmdMethod);
            // select the method in the combobox
            methodSelector.select(i);
            return;
        }
    }
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Point(org.eclipse.swt.graphics.Point)

Example 13 with ASTMethodDeclaration

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

the class AbstractStructureInspectorPage method refreshPMDMethods.

/**
 * Refreshes the list of PMD methods for the combobox.
 *
 * @see #getPMDMethods(IResource)
 */
protected void refreshPMDMethods() {
    methodSelector.removeAll();
    pmdMethodList = getPMDMethods();
    for (ASTMethodDeclaration pmdMethod : pmdMethodList) {
        methodSelector.add(ASTUtil.getMethodLabel(pmdMethod, false));
    }
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)

Example 14 with ASTMethodDeclaration

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

the class UnsynchronizedStaticDateFormatterRule method visit.

@Override
public Object visit(ASTFieldDeclaration node, Object data) {
    if (!node.isStatic()) {
        return data;
    }
    ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
    if (cit == null || !targets.contains(cit.getImage())) {
        return data;
    }
    ASTVariableDeclaratorId var = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
    for (NameOccurrence occ : var.getUsages()) {
        Node n = occ.getLocation();
        if (n.getFirstParentOfType(ASTSynchronizedStatement.class) != null) {
            continue;
        }
        // ignore usages, that don't call a method.
        if (!n.getImage().contains(".")) {
            continue;
        }
        ASTMethodDeclaration method = n.getFirstParentOfType(ASTMethodDeclaration.class);
        if (method != null && !method.isSynchronized()) {
            addViolation(data, n);
        }
    }
    return data;
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) Node(net.sourceforge.pmd.lang.ast.Node) ASTSynchronizedStatement(net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 15 with ASTMethodDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration 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)

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