Search in sources :

Example 11 with ASTClassOrInterfaceType

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

the class CouplingBetweenObjectsRule method visit.

@Override
public Object visit(ASTResultType node, Object data) {
    for (int x = 0; x < node.jjtGetNumChildren(); x++) {
        Node tNode = node.jjtGetChild(x);
        if (tNode instanceof ASTType) {
            Node reftypeNode = tNode.jjtGetChild(0);
            if (reftypeNode instanceof ASTReferenceType) {
                Node classOrIntType = reftypeNode.jjtGetChild(0);
                if (classOrIntType instanceof ASTClassOrInterfaceType) {
                    Node nameNode = classOrIntType;
                    this.checkVariableType(nameNode, nameNode.getImage());
                }
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 12 with ASTClassOrInterfaceType

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

the class CheckResultSetRule method visit.

@Override
public Object visit(ASTLocalVariableDeclaration node, Object data) {
    ASTClassOrInterfaceType type = node.getFirstChildOfType(ASTType.class).getFirstDescendantOfType(ASTClassOrInterfaceType.class);
    if (type != null && (type.getType() != null && "java.sql.ResultSet".equals(type.getType().getName()) || "ResultSet".equals(type.getImage()))) {
        ASTVariableDeclarator declarator = node.getFirstChildOfType(ASTVariableDeclarator.class);
        if (declarator != null) {
            ASTName name = declarator.getFirstDescendantOfType(ASTName.class);
            if (type.getType() != null || name != null && name.getImage().endsWith("executeQuery")) {
                ASTVariableDeclaratorId id = declarator.getFirstChildOfType(ASTVariableDeclaratorId.class);
                resultSetVariables.put(id.getImage(), node);
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTVariableDeclarator(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 13 with ASTClassOrInterfaceType

use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType 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 14 with ASTClassOrInterfaceType

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

the class ClassScope method determineSuper.

private SimpleTypedNameDeclaration determineSuper(Node declaringNode) {
    SimpleTypedNameDeclaration result = null;
    if (declaringNode instanceof ASTClassOrInterfaceDeclaration) {
        ASTClassOrInterfaceDeclaration classDeclaration = (ASTClassOrInterfaceDeclaration) declaringNode;
        ASTImplementsList implementsList = classDeclaration.getFirstChildOfType(ASTImplementsList.class);
        if (implementsList != null) {
            List<ASTClassOrInterfaceType> types = implementsList.findChildrenOfType(ASTClassOrInterfaceType.class);
            SimpleTypedNameDeclaration type = convertToSimpleType(types);
            result = type;
        }
        ASTExtendsList extendsList = classDeclaration.getFirstChildOfType(ASTExtendsList.class);
        if (extendsList != null) {
            List<ASTClassOrInterfaceType> types = extendsList.findChildrenOfType(ASTClassOrInterfaceType.class);
            SimpleTypedNameDeclaration type = convertToSimpleType(types);
            if (result == null) {
                result = type;
            } else {
                result.addNext(type);
            }
        }
    }
    return result;
}
Also used : ASTImplementsList(net.sourceforge.pmd.lang.java.ast.ASTImplementsList) ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList)

Example 15 with ASTClassOrInterfaceType

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

the class InefficientStringBufferingRule method isAllocatedStringBuffer.

private boolean isAllocatedStringBuffer(ASTAdditiveExpression node) {
    ASTAllocationExpression ao = node.getFirstParentOfType(ASTAllocationExpression.class);
    if (ao == null) {
        return false;
    }
    // note that the child can be an ArrayDimsAndInits, for example, from
    // java.lang.FloatingDecimal: t = new int[ nWords+wordcount+1 ];
    ASTClassOrInterfaceType an = ao.getFirstChildOfType(ASTClassOrInterfaceType.class);
    return an != null && TypeHelper.isEither(an, StringBuffer.class, StringBuilder.class);
}
Also used : ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Aggregations

ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)25 Node (net.sourceforge.pmd.lang.ast.Node)13 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)7 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)7 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)5 ASTImplementsList (net.sourceforge.pmd.lang.java.ast.ASTImplementsList)5 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)5 ArrayList (java.util.ArrayList)4 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)4 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)4 List (java.util.List)3 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)3 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)3 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)3 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)3 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)3 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)3 Map (java.util.Map)2 RuleContext (net.sourceforge.pmd.RuleContext)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2