Search in sources :

Example 1 with ResolvedWildcard

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;
}
Also used : ResolvedWildcard(com.github.javaparser.resolution.types.ResolvedWildcard) ResolvedType(com.github.javaparser.resolution.types.ResolvedType)

Example 2 with ResolvedWildcard

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());
    }
}
Also used : ResolvedTypeParameterDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration) List(java.util.List) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) ResolvedArrayType(com.github.javaparser.resolution.types.ResolvedArrayType) ResolvedWildcard(com.github.javaparser.resolution.types.ResolvedWildcard) Map(java.util.Map) HashMap(java.util.HashMap) ResolvedLambdaConstraintType(com.github.javaparser.resolution.types.ResolvedLambdaConstraintType) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ResolvedLambdaConstraintType(com.github.javaparser.resolution.types.ResolvedLambdaConstraintType) ResolvedType(com.github.javaparser.resolution.types.ResolvedType) ResolvedWildcard(com.github.javaparser.resolution.types.ResolvedWildcard)

Aggregations

ResolvedType (com.github.javaparser.resolution.types.ResolvedType)2 ResolvedWildcard (com.github.javaparser.resolution.types.ResolvedWildcard)2 ResolvedTypeParameterDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration)1 ResolvedArrayType (com.github.javaparser.resolution.types.ResolvedArrayType)1 ResolvedLambdaConstraintType (com.github.javaparser.resolution.types.ResolvedLambdaConstraintType)1 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1