Search in sources :

Example 16 with MethodCallExpr

use of com.github.javaparser.ast.expr.MethodCallExpr in project javaparser by javaparser.

the class ContextTest method resolveLambdaType.

@Test
public void resolveLambdaType() 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 callToFilter = Navigator.findMethodCall(method, "filter").get();
    Expression lambdaExpr = callToFilter.getArguments().get(0);
    String pathToJar = adaptPath("src/test/resources/javaparser-core-2.1.0.jar");
    TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JarTypeSolver(pathToJar));
    ResolvedType typeOfLambdaExpr = JavaParserFacade.get(typeSolver).getType(lambdaExpr);
    assertEquals("java.util.function.Predicate<? super com.github.javaparser.ast.body.TypeDeclaration>", typeOfLambdaExpr.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Expression(com.github.javaparser.ast.expr.Expression) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 17 with MethodCallExpr

use of com.github.javaparser.ast.expr.MethodCallExpr in project javaparser by javaparser.

the class ContextTest method resolveReferenceToOverloadMethodFindOnlyCompatible.

@Test
public void resolveReferenceToOverloadMethodFindOnlyCompatible() {
    CompilationUnit cu = parseSample("OverloadedMethods");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "OverloadedMethods");
    MethodDeclaration method = Navigator.demandMethod(clazz, "m3");
    MethodCallExpr call = Navigator.findMethodCall(method, "overloaded").get();
    ReflectionTypeSolver typeSolver = new ReflectionTypeSolver();
    MethodUsage ref = JavaParserFacade.get(typeSolver).solveMethodAsUsage(call);
    assertEquals("overloaded", ref.getName());
    assertEquals(1, ref.getNoParams());
    assertEquals("java.lang.Object", ref.getParamTypes().get(0).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) 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 MethodCallExpr

use of com.github.javaparser.ast.expr.MethodCallExpr in project javaparser by javaparser.

the class ContextTest method resolveInheritedMethodFromInterface.

@Test
public void resolveInheritedMethodFromInterface() {
    CompilationUnit cu = parseSample("InterfaceInheritance");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Test");
    MethodDeclaration method = Navigator.demandMethod(clazz, "test");
    MethodCallExpr call = Navigator.findMethodCall(method, "foobar").get();
    File src = adaptPath(new File("src/test/resources"));
    TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JavaParserTypeSolver(src));
    ResolvedType type = JavaParserFacade.get(typeSolver).getType(call);
    assertEquals("double", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) File(java.io.File) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 19 with MethodCallExpr

use of com.github.javaparser.ast.expr.MethodCallExpr in project javaparser by javaparser.

the class ContextTest method resolveReferenceToMethodCalledOnArrayAccess.

@Test
public void resolveReferenceToMethodCalledOnArrayAccess() {
    CompilationUnit cu = parseSample("ArrayAccess");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "ArrayAccess");
    MethodDeclaration method = Navigator.demandMethod(clazz, "access");
    MethodCallExpr callToTrim = Navigator.findMethodCall(method, "trim").get();
    File src = adaptPath(new File("src/test/resources"));
    TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JavaParserTypeSolver(src));
    SymbolSolver symbolSolver = new SymbolSolver(typeSolver);
    MethodUsage ref = symbolSolver.solveMethod("trim", Collections.emptyList(), callToTrim);
    assertEquals("trim", 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) File(java.io.File) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 20 with MethodCallExpr

use of com.github.javaparser.ast.expr.MethodCallExpr 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)

Aggregations

MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)77 Test (org.junit.Test)70 CompilationUnit (com.github.javaparser.ast.CompilationUnit)63 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)56 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)46 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)40 MethodUsage (com.github.javaparser.resolution.MethodUsage)33 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)30 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)25 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)21 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)16 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)16 Expression (com.github.javaparser.ast.expr.Expression)14 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)14 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)14 SymbolReference (com.github.javaparser.symbolsolver.model.resolution.SymbolReference)8 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)5 JavaParserTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver)5 JavaParserClassDeclaration (com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration)4 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)3