Search in sources :

Example 6 with JavaParserFacade

use of com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade 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 7 with JavaParserFacade

use of com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade in project javaparser by javaparser.

the class Issue228 method testSolvingMethodWitPrimitiveParameterTypeAsUsage.

@Test
public void testSolvingMethodWitPrimitiveParameterTypeAsUsage() {
    String code = "class Test { " + "  long l = call(1); " + "  long call(final long i) { " + "    return i; " + "  }" + "}";
    CompilationUnit cu = JavaParser.parse(code);
    MethodCallExpr methodCall = cu.findAll(MethodCallExpr.class).get(0);
    JavaParserFacade parserFacade = JavaParserFacade.get(new ReflectionTypeSolver());
    MethodUsage solvedCall = parserFacade.solveMethodAsUsage(methodCall);
    assertEquals("long", solvedCall.getParamType(0).describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 8 with JavaParserFacade

use of com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade in project javaparser by javaparser.

the class Issue128 method loopOnStaticallyImportedType.

@Test
public void loopOnStaticallyImportedType() {
    CompilationUnit cu = parseSampleWithStandardExtension("issue128/foo/Issue128");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "JavaTest");
    ExpressionStmt expressionStmt = (ExpressionStmt) clazz.getMethodsByName("test").get(0).getBody().get().getStatement(0);
    MethodCallExpr methodCallExpr = (MethodCallExpr) expressionStmt.getExpression();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    assertEquals(false, javaParserFacade.solve(methodCallExpr).isSolved());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 9 with JavaParserFacade

use of com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade in project javaparser by javaparser.

the class ContextTest method resolveReferenceToLambdaParamBase.

@Test
public void resolveReferenceToLambdaParamBase() {
    CompilationUnit cu = parseSample("NavigatorSimplified");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Navigator");
    MethodDeclaration method = Navigator.demandMethod(clazz, "findType");
    NameExpr refToT = Navigator.findNameExpression(method, "t").get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    ResolvedType ref = javaParserFacade.getType(refToT);
    assertEquals("? super java.lang.String", ref.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) NameExpr(com.github.javaparser.ast.expr.NameExpr) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) AbstractTest(com.github.javaparser.symbolsolver.AbstractTest) Test(org.junit.Test)

Example 10 with JavaParserFacade

use of com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade in project javaparser by javaparser.

the class GenericsResolutionTest method methodWithGenericParameterTypes.

@Test
public void methodWithGenericParameterTypes() {
    CompilationUnit cu = parseSample("GenericCollectionWithExtension");
    ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "Foo");
    MethodDeclaration method = Navigator.demandMethod(clazz, "bar");
    MethodCallExpr methodCall = Navigator.findMethodCall(method, "foo").get();
    TypeSolver typeSolver = new ReflectionTypeSolver();
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    MethodUsage methodUsage = javaParserFacade.solveMethodAsUsage(methodCall);
    assertEquals("foo", methodUsage.getName());
}
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) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Aggregations

JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)45 CompilationUnit (com.github.javaparser.ast.CompilationUnit)44 Test (org.junit.Test)42 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)40 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)34 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)27 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)23 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)23 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)21 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)19 Expression (com.github.javaparser.ast.expr.Expression)18 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)14 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)8 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)8 JavaParserTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver)7 MethodUsage (com.github.javaparser.resolution.MethodUsage)6 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)5 File (java.io.File)5 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)2 JavaParserTypeParameter (com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserTypeParameter)2