Search in sources :

Example 41 with ReflectionTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver 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 42 with ReflectionTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver 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 43 with ReflectionTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver in project javaparser by javaparser.

the class Issue128 method setup.

@Before
public void setup() throws IOException {
    File srcDir = adaptPath(new File("src/test/resources/issue128"));
    typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JavaParserTypeSolver(srcDir));
}
Also used : JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) File(java.io.File) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) Before(org.junit.Before)

Example 44 with ReflectionTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver in project javaparser by javaparser.

the class TypeCompatibleWithType method reduce.

@Override
public ReductionResult reduce(BoundSet currentBoundSet) {
    if (isProperType(s) && isProperType(t)) {
        if (isCompatibleInALooseInvocationContext(s, t)) {
            return ReductionResult.trueResult();
        } else {
            return ReductionResult.falseResult();
        }
    }
    if (s.isPrimitive()) {
        ReflectionTypeSolver typeSolver = new ReflectionTypeSolver();
        ResolvedType sFirst = new ReferenceTypeImpl(typeSolver.solveType(s.asPrimitive().getBoxTypeQName()), typeSolver);
        return ReductionResult.oneConstraint(new TypeCompatibleWithType(typeSolver, sFirst, t));
    }
    if (t.isPrimitive()) {
        ReflectionTypeSolver typeSolver = new ReflectionTypeSolver();
        ResolvedType tFirst = new ReferenceTypeImpl(typeSolver.solveType(t.asPrimitive().getBoxTypeQName()), typeSolver);
        return ReductionResult.oneConstraint(new TypeSameAsType(s, tFirst));
    }
    if (t.isReferenceType() && !t.asReferenceType().getTypeDeclaration().getTypeParameters().isEmpty()) {
        // FIXME I really cannot understand what the specification means...
        // there exists a type of the form G<...> that is a supertype of S?
        boolean condition1 = t.isAssignableBy(s);
        // the raw type G is a supertype of S
        ResolvedType G = t.asReferenceType().toRawType();
        boolean condition2 = G.isAssignableBy(s);
        if (!condition1 && condition2) {
            return ReductionResult.trueResult();
        }
    // throw new UnsupportedOperationException();
    }
    if (t.isArray()) {
        throw new UnsupportedOperationException();
    }
    return ReductionResult.empty().withConstraint(new TypeSubtypeOfType(typeSolver, s, t));
}
Also used : ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 45 with ReflectionTypeSolver

use of com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver in project javaparser by javaparser.

the class EnumResolutionTest method enumAndStaticInitializer.

@Test
public void enumAndStaticInitializer() {
    CompilationUnit cu = parseSample("EnumAndStaticInitializer");
    com.github.javaparser.ast.body.ClassOrInterfaceDeclaration clazz = Navigator.demandClass(cu, "MyClass");
    MethodCallExpr call = Navigator.findMethodCall(clazz, "put").get();
    ResolvedType ref = JavaParserFacade.get(new ReflectionTypeSolver()).getType(call);
    assertEquals("MyClass.Primitive", ref.describe());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) 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)

Aggregations

ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)206 Test (org.junit.Test)165 CompilationUnit (com.github.javaparser.ast.CompilationUnit)128 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)81 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)77 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)75 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)57 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)57 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)52 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)47 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)41 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)31 JavaParserTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver)28 Expression (com.github.javaparser.ast.expr.Expression)26 Before (org.junit.Before)26 Context (com.github.javaparser.symbolsolver.core.resolution.Context)25 MethodUsage (com.github.javaparser.resolution.MethodUsage)23 File (java.io.File)23 CompilationUnitContext (com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext)22 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)22