use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.
the class ReflectionMethodResolutionLogic method solveMethodAsUsage.
static Optional<MethodUsage> solveMethodAsUsage(String name, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, Context invokationContext, List<ResolvedType> typeParameterValues, ResolvedReferenceTypeDeclaration scopeType, Class clazz) {
if (typeParameterValues.size() != scopeType.getTypeParameters().size()) {
// if it is zero we are going to ignore them
if (!scopeType.getTypeParameters().isEmpty()) {
// Parameters not specified, so default to Object
typeParameterValues = new ArrayList<>();
for (int i = 0; i < scopeType.getTypeParameters().size(); i++) {
typeParameterValues.add(new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver));
}
}
}
List<MethodUsage> methods = new ArrayList<>();
for (Method method : clazz.getMethods()) {
if (method.getName().equals(name) && !method.isBridge() && !method.isSynthetic()) {
ResolvedMethodDeclaration methodDeclaration = new ReflectionMethodDeclaration(method, typeSolver);
MethodUsage methodUsage = replaceParams(typeParameterValues, scopeType, methodDeclaration);
methods.add(methodUsage);
}
}
for (ResolvedReferenceType ancestor : scopeType.getAncestors()) {
SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(ancestor.getTypeDeclaration(), name, argumentsTypes, typeSolver);
if (ref.isSolved()) {
ResolvedMethodDeclaration correspondingDeclaration = ref.getCorrespondingDeclaration();
MethodUsage methodUsage = replaceParams(typeParameterValues, ancestor.getTypeDeclaration(), correspondingDeclaration);
methods.add(methodUsage);
}
}
if (scopeType.getAncestors().isEmpty()) {
ReferenceTypeImpl objectClass = new ReferenceTypeImpl(new ReflectionClassDeclaration(Object.class, typeSolver), typeSolver);
SymbolReference<ResolvedMethodDeclaration> ref = MethodResolutionLogic.solveMethodInType(objectClass.getTypeDeclaration(), name, argumentsTypes, typeSolver);
if (ref.isSolved()) {
MethodUsage usage = replaceParams(typeParameterValues, objectClass.getTypeDeclaration(), ref.getCorrespondingDeclaration());
methods.add(usage);
}
}
final List<ResolvedType> finalTypeParameterValues = typeParameterValues;
argumentsTypes = argumentsTypes.stream().map((pt) -> {
int i = 0;
for (ResolvedTypeParameterDeclaration tp : scopeType.getTypeParameters()) {
pt = pt.replaceTypeVariables(tp, finalTypeParameterValues.get(i));
i++;
}
return pt;
}).collect(Collectors.toList());
return MethodResolutionLogic.findMostApplicableUsage(methods, name, argumentsTypes, typeSolver);
}
use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.
the class ConstructorResolutionLogic method isApplicable.
private static boolean isApplicable(ResolvedConstructorDeclaration constructor, List<ResolvedType> argumentsTypes, TypeSolver typeSolver, boolean withWildcardTolerance) {
if (constructor.hasVariadicParameter()) {
int pos = constructor.getNumberOfParams() - 1;
if (constructor.getNumberOfParams() == argumentsTypes.size()) {
// check if the last value is directly assignable as an array
ResolvedType expectedType = constructor.getLastParam().getType();
ResolvedType actualType = argumentsTypes.get(pos);
if (!expectedType.isAssignableBy(actualType)) {
for (ResolvedTypeParameterDeclaration tp : constructor.getTypeParameters()) {
expectedType = MethodResolutionLogic.replaceTypeParam(expectedType, tp, typeSolver);
}
if (!expectedType.isAssignableBy(actualType)) {
if (actualType.isArray() && expectedType.isAssignableBy(actualType.asArrayType().getComponentType())) {
argumentsTypes.set(pos, actualType.asArrayType().getComponentType());
} else {
argumentsTypes = groupVariadicParamValues(argumentsTypes, pos, constructor.getLastParam().getType());
}
}
}
// else it is already assignable, nothing to do
} else {
if (pos > argumentsTypes.size()) {
return false;
}
argumentsTypes = groupVariadicParamValues(argumentsTypes, pos, constructor.getLastParam().getType());
}
}
if (constructor.getNumberOfParams() != argumentsTypes.size()) {
return false;
}
Map<String, ResolvedType> matchedParameters = new HashMap<>();
boolean needForWildCardTolerance = false;
for (int i = 0; i < constructor.getNumberOfParams(); i++) {
ResolvedType expectedType = constructor.getParam(i).getType();
ResolvedType actualType = argumentsTypes.get(i);
if ((expectedType.isTypeVariable() && !(expectedType.isWildcard())) && expectedType.asTypeParameter().declaredOnMethod()) {
matchedParameters.put(expectedType.asTypeParameter().getName(), actualType);
continue;
}
boolean isAssignableWithoutSubstitution = expectedType.isAssignableBy(actualType) || (constructor.getParam(i).isVariadic() && new ResolvedArrayType(expectedType).isAssignableBy(actualType));
if (!isAssignableWithoutSubstitution && expectedType.isReferenceType() && actualType.isReferenceType()) {
isAssignableWithoutSubstitution = MethodResolutionLogic.isAssignableMatchTypeParameters(expectedType.asReferenceType(), actualType.asReferenceType(), matchedParameters);
}
if (!isAssignableWithoutSubstitution) {
List<ResolvedTypeParameterDeclaration> typeParameters = constructor.getTypeParameters();
typeParameters.addAll(constructor.declaringType().getTypeParameters());
for (ResolvedTypeParameterDeclaration tp : typeParameters) {
expectedType = MethodResolutionLogic.replaceTypeParam(expectedType, tp, typeSolver);
}
if (!expectedType.isAssignableBy(actualType)) {
if (actualType.isWildcard() && withWildcardTolerance && !expectedType.isPrimitive()) {
needForWildCardTolerance = true;
continue;
}
if (constructor.hasVariadicParameter() && i == constructor.getNumberOfParams() - 1) {
if (new ResolvedArrayType(expectedType).isAssignableBy(actualType)) {
continue;
}
}
return false;
}
}
}
return !withWildcardTolerance || needForWildCardTolerance;
}
use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.
the class JavassistUtils method signatureTypeToType.
static ResolvedType signatureTypeToType(SignatureAttribute.Type signatureType, TypeSolver typeSolver, ResolvedTypeParametrizable typeParametrizable) {
if (signatureType instanceof SignatureAttribute.ClassType) {
SignatureAttribute.ClassType classType = (SignatureAttribute.ClassType) signatureType;
List<ResolvedType> typeArguments = classType.getTypeArguments() == null ? Collections.emptyList() : Arrays.stream(classType.getTypeArguments()).map(ta -> typeArgumentToType(ta, typeSolver, typeParametrizable)).collect(Collectors.toList());
final String typeName = classType.getDeclaringClass() != null ? classType.getDeclaringClass().getName() + "." + classType.getName() : classType.getName();
ResolvedReferenceTypeDeclaration typeDeclaration = typeSolver.solveType(removeTypeArguments(internalNameToCanonicalName(typeName)));
return new ReferenceTypeImpl(typeDeclaration, typeArguments, typeSolver);
} else if (signatureType instanceof SignatureAttribute.TypeVariable) {
SignatureAttribute.TypeVariable typeVariableSignature = (SignatureAttribute.TypeVariable) signatureType;
Optional<ResolvedTypeParameterDeclaration> typeParameterDeclarationOpt = typeParametrizable.findTypeParameter(typeVariableSignature.getName());
if (!typeParameterDeclarationOpt.isPresent()) {
throw new UnsolvedSymbolException("Unable to solve TypeVariable " + typeVariableSignature);
}
ResolvedTypeParameterDeclaration typeParameterDeclaration = typeParameterDeclarationOpt.get();
return new ResolvedTypeVariable(typeParameterDeclaration);
} else if (signatureType instanceof SignatureAttribute.ArrayType) {
SignatureAttribute.ArrayType arrayType = (SignatureAttribute.ArrayType) signatureType;
return new ResolvedArrayType(signatureTypeToType(arrayType.getComponentType(), typeSolver, typeParametrizable));
} else if (signatureType instanceof SignatureAttribute.BaseType) {
SignatureAttribute.BaseType baseType = (SignatureAttribute.BaseType) signatureType;
if (baseType.toString().equals("void")) {
return ResolvedVoidType.INSTANCE;
} else {
return ResolvedPrimitiveType.byName(baseType.toString());
}
} else {
throw new RuntimeException(signatureType.getClass().getCanonicalName());
}
}
use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.
the class TypeInference method boundSetup.
// /
// / Private instance methods
// /
/**
* When inference begins, a bound set is typically generated from a list of type parameter declarations P1, ..., Pp
* and associated inference variables α1, ..., αp
*
* @param typeParameterDeclarations
* @param inferenceVariables
* @return
*/
private BoundSet boundSetup(List<ResolvedTypeParameterDeclaration> typeParameterDeclarations, List<InferenceVariable> inferenceVariables) {
if (typeParameterDeclarations.size() != inferenceVariables.size()) {
throw new IllegalArgumentException();
}
// When inference begins, a bound set is typically generated from a list of
// type parameter declarations P1, ..., Pp and associated inference variables α1, ..., αp.
// Such a bound set is constructed as follows. For each l (1 ≤ l ≤ p):
BoundSet boundSet = BoundSet.empty();
for (int l = 0; l < typeParameterDeclarations.size(); l++) {
ResolvedTypeParameterDeclaration Pl = typeParameterDeclarations.get(l);
InferenceVariable alphaL = inferenceVariables.get(l);
if (Pl.getBounds().isEmpty()) {
boundSet = boundSet.withBound(new SubtypeOfBound(alphaL, object));
} else {
for (ResolvedTypeParameterDeclaration.Bound bound : Pl.getBounds()) {
ResolvedType T = bound.getType();
Substitution substitution = Substitution.empty();
for (int j = 0; j < typeParameterDeclarations.size(); j++) {
substitution = substitution.withPair(typeParameterDeclarations.get(j), inferenceVariables.get(j));
}
ResolvedType TWithSubstitutions = substitution.apply(T);
boundSet = boundSet.withBound(new SubtypeOfBound(alphaL, TWithSubstitutions));
if (boundSet.getProperUpperBoundsFor(alphaL).isEmpty()) {
boundSet = boundSet.withBound(new SubtypeOfBound(alphaL, object));
}
}
}
}
return boundSet;
}
use of com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration in project javaparser by javaparser.
the class ReferenceTypeTest method testReplaceTypeVariablesWithLambdaInBetween.
@Test
public void testReplaceTypeVariablesWithLambdaInBetween() {
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());
}
Aggregations