Search in sources :

Example 1 with JavaTypeDefinition

use of net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition in project pmd by pmd.

the class ClassTypeResolverTest method testJavaTypeDefinitionGetErasedSuperTypeSet.

@Test
public void testJavaTypeDefinitionGetErasedSuperTypeSet() {
    JavaTypeDefinition originalTypeDef = forClass(List.class, forClass(Integer.class));
    Set<Class<?>> set = originalTypeDef.getErasedSuperTypeSet();
    assertEquals(set.size(), 4);
    assertTrue(set.contains(Object.class));
    assertTrue(set.contains(Collection.class));
    assertTrue(set.contains(Iterable.class));
    assertTrue(set.contains(List.class));
}
Also used : JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) Collection(java.util.Collection) AnonymousInnerClass(net.sourceforge.pmd.typeresolution.testdata.AnonymousInnerClass) GenericClass(net.sourceforge.pmd.typeresolution.testdata.dummytypes.GenericClass) EnumWithAnonymousInnerClass(net.sourceforge.pmd.typeresolution.testdata.EnumWithAnonymousInnerClass) NestedAnonymousClass(net.sourceforge.pmd.typeresolution.testdata.NestedAnonymousClass) JavaTypeDefinition.forClass(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition.forClass) InnerClass(net.sourceforge.pmd.typeresolution.testdata.InnerClass) ExtraTopLevelClass(net.sourceforge.pmd.typeresolution.testdata.ExtraTopLevelClass) AnoymousExtendingObject(net.sourceforge.pmd.typeresolution.testdata.AnoymousExtendingObject) ArrayList(java.util.ArrayList) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) List(java.util.List) Test(org.junit.Test)

Example 2 with JavaTypeDefinition

use of net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition in project pmd by pmd.

the class TypeInferenceTest method testResolution.

@Test
public void testResolution() {
    List<Bound> bounds = new ArrayList<>();
    bounds.add(new Bound(JavaTypeDefinition.forClass(SuperClassA.class), alpha, SUBTYPE));
    bounds.add(new Bound(JavaTypeDefinition.forClass(SuperClassAOther.class), alpha, SUBTYPE));
    Map<Variable, JavaTypeDefinition> result = TypeInferenceResolver.resolveVariables(bounds);
    assertEquals(1, result.size());
    assertEquals(JavaTypeDefinition.forClass(SuperClassA2.class), result.get(alpha));
}
Also used : SuperClassA2(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA2) Variable(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable) JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) Bound(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Bound) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with JavaTypeDefinition

use of net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition in project pmd by pmd.

the class ClassTypeResolver method visit.

@Override
public Object visit(ASTWildcardBounds node, Object data) {
    super.visit(node, data);
    JavaTypeDefinition childType = ((TypeNode) node.jjtGetChild(0)).getTypeDefinition();
    if (node.jjtGetFirstToken().toString().equals("super")) {
        node.setTypeDefinition(JavaTypeDefinition.forClass(LOWER_WILDCARD, childType));
    } else {
        // equals "extends"
        node.setTypeDefinition(JavaTypeDefinition.forClass(UPPER_WILDCARD, childType));
    }
    return data;
}
Also used : JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)

Example 4 with JavaTypeDefinition

use of net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition in project pmd by pmd.

the class ClassTypeResolver method populateImports.

/**
 * If the outer class wasn't found then we'll get in here
 *
 * @param node
 */
private void populateImports(ASTCompilationUnit node) {
    List<ASTImportDeclaration> theImportDeclarations = node.findChildrenOfType(ASTImportDeclaration.class);
    importedClasses.putAll(JAVA_LANG);
    // go through the imports
    for (ASTImportDeclaration anImportDeclaration : theImportDeclarations) {
        String strPackage = anImportDeclaration.getPackageName();
        if (anImportDeclaration.isStatic()) {
            if (anImportDeclaration.isImportOnDemand()) {
                importOnDemandStaticClasses.add(JavaTypeDefinition.forClass(loadClass(strPackage)));
            } else {
                // not import on-demand
                String strName = anImportDeclaration.getImportedName();
                String fieldName = strName.substring(strName.lastIndexOf('.') + 1);
                Class<?> staticClassWithField = loadClass(strPackage);
                if (staticClassWithField != null) {
                    JavaTypeDefinition typeDef = getFieldType(JavaTypeDefinition.forClass(staticClassWithField), fieldName, currentAcu.getType());
                    staticFieldImageToTypeDef.put(fieldName, typeDef);
                }
                List<JavaTypeDefinition> typeList = staticNamesToClasses.get(fieldName);
                if (typeList == null) {
                    typeList = new ArrayList<>();
                }
                typeList.add(JavaTypeDefinition.forClass(staticClassWithField));
                staticNamesToClasses.put(fieldName, typeList);
            }
        } else {
            // non-static
            if (anImportDeclaration.isImportOnDemand()) {
                importedOnDemand.add(strPackage);
            } else {
                // not import on-demand
                String strName = anImportDeclaration.getImportedName();
                importedClasses.put(strName, strName);
                importedClasses.put(strName.substring(strPackage.length() + 1), strName);
            }
        }
    }
}
Also used : ASTImportDeclaration(net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration) JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition)

Example 5 with JavaTypeDefinition

use of net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition 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)

Aggregations

JavaTypeDefinition (net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition)19 ArrayList (java.util.ArrayList)11 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)6 Test (org.junit.Test)6 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)5 List (java.util.List)4 TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)4 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)3 Method (java.lang.reflect.Method)2 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)2 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)2 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)2 ASTTypeArguments (net.sourceforge.pmd.lang.java.ast.ASTTypeArguments)2 Bound (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Bound)2 Variable (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable)2 AnoymousExtendingObject (net.sourceforge.pmd.typeresolution.testdata.AnoymousExtendingObject)2 SuperClassA2 (net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA2)2 Type (java.lang.reflect.Type)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1