Search in sources :

Example 66 with MethodCallExpr

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

the class ContextTest method resolveReferenceToOverloadMethodWithNullParam.

@Test
public void resolveReferenceToOverloadMethodWithNullParam() {
    CompilationUnit cu = parseSample("OverloadedMethods");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "OverloadedMethods");
    MethodDeclaration method = Navigator.demandMethod(clazz, "m1");
    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.String", 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 67 with MethodCallExpr

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

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

the class GenericsResolutionTest method methodTypeParams.

@Test
public void methodTypeParams() {
    CompilationUnit cu = parseSample("MethodTypeParams");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "VoidVisitorAdapter");
    MethodDeclaration method = Navigator.demandMethod(clazz, "visit");
    MethodCallExpr call = Navigator.findMethodCall(method, "accept").get();
    ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(call);
    assertEquals(false, type.isTypeVariable());
    assertEquals("void", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 69 with MethodCallExpr

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

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

the class LambdaExprContextResolutionTest method solveParameterOfLambdaInMethodCallExpr.

@Test
public void solveParameterOfLambdaInMethodCallExpr() {
    CompilationUnit cu = parseSample("Lambda");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda");
    MethodDeclaration method = Navigator.demandMethod(clazz, "lambdaMap");
    ReturnStmt returnStmt = Navigator.findReturnStmt(method);
    MethodCallExpr methodCallExpr = (MethodCallExpr) returnStmt.getExpression().get();
    LambdaExpr lambdaExpr = (LambdaExpr) methodCallExpr.getArguments().get(0);
    Context context = new LambdaExprContext(lambdaExpr, typeSolver);
    Optional<Value> ref = context.solveSymbolAsValue("p", typeSolver);
    assertTrue(ref.isPresent());
    assertEquals("? super java.lang.String", ref.get().getType().describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Context(com.github.javaparser.symbolsolver.core.resolution.Context) LambdaExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) LambdaExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext) Value(com.github.javaparser.symbolsolver.model.resolution.Value) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) 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