use of com.github.javaparser.ast.stmt.ReturnStmt in project javaparser by javaparser.
the class GenericsResolutionTest method genericCollectionWithWildcardsAndExtensions.
/*@Test
public void genericCollectionWithWildcardsAndExtensionsPrep() {
CompilationUnit cu = parseSample("GenericCollectionWithExtension");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Foo");
MethodDeclaration method = Navigator.demandMethod(clazz, "bar");
ReturnStmt returnStmt = Navigator.findReturnStmt(method);
TypeSolver typeSolver = new JreTypeSolver();
MethodCallExpr call = (MethodCallExpr) returnStmt.getExpr();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
List<TypeUsage> params = new ArrayList<>();
if (call.getArgs() != null) {
for (Expression param : call.getArgs()) {
params.add(javaParserFacade.getType(param, false));
}
}
Context context = JavaParserFactory.getContext(call, typeSolver);
ReferenceTypeUsage typeOfScope = javaParserFacade.getType(call.getScope()).asReferenceType();
me.tomassetti.symbolsolver.model.declarations.TypeDeclaration typeDeclaration = typeOfScope.getTypeDeclaration();
List<TypeUsage> typeParametersValues = typeOfScope.typeParametersValues();
List<MethodUsage> methods = new ArrayList<>();
for (Method m : List.class.getMethods()) {
if (m.getName().equals("addAll") && !m.isBridge() && !m.isSynthetic()) {
me.tomassetti.symbolsolver.model.declarations.MethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(m, typeSolver);
if (methods.size() == 0) {
// ok, e' il primo
ReferenceTypeUsage paramType = methodDeclaration.getParam(0).getType(typeSolver).asReferenceType();
assertTrue(paramType.asReferenceType().typeParametersValues().get(0).isWildcard());
}
MethodUsage mu = new MethodUsage(methodDeclaration, typeSolver);
int i = 0;
for (TypeParameter tp : typeDeclaration.getTypeParameters()) {
mu = mu.replaceTypeParameter(tp.getName(), typeParametersValues.get(i));
i++;
}
methods.add(mu);
}
}
assertTrue(MethodResolutionLogic.isApplicable(methods.get(0), "addAll", params, typeSolver));
//Optional<MethodUsage> methodUsage = MethodResolutionLogic.findMostApplicableUsage(methods, "addAll", params, typeSolver);
//Optional<MethodUsage> methodUsage = typeDeclaration.solveMethodAsUsage("addAll", params, typeSolver, context, typeParametersValues);
//Optional<MethodUsage> methodUsage = context.solveMethodAsUsage("addAll", params, typeSolver);
//assertTrue(methodUsage.isPresent());
}*/
@Test
public void genericCollectionWithWildcardsAndExtensions() {
CompilationUnit cu = parseSample("GenericCollectionWithExtension");
ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Foo");
MethodDeclaration method = Navigator.demandMethod(clazz, "bar");
ReturnStmt returnStmt = Navigator.findReturnStmt(method);
TypeSolver typeSolver = new ReflectionTypeSolver();
Expression returnStmtExpr = returnStmt.getExpression().get();
JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
ResolvedType type = javaParserFacade.getType(returnStmtExpr);
assertEquals(false, type.isTypeVariable());
assertEquals("boolean", type.describe());
}
use of com.github.javaparser.ast.stmt.ReturnStmt 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());
}
Aggregations