Search in sources :

Example 16 with MethodUsage

use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.

the class ContextTest method resolveReferenceToLambdaParamSimplified.

@Test
public void resolveReferenceToLambdaParamSimplified() {
    CompilationUnit cu = parseSample("NavigatorSimplified");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
    MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
    MethodCallExpr call = Navigator.findMethodCall(method, "isEmpty").get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
    MethodUsage ref = symbolSolver.solveMethod("isEmpty", Collections.emptyList(), call);
    assertEquals("isEmpty", ref.getName());
    assertEquals("java.lang.String", ref.declaringType().getQualifiedName());
}
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 17 with MethodUsage

use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.

the class ContextTest method resolveTypeUsageOfMethodInGenericClass.

@Test
public void resolveTypeUsageOfMethodInGenericClass() 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 callToStream = Navigator.findMethodCall(method, "stream").get();
    String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
    TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
    MethodUsage filterUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(callToStream);
    assertEquals("java.util.stream.Stream<com.github.javaparser.ast.body.TypeDeclaration>", filterUsage.returnType().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 18 with MethodUsage

use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.

the class ContextTest method resolveReferenceToCallOnLambdaParam.

@Test
public void resolveReferenceToCallOnLambdaParam() 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 callToGetName = Navigator.findMethodCall(method, "getName").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(callToGetName);
    assertEquals("getName", methodUsage.getName());
    assertEquals("com.github.javaparser.ast.body.TypeDeclaration", methodUsage.declaringType().getQualifiedName());
}
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 19 with MethodUsage

use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.

the class GenericsResolutionTest method methodWithGenericParameterTypes.

@Test
public void methodWithGenericParameterTypes() {
    CompilationUnit cu = parseSample("GenericCollectionWithExtension");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Foo");
    MethodDeclaration method = Navigator.demandMethod(clazz, "bar");
    MethodCallExpr methodCall = Navigator.findMethodCall(method, "foo").get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    MethodUsage methodUsage = javaParserFacade.solveMethodAsUsage(methodCall);
    assertEquals("foo", methodUsage.getName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) 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 20 with MethodUsage

use of com.github.javaparser.resolution.MethodUsage in project javaparser by javaparser.

the class VariadicResolutionTest method resolveVariadicMethodWithGenericArgument.

@Test
public void resolveVariadicMethodWithGenericArgument() {
    CompilationUnit cu = parseSample("MethodCalls");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MethodCalls");
    MethodDeclaration method = Navigator.demandMethod(clazz, "genericMethodTest");
    MethodCallExpr callExpr = Navigator.findMethodCall(method, "variadicWithGenericArg").get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    MethodUsage callee = javaParserFacade.solveMethodAsUsage(callExpr);
    assertEquals("variadicWithGenericArg", callee.getName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) 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)

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