Search in sources :

Example 1 with ReturnStmt

use of com.github.javaparser.ast.stmt.ReturnStmt in project javaparser by javaparser.

the class TransformationsTest method exampleParam5.

@Test
public void exampleParam5() throws IOException {
    considerExample("Example_param3_original");
    MethodDeclaration md = (MethodDeclaration) cu.getClassByName("A").get().getMember(0).asMethodDeclaration();
    md.setType(PrimitiveType.intType());
    assertTransformed("Example_param5b", cu);
    md.getBody().get().getStatements().add(new ReturnStmt(new NameExpr("p1")));
    assertTransformed("Example_param5", cu);
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) NameExpr(com.github.javaparser.ast.expr.NameExpr) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) Test(org.junit.Test)

Example 2 with ReturnStmt

use of com.github.javaparser.ast.stmt.ReturnStmt in project javaparser by javaparser.

the class Issue200 method issue200.

@Test
public void issue200() {
    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.findAll(ReturnStmt.class);
    assertEquals("java.util.stream.Stream<JavaTest.Solved>", javaParserFacade.getType((nodesByType.get(0)).getExpression().get()).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) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 3 with ReturnStmt

use of com.github.javaparser.ast.stmt.ReturnStmt in project javaparser by javaparser.

the class TokenKindGenerator method generateValueOfEntry.

private void generateValueOfEntry(SwitchStmt valueOfSwitch, String name, IntegerLiteralExpr kind) {
    final SwitchEntryStmt entry = new SwitchEntryStmt(kind, new NodeList<>(new ReturnStmt(name)));
    valueOfSwitch.getEntries().addFirst(entry);
}
Also used : SwitchEntryStmt(com.github.javaparser.ast.stmt.SwitchEntryStmt) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

Example 4 with ReturnStmt

use of com.github.javaparser.ast.stmt.ReturnStmt in project javaparser by javaparser.

the class GenericsResolutionTest method typeParamOnReturnType.

@Test
public void typeParamOnReturnType() {
    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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 5 with ReturnStmt

use of com.github.javaparser.ast.stmt.ReturnStmt in project javaparser by javaparser.

the class GenericsResolutionTest method genericCollectionWithWildcards.

@Test
public void genericCollectionWithWildcards() {
    CompilationUnit cu = parseSample("GenericCollection");
    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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) 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) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Aggregations

ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)27 CompilationUnit (com.github.javaparser.ast.CompilationUnit)23 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)23 Test (org.junit.Test)23 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)22 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)21 Expression (com.github.javaparser.ast.expr.Expression)15 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)14 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)9 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)5 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)3 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)2 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)2 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)2 Context (com.github.javaparser.symbolsolver.core.resolution.Context)2 Value (com.github.javaparser.symbolsolver.model.resolution.Value)2 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)2 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)1 Parameter (com.github.javaparser.ast.body.Parameter)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1