use of com.github.javaparser.resolution.types.ResolvedWildcard in project javaparser by javaparser.
the class JavaParserFacade method solveGenericTypes.
protected static ResolvedType solveGenericTypes(ResolvedType type, Context context) {
if (type.isTypeVariable()) {
return context.solveGenericType(type.describe()).orElse(type);
}
if (type.isWildcard()) {
if (type.asWildcard().isExtends() || type.asWildcard().isSuper()) {
ResolvedWildcard wildcardUsage = type.asWildcard();
ResolvedType boundResolved = solveGenericTypes(wildcardUsage.getBoundedType(), context);
if (wildcardUsage.isExtends()) {
return ResolvedWildcard.extendsBound(boundResolved);
} else {
return ResolvedWildcard.superBound(boundResolved);
}
}
}
return type;
}
use of com.github.javaparser.resolution.types.ResolvedWildcard in project javaparser by javaparser.
the class InferenceContext method registerCorrespondance.
private void registerCorrespondance(ResolvedType formalType, ResolvedType actualType) {
if (formalType.isReferenceType() && actualType.isReferenceType()) {
ResolvedReferenceType formalTypeAsReference = formalType.asReferenceType();
ResolvedReferenceType actualTypeAsReference = actualType.asReferenceType();
if (!formalTypeAsReference.getQualifiedName().equals(actualTypeAsReference.getQualifiedName())) {
List<ResolvedReferenceType> ancestors = actualTypeAsReference.getAllAncestors();
final String formalParamTypeQName = formalTypeAsReference.getQualifiedName();
List<ResolvedType> correspondingFormalType = ancestors.stream().filter((a) -> a.getQualifiedName().equals(formalParamTypeQName)).collect(Collectors.toList());
if (correspondingFormalType.isEmpty()) {
ancestors = formalTypeAsReference.getAllAncestors();
final String actualParamTypeQname = actualTypeAsReference.getQualifiedName();
List<ResolvedType> correspondingActualType = ancestors.stream().filter(a -> a.getQualifiedName().equals(actualParamTypeQname)).collect(Collectors.toList());
if (correspondingActualType.isEmpty()) {
throw new ConfilictingGenericTypesException(formalType, actualType);
}
correspondingFormalType = correspondingActualType;
}
actualTypeAsReference = correspondingFormalType.get(0).asReferenceType();
}
if (formalTypeAsReference.getQualifiedName().equals(actualTypeAsReference.getQualifiedName())) {
if (!formalTypeAsReference.typeParametersValues().isEmpty()) {
if (actualTypeAsReference.isRawType()) {
// nothing to do
} else {
int i = 0;
for (ResolvedType formalTypeParameter : formalTypeAsReference.typeParametersValues()) {
registerCorrespondance(formalTypeParameter, actualTypeAsReference.typeParametersValues().get(i));
i++;
}
}
}
}
} else if (formalType instanceof InferenceVariableType && !actualType.isPrimitive()) {
((InferenceVariableType) formalType).registerEquivalentType(actualType);
if (actualType instanceof InferenceVariableType) {
((InferenceVariableType) actualType).registerEquivalentType(formalType);
}
} else if (actualType.isNull()) {
// nothing to do
} else if (actualType.equals(formalType)) {
// nothing to do
} else if (actualType.isArray() && formalType.isArray()) {
registerCorrespondance(formalType.asArrayType().getComponentType(), actualType.asArrayType().getComponentType());
} else if (formalType.isWildcard()) {
// nothing to do
if ((actualType instanceof InferenceVariableType) && formalType.asWildcard().isBounded()) {
((InferenceVariableType) actualType).registerEquivalentType(formalType.asWildcard().getBoundedType());
if (formalType.asWildcard().getBoundedType() instanceof InferenceVariableType) {
((InferenceVariableType) formalType.asWildcard().getBoundedType()).registerEquivalentType(actualType);
}
}
if (actualType.isWildcard()) {
ResolvedWildcard formalWildcard = formalType.asWildcard();
ResolvedWildcard actualWildcard = actualType.asWildcard();
if (formalWildcard.isBounded() && formalWildcard.getBoundedType() instanceof InferenceVariableType) {
if (formalWildcard.isSuper() && actualWildcard.isSuper()) {
((InferenceVariableType) formalType.asWildcard().getBoundedType()).registerEquivalentType(actualWildcard.getBoundedType());
} else if (formalWildcard.isExtends() && actualWildcard.isExtends()) {
((InferenceVariableType) formalType.asWildcard().getBoundedType()).registerEquivalentType(actualWildcard.getBoundedType());
}
}
}
if (actualType.isReferenceType()) {
if (formalType.asWildcard().isBounded()) {
registerCorrespondance(formalType.asWildcard().getBoundedType(), actualType);
}
}
} else if (actualType instanceof InferenceVariableType) {
if (formalType instanceof ResolvedReferenceType) {
((InferenceVariableType) actualType).registerEquivalentType(formalType);
} else if (formalType instanceof InferenceVariableType) {
((InferenceVariableType) actualType).registerEquivalentType(formalType);
}
} else if (actualType.isConstraint()) {
ResolvedLambdaConstraintType constraintType = actualType.asConstraintType();
if (constraintType.getBound() instanceof InferenceVariableType) {
((InferenceVariableType) constraintType.getBound()).registerEquivalentType(formalType);
}
} else if (actualType.isPrimitive()) {
if (formalType.isPrimitive()) {
// nothing to do
} else {
registerCorrespondance(formalType, objectProvider.byName(actualType.asPrimitive().getBoxTypeQName()));
}
} else if (actualType.isReferenceType()) {
if (formalType.isPrimitive()) {
if (formalType.asPrimitive().getBoxTypeQName().equals(actualType.describe())) {
registerCorrespondance(objectProvider.byName(formalType.asPrimitive().getBoxTypeQName()), actualType);
} else {
// nothing to do
}
} else {
// nothing to do
}
} else if (formalType.isReferenceType()) {
ResolvedReferenceType formalTypeAsReference = formalType.asReferenceType();
if (formalTypeAsReference.isJavaLangObject()) {
// nothing to do
} else {
throw new UnsupportedOperationException(formalType.describe() + " " + actualType.describe());
}
} else {
throw new UnsupportedOperationException(formalType.describe() + " " + actualType.describe());
}
}
Aggregations