Search in sources :

Example 6 with ASTType

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

the class UselessStringValueOfRule method visit.

@Override
public Object visit(ASTPrimaryPrefix node, Object data) {
    if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
        return super.visit(node, data);
    }
    String image = ((ASTName) node.jjtGetChild(0)).getImage();
    if ("String.valueOf".equals(image)) {
        Node parent = node.jjtGetParent();
        if (parent.jjtGetNumChildren() != 2) {
            return super.visit(node, data);
        }
        // skip String.valueOf(anyarraytype[])
        ASTArgumentList args = parent.getFirstDescendantOfType(ASTArgumentList.class);
        if (args != null) {
            ASTName arg = args.getFirstDescendantOfType(ASTName.class);
            if (arg != null) {
                NameDeclaration declaration = arg.getNameDeclaration();
                if (declaration != null) {
                    ASTType argType = declaration.getNode().jjtGetParent().jjtGetParent().getFirstDescendantOfType(ASTType.class);
                    if (argType != null && argType.jjtGetChild(0) instanceof ASTReferenceType && ((ASTReferenceType) argType.jjtGetChild(0)).isArray()) {
                        return super.visit(node, data);
                    }
                }
            }
        }
        Node gp = parent.jjtGetParent();
        if (parent instanceof ASTPrimaryExpression && gp instanceof ASTAdditiveExpression && "+".equals(gp.getImage())) {
            boolean ok = false;
            if (gp.jjtGetChild(0) == parent) {
                ok = !isPrimitive(gp.jjtGetChild(1));
            } else {
                for (int i = 0; !ok && gp.jjtGetChild(i) != parent; i++) {
                    ok = !isPrimitive(gp.jjtGetChild(i));
                }
            }
            if (ok) {
                super.addViolation(data, node);
                return data;
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

Example 7 with ASTType

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

the class ClassScope method createBuiltInMethodDeclaration.

/**
 * Creates a fake method name declaration for built-in methods from Java
 * like the Enum Method "valueOf".
 *
 * @param methodName
 *            the method name
 * @param parameterTypes
 *            the reference types of each parameter of the method
 * @return a method name declaration
 */
private MethodNameDeclaration createBuiltInMethodDeclaration(final String methodName, final String... parameterTypes) {
    ASTMethodDeclaration methodDeclaration = new ASTMethodDeclaration(JavaParserTreeConstants.JJTMETHODDECLARATION);
    methodDeclaration.setPublic(true);
    methodDeclaration.setScope(this);
    ASTMethodDeclarator methodDeclarator = new ASTMethodDeclarator(JavaParserTreeConstants.JJTMETHODDECLARATOR);
    methodDeclarator.setImage(methodName);
    methodDeclarator.setScope(this);
    ASTFormalParameters formalParameters = new ASTFormalParameters(JavaParserTreeConstants.JJTFORMALPARAMETERS);
    formalParameters.setScope(this);
    methodDeclaration.jjtAddChild(methodDeclarator, 0);
    methodDeclarator.jjtSetParent(methodDeclaration);
    methodDeclarator.jjtAddChild(formalParameters, 0);
    formalParameters.jjtSetParent(methodDeclarator);
    /*
         * jjtAddChild resizes it's child node list according to known indexes.
         * Going backwards makes sure the first time it gets the right size avoiding copies.
         */
    for (int i = parameterTypes.length - 1; i >= 0; i--) {
        ASTFormalParameter formalParameter = new ASTFormalParameter(JavaParserTreeConstants.JJTFORMALPARAMETER);
        formalParameters.jjtAddChild(formalParameter, i);
        formalParameter.jjtSetParent(formalParameters);
        ASTVariableDeclaratorId variableDeclaratorId = new ASTVariableDeclaratorId(JavaParserTreeConstants.JJTVARIABLEDECLARATORID);
        variableDeclaratorId.setImage("arg" + i);
        formalParameter.jjtAddChild(variableDeclaratorId, 1);
        variableDeclaratorId.jjtSetParent(formalParameter);
        ASTType type = new ASTType(JavaParserTreeConstants.JJTTYPE);
        formalParameter.jjtAddChild(type, 0);
        type.jjtSetParent(formalParameter);
        if (PRIMITIVE_TYPES.contains(parameterTypes[i])) {
            ASTPrimitiveType primitiveType = new ASTPrimitiveType(JavaParserTreeConstants.JJTPRIMITIVETYPE);
            primitiveType.setImage(parameterTypes[i]);
            type.jjtAddChild(primitiveType, 0);
            primitiveType.jjtSetParent(type);
        } else {
            ASTReferenceType referenceType = new ASTReferenceType(JavaParserTreeConstants.JJTREFERENCETYPE);
            type.jjtAddChild(referenceType, 0);
            referenceType.jjtSetParent(type);
            // TODO : this could actually be a primitive array...
            ASTClassOrInterfaceType classOrInterfaceType = new ASTClassOrInterfaceType(JavaParserTreeConstants.JJTCLASSORINTERFACETYPE);
            classOrInterfaceType.setImage(parameterTypes[i]);
            referenceType.jjtAddChild(classOrInterfaceType, 0);
            classOrInterfaceType.jjtSetParent(referenceType);
        }
    }
    return new MethodNameDeclaration(methodDeclarator);
}
Also used : ASTMethodDeclarator(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator) ASTPrimitiveType(net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTFormalParameters(net.sourceforge.pmd.lang.java.ast.ASTFormalParameters) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 8 with ASTType

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

the class ClassTypeResolver method getTypeDefinitionOfVariableFromScope.

/**
 * Search for a field by it's image stating from a scope and taking into account if it's visible from the
 * accessingClass Class. The method takes into account that Nested inherited fields shadow outer scope fields.
 *
 * @param scope          The scope to start the search from.
 * @param image          The name of the field, local variable or method parameter.
 * @param accessingClass The Class (which is defined in the current ACU) that is trying to access the field.
 * @return Type def. of the field, or null if it could not be resolved.
 */
private JavaTypeDefinition getTypeDefinitionOfVariableFromScope(Scope scope, String image, Class<?> accessingClass) {
    if (accessingClass == null) {
        return null;
    }
    for (; /* empty */
    scope != null; scope = scope.getParent()) {
        // search each enclosing scope one by one
        for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : scope.getDeclarations(VariableNameDeclaration.class).entrySet()) {
            if (entry.getKey().getImage().equals(image)) {
                ASTType typeNode = entry.getKey().getDeclaratorId().getTypeNode();
                if (typeNode == null) {
                    // TODO : Type is infered, ie, this is a lambda such as (var) -> var.equals(other)
                    return null;
                }
                if (typeNode.jjtGetChild(0) instanceof ASTReferenceType) {
                    return ((TypeNode) typeNode.jjtGetChild(0)).getTypeDefinition();
                } else {
                    // primitive type
                    return JavaTypeDefinition.forClass(typeNode.getType());
                }
            }
        }
        // Nested class' inherited fields shadow enclosing variables
        if (scope instanceof ClassScope) {
            try {
                // get the superclass type def. ot the Class the ClassScope belongs to
                JavaTypeDefinition superClass = getSuperClassTypeDefinition(((ClassScope) scope).getClassDeclaration().getNode(), null);
                // TODO: check if anonymous classes are class scope
                // try searching this type def.
                JavaTypeDefinition foundTypeDef = getFieldType(superClass, image, accessingClass);
                if (foundTypeDef != null) {
                    // if null, then it's not an inherited field
                    return foundTypeDef;
                }
            } catch (ClassCastException ignored) {
            // if there is an anonymous class, getClassDeclaration().getType() will throw
            // TODO: maybe there is a better way to handle this, maybe this hides bugs
            }
        }
    }
    // will return null if not found
    return searchImportedStaticFields(image);
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) List(java.util.List) ArrayList(java.util.ArrayList) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) Map(java.util.Map) HashMap(java.util.HashMap) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope)

Example 9 with ASTType

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

the class VariableNameDeclaration method isArray.

public boolean isArray() {
    ASTVariableDeclaratorId astVariableDeclaratorId = (ASTVariableDeclaratorId) node;
    ASTType typeNode = astVariableDeclaratorId.getTypeNode();
    if (typeNode != null) {
        return ((Dimensionable) typeNode.jjtGetParent()).isArray();
    } else {
        return false;
    }
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) Dimensionable(net.sourceforge.pmd.lang.java.ast.Dimensionable)

Example 10 with ASTType

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

Aggregations

ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)19 Node (net.sourceforge.pmd.lang.ast.Node)8 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)8 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)6 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)5 ArrayList (java.util.ArrayList)4 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)4 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)4 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)3 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)3 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)3 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)2 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)2 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)2 ASTPrimitiveType (net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType)2 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)2 ASTTryStatement (net.sourceforge.pmd.lang.java.ast.ASTTryStatement)2 Dimensionable (net.sourceforge.pmd.lang.java.ast.Dimensionable)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2