use of com.github.javaparser.resolution.types.ResolvedTypeVariable in project javaparser by javaparser.
the class VoidTypeTest method setup.
@Before
public void setup() {
typeSolver = new ReflectionTypeSolver();
OBJECT = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
STRING = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver);
arrayOfBooleans = new ResolvedArrayType(ResolvedPrimitiveType.BOOLEAN);
arrayOfListOfA = new ResolvedArrayType(new ReferenceTypeImpl(new ReflectionInterfaceDeclaration(List.class, typeSolver), ImmutableList.of(new ResolvedTypeVariable(ResolvedTypeParameterDeclaration.onType("A", "foo.Bar", Collections.emptyList()))), typeSolver));
}
use of com.github.javaparser.resolution.types.ResolvedTypeVariable 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());
}
use of com.github.javaparser.resolution.types.ResolvedTypeVariable 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.types.ResolvedTypeVariable in project javaparser by javaparser.
the class ReflectionMethodResolutionLogic method replaceParams.
private static MethodUsage replaceParams(List<ResolvedType> typeParameterValues, ResolvedReferenceTypeDeclaration typeParametrizable, ResolvedMethodDeclaration methodDeclaration) {
MethodUsage methodUsage = new MethodUsage(methodDeclaration);
int i = 0;
// Only replace if we have enough values provided
if (typeParameterValues.size() == typeParametrizable.getTypeParameters().size()) {
for (ResolvedTypeParameterDeclaration tp : typeParametrizable.getTypeParameters()) {
methodUsage = methodUsage.replaceTypeParameter(tp, typeParameterValues.get(i));
i++;
}
}
for (ResolvedTypeParameterDeclaration methodTypeParameter : methodDeclaration.getTypeParameters()) {
methodUsage = methodUsage.replaceTypeParameter(methodTypeParameter, new ResolvedTypeVariable(methodTypeParameter));
}
return methodUsage;
}
use of com.github.javaparser.resolution.types.ResolvedTypeVariable in project javaparser by javaparser.
the class InferenceContextTest method setup.
@Before
public void setup() {
typeSolver = new ReflectionTypeSolver();
string = new ReferenceTypeImpl(new ReflectionClassDeclaration(String.class, typeSolver), typeSolver);
object = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
listOfString = listOf(string);
tpE = mock(ResolvedTypeParameterDeclaration.class);
when(tpE.getName()).thenReturn("T");
listOfE = listOf(new ResolvedTypeVariable(tpE));
}
Aggregations