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()));
}
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());
}
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());
}
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());
}
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());
}
Aggregations