Search in sources :

Example 6 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.

the class InferenceContextTest method placingASingleVariableInside.

@Test
public void placingASingleVariableInside() {
    ResolvedType result = new InferenceContext(MyObjectProvider.INSTANCE).addPair(listOfE, listOfString);
    assertEquals(listOf(new InferenceVariableType(0, MyObjectProvider.INSTANCE)), result);
}
Also used : ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Example 7 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.

the class Issue241 method testSolveStaticallyImportedMemberType.

@Test
public void testSolveStaticallyImportedMemberType() {
    File src = adaptPath(new File("src/test/resources"));
    TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(), new JavaParserTypeSolver(src));
    JavaParserFacade javaParserFacade = JavaParserFacade.get(typeSolver);
    CompilationUnit cu = parseSample("Issue241");
    ClassOrInterfaceDeclaration cls = Navigator.demandClassOrInterface(cu, "Main");
    VariableDeclarator v = Navigator.demandVariableDeclaration(cls, "foo").get();
    Type t = v.getType();
    ResolvedType t2 = javaParserFacade.convert(t, t);
    String typeName = t2.asReferenceType().getQualifiedName();
    assertEquals("issue241.TypeWithMemberType.MemberInterface", typeName);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Type(com.github.javaparser.ast.type.Type) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) JavaParserTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver) JavaParserFacade(com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) File(java.io.File) CombinedTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 8 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.

the class Issue347 method resolvingReferenceToEnumDeclarationInSameFile.

@Test
public void resolvingReferenceToEnumDeclarationInSameFile() {
    String code = "package foo.bar;\nenum Foo {\n" + "    FOO_A, FOO_B\n" + "}\n" + "\n" + "class UsingFoo {\n" + "    Foo myFooField;\n" + "}";
    CompilationUnit cu = JavaParser.parse(code);
    FieldDeclaration fieldDeclaration = Navigator.findNodeOfGivenClass(cu, FieldDeclaration.class);
    ResolvedType fieldType = javaParserFacade.getType(fieldDeclaration);
    assertEquals(true, fieldType.isReferenceType());
    assertEquals(true, fieldType.asReferenceType().getTypeDeclaration().isEnum());
    assertEquals("foo.bar.Foo", fieldType.asReferenceType().getQualifiedName());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 9 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType in project javaparser by javaparser.

the class ExpressionCompatibleWithType method replaceTypeVariablesWithInferenceVariables.

private MethodType replaceTypeVariablesWithInferenceVariables(MethodType methodType) {
    // Find all type variable
    Map<ResolvedTypeVariable, InferenceVariable> correspondences = new HashMap<>();
    List<ResolvedType> newFormalArgumentTypes = new LinkedList<>();
    for (ResolvedType formalArg : methodType.getFormalArgumentTypes()) {
        newFormalArgumentTypes.add(replaceTypeVariablesWithInferenceVariables(formalArg, correspondences));
    }
    ResolvedType newReturnType = replaceTypeVariablesWithInferenceVariables(methodType.getReturnType(), correspondences);
    return new MethodType(methodType.getTypeParameters(), newFormalArgumentTypes, newReturnType, methodType.getExceptionTypes());
}
Also used : ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) HashMap(java.util.HashMap) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) LinkedList(java.util.LinkedList)

Example 10 with ResolvedType

use of com.github.javaparser.resolution.types.ResolvedType 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)

Aggregations

ResolvedType (com.github.javaparser.resolution.types.ResolvedType)119 Test (org.junit.Test)78 CompilationUnit (com.github.javaparser.ast.CompilationUnit)68 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)58 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)41 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)39 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)32 Expression (com.github.javaparser.ast.expr.Expression)27 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)22 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)21 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)20 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)18 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)18 MethodUsage (com.github.javaparser.resolution.MethodUsage)17 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)15 Context (com.github.javaparser.symbolsolver.core.resolution.Context)15 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)14 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)10 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)10 Collectors (java.util.stream.Collectors)10