@Override public void visit(ReturnStmt n, Void arg) { out.println("ReturnStmt: " + (extended ? n : n.getExpression())); super.visit(n, arg); }
private List<Expression> getAllReturnExpressions(BlockStmt blockStmt) { return Navigator.findAllNodesOfGivenClass(blockStmt, ReturnStmt.class).stream() .filter(r -> r.getExpression().isPresent()) .map(r -> r.getExpression().get()) .collect(Collectors.toList()); }
private List<Expression> getAllReturnExpressions(BlockStmt blockStmt) { return blockStmt.findAll(ReturnStmt.class).stream() .filter(r -> r.getExpression().isPresent()) .map(r -> r.getExpression().get()) .collect(toList()); }
private boolean isValueCompatibleBlock(Statement statement) { // A block lambda body is value-compatible if it cannot complete normally (ยง14.21) and every return statement // in the block has the form return Expression;. if (statement instanceof BlockStmt) { if (!ControlFlowLogic.getInstance().canCompleteNormally(statement)) { return true; } List<ReturnStmt> returnStmts = statement.findAll(ReturnStmt.class); return returnStmts.stream().allMatch(r -> r.getExpression().isPresent()); } return false; }
private void addCastExpr(Statement stmt, Type castType) { ReturnStmt rstmt = (ReturnStmt) stmt; Optional<Expression> o_expr = rstmt.getExpression(); Expression expr = o_expr.isPresent() ? o_expr.get() : null; CastExpr ce = new CastExpr(castType, expr); rstmt.setExpression(ce); // removes the parent link from expr if (expr != null) { expr.setParentNode(ce); // restore it } }
@Test public void issue200() throws ParseException { CompilationUnit cu = parseSample("Issue200"); ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "JavaTest"); MethodDeclaration methodDeclaration = Navigator.demandMethod(clazz, "foo"); TypeSolver typeSolver = new ReflectionTypeSolver(); JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver); List<ReturnStmt> nodesByType = methodDeclaration.getNodesByType(ReturnStmt.class); assertEquals("java.util.stream.Stream<JavaTest.Solved>", javaParserFacade.getType((nodesByType.get(0)).getExpression().get()).describe()); } }
@Test public void accessClassFieldThroughThis() throws ParseException { CompilationUnit cu = parseSample("AccessClassMemberThroughThis"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "AccessClassMemberThroughThis"); MethodDeclaration method = Navigator.demandMethod(clazz, "getLabel2"); ReturnStmt returnStmt = (ReturnStmt) method.getBody().get().getStatements().get(0); Expression expression = returnStmt.getExpression().get(); ResolvedType ref = JavaParserFacade.get(new ReflectionTypeSolver()).getType(expression); assertEquals("java.lang.String", ref.describe()); }
@Test public void accessFieldThroughSuper() throws ParseException { CompilationUnit cu = parseSample("AccessThroughSuper"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "AccessThroughSuper.SubClass"); MethodDeclaration method = Navigator.demandMethod(clazz, "fieldTest"); ReturnStmt returnStmt = (ReturnStmt) method.getBody().get().getStatements().get(0); Expression expression = returnStmt.getExpression().get(); ResolvedType ref = JavaParserFacade.get(new ReflectionTypeSolver()).getType(expression); assertEquals("java.lang.String", ref.describe()); } }
@Test public void accessEnumMethodThroughThis() throws ParseException { CompilationUnit cu = parseSample("AccessEnumMemberThroughThis"); com.github.javaparser.ast.body.EnumDeclaration enumDecl = Navigator.demandEnum(cu, "AccessEnumMemberThroughThis"); MethodDeclaration method = Navigator.demandMethod(enumDecl, "getLabel2"); ReturnStmt returnStmt = (ReturnStmt) method.getBody().get().getStatements().get(0); Expression expression = returnStmt.getExpression().get(); ResolvedType ref = JavaParserFacade.get(new ReflectionTypeSolver()).getType(expression); assertEquals("java.lang.String", ref.describe()); }
@Test public void conditionalExpressionExample() throws ParseException { CompilationUnit cu = parseSample("JreConditionalExpression"); ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MyClass"); MethodDeclaration method = Navigator.demandMethod(clazz, "foo1"); ReturnStmt returnStmt = Navigator.findReturnStmt(method); Expression expression = returnStmt.getExpression().get(); TypeSolver typeSolver = new ReflectionTypeSolver(); ResolvedType ref = JavaParserFacade.get(typeSolver).getType(expression); assertEquals("java.lang.String", ref.describe()); }
@Test public void lambdaBlockMultiLineReturn() throws ParseException { CompilationUnit cu = parseSample("LambdaMulti"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda"); MethodDeclaration method = Navigator.demandMethod(clazz, "multiLineReturn"); ReturnStmt returnStmt = Navigator.findReturnStmt(method); Expression expression = returnStmt.getExpression().get(); JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver()); ResolvedType type = javaParserFacade.getType(expression); assertEquals("java.lang.String", type.describe()); }
@Test public void lambdaCollect() throws ParseException { 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); Expression expression = returnStmt.getExpression().get(); JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver()); ResolvedType type = javaParserFacade.getType(expression); assertEquals("java.util.List<java.lang.String>", type.describe()); }
@Test public void lambdaBifunc() throws ParseException { CompilationUnit cu = parseSample("Lambda"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda"); MethodDeclaration method = Navigator.demandMethod(clazz, "bifunc"); ReturnStmt returnStmt = Navigator.findReturnStmt(method); Expression expr = returnStmt.getExpression().get(); JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver()); ResolvedType type1 = javaParserFacade.getType(expr); assertEquals("double", type1.describe()); }
@Test public void lambdaReduce() throws ParseException { CompilationUnit cu = parseSample("Lambda"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda"); MethodDeclaration method = Navigator.demandMethod(clazz, "reduce"); ReturnStmt returnStmt = Navigator.findReturnStmt(method); Expression expr = returnStmt.getExpression().get(); JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver()); ResolvedType type1 = javaParserFacade.getType(expr); assertEquals("java.util.Optional<java.lang.Integer>", type1.describe()); }
@Test public void lambdaBlockExplicitReturn() throws ParseException { CompilationUnit cu = parseSample("LambdaMulti"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda"); MethodDeclaration method = Navigator.demandMethod(clazz, "lambdaSingleReturn"); ReturnStmt returnStmt = Navigator.findReturnStmt(method); Expression expression = returnStmt.getExpression().get(); JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver()); ResolvedType type = javaParserFacade.getType(expression); assertEquals("java.lang.String", type.describe()); }
@Test public void personsStream() throws ParseException { 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); Expression expression = returnStmt.getExpression().get(); expression = Navigator.findMethodCall(expression, "stream"); JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver()); ResolvedType type = javaParserFacade.getType(expression); assertEquals("java.util.stream.Stream<java.lang.String>", type.describe()); }
@Test public void typeOfVoidLambda() throws ParseException { CompilationUnit cu = parseSample("LambdaVoid"); com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Agenda"); MethodDeclaration method = Navigator.demandMethod(clazz, "lambdaEmpty"); ReturnStmt returnStmt = Navigator.findReturnStmt(method); Expression expression = returnStmt.getExpression().get(); LambdaExpr lambdaExpr = Navigator.findNodeOfGivenClass(expression, LambdaExpr.class); JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver()); ResolvedType type = javaParserFacade.getType(lambdaExpr); assertEquals("void", type.describe()); }
@Test public void typeParamOnReturnType() throws ParseException { CompilationUnit cu = parseSample("TypeParamOnReturnType"); ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "TypeParamOnReturnType"); MethodDeclaration method = Navigator.demandMethod(clazz, "nodeEquals"); ReturnStmt returnStmt = Navigator.findReturnStmt(method); ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(returnStmt.getExpression().get()); assertEquals(false, type.isTypeVariable()); assertEquals("boolean", type.describe()); }
@Test public void lambdaMapParameter() throws ParseException { 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()); }
@Test public void classCast() throws ParseException { CompilationUnit cu = parseSample("ClassCast"); ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "ClassCast"); MethodDeclaration method = Navigator.demandMethod(clazz, "getNodesByType"); ReturnStmt returnStmt = Navigator.findReturnStmt(method); ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(returnStmt.getExpression().get()); assertEquals(true, type.isTypeVariable()); assertEquals("N", type.describe()); }