Search in sources :

Example 6 with ASTReferenceType

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

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

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

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

the class ConstructorCallsOverridableMethodRule method getMethodDeclaratorParameterTypes.

private static List<String> getMethodDeclaratorParameterTypes(Node methodOrConstructorDeclarator) {
    List<ASTFormalParameter> parameters = methodOrConstructorDeclarator.findDescendantsOfType(ASTFormalParameter.class);
    List<String> parameterTypes = new ArrayList<>();
    if (parameters != null) {
        for (ASTFormalParameter p : parameters) {
            ASTType type = p.getFirstChildOfType(ASTType.class);
            if (type.jjtGetChild(0) instanceof ASTPrimitiveType) {
                parameterTypes.add(type.jjtGetChild(0).getImage());
            } else if (type.jjtGetChild(0) instanceof ASTReferenceType) {
                parameterTypes.add("ref");
            } else {
                parameterTypes.add("<unkown>");
            }
        }
    }
    return parameterTypes;
}
Also used : ASTPrimitiveType(net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ArrayList(java.util.ArrayList) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType)

Aggregations

ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)9 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)8 Node (net.sourceforge.pmd.lang.ast.Node)5 ArrayList (java.util.ArrayList)4 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)4 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)3 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)3 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)3 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)2 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)2 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)2 ASTPrimitiveType (net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType)2 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)2 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)2 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)1