use of com.github.javaparser.resolution.types.ResolvedReferenceType in project javaparser by javaparser.
the class GenericsResolutionTest method genericsInheritance.
@Test
public void genericsInheritance() {
CompilationUnit cu = parseSample("MethodTypeParams");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "VoidVisitorAdapter");
MethodDeclaration method = Navigator.demandMethod(clazz, "visit");
MethodCallExpr call = Navigator.findMethodCall(method, "accept").get();
Expression thisRef = call.getArguments().get(0);
TypeSolver typeSolver = new ReflectionTypeSolver();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
ResolvedType voidVisitorAdapterOfA = javaParserFacade.getType(thisRef);
List<ResolvedReferenceType> allAncestors = voidVisitorAdapterOfA.asReferenceType().getAllAncestors();
assertEquals(2, allAncestors.size());
}
use of com.github.javaparser.resolution.types.ResolvedReferenceType in project javaparser by javaparser.
the class JavaParserFacadeResolutionTest method typeDeclarationSuperClassImplicitlyIncludeObject.
@Test
public void typeDeclarationSuperClassImplicitlyIncludeObject() {
CompilationUnit cu = parseSample("Generics");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Generics");
ResolvedTypeDeclaration typeDeclaration = JavaParserFacade.get(new ReflectionTypeSolver()).getTypeDeclaration(clazz);
ResolvedReferenceType superclass = typeDeclaration.asClass().getSuperClass();
assertEquals(Object.class.getCanonicalName(), superclass.getQualifiedName());
}
use of com.github.javaparser.resolution.types.ResolvedReferenceType in project javaparser by javaparser.
the class MethodsResolutionLogicTest method compatibilityShouldConsiderAlsoTypeVariablesNegative.
@Test
public void compatibilityShouldConsiderAlsoTypeVariablesNegative() {
JavaParserClassDeclaration constructorDeclaration = (JavaParserClassDeclaration) typeSolver.solveType("com.github.javaparser.ast.body.ConstructorDeclaration");
ResolvedReferenceType stringType = (ResolvedReferenceType) ReflectionFactory.typeUsageFor(String.class, typeSolver);
ResolvedReferenceType rawClassType = (ResolvedReferenceType) ReflectionFactory.typeUsageFor(Class.class, typeSolver);
assertEquals(true, rawClassType.isRawType());
ResolvedReferenceType classOfStringType = (ResolvedReferenceType) rawClassType.replaceTypeVariables(rawClassType.getTypeDeclaration().getTypeParameters().get(0), stringType);
MethodUsage mu = constructorDeclaration.getAllMethods().stream().filter(m -> m.getDeclaration().getSignature().equals("isThrows(java.lang.Class<? extends java.lang.Throwable>)")).findFirst().get();
assertEquals(false, MethodResolutionLogic.isApplicable(mu, "isThrows", ImmutableList.of(classOfStringType), typeSolver));
}
use of com.github.javaparser.resolution.types.ResolvedReferenceType in project javaparser by javaparser.
the class MethodsResolutionLogicTest method compatibilityShouldConsiderAlsoTypeVariablesPositive.
@Test
public void compatibilityShouldConsiderAlsoTypeVariablesPositive() {
JavaParserClassDeclaration constructorDeclaration = (JavaParserClassDeclaration) typeSolver.solveType("com.github.javaparser.ast.body.ConstructorDeclaration");
ResolvedReferenceType runtimeException = (ResolvedReferenceType) ReflectionFactory.typeUsageFor(RuntimeException.class, typeSolver);
ResolvedReferenceType rawClassType = (ResolvedReferenceType) ReflectionFactory.typeUsageFor(Class.class, typeSolver);
ResolvedReferenceType classOfRuntimeType = (ResolvedReferenceType) rawClassType.replaceTypeVariables(rawClassType.getTypeDeclaration().getTypeParameters().get(0), runtimeException);
MethodUsage mu = constructorDeclaration.getAllMethods().stream().filter(m -> m.getDeclaration().getSignature().equals("isThrows(java.lang.Class<? extends java.lang.Throwable>)")).findFirst().get();
assertEquals(true, MethodResolutionLogic.isApplicable(mu, "isThrows", ImmutableList.of(classOfRuntimeType), typeSolver));
}
use of com.github.javaparser.resolution.types.ResolvedReferenceType in project javaparser by javaparser.
the class AnonymousClassDeclarationContext method solveType.
@Override
public SymbolReference<ResolvedTypeDeclaration> solveType(String name, TypeSolver typeSolver) {
List<com.github.javaparser.ast.body.TypeDeclaration> typeDeclarations = myDeclaration.findMembersOfKind(com.github.javaparser.ast.body.TypeDeclaration.class);
Optional<SymbolReference<ResolvedTypeDeclaration>> exactMatch = typeDeclarations.stream().filter(internalType -> internalType.getName().getId().equals(name)).findFirst().map(internalType -> SymbolReference.solved(JavaParserFacade.get(typeSolver).getTypeDeclaration(internalType)));
if (exactMatch.isPresent()) {
return exactMatch.get();
}
Optional<SymbolReference<ResolvedTypeDeclaration>> recursiveMatch = typeDeclarations.stream().filter(internalType -> name.startsWith(String.format("%s.", internalType.getName()))).findFirst().map(internalType -> JavaParserFactory.getContext(internalType, typeSolver).solveType(name.substring(internalType.getName().getId().length() + 1), typeSolver));
if (recursiveMatch.isPresent()) {
return recursiveMatch.get();
}
Optional<SymbolReference<ResolvedTypeDeclaration>> typeArgumentsMatch = wrappedNode.getTypeArguments().map(nodes -> ((NodeWithTypeArguments<?>) nodes).getTypeArguments().orElse(new NodeList<>())).orElse(new NodeList<>()).stream().filter(type -> type.toString().equals(name)).findFirst().map(matchingType -> SymbolReference.solved(new JavaParserTypeParameter(new TypeParameter(matchingType.toString()), typeSolver)));
if (typeArgumentsMatch.isPresent()) {
return typeArgumentsMatch.get();
}
// Look into extended classes and implemented interfaces
for (ResolvedReferenceType ancestor : myDeclaration.getAncestors()) {
// look at names of extended classes and implemented interfaces (this may not be important because they are checked in CompilationUnitContext)
if (ancestor.getTypeDeclaration().getName().equals(name)) {
return SymbolReference.solved(ancestor.getTypeDeclaration());
}
// look into internal types of extended classes and implemented interfaces
try {
for (ResolvedTypeDeclaration internalTypeDeclaration : ancestor.getTypeDeclaration().internalTypes()) {
if (internalTypeDeclaration.getName().equals(name)) {
return SymbolReference.solved(internalTypeDeclaration);
}
}
} catch (UnsupportedOperationException e) {
// just continue using the next ancestor
}
}
return getParent().solveType(name, typeSolver);
}
Aggregations