Search in sources :

Example 61 with MethodCallExpr

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

the class MethodsResolutionTest method solveMethodInInterfaceParent.

@Test
public void solveMethodInInterfaceParent() {
    CompilationUnit cu = parseSample("MethodCalls");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MethodCalls");
    MethodDeclaration method = Navigator.demandMethod(clazz, "inheritedInterfaceMethod");
    MethodCallExpr expression = Navigator.findMethodCall(method, "toString").get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    MethodUsage call1 = javaParserFacade.solveMethodAsUsage(expression);
    assertEquals("java.lang.Object.toString()", call1.getQualifiedSignature());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) 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 62 with MethodCallExpr

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

the class MethodsResolutionTest method solveMethodWithTypePromotionsToByteWithExtraParam.

@Test
public void solveMethodWithTypePromotionsToByteWithExtraParam() {
    CompilationUnit cu = parseSample("Issue338");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "TypePromotionsWithExtraParam");
    MethodDeclaration method = Navigator.demandMethod(clazz, "callingByte");
    {
        MethodCallExpr expression = method.getBody().get().getStatements().get(0).asExpressionStmt().getExpression().asMethodCallExpr();
        SymbolReference<ResolvedMethodDeclaration> reference = JavaParserFacade.get(new ReflectionTypeSolver()).solve(expression);
        assertEquals(true, reference.isSolved());
        assertEquals("byteParam", reference.getCorrespondingDeclaration().getName());
    }
    {
        MethodCallExpr expression = method.getBody().get().getStatements().get(1).asExpressionStmt().getExpression().asMethodCallExpr();
        SymbolReference<ResolvedMethodDeclaration> reference = JavaParserFacade.get(new ReflectionTypeSolver()).solve(expression);
        assertEquals(false, reference.isSolved());
    }
    {
        MethodCallExpr expression = method.getBody().get().getStatements().get(2).asExpressionStmt().getExpression().asMethodCallExpr();
        SymbolReference<ResolvedMethodDeclaration> reference = JavaParserFacade.get(new ReflectionTypeSolver()).solve(expression);
        assertEquals(false, reference.isSolved());
    }
    {
        MethodCallExpr expression = method.getBody().get().getStatements().get(3).asExpressionStmt().getExpression().asMethodCallExpr();
        SymbolReference<ResolvedMethodDeclaration> reference = JavaParserFacade.get(new ReflectionTypeSolver()).solve(expression);
        assertEquals(false, reference.isSolved());
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 63 with MethodCallExpr

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

the class MethodsResolutionTest method solveMethodWithClassExpressionAsParameter.

@Test
public void solveMethodWithClassExpressionAsParameter() {
    CompilationUnit cu = parseSample("ClassExpression");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "ClassExpression");
    MethodDeclaration method = Navigator.demandMethod(clazz, "foo");
    MethodCallExpr expression = Navigator.findMethodCall(method, "noneOf").get();
    MethodUsage methodUsage = JavaParserFacade.get(new ReflectionTypeSolver()).solveMethodAsUsage(expression);
    assertEquals("noneOf", methodUsage.getName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 64 with MethodCallExpr

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

the class StatementContextResolutionTest method resolveLocalAndSeveralAnnidatedLevels.

@Test
public void resolveLocalAndSeveralAnnidatedLevels() {
    CompilationUnit cu = parseSample("LocalVariableInParent");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration referencesToField = Navigator.demandClass(cu, "LocalVariableInParent");
    MethodDeclaration method = Navigator.demandMethod(referencesToField, "foo4");
    MethodCallExpr call = Navigator.findMethodCall(method, "add").get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    SymbolReference<? extends ResolvedValueDeclaration> ref = JavaParserFacade.get(typeSolver).solve(call.getScope().get());
    assertTrue(ref.isSolved());
    assertEquals("java.util.List<Comment>", ref.getCorrespondingDeclaration().getType().describe());
    MethodUsage methodUsage = JavaParserFacade.get(typeSolver).solveMethodAsUsage(call);
    assertEquals("add", methodUsage.getName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) 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 65 with MethodCallExpr

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

the class ContextTest method resolveReferenceToMethodWithLambda.

@Test
public void resolveReferenceToMethodWithLambda() {
    CompilationUnit cu = parseSample("NavigatorSimplified");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
    MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
    MethodCallExpr methodCallExpr = Navigator.findMethodCall(method, "filter").get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    ResolvedType ref = JavaParserFacade.get(typeSolver).getType(methodCallExpr);
    assertEquals("java.util.stream.Stream<java.lang.String>", ref.describe());
    assertEquals(1, ref.asReferenceType().typeParametersValues().size());
    assertEquals("java.lang.String", ref.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) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) 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