use of com.github.javaparser.ast.body.VariableDeclarator 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.ast.body.VariableDeclarator 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());
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class LambdaExprContextResolutionTest method solveParameterOfLambdaInFieldDecl.
@Test
public void solveParameterOfLambdaInFieldDecl() {
CompilationUnit cu = parseSample("Lambda");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
VariableDeclarator field = Navigator.demandField(clazz, "functional");
LambdaExpr lambdaExpr = (LambdaExpr) field.getInitializer().get();
File src = new File("src/test/resources");
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new ReflectionTypeSolver());
combinedTypeSolver.add(new JavaParserTypeSolver(adaptPath(src)));
Context context = new LambdaExprContext(lambdaExpr, combinedTypeSolver);
Optional<Value> ref = context.solveSymbolAsValue("p", typeSolver);
assertTrue(ref.isPresent());
assertEquals("java.lang.String", ref.get().getType().describe());
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class LambdaExprContextResolutionTest method solveParameterOfLambdaInVarDecl.
@Test
public void solveParameterOfLambdaInVarDecl() {
CompilationUnit cu = parseSample("Lambda");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
MethodDeclaration method = Navigator.demandMethod(clazz, "testFunctionalVar");
VariableDeclarator varDecl = Navigator.demandVariableDeclaration(method, "a").get();
LambdaExpr lambdaExpr = (LambdaExpr) varDecl.getInitializer().get();
File src = adaptPath(new File("src/test/resources"));
CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new ReflectionTypeSolver());
combinedTypeSolver.add(new JavaParserTypeSolver(src));
Context context = new LambdaExprContext(lambdaExpr, combinedTypeSolver);
Optional<Value> ref = context.solveSymbolAsValue("p", typeSolver);
assertTrue(ref.isPresent());
assertEquals("java.lang.String", ref.get().getType().describe());
}
use of com.github.javaparser.ast.body.VariableDeclarator in project javaparser by javaparser.
the class SymbolResolutionResolutionTest method getTypeOfFieldAccess.
@Test
public void getTypeOfFieldAccess() {
CompilationUnit cu = parseSample("ReflectionFieldOfItself");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MyClass");
VariableDeclarator field = Navigator.demandField(clazz, "PUBLIC");
TypeSolver typeSolver = new ReflectionTypeSolver();
ResolvedType ref = JavaParserFacade.get(typeSolver).getType(field.getInitializer().get());
assertEquals("int", ref.describe());
}
Aggregations