Search in sources :

Example 26 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.

the class ReflectionMethodResolutionLogic method solveMethod.

static SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> parameterTypes, boolean staticOnly, TypeSolver typeSolver, ResolvedReferenceTypeDeclaration scopeType, Class clazz) {
    List<ResolvedMethodDeclaration> methods = new ArrayList<>();
    Predicate<Method> staticOnlyCheck = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers()));
    for (Method method : clazz.getMethods()) {
        if (method.isBridge() || method.isSynthetic() || !method.getName().equals(name) || !staticOnlyCheck.test(method))
            continue;
        ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver);
        methods.add(methodDeclaration);
    }
    for (ResolvedReferenceType ancestor : scopeType.getAncestors()) {
        SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(ancestor.getTypeDeclaration(), name, parameterTypes, staticOnly, typeSolver);
        if (ref.isSolved()) {
            methods.add(ref.getCorrespondingDeclaration());
        }
    }
    if (scopeType.getAncestors().isEmpty()) {
        ReferenceTypeImpl objectClass = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
        SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(objectClass.getTypeDeclaration(), name, parameterTypes, staticOnly, typeSolver);
        if (ref.isSolved()) {
            methods.add(ref.getCorrespondingDeclaration());
        }
    }
    return MethodResolutionLogic.findMostApplicable(methods, name, parameterTypes, typeSolver);
}
Also used : ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) Predicate(java.util.function.Predicate) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) List(java.util.List) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodResolutionLogic(com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic) Context(com.github.javaparser.symbolsolver.core.resolution.Context) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) Method(java.lang.reflect.Method) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ResolvedMethodDeclaration(com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)

Example 27 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.

the class BoundSet method deriveImpliedBounds.

public BoundSet deriveImpliedBounds(TypeSolver typeSolver) {
    // As bound sets are constructed and grown during inference, it is possible that new bounds can be inferred
    // based on the assertions of the original bounds. The process of incorporation identifies these new bounds
    // and adds them to the bound set.
    // 
    // Incorporation can happen in two scenarios. One scenario is that the bound set contains complementary pairs
    // of bounds; this implies new constraint formulas, as specified in §18.3.1. The other scenario is that the
    // bound set contains a bound involving capture conversion; this implies new bounds and may imply new
    // constraint formulas, as specified in §18.3.2. In both scenarios, any new constraint formulas are reduced,
    // and any new bounds are added to the bound set. This may trigger further incorporation; ultimately, the set
    // will reach a fixed point and no further bounds can be inferred.
    // 
    // If incorporation of a bound set has reached a fixed point, and the set does not contain the bound false,
    // then the bound set has the following properties:
    // 
    // - For each combination of a proper lower bound L and a proper upper bound U of an inference variable, L <: U.
    // 
    // - If every inference variable mentioned by a bound has an instantiation, the bound is satisfied by the
    // corresponding substitution.
    // 
    // - Given a dependency α = β, every bound of α matches a bound of β, and vice versa.
    // 
    // - Given a dependency α <: β, every lower bound of α is a lower bound of β, and every upper bound of β is an
    // upper bound of α.
    ConstraintFormulaSet newConstraintsSet = ConstraintFormulaSet.empty();
    // SECTION Complementary Pairs of Bounds
    // (In this section, S and T are inference variables or types, and U is a proper type. For conciseness, a bound
    // of the form α = T may also match a bound of the form T = α.)
    // 
    // When a bound set contains a pair of bounds that match one of the following rules, a new constraint formula
    // is implied:
    // 
    // - α = S and α = T imply ‹S = T›
    newConstraintsSet = forEachPairSameAs((a, b, currentConstraintSet) -> {
        if (areSameTypeInference(a.getS(), b.getS())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(a.getT(), b.getT()));
        }
        if (areSameTypeInference(a.getS(), b.getT())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(a.getS(), b.getT()));
        }
        if (areSameTypeInference(a.getT(), b.getS())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(a.getT(), b.getS()));
        }
        if (areSameTypeInference(a.getT(), b.getT())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(a.getS(), b.getS()));
        }
        return currentConstraintSet;
    }, newConstraintsSet);
    // - α = S and α <: T imply ‹S <: T›
    newConstraintsSet = forEachPairSameAndSubtype((a, b, currentConstraintSet) -> {
        if (areSameTypeInference(a.getS(), b.getS())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, a.getT(), b.getT()));
        }
        if (areSameTypeInference(a.getT(), b.getS())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, a.getS(), b.getT()));
        }
        return currentConstraintSet;
    }, newConstraintsSet);
    // - α = S and T <: α imply ‹T <: S›
    newConstraintsSet = forEachPairSameAndSubtype((a, b, currentConstraintSet) -> {
        if (areSameTypeInference(a.getS(), b.getT())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, b.getS(), a.getT()));
        }
        if (areSameTypeInference(a.getT(), b.getT())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, b.getS(), a.getS()));
        }
        return currentConstraintSet;
    }, newConstraintsSet);
    // - S <: α and α <: T imply ‹S <: T›
    newConstraintsSet = forEachPairSubtypeAndSubtype((a, b, currentConstraintSet) -> {
        if (areSameTypeInference(a.getT(), b.getS())) {
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, b.getS(), a.getT()));
        }
        return currentConstraintSet;
    }, newConstraintsSet);
    // - α = U and S = T imply ‹S[α:=U] = T[α:=U]›
    newConstraintsSet = forEachPairSameAs((a, b, currentConstraintSet) -> {
        if (isInferenceVariable(a.getS()) && isProperType(a.getT())) {
            InferenceVariable alpha = (InferenceVariable) a.getS();
            ResolvedType U = a.getT();
            ResolvedType S = b.getS();
            ResolvedType T = b.getT();
            Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T)));
        }
        if (isInferenceVariable(a.getT()) && isProperType(a.getS())) {
            InferenceVariable alpha = (InferenceVariable) a.getT();
            ResolvedType U = a.getS();
            ResolvedType S = b.getS();
            ResolvedType T = b.getT();
            Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T)));
        }
        if (isInferenceVariable(b.getS()) && isProperType(b.getT())) {
            InferenceVariable alpha = (InferenceVariable) b.getS();
            ResolvedType U = b.getT();
            ResolvedType S = a.getS();
            ResolvedType T = a.getT();
            Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T)));
        }
        if (isInferenceVariable(b.getT()) && isProperType(b.getS())) {
            InferenceVariable alpha = (InferenceVariable) b.getT();
            ResolvedType U = b.getS();
            ResolvedType S = a.getS();
            ResolvedType T = a.getT();
            Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(sub.apply(S), sub.apply(T)));
        }
        return currentConstraintSet;
    }, newConstraintsSet);
    // - α = U and S <: T imply ‹S[α:=U] <: T[α:=U]›
    newConstraintsSet = forEachPairSameAndSubtype((a, b, currentConstraintSet) -> {
        if (isInferenceVariable(a.getS()) && isProperType(a.getT())) {
            InferenceVariable alpha = (InferenceVariable) a.getS();
            ResolvedType U = a.getT();
            ResolvedType S = b.getS();
            ResolvedType T = b.getT();
            Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, sub.apply(S), sub.apply(T)));
        }
        if (isInferenceVariable(a.getT()) && isProperType(a.getS())) {
            InferenceVariable alpha = (InferenceVariable) a.getT();
            ResolvedType U = a.getS();
            ResolvedType S = b.getS();
            ResolvedType T = b.getT();
            Substitution sub = Substitution.empty().withPair(alpha.getTypeParameterDeclaration(), U);
            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSubtypeOfType(typeSolver, sub.apply(S), sub.apply(T)));
        }
        return currentConstraintSet;
    }, newConstraintsSet);
    // When a bound set contains a pair of bounds α <: S and α <: T, and there exists a supertype of S of the
    // form G<S1, ..., Sn> and a supertype of T of the form G<T1, ..., Tn> (for some generic class or interface, G),
    // then for all i (1 ≤ i ≤ n), if Si and Ti are types (not wildcards), the constraint formula ‹Si = Ti› is
    // implied.
    newConstraintsSet = forEachPairSubtypeAndSubtype((a, b, currentConstraintSet) -> {
        if (isInferenceVariable(a.getS()) && isInferenceVariable(b.getS())) {
            if (a.getT().isReferenceType() && b.getT().isReferenceType()) {
                ResolvedReferenceType S = a.getT().asReferenceType();
                ResolvedReferenceType T = b.getT().asReferenceType();
                List<Pair<ResolvedReferenceType, ResolvedReferenceType>> pairs = findPairsOfCommonAncestors(S, T);
                for (Pair<ResolvedReferenceType, ResolvedReferenceType> pair : pairs) {
                    for (int i = 0; i < Math.min(pair.a.typeParametersValues().size(), pair.b.typeParametersValues().size()); i++) {
                        ResolvedType si = pair.a.typeParametersValues().get(i);
                        ResolvedType ti = pair.b.typeParametersValues().get(i);
                        if (!si.isWildcard() && !ti.isWildcard()) {
                            currentConstraintSet = currentConstraintSet.withConstraint(new TypeSameAsType(si, ti));
                        }
                    }
                }
            }
        }
        return currentConstraintSet;
    }, newConstraintsSet);
    for (Bound b : this.bounds.stream().filter(b -> b instanceof CapturesBound).collect(Collectors.toList())) {
        CapturesBound capturesBound = (CapturesBound) b;
        throw new UnsupportedOperationException();
    // Let P1, ..., Pn represent the type parameters of G and let B1, ..., Bn represent the bounds of these type
    // parameters. Let θ represent the substitution [P1:=α1, ..., Pn:=αn]. Let R be a type that is not an inference
    // variable (but is not necessarily a proper type).
    // 
    // A set of bounds on α1, ..., αn is implied, constructed from the declared bounds of P1, ..., Pn as specified
    // in §18.1.3.
    // 
    // In addition, for all i (1 ≤ i ≤ n):
    // 
    // - If Ai is not a wildcard, then the bound αi = Ai is implied.
    // 
    // - If Ai is a wildcard of the form ?:
    // 
    // - αi = R implies the bound false
    // 
    // - αi <: R implies the constraint formula ‹Bi θ <: R›
    // 
    // - R <: αi implies the bound false
    // 
    // - If Ai is a wildcard of the form ? extends T:
    // 
    // - αi = R implies the bound false
    // 
    // - If Bi is Object, then αi <: R implies the constraint formula ‹T <: R›
    // 
    // - If T is Object, then αi <: R implies the constraint formula ‹Bi θ <: R›
    // 
    // - R <: αi implies the bound false
    // 
    // - If Ai is a wildcard of the form ? super T:
    // 
    // - αi = R implies the bound false
    // 
    // - αi <: R implies the constraint formula ‹Bi θ <: R›
    // 
    // - R <: αi implies the constraint formula ‹R <: T›
    }
    if (newConstraintsSet.isEmpty()) {
        return this;
    } else {
        BoundSet newBounds = newConstraintsSet.reduce(typeSolver);
        if (newBounds.isEmpty()) {
            return this;
        }
        return this.incorporate(newBounds, typeSolver);
    }
}
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) TypeSubtypeOfType(com.github.javaparser.symbolsolver.resolution.typeinference.constraintformulas.TypeSubtypeOfType) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) TypeSameAsType(com.github.javaparser.symbolsolver.resolution.typeinference.constraintformulas.TypeSameAsType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) Pair(com.github.javaparser.utils.Pair)

Example 28 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver 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 29 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.

the class ReflectionClassDeclaration method solveMethod.

@Deprecated
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly) {
    List<ResolvedMethodDeclaration> methods = new ArrayList<>();
    Predicate<Method> staticFilter = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers()));
    for (Method method : Arrays.stream(clazz.getDeclaredMethods()).filter((m) -> m.getName().equals(name)).filter(staticFilter).sorted(new MethodComparator()).collect(Collectors.toList())) {
        if (method.isBridge() || method.isSynthetic())
            continue;
        ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver);
        methods.add(methodDeclaration);
    }
    if (getSuperClass() != null) {
        ResolvedClassDeclaration superClass = (ResolvedClassDeclaration) getSuperClass().getTypeDeclaration();
        SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(superClass, name, argumentsTypes, staticOnly, typeSolver);
        if (ref.isSolved()) {
            methods.add(ref.getCorrespondingDeclaration());
        }
    }
    for (ResolvedReferenceType interfaceDeclaration : getInterfaces()) {
        SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(interfaceDeclaration.getTypeDeclaration(), name, argumentsTypes, staticOnly, typeSolver);
        if (ref.isSolved()) {
            methods.add(ref.getCorrespondingDeclaration());
        }
    }
    return MethodResolutionLogic.findMostApplicable(methods, name, argumentsTypes, typeSolver);
}
Also used : java.util(java.util) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) Predicate(java.util.function.Predicate) MethodUsage(com.github.javaparser.resolution.MethodUsage) ReferenceTypeImpl(com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl) AccessSpecifier(com.github.javaparser.ast.AccessSpecifier) AbstractClassDeclaration(com.github.javaparser.symbolsolver.logic.AbstractClassDeclaration) 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) ContextHelper(com.github.javaparser.symbolsolver.javaparsermodel.contexts.ContextHelper) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) MethodResolutionLogic(com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic) Context(com.github.javaparser.symbolsolver.core.resolution.Context) SymbolReference(com.github.javaparser.symbolsolver.model.resolution.SymbolReference) Modifier(java.lang.reflect.Modifier) MethodComparator(com.github.javaparser.symbolsolver.reflectionmodel.comparators.MethodComparator) Method(java.lang.reflect.Method) Node(com.github.javaparser.ast.Node) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) MethodComparator(com.github.javaparser.symbolsolver.reflectionmodel.comparators.MethodComparator) Method(java.lang.reflect.Method)

Example 30 with TypeSolver

use of com.github.javaparser.symbolsolver.model.resolution.TypeSolver in project javaparser by javaparser.

the class ReflectionFactory method typeUsageFor.

public static ResolvedType typeUsageFor(java.lang.reflect.Type type, TypeSolver typeSolver) {
    if (type instanceof java.lang.reflect.TypeVariable) {
        java.lang.reflect.TypeVariable<?> tv = (java.lang.reflect.TypeVariable<?>) type;
        boolean declaredOnClass = tv.getGenericDeclaration() instanceof java.lang.reflect.Type;
        ResolvedTypeParameterDeclaration typeParameter = new ReflectionTypeParameter(tv, declaredOnClass, typeSolver);
        return new ResolvedTypeVariable(typeParameter);
    } else if (type instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) type;
        ResolvedReferenceType rawType = typeUsageFor(pt.getRawType(), typeSolver).asReferenceType();
        List<java.lang.reflect.Type> actualTypes = new ArrayList<>();
        actualTypes.addAll(Arrays.asList(pt.getActualTypeArguments()));
        // we consume the actual types
        rawType = rawType.transformTypeParameters(tp -> typeUsageFor(actualTypes.remove(0), typeSolver)).asReferenceType();
        return rawType;
    } else if (type instanceof Class) {
        Class<?> c = (Class<?>) type;
        if (c.isPrimitive()) {
            if (c.getName().equals(Void.TYPE.getName())) {
                return ResolvedVoidType.INSTANCE;
            } else {
                return ResolvedPrimitiveType.byName(c.getName());
            }
        } else if (c.isArray()) {
            return new ResolvedArrayType(typeUsageFor(c.getComponentType(), typeSolver));
        } else {
            return new ReferenceTypeImpl(typeDeclarationFor(c, typeSolver), typeSolver);
        }
    } else if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = (GenericArrayType) type;
        return new ResolvedArrayType(typeUsageFor(genericArrayType.getGenericComponentType(), typeSolver));
    } else if (type instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) type;
        if (wildcardType.getLowerBounds().length > 0 && wildcardType.getUpperBounds().length > 0) {
            if (wildcardType.getUpperBounds().length == 1 && wildcardType.getUpperBounds()[0].getTypeName().equals("java.lang.Object")) {
            // ok, it does not matter
            }
        }
        if (wildcardType.getLowerBounds().length > 0) {
            if (wildcardType.getLowerBounds().length > 1) {
                throw new UnsupportedOperationException();
            }
            return ResolvedWildcard.superBound(typeUsageFor(wildcardType.getLowerBounds()[0], typeSolver));
        }
        if (wildcardType.getUpperBounds().length > 0) {
            if (wildcardType.getUpperBounds().length > 1) {
                throw new UnsupportedOperationException();
            }
            return ResolvedWildcard.extendsBound(typeUsageFor(wildcardType.getUpperBounds()[0], typeSolver));
        }
        return ResolvedWildcard.UNBOUNDED;
    } else {
        throw new UnsupportedOperationException(type.getClass().getCanonicalName() + " " + type);
    }
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) Arrays(java.util.Arrays) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) com.github.javaparser.symbolsolver.model.typesystem(com.github.javaparser.symbolsolver.model.typesystem) WildcardType(java.lang.reflect.WildcardType) AccessSpecifier(com.github.javaparser.ast.AccessSpecifier) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ArrayList(java.util.ArrayList) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) List(java.util.List) ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) ParameterizedType(java.lang.reflect.ParameterizedType) com.github.javaparser.resolution.types(com.github.javaparser.resolution.types) Modifier(java.lang.reflect.Modifier) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) ResolvedTypeVariable(com.github.javaparser.resolution.types.ResolvedTypeVariable) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)114 Test (org.junit.Test)89 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)76 CompilationUnit (com.github.javaparser.ast.CompilationUnit)59 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)46 AbstractTest (com.github.javaparser.symbolsolver.AbstractTest)40 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)33 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)32 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)30 MethodUsage (com.github.javaparser.resolution.MethodUsage)27 JavaParserFacade (com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade)25 AbstractResolutionTest (com.github.javaparser.symbolsolver.resolution.AbstractResolutionTest)21 ReflectionClassDeclaration (com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration)20 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)18 ReferenceTypeImpl (com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl)18 Collectors (java.util.stream.Collectors)17 ResolvedMethodDeclaration (com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration)16 ResolvedClassDeclaration (com.github.javaparser.resolution.declarations.ResolvedClassDeclaration)13 java.util (java.util)13 CombinedTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver)12