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);
}
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();
}
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;
}
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());
}
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);
}
Aggregations