Search in sources :

Example 6 with ReferenceTypeImpl

use of com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl in project javaparser by javaparser.

the class SymbolSolver method solveTypeUsage.

public ResolvedType solveTypeUsage(String name, Context context) {
    Optional<ResolvedType> genericType = context.solveGenericType(name, typeSolver);
    if (genericType.isPresent()) {
        return genericType.get();
    }
    ResolvedReferenceTypeDeclaration typeDeclaration = typeSolver.solveType(name);
    return new ReferenceTypeImpl(typeDeclaration, typeSolver);
}
Also used : ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 7 with ReferenceTypeImpl

use of com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl in project javaparser by javaparser.

the class BoundSet method performResolution.

/**
 * Examines the bounds on an inference variable and determines an instantiation that is compatible with those
 * bounds. It also decides the order in which interdependent inference variables are to be resolved.
 */
public Optional<InstantiationSet> performResolution(List<InferenceVariable> variablesToResolve, TypeSolver typeSolver) {
    if (this.containsFalse()) {
        return Optional.empty();
    }
    List<VariableDependency> dependencies = new LinkedList<>();
    for (Bound b : bounds) {
        if (b instanceof CapturesBound) {
            throw new UnsupportedOperationException();
        }
    }
    for (Bound b : bounds) {
        if (b instanceof CapturesBound) {
            throw new UnsupportedOperationException();
        }
    }
    for (int i = 0; i < dependencies.size(); i++) {
        VariableDependency di = dependencies.get(i);
        for (int j = i + 1; j < dependencies.size(); j++) {
            VariableDependency dj = dependencies.get(j);
            if (di.dependedOn.equals(dj.depending)) {
                dependencies.add(new VariableDependency(di.getDepending(), dj.getDependedOn()));
            }
        }
    }
    for (InferenceVariable v : allInferenceVariables()) {
        dependencies.add(new VariableDependency(v, v));
    }
    // Given a set of inference variables to resolve, let V be the union of this set and all variables upon which
    // the resolution of at least one variable in this set depends.
    Set<InferenceVariable> V = new HashSet<>();
    V.addAll(variablesToResolve);
    for (VariableDependency dependency : dependencies) {
        if (variablesToResolve.contains(dependency.depending)) {
            V.add(dependency.dependedOn);
        }
    }
    // If every variable in V has an instantiation, then resolution succeeds and this procedure terminates.
    boolean ok = true;
    for (InferenceVariable v : V) {
        if (!hasInstantiationFor(v)) {
            ok = false;
        }
    }
    if (ok) {
        InstantiationSet instantiationSet = InstantiationSet.empty();
        for (InferenceVariable v : V) {
            instantiationSet = instantiationSet.withInstantiation(getInstantiationFor(v));
        }
        return Optional.of(instantiationSet);
    }
    // Otherwise, let { α1, ..., αn } be a non-empty subset of uninstantiated variables in V such that i)
    // for all i (1 ≤ i ≤ n), if αi depends on the resolution of a variable β, then either β has an instantiation
    // or there is some j such that β = αj; and ii) there exists no non-empty proper subset of { α1, ..., αn }
    // with this property.
    Set<InferenceVariable> uninstantiatedPortionOfV = new HashSet<>();
    for (InferenceVariable v : V) {
        if (!hasInstantiationFor(v)) {
            uninstantiatedPortionOfV.add(v);
        }
    }
    for (Set<InferenceVariable> alphas : allSetsWithProperty(uninstantiatedPortionOfV, dependencies)) {
        // Resolution proceeds by generating an instantiation for each of α1, ..., αn based on the
        // bounds in the bound set:
        boolean hasSomeCaptureForAlphas = alphas.stream().anyMatch(alphaI -> appearInLeftPartOfCapture(alphaI));
        if (!hasSomeCaptureForAlphas) {
            BoundSet newBounds = BoundSet.empty();
            for (InferenceVariable alphaI : alphas) {
                Set<ResolvedType> properLowerBounds = bounds.stream().filter(b -> b.isProperLowerBoundFor(alphaI).isPresent()).map(b -> b.isProperLowerBoundFor(alphaI).get().getProperType()).collect(Collectors.toSet());
                ResolvedType Ti = null;
                if (properLowerBounds.size() > 0) {
                    Ti = leastUpperBound(properLowerBounds);
                }
                // - Otherwise, if the bound set contains throws αi, and the proper upper bounds of αi are, at most,
                // Exception, Throwable, and Object, then Ti = RuntimeException.
                boolean throwsBound = bounds.stream().anyMatch(b -> b.isThrowsBoundOn(alphaI));
                if (Ti == null && throwsBound && properUpperBoundsAreAtMostExceptionThrowableAndObject(alphaI)) {
                    Ti = new ReferenceTypeImpl(typeSolver.solveType(RuntimeException.class.getCanonicalName()), typeSolver);
                }
                if (Ti == null) {
                    Set<ResolvedType> properUpperBounds = bounds.stream().filter(b -> b.isProperUpperBoundFor(alphaI).isPresent()).map(b -> b.isProperUpperBoundFor(alphaI).get().getProperType()).collect(Collectors.toSet());
                    if (properUpperBounds.size() == 0) {
                        throw new IllegalStateException();
                    }
                    Ti = glb(properUpperBounds);
                }
                newBounds = newBounds.withBound(new SameAsBound(alphaI, Ti));
            }
            // The bounds α1 = T1, ..., αn = Tn are incorporated with the current bound set.
            BoundSet incorporatedBoundSet = this.incorporate(newBounds, typeSolver);
            if (!incorporatedBoundSet.containsFalse()) {
                return incorporatedBoundSet.performResolution(variablesToResolve, typeSolver);
            }
            throw new UnsupportedOperationException();
        } else // - If the bound set contains a bound of the form G<..., αi, ...> = capture(G<...>) for some i (1 ≤ i ≤ n), or;
        {
            throw new UnsupportedOperationException();
        }
    }
    return Optional.empty();
}
Also used : TypeSameAsType(com.github.javaparser.symbolsolver.resolution.typeinference.constraintformulas.TypeSameAsType) TypeHelper(com.github.javaparser.symbolsolver.resolution.typeinference.TypeHelper) TypeSubtypeOfType(com.github.javaparser.symbolsolver.resolution.typeinference.constraintformulas.TypeSubtypeOfType) java.util(java.util) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) Predicate(java.util.function.Predicate) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) com.github.javaparser.symbolsolver.resolution.typeinference.bounds(com.github.javaparser.symbolsolver.resolution.typeinference.bounds) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Collectors(java.util.stream.Collectors) Pair(com.github.javaparser.utils.Pair) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 8 with ReferenceTypeImpl

use of com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl in project javaparser by javaparser.

the class ReflectionClassAdapter method getInterfaces.

public List<ResolvedReferenceType> getInterfaces() {
    List<ResolvedReferenceType> interfaces = new ArrayList<>();
    for (java.lang.reflect.Type superInterface : clazz.getGenericInterfaces()) {
        if (superInterface instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) superInterface;
            List<ResolvedType> typeParameters = Arrays.stream(parameterizedType.getActualTypeArguments()).map((t) -> ReflectionFactory.typeUsageFor(t, typeSolver)).collect(Collectors.toList());
            interfaces.add(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration((Class<?>) ((ParameterizedType) superInterface).getRawType(), typeSolver), typeParameters, typeSolver));
        } else {
            interfaces.add(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration((Class<?>) superInterface, typeSolver), typeSolver));
        }
    }
    return interfaces;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) NullType(com.github.javaparser.symbolsolver.model.typesystem.NullType) java.util(java.util) UnsolvedSymbolException(com.github.javaparser.resolution.UnsolvedSymbolException) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) TypeVariable(java.lang.reflect.TypeVariable) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Field(java.lang.reflect.Field) com.github.javaparser.resolution.declarations(com.github.javaparser.resolution.declarations) Collectors(java.util.stream.Collectors) LambdaArgumentTypePlaceholder(com.github.javaparser.symbolsolver.javaparsermodel.LambdaArgumentTypePlaceholder) FunctionalInterfaceLogic(com.github.javaparser.symbolsolver.logic.FunctionalInterfaceLogic) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) ParameterizedType(java.lang.reflect.ParameterizedType) Annotation(java.lang.annotation.Annotation) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 9 with ReferenceTypeImpl

use of com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl in project javaparser by javaparser.

the class ClassOrInterfaceDeclarationContextResolutionTest method solveMethodAsUsageWithMoreSpecializedParameter.

@Test
public void solveMethodAsUsageWithMoreSpecializedParameter() {
    CompilationUnit cu = parseSample("ClassWithMethods");
    ClassOrInterfaceDeclaration classOrInterfaceDeclaration = Navigator.demandClass(cu, "A");
    Context context = new ClassOrInterfaceDeclarationContext(classOrInterfaceDeclaration, typeSolver);
    ResolvedType stringType = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver);
    Optional<MethodUsage> ref = context.solveMethodAsUsage("foo4", ImmutableList.of(stringType), new ReflectionTypeSolver());
    assertEquals(true, ref.isPresent());
    assertEquals("A", ref.get().declaringType().getName());
    assertEquals(1, ref.get().getNoParams());
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext) Context(com.github.javaparser.symbolsolver.core.resolution.Context) CompilationUnitContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.CompilationUnitContext) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ClassOrInterfaceDeclarationContext(com.github.javaparser.symbolsolver.javaparsermodel.contexts.ClassOrInterfaceDeclarationContext) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) AbstractResolutionTest(com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest) Test(org.junit.Test)

Example 10 with ReferenceTypeImpl

use of com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl in project javaparser by javaparser.

the class VarTypeTest method resolveAReferenceType.

@Test
public void resolveAReferenceType() {
    CompilationUnit ast = javaParser.parse(ParseStart.COMPILATION_UNIT, provider("class X{void x(){var abc = \"\";}}")).getResult().get();
    VarType varType = ast.findFirst(VarType.class).get();
    ResolvedType resolvedType = varType.resolve();
    assertEquals(new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver), resolvedType);
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ReflectionClassDeclaration(com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration) VarType(com.github.javaparser.ast.type.VarType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Test(org.junit.Test)

Aggregations

ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)27 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)16 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)13 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)12 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)12 Test (org.junit.Test)9 MethodUsage (com.github.javaparser.resolution.MethodUsage)5 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)5 CompilationUnit (com.github.javaparser.ast.CompilationUnit)4 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)4 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)4 ResolvedTypeVariable (com.github.javaparser.resolution.types.ResolvedTypeVariable)4 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)4 UnsolvedSymbolException (com.github.javaparser.resolution.UnsolvedSymbolException)3 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)3 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)3 Context (com.github.javaparser.symbolsolver.core.resolution.Context)3 JavaParserTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver)3 File (java.io.File)3 Collectors (java.util.stream.Collectors)3