use of com.github.javaparser.resolution.MethodUsage 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.resolution.MethodUsage in project javaparser by javaparser.
the class GenericsResolutionTest method resolveUsageOfMethodOfGenericClass.
@Test
public void resolveUsageOfMethodOfGenericClass() {
CompilationUnit cu = parseSample("Generics");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "GenericMethodCalls.Derived");
MethodDeclaration method = Navigator.demandMethod(clazz, "caller");
MethodCallExpr expression = Navigator.findMethodCall(method, "callee").get();
MethodUsage methodUsage = JavaParserFacade.get(new ReflectionTypeSolver()).solveMethodAsUsage(expression);
assertEquals("callee", methodUsage.getName());
}
use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.
the class MethodCallExprContextResolutionTest method assertCanSolveGenericMethodCallCanInferFromArguments.
private void assertCanSolveGenericMethodCallCanInferFromArguments(String callMethodName) {
MethodCallExpr methodCallExpr = getMethodCallExpr("genericMethodTest", callMethodName);
CombinedTypeSolver typeSolver = createTypeSolver();
MethodCallExprContext context = new MethodCallExprContext(methodCallExpr, typeSolver);
ResolvedReferenceTypeDeclaration stringType = typeSolver.solveType("java.lang.String");
List<ResolvedType> argumentsTypes = new ArrayList<>();
argumentsTypes.add(new ReferenceTypeImpl(stringType, typeSolver));
Optional<MethodUsage> ref = context.solveMethodAsUsage(callMethodName, argumentsTypes, typeSolver);
assertTrue(ref.isPresent());
assertEquals("MethodCalls", ref.get().declaringType().getQualifiedName());
assertEquals(Collections.singletonList("java.lang.String"), ref.get().typeParametersMap().getTypes().stream().map(ty -> ty.asReferenceType().describe()).collect(Collectors.toList()));
}
use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.
the class MethodCallExprContextResolutionTest method solveNestedMethodCallExprContextWithoutScope.
@Test
public void solveNestedMethodCallExprContextWithoutScope() {
MethodCallExpr methodCallExpr = getMethodCallExpr("bar1", "foo");
CombinedTypeSolver typeSolver = createTypeSolver();
Context context = new MethodCallExprContext(methodCallExpr, typeSolver);
Optional<MethodUsage> ref = context.solveMethodAsUsage("foo", Collections.emptyList(), typeSolver);
assertTrue(ref.isPresent());
assertEquals("MethodCalls", ref.get().declaringType().getQualifiedName());
}
Aggregations