use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.
the class GenericsResolutionTest method typeParamOnReturnType.
@Test
public void typeParamOnReturnType() {
CompilationUnit cu = parseSample("TypeParamOnReturnType");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "TypeParamOnReturnType");
MethodDeclaration method = Navigator.demandMethod(clazz, "nodeEquals");
ReturnStmt returnStmt = Navigator.findReturnStmt(method);
ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(returnStmt.getExpression().get());
assertEquals(false, type.isTypeVariable());
assertEquals("boolean", type.describe());
}
use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.
the class GenericsResolutionTest method resolveFieldOfGenericReferringToVariableType.
@Test
public void resolveFieldOfGenericReferringToVariableType() {
CompilationUnit cu = parseSample("Generics");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SomeCollection");
VariableDeclarator field = Navigator.demandField(clazz, "as");
SymbolSolver symbolSolver = new SymbolSolver(new ReflectionTypeSolver());
Optional<Value> symbolReference = symbolSolver.solveSymbolAsValue("as", field);
assertEquals(true, symbolReference.isPresent());
assertEquals("as", symbolReference.get().getName());
ResolvedType type = symbolReference.get().getType();
assertEquals(false, type.isTypeVariable());
assertEquals("java.util.List<A>", type.describe());
assertEquals(1, type.asReferenceType().typeParametersValues().size());
ResolvedType typeParam = type.asReferenceType().typeParametersValues().get(0);
assertEquals(true, typeParam.isTypeVariable());
assertEquals("A", typeParam.describe());
}
use of com.github.javaparser.resolution.types.ResolvedType 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.ResolvedType in project javaparser by javaparser.
the class GenericsResolutionTest method genericCollectionWithWildcards.
@Test
public void genericCollectionWithWildcards() {
CompilationUnit cu = parseSample("GenericCollection");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Foo");
MethodDeclaration method = Navigator.demandMethod(clazz, "bar");
ReturnStmt returnStmt = Navigator.findReturnStmt(method);
TypeSolver typeSolver = new ReflectionTypeSolver();
Expression returnStmtExpr = returnStmt.getExpression().get();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
ResolvedType type = javaParserFacade.getType(returnStmtExpr);
assertEquals(false, type.isTypeVariable());
assertEquals("boolean", type.describe());
}
use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.
the class GenericsResolutionTest method resolveFieldOfVariableType.
@Test
public void resolveFieldOfVariableType() {
CompilationUnit cu = parseSample("Generics");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SomeCollection");
VariableDeclarator field = Navigator.demandField(clazz, "a");
SymbolSolver symbolSolver = new SymbolSolver(new ReflectionTypeSolver());
Optional<Value> symbolReference = symbolSolver.solveSymbolAsValue("a", field);
assertEquals(true, symbolReference.isPresent());
assertEquals("a", symbolReference.get().getName());
ResolvedType type = symbolReference.get().getType();
assertEquals(true, type.isTypeVariable());
assertEquals("A", type.describe());
}
Aggregations