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));
}
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));
}
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;
}
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);
}
}
}
}
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);
}
Aggregations