Search in sources :

Example 56 with MethodCallExpr

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

the class JavaParserAnonymousClassDeclarationTest method usingAnonymousClassInnerType.

@Test
public void usingAnonymousClassInnerType() {
    CompilationUnit cu = parseSample("AnonymousClassDeclarations");
    ClassOrInterfaceDeclaration aClass = Navigator.demandClass(cu, "AnonymousClassDeclarations");
    MethodDeclaration method = Navigator.demandMethod(aClass, "fooBar5");
    MethodCallExpr methodCall = Navigator.findMethodCall(method, "toString").get();
    CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
    combinedTypeSolver.add(new ReflectionTypeSolver());
    MethodUsage methodUsage = JavaParserFacade.get(combinedTypeSolver).solveMethodAsUsage(methodCall);
    assertThat(methodUsage.getQualifiedSignature(), is("java.lang.Enum.toString()"));
}
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) 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)

Example 57 with MethodCallExpr

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

the class JavaParserFacadeResolutionTest method solvingReferenceToCatchClauseParam.

// See issue 46
@Test
public void solvingReferenceToCatchClauseParam() {
    String code = "public class Bla {\n" + "    public void main()\n" + "    {\n" + "        try\n" + "        {\n" + "            int i = 0;\n" + "        }\n" + "        catch (UnsupportedOperationException e)\n" + "        {\n" + "            String s;\n" + "            e.getMessage();\n" + "        }\n" + "    }\n" + "}";
    MethodCallExpr methodCallExpr = Navigator.findNodeOfGivenClass(JavaParser.parse(code), MethodCallExpr.class);
    NameExpr nameE = (NameExpr) methodCallExpr.getScope().get();
    SymbolReference<? extends ResolvedValueDeclaration> symbolReference = JavaParserFacade.get(new ReflectionTypeSolver()).solve(nameE);
    assertEquals(true, symbolReference.isSolved());
    assertEquals(true, symbolReference.getCorrespondingDeclaration().isParameter());
    assertEquals("e", symbolReference.getCorrespondingDeclaration().asParameter().getName());
    assertEquals("java.lang.UnsupportedOperationException", symbolReference.getCorrespondingDeclaration().asParameter().getType().asReferenceType().getQualifiedName());
}
Also used : NameExpr(com.github.javaparser.ast.expr.NameExpr) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 58 with MethodCallExpr

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

the class JavaParserFacadeResolutionTest method solvingReferenceToUnsupportedOperationException.

// See issue 42
@Test
public void solvingReferenceToUnsupportedOperationException() {
    String code = "public class Bla {\n" + "    public void main()\n" + "    {\n" + "        try\n" + "        {\n" + "            int i = 0;\n" + "        }\n" + "        catch (UnsupportedOperationException e)\n" + "        {\n" + "            String s;\n" + "            e.getMessage();\n" + "        }\n" + "    }\n" + "}";
    MethodCallExpr methodCallExpr = Navigator.findNodeOfGivenClass(JavaParser.parse(code), MethodCallExpr.class);
    MethodUsage methodUsage = JavaParserFacade.get(new ReflectionTypeSolver()).solveMethodAsUsage(methodCallExpr);
    assertEquals("java.lang.Throwable.getMessage()", methodUsage.getQualifiedSignature());
}
Also used : 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 59 with MethodCallExpr

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

the class LambdaResolutionTest method lambdaMapParameter.

@Test
public void lambdaMapParameter() {
    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();
    Expression expression = methodCallExpr.getArguments().get(0);
    JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver());
    ResolvedType type = javaParserFacade.getType(expression);
    assertEquals("java.util.function.Function<? super java.lang.String, ? extends java.lang.String>", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 60 with MethodCallExpr

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

the class LambdaResolutionTest method lambdaCollectParam.

@Test
public void lambdaCollectParam() {
    CompilationUnit cu = parseSample("LambdaCollect");
    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();
    // Collectors.toList()
    Expression expression = methodCallExpr.getArguments().get(0);
    JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver());
    ResolvedType type = javaParserFacade.getType(expression);
    assertEquals("java.util.stream.Collector<T, ? extends java.lang.Object, java.util.List<T>>", type.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) 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