Search in sources :

Example 56 with MethodUsage

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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) MethodUsage(com.github.javaparser.resolution.MethodUsage) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 57 with MethodUsage

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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 58 with MethodUsage

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()));
}
Also used : MethodCallExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.MethodCallExprContext) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ArrayList(java.util.ArrayList) MethodUsage(com.github.javaparser.resolution.MethodUsage) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 59 with MethodUsage

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());
}
Also used : Context(com.github.javaparser.symbolsolver.core.resolution.Context) MethodCallExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.MethodCallExprContext) MethodCallExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.MethodCallExprContext) MethodUsage(com.github.javaparser.resolution.MethodUsage) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Aggregations

MethodUsage (com.github.javaparser.resolution.MethodUsage)59 Test (org.junit.Test)39 CompilationUnit (com.github.javaparser.ast.CompilationUnit)36 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)33 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)27 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)24 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)20 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)19 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)14 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)12 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)12 Context (com.github.javaparser.symbolsolver.core.resolution.Context)12 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)11 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)7 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)6 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)6 ClassOrInterfaceDeclarationContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext)6 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)6 InferenceContext (com.github.javaparser.symbolsolver.logic.InferenceContext)6 UnsolvedSymbolException (com.github.javaparser.resolution.UnsolvedSymbolException)5