Search in sources :

Example 31 with Expression

use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.

the class GenericsResolutionTest method genericsInheritance.

@Test
public void genericsInheritance() {
    CompilationUnit cu = parseSample("MethodTypeParams");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "VoidVisitorAdapter");
    MethodDeclaration method = Navigator.demandMethod(clazz, "visit");
    MethodCallExpr call = Navigator.findMethodCall(method, "accept").get();
    Expression thisRef = call.getArguments().get(0);
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    ResolvedType voidVisitorAdapterOfA = javaParserFacade.getType(thisRef);
    List<ResolvedReferenceType> allAncestors = voidVisitorAdapterOfA.asReferenceType().getAllAncestors();
    assertEquals(2, allAncestors.size());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Expression(com.github.javaparser.ast.expr.Expression) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) 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) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 32 with Expression

use of com.github.javaparser.ast.expr.Expression 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)

Example 33 with Expression

use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.

the class GenericsResolutionTest method resolveUsageOfGenericFieldAdvancedCase.

@Test
public void resolveUsageOfGenericFieldAdvancedCase() {
    CompilationUnit cu = parseSample("Generics");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "SomeCollection");
    MethodDeclaration method = Navigator.demandMethod(clazz, "foo2");
    ExpressionStmt stmt = (ExpressionStmt) method.getBody().get().getStatements().get(0);
    Expression expression = stmt.getExpression();
    ResolvedType type = JavaParserFacade.get(new ReflectionTypeSolver()).getType(expression);
    assertEquals(false, type.isTypeVariable());
    assertEquals("java.util.List<java.lang.String>", type.describe());
    assertEquals(1, type.asReferenceType().typeParametersValues().size());
    assertEquals(false, type.asReferenceType().typeParametersValues().get(0).isTypeVariable());
    assertEquals("java.lang.String", type.asReferenceType().typeParametersValues().get(0).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) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 34 with Expression

use of com.github.javaparser.ast.expr.Expression in project javaparser by javaparser.

the class GenericsResolutionTest method classCastScope.

@Test
public void classCastScope() {
    CompilationUnit cu = parseSample("ClassCast");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "ClassCast");
    MethodDeclaration method = Navigator.demandMethod(clazz, "getNodesByType");
    MethodCallExpr call = Navigator.findMethodCall(method, "cast").get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    Expression scope = call.getScope().get();
    ResolvedType type = JavaParserFacade.get(typeSolver).getType(scope);
    // System.out.println(typeUsage);
    assertEquals(false, type.isTypeVariable());
    assertEquals("java.lang.Class<N>", 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) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 35 with Expression

use of com.github.javaparser.ast.expr.Expression 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)

Aggregations

Expression (com.github.javaparser.ast.expr.Expression)429 Test (org.junit.Test)166 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)149 NameExpr (com.github.javaparser.ast.expr.NameExpr)135 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)94 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)90 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)84 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)73 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)71 NodeList (com.github.javaparser.ast.NodeList)66 DrlxParser.parseExpression (org.drools.mvel.parser.DrlxParser.parseExpression)61 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)53 CompilationUnit (com.github.javaparser.ast.CompilationUnit)51 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)50 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)49 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)49 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)46 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)44 List (java.util.List)44 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)43