Search in sources :

Example 6 with ASTMethod

use of net.sourceforge.pmd.lang.apex.ast.ASTMethod in project pmd by pmd.

the class MethodWithSameNameAsEnclosingClassRule method visit.

@Override
public Object visit(ASTUserClass node, Object data) {
    String className = node.getImage();
    List<ASTMethod> methods = node.findDescendantsOfType(ASTMethod.class);
    for (ASTMethod m : methods) {
        String methodName = m.getImage();
        if (!m.getNode().getMethodInfo().isConstructor() && methodName.equalsIgnoreCase(className)) {
            addViolation(data, m);
        }
    }
    return super.visit(node, data);
}
Also used : ASTMethod(net.sourceforge.pmd.lang.apex.ast.ASTMethod)

Example 7 with ASTMethod

use of net.sourceforge.pmd.lang.apex.ast.ASTMethod in project pmd by pmd.

the class ApexMetricsComputer method findOperations.

@Override
protected List<ASTMethod> findOperations(ASTUserClassOrInterface<?> node) {
    List<ASTMethod> candidates = node.findChildrenOfType(ASTMethod.class);
    List<ASTMethod> result = new ArrayList<>(candidates);
    for (ASTMethod method : candidates) {
        if (method.getImage().matches("(<clinit>|<init>|clone)")) {
            result.remove(method);
        }
    }
    return result;
}
Also used : ASTMethod(net.sourceforge.pmd.lang.apex.ast.ASTMethod) ArrayList(java.util.ArrayList)

Example 8 with ASTMethod

use of net.sourceforge.pmd.lang.apex.ast.ASTMethod in project pmd by pmd.

the class ApexCRUDViolationRule method visit.

@Override
public Object visit(ASTUserClass node, Object data) {
    if (Helper.isTestMethodOrClass(node) || Helper.isSystemLevelClass(node)) {
        // stops all the rules
        return data;
    }
    className = node.getImage();
    for (ASTMethod n : node.findDescendantsOfType(ASTMethod.class)) {
        StringBuilder sb = new StringBuilder().append(n.getNode().getDefiningType().getApexName()).append(":").append(n.getNode().getMethodInfo().getCanonicalName()).append(":").append(n.getNode().getMethodInfo().getParameterTypes().size());
        classMethods.put(sb.toString(), n);
    }
    return super.visit(node, data);
}
Also used : ASTMethod(net.sourceforge.pmd.lang.apex.ast.ASTMethod)

Example 9 with ASTMethod

use of net.sourceforge.pmd.lang.apex.ast.ASTMethod in project pmd by pmd.

the class ApexCRUDViolationRule method getPreviousMethodCalls.

private Set<ASTMethodCallExpression> getPreviousMethodCalls(final AbstractApexNode<?> self) {
    final Set<ASTMethodCallExpression> innerMethodCalls = new HashSet<>();
    final ASTMethod outerMethod = self.getFirstParentOfType(ASTMethod.class);
    if (outerMethod != null) {
        final ASTBlockStatement blockStatement = outerMethod.getFirstChildOfType(ASTBlockStatement.class);
        recursivelyEvaluateCRUDMethodCalls(self, innerMethodCalls, blockStatement);
        final List<ASTMethod> constructorMethods = findConstructorlMethods();
        for (ASTMethod method : constructorMethods) {
            innerMethodCalls.addAll(method.findDescendantsOfType(ASTMethodCallExpression.class));
        }
        // some methods might be within this class
        mapCallToMethodDecl(self, innerMethodCalls, new ArrayList<ASTMethodCallExpression>(innerMethodCalls));
    }
    return innerMethodCalls;
}
Also used : ASTMethod(net.sourceforge.pmd.lang.apex.ast.ASTMethod) ASTBlockStatement(net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement) ASTMethodCallExpression(net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression) HashSet(java.util.HashSet)

Example 10 with ASTMethod

use of net.sourceforge.pmd.lang.apex.ast.ASTMethod in project pmd by pmd.

the class ApexMultifileVisitorTest method testOperationsAreThere.

@Test
public void testOperationsAreThere() throws IOException {
    ApexNode<Compilation> acu = parseAndVisitForString(IOUtils.toString(ApexMultifileVisitorTest.class.getResourceAsStream("MetadataDeployController.cls")));
    final ApexSignatureMatcher toplevel = ApexProjectMirror.INSTANCE;
    final ApexOperationSigMask opMask = new ApexOperationSigMask();
    // We could parse qnames from string but probably simpler to do that
    acu.jjtAccept(new ApexParserVisitorAdapter() {

        @Override
        public Object visit(ASTMethod node, Object data) {
            if (!node.getImage().matches("(<clinit>|<init>|clone)")) {
                assertTrue(toplevel.hasMatchingSig(node.getQualifiedName(), opMask));
            }
            return data;
        }
    }, null);
}
Also used : Compilation(apex.jorje.semantic.ast.compilation.Compilation) ApexOperationSigMask(net.sourceforge.pmd.lang.apex.metrics.signature.ApexOperationSigMask) ASTMethod(net.sourceforge.pmd.lang.apex.ast.ASTMethod) ApexSignatureMatcher(net.sourceforge.pmd.lang.apex.metrics.ApexSignatureMatcher) ApexParserVisitorAdapter(net.sourceforge.pmd.lang.apex.ast.ApexParserVisitorAdapter) Test(org.junit.Test) ApexParserTest(net.sourceforge.pmd.lang.apex.ast.ApexParserTest)

Aggregations

ASTMethod (net.sourceforge.pmd.lang.apex.ast.ASTMethod)10 ASTMethodCallExpression (net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression)4 ASTUserClass (net.sourceforge.pmd.lang.apex.ast.ASTUserClass)3 ArrayList (java.util.ArrayList)2 ASTAssignmentExpression (net.sourceforge.pmd.lang.apex.ast.ASTAssignmentExpression)2 ASTVariableDeclaration (net.sourceforge.pmd.lang.apex.ast.ASTVariableDeclaration)2 ASTVariableExpression (net.sourceforge.pmd.lang.apex.ast.ASTVariableExpression)2 ApexParserVisitorAdapter (net.sourceforge.pmd.lang.apex.ast.ApexParserVisitorAdapter)2 Identifier (apex.jorje.data.Identifier)1 ClassTypeRef (apex.jorje.data.ast.TypeRefs.ClassTypeRef)1 Compilation (apex.jorje.semantic.ast.compilation.Compilation)1 HashSet (java.util.HashSet)1 ASTBlockStatement (net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement)1 ASTFieldDeclaration (net.sourceforge.pmd.lang.apex.ast.ASTFieldDeclaration)1 ASTNewKeyValueObjectExpression (net.sourceforge.pmd.lang.apex.ast.ASTNewKeyValueObjectExpression)1 ASTReturnStatement (net.sourceforge.pmd.lang.apex.ast.ASTReturnStatement)1 ApexParserTest (net.sourceforge.pmd.lang.apex.ast.ApexParserTest)1 ApexSignatureMatcher (net.sourceforge.pmd.lang.apex.metrics.ApexSignatureMatcher)1 ApexOperationSigMask (net.sourceforge.pmd.lang.apex.metrics.signature.ApexOperationSigMask)1 MetricMemoizer (net.sourceforge.pmd.lang.metrics.MetricMemoizer)1