use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.
the class ReferenceTypeTest method testReplaceParam.
@Test
public void testReplaceParam() {
ResolvedTypeParameterDeclaration tpA = ResolvedTypeParameterDeclaration.onType("A", "foo.Bar", Collections.emptyList());
assertTrue(object == object.replaceTypeVariables(tpA, object));
assertTrue(string == string.replaceTypeVariables(tpA, object));
assertEquals(listOfStrings, listOfStrings.replaceTypeVariables(tpA, object));
assertEquals(listOfStrings, listOfA.replaceTypeVariables(tpA, string));
}
use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.
the class ReferenceTypeTest method testReplaceTypeVariables.
@Test
public void testReplaceTypeVariables() {
TypeSolver typeResolver = new ReflectionTypeSolver();
ResolvedInterfaceDeclaration streamInterface = new ReflectionInterfaceDeclaration(Stream.class, typeResolver);
ResolvedReferenceType stream = new ReferenceTypeImpl(streamInterface, typeResolver);
ResolvedMethodDeclaration streamMap = streamInterface.getDeclaredMethods().stream().filter(m -> m.getName().equals("map")).findFirst().get();
ResolvedTypeParameterDeclaration streamMapR = streamMap.findTypeParameter("T").get();
ResolvedTypeVariable typeVariable = new ResolvedTypeVariable(streamMapR);
stream = stream.deriveTypeParameters(stream.typeParametersMap().toBuilder().setValue(stream.getTypeDeclaration().getTypeParameters().get(0), typeVariable).build());
ResolvedTypeParameterDeclaration tpToReplace = streamInterface.getTypeParameters().get(0);
ResolvedType replaced = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeResolver), typeResolver);
ResolvedType streamReplaced = stream.replaceTypeVariables(tpToReplace, replaced);
assertEquals("java.util.stream.Stream<java.lang.String>", streamReplaced.describe());
}
use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.
the class WildcardUsageTest method testReplaceParam.
@Test
public void testReplaceParam() {
ResolvedTypeParameterDeclaration tpA = ResolvedTypeParameterDeclaration.onType("A", "foo.Bar", Collections.emptyList());
ResolvedTypeParameterDeclaration tpB = ResolvedTypeParameterDeclaration.onType("B", "foo.Bar", Collections.emptyList());
assertTrue(unbounded == unbounded.replaceTypeVariables(tpA, string));
assertTrue(superFoo == superFoo.replaceTypeVariables(tpA, string));
assertTrue(extendsFoo == extendsFoo.replaceTypeVariables(tpA, string));
assertEquals(superString, superA.replaceTypeVariables(tpA, string));
assertEquals(extendsString, extendsA.replaceTypeVariables(tpA, string));
assertTrue(superA == superA.replaceTypeVariables(tpB, string));
assertTrue(extendsA == extendsA.replaceTypeVariables(tpB, string));
}
use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration 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);
}
}
use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.
the class TypeInference method instantiationInference.
public Optional<InstantiationSet> instantiationInference(List<Expression> argumentExpressions, ResolvedMethodDeclaration methodDeclaration) {
// if (methodCallExpr.getTypeArguments().isPresent()) {
// throw new IllegalArgumentException("Type inference unnecessary as type arguments have been specified");
// }
// Given a method invocation that provides no explicit type arguments, the process to determine whether a
// potentially applicable generic method m is applicable is as follows:
// - Where P1, ..., Pp (p ≥ 1) are the type parameters of m, let α1, ..., αp be inference variables, and
// let θ be the substitution [P1:=α1, ..., Pp:=αp].
List<ResolvedTypeParameterDeclaration> Ps = methodDeclaration.getTypeParameters();
List<InferenceVariable> alphas = InferenceVariable.instantiate(Ps);
Substitution theta = Substitution.empty();
for (int i = 0; i < Ps.size(); i++) {
theta = theta.withPair(Ps.get(0), alphas.get(0));
}
// - An initial bound set, B0, is constructed from the declared bounds of P1, ..., Pp, as described in §18.1.3.
BoundSet B0 = boundSetup(Ps, alphas);
// - For all i (1 ≤ i ≤ p), if Pi appears in the throws clause of m, then the bound throws αi is implied.
// These bounds, if any, are incorporated with B0 to produce a new bound set, B1.
BoundSet B1 = B0;
for (int i = 0; i < Ps.size(); i++) {
ResolvedTypeParameterDeclaration Pi = Ps.get(i);
if (appearInThrowsClause(Pi, methodDeclaration)) {
B1 = B1.withBound(new ThrowsBound(alphas.get(i)));
}
}
// - A set of constraint formulas, C, is constructed as follows.
//
// Let F1, ..., Fn be the formal parameter types of m, and let e1, ..., ek be the actual argument expressions
// of the invocation. Then:
List<ResolvedType> Fs = formalParameterTypes(methodDeclaration);
List<Expression> es = argumentExpressions;
Optional<ConstraintFormulaSet> C = Optional.empty();
if (!C.isPresent()) {
C = testForApplicabilityByStrictInvocation(Fs, es, theta);
}
if (!C.isPresent()) {
C = testForApplicabilityByLooseInvocation(Fs, es, theta);
}
if (!C.isPresent()) {
C = testForApplicabilityByVariableArityInvocation(Fs, es, theta);
}
if (!C.isPresent()) {
return Optional.empty();
}
// - C is reduced (§18.2) and the resulting bounds are incorporated with B1 to produce a new bound set, B2.
BoundSet resultingBounds = C.get().reduce(typeSolver);
BoundSet B2 = B1.incorporate(resultingBounds, typeSolver);
if (B2.containsFalse()) {
return Optional.empty();
}
Optional<InstantiationSet> instantiation = B2.performResolution(alphas, typeSolver);
return instantiation;
}
Aggregations