Search in sources :

Example 6 with ReturnStmt

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

the class GenericsResolutionTest method classCast.

@Test
public void classCast() {
    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());
}
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 7 with ReturnStmt

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

the class VariadicResolutionTest method issue7.

@Test
public void issue7() {
    CompilationUnit cu = parseSample("Generics_issue7");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SomeCollection");
    MethodDeclaration method = Navigator.demandMethod(clazz, "foo3");
    ReturnStmt stmt = (ReturnStmt) method.getBody().get().getStatements().get(0);
    Expression expression = stmt.getExpression().get();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(new ReflectionTypeSolver());
    ResolvedType type = javaParserFacade.getType(expression);
    assertEquals(true, type.isReferenceType());
    assertEquals(List.class.getCanonicalName(), type.asReferenceType().getQualifiedName());
    assertEquals("java.util.List<java.lang.Long>", 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) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) List(java.util.List) 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 8 with ReturnStmt

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

the class FieldsResolutionTest method accessClassFieldThroughThis.

@Test
public void accessClassFieldThroughThis() {
    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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) 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 9 with ReturnStmt

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

the class FieldsResolutionTest method accessEnumMethodThroughThis.

@Test
public void accessEnumMethodThroughThis() {
    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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) 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 10 with ReturnStmt

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

the class FieldsResolutionTest method accessFieldThroughSuper.

@Test
public void accessFieldThroughSuper() {
    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());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) 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)

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