use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class ContextTest method resolveReferenceToJreType.
@Test
public void resolveReferenceToJreType() {
CompilationUnit cu = parseSample("NavigatorSimplified");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(clazz, "foo");
com.github.javaparser.ast.type.Type streamJavaParserType = method.getParameters().get(0).getType();
TypeSolver typeSolver = new ReflectionTypeSolver();
ResolvedType streamType = JavaParserFacade.get(typeSolver).convert(streamJavaParserType, method);
assertEquals("java.util.stream.Stream<java.lang.String>", streamType.describe());
}
use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class ContextTest method resolveReferenceToClassInJavaLang.
@Test
public void resolveReferenceToClassInJavaLang() {
CompilationUnit cu = parseSample("Navigator");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(referencesToField, "findType");
Parameter param = method.getParameters().get(1);
ResolvedClassDeclaration stringDecl = mock(ResolvedClassDeclaration.class);
when(stringDecl.getName()).thenReturn("String");
when(stringDecl.getQualifiedName()).thenReturn("java.lang.String");
TypeSolver typeSolver = mock(TypeSolver.class);
when(typeSolver.tryToSolveType("me.tomassetti.symbolsolver.javaparser.String")).thenReturn(SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class));
when(typeSolver.getRoot()).thenReturn(typeSolver);
when(typeSolver.solveType("java.lang.Object")).thenReturn(new ReflectionClassDeclaration(Object.class, typeSolver));
when(typeSolver.tryToSolveType("java.lang.String")).thenReturn(SymbolReference.solved(stringDecl));
SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
SymbolReference<? extends ResolvedTypeDeclaration> ref = symbolSolver.solveType("String", param);
assertEquals(true, ref.isSolved());
assertEquals("String", ref.getCorrespondingDeclaration().getName());
assertEquals("java.lang.String", ref.getCorrespondingDeclaration().getQualifiedName());
}
use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class ContextTest method resolveGenericReturnTypeOfMethodInJar.
@Test
public void resolveGenericReturnTypeOfMethodInJar() throws ParseException, IOException {
CompilationUnit cu = parseSample("Navigator");
com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
MethodCallExpr call = Navigator.findMethodCall(method, "getTypes").get();
String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
MethodUsage methodUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(call);
assertEquals("getTypes", methodUsage.getName());
assertEquals("java.util.List<com.github.javaparser.ast.body.TypeDeclaration>", methodUsage.returnType().describe());
assertEquals(1, methodUsage.returnType().asReferenceType().typeParametersValues().size());
assertEquals("com.github.javaparser.ast.body.TypeDeclaration", methodUsage.returnType().asReferenceType().typeParametersValues().get(0).describe());
}
use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class GenericsResolutionTest method genericCollectionWithWildcardsAndExtensions.
/*@Test
public void genericCollectionWithWildcardsAndExtensionsPrep() {
CompilationUnit cu = parseSample("GenericCollectionWithExtension");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Foo");
MethodDeclaration method = Navigator.demandMethod(clazz, "bar");
ReturnStmt returnStmt = Navigator.findReturnStmt(method);
TypeSolver typeSolver = new JreTypeSolver();
MethodCallExpr call = (MethodCallExpr) returnStmt.getExpr();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
List<TypeUsage> params = new ArrayList<>();
if (call.getArgs() != null) {
for (Expression param : call.getArgs()) {
params.add(javaParserFacade.getType(param, false));
}
}
Context context = JavaParserFactory.getContext(call, typeSolver);
ReferenceTypeUsage typeOfScope = javaParserFacade.getType(call.getScope()).asReferenceType();
me.tomassetti.symbolsolver.model.declarations.TypeDeclaration typeDeclaration = typeOfScope.getTypeDeclaration();
List<TypeUsage> typeParametersValues = typeOfScope.typeParametersValues();
List<MethodUsage> methods = new ArrayList<>();
for (Method m : List.class.getMethods()) {
if (m.getName().equals("addAll") && !m.isBridge() && !m.isSynthetic()) {
me.tomassetti.symbolsolver.model.declarations.MethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(m, typeSolver);
if (methods.size() == 0) {
// ok, e' il primo
ReferenceTypeUsage paramType = methodDeclaration.getParam(0).getType(typeSolver).asReferenceType();
assertTrue(paramType.asReferenceType().typeParametersValues().get(0).isWildcard());
}
MethodUsage mu = new MethodUsage(methodDeclaration, typeSolver);
int i = 0;
for (TypeParameter tp : typeDeclaration.getTypeParameters()) {
mu = mu.replaceTypeParameter(tp.getName(), typeParametersValues.get(i));
i++;
}
methods.add(mu);
}
}
assertTrue(MethodResolutionLogic.isApplicable(methods.get(0), "addAll", params, typeSolver));
//Optional<MethodUsage> methodUsage = MethodResolutionLogic.findMostApplicableUsage(methods, "addAll", params, typeSolver);
//Optional<MethodUsage> methodUsage = typeDeclaration.solveMethodAsUsage("addAll", params, typeSolver, context, typeParametersValues);
//Optional<MethodUsage> methodUsage = context.solveMethodAsUsage("addAll", params, typeSolver);
//assertTrue(methodUsage.isPresent());
}*/
@Test
public void genericCollectionWithWildcardsAndExtensions() {
CompilationUnit cu = parseSample("GenericCollectionWithExtension");
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.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.
the class ReflectionMethodDeclarationTest method testGetGenericParameters.
@Test
public void testGetGenericParameters() {
TypeSolver typeResolver = new ReflectionTypeSolver();
ResolvedInterfaceDeclaration map = new ReflectionInterfaceDeclaration(Map.class, typeResolver);
ResolvedMethodDeclaration put = map.getAllMethods().stream().filter(m -> m.getName().equals("put")).findFirst().get().getDeclaration();
assertEquals(true, put.getParam(0).getType().isTypeVariable());
assertEquals(true, put.getParam(0).getType().asTypeParameter().declaredOnType());
assertEquals("java.util.Map.K", put.getParam(0).getType().asTypeParameter().getQualifiedName());
assertEquals(true, put.getParam(1).getType().isTypeVariable());
assertEquals(true, put.getParam(1).getType().asTypeParameter().declaredOnType());
assertEquals("java.util.Map.V", put.getParam(1).getType().asTypeParameter().getQualifiedName());
}
Aggregations