Search in sources :

Example 71 with MethodCallExpr

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

the class MethodCallExprContextResolutionTest method assertCanSolveGenericMethodCallCanInferFromArguments.

private void assertCanSolveGenericMethodCallCanInferFromArguments(String callMethodName) {
    MethodCallExpr methodCallExpr = getMethodCallExpr("genericMethodTest", callMethodName);
    CombinedTypeSolver typeSolver = createTypeSolver();
    MethodCallExprContext context = new MethodCallExprContext(methodCallExpr, typeSolver);
    ResolvedReferenceTypeDeclaration stringType = typeSolver.solveType("java.lang.String");
    List<ResolvedType> argumentsTypes = new ArrayList<>();
    argumentsTypes.add(new ReferenceTypeImpl(stringType, typeSolver));
    Optional<MethodUsage> ref = context.solveMethodAsUsage(callMethodName, argumentsTypes, typeSolver);
    assertTrue(ref.isPresent());
    assertEquals("MethodCalls", ref.get().declaringType().getQualifiedName());
    assertEquals(Collections.singletonList("java.lang.String"), ref.get().typeParametersMap().getTypes().stream().map(ty -> ty.asReferenceType().describe()).collect(Collectors.toList()));
}
Also used : MethodCallExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.MethodCallExprContext) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ArrayList(java.util.ArrayList) MethodUsage(com.github.javaparser.resolution.MethodUsage) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 72 with MethodCallExpr

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

the class MethodCallExprContextResolutionTest method solveNestedMethodCallExprContextWithoutScope.

@Test
public void solveNestedMethodCallExprContextWithoutScope() {
    MethodCallExpr methodCallExpr = getMethodCallExpr("bar1", "foo");
    CombinedTypeSolver typeSolver = createTypeSolver();
    Context context = new MethodCallExprContext(methodCallExpr, typeSolver);
    Optional<MethodUsage> ref = context.solveMethodAsUsage("foo", Collections.emptyList(), typeSolver);
    assertTrue(ref.isPresent());
    assertEquals("MethodCalls", ref.get().declaringType().getQualifiedName());
}
Also used : Context(com.github.javaparser.symbolsolver.core.resolution.Context) MethodCallExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.MethodCallExprContext) MethodCallExprContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.MethodCallExprContext) MethodUsage(com.github.javaparser.resolution.MethodUsage) 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 73 with MethodCallExpr

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

the class JavaParserTypeParameterResolutionTest method testGenericArguments.

private void testGenericArguments(String containingMethodName) {
    CompilationUnit cu = parseSample("GenericMethodArguments");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    ClassOrInterfaceDeclaration classDecl = Navigator.demandClass(cu, "GenericMethodArguments");
    MethodDeclaration containingMethod = Navigator.demandMethod(classDecl, containingMethodName);
    MethodCallExpr bar = Navigator.findMethodCall(containingMethod, "apply").get();
    assertTrue(javaParserFacade.solve(bar).isSolved());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 74 with MethodCallExpr

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

the class JavaParserTypeParameterResolutionTest method declaredOnMethodNegativeCase.

@Test
public void declaredOnMethodNegativeCase() {
    CompilationUnit cu = parseSample("ClassTypeParameter");
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    ClassOrInterfaceDeclaration classDecl = Navigator.demandClass(cu, "Foo");
    MethodDeclaration methodDecl = Navigator.demandMethod(classDecl, "usage");
    MethodCallExpr callToFoo = (MethodCallExpr) Navigator.findReturnStmt(methodDecl).getExpression().get();
    ResolvedMethodDeclaration methodDeclaration = javaParserFacade.solve(callToFoo).getCorrespondingDeclaration();
    ResolvedReferenceTypeDeclaration typeDeclaration = methodDeclaration.declaringType();
    assertEquals(2, typeDeclaration.getTypeParameters().size());
    assertTrue(typeDeclaration.getTypeParameters().get(0) instanceof JavaParserTypeParameter);
    assertEquals("A", typeDeclaration.getTypeParameters().get(0).getName());
    assertEquals(false, typeDeclaration.getTypeParameters().get(0).declaredOnMethod());
    assertEquals(true, typeDeclaration.getTypeParameters().get(0).declaredOnType());
    assertTrue(typeDeclaration.getTypeParameters().get(1) instanceof JavaParserTypeParameter);
    assertEquals("B", typeDeclaration.getTypeParameters().get(1).getName());
    assertEquals(false, typeDeclaration.getTypeParameters().get(1).declaredOnMethod());
    assertEquals(true, typeDeclaration.getTypeParameters().get(1).declaredOnType());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) JavaParserTypeParameter(com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 75 with MethodCallExpr

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

the class SymbolResolutionResolutionTest method conditionalExpressionExampleFollowUp1.

@Test
public void conditionalExpressionExampleFollowUp1() {
    CompilationUnit cu = parseSample("JreConditionalExpression");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MyClass");
    MethodDeclaration method = Navigator.demandMethod(clazz, "foo1");
    MethodCallExpr expression = Navigator.findMethodCall(method, "next").get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    ResolvedType ref = JavaParserFacade.get(typeSolver).getType(expression);
    assertEquals("java.lang.String", ref.describe());
}
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) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) 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