Search in sources :

Example 1 with TSuperU

use of org.checkerframework.framework.util.typeinference.constraint.TSuperU in project checker-framework by typetools.

the class DefaultTypeArgumentInference method addConstraintsBetweenTargets.

/**
 * Declarations of the form: {@code <A, B extends A>} implies a TUConstraint of {@code B <: A}.
 * Add these to the constraint list.
 */
public void addConstraintsBetweenTargets(Set<TUConstraint> constraints, Set<TypeVariable> targets, boolean asSubtype, AnnotatedTypeFactory typeFactory) {
    final Types types = typeFactory.getProcessingEnv().getTypeUtils();
    final List<TypeVariable> targetList = new ArrayList<>(targets);
    final Map<TypeVariable, AnnotatedTypeVariable> paramDeclarations = new HashMap<>();
    for (int i = 0; i < targetList.size(); i++) {
        final TypeVariable earlierTarget = targetList.get(i);
        for (int j = i + 1; j < targetList.size(); j++) {
            final TypeVariable laterTarget = targetList.get(j);
            if (types.isSameType(earlierTarget.getUpperBound(), laterTarget)) {
                final AnnotatedTypeVariable headDecl = addOrGetDeclarations(earlierTarget, typeFactory, paramDeclarations);
                final AnnotatedTypeVariable nextDecl = addOrGetDeclarations(laterTarget, typeFactory, paramDeclarations);
                if (asSubtype) {
                    constraints.add(new TSubU(headDecl, nextDecl));
                } else {
                    constraints.add(new TSuperU(nextDecl, headDecl));
                }
            } else if (types.isSameType(laterTarget.getUpperBound(), earlierTarget)) {
                final AnnotatedTypeVariable headDecl = addOrGetDeclarations(earlierTarget, typeFactory, paramDeclarations);
                final AnnotatedTypeVariable nextDecl = addOrGetDeclarations(laterTarget, typeFactory, paramDeclarations);
                if (asSubtype) {
                    constraints.add(new TSubU(nextDecl, headDecl));
                } else {
                    constraints.add(new TSuperU(headDecl, nextDecl));
                }
            }
        }
    }
}
Also used : Types(javax.lang.model.util.Types) AnnotatedTypes(org.checkerframework.framework.util.AnnotatedTypes) TypeVariable(javax.lang.model.type.TypeVariable) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) TSuperU(org.checkerframework.framework.util.typeinference.constraint.TSuperU) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) AFConstraint(org.checkerframework.framework.util.typeinference.constraint.AFConstraint) TUConstraint(org.checkerframework.framework.util.typeinference.constraint.TUConstraint) TSubU(org.checkerframework.framework.util.typeinference.constraint.TSubU)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 TypeVariable (javax.lang.model.type.TypeVariable)1 Types (javax.lang.model.util.Types)1 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)1 AnnotatedTypes (org.checkerframework.framework.util.AnnotatedTypes)1 AFConstraint (org.checkerframework.framework.util.typeinference.constraint.AFConstraint)1 TSubU (org.checkerframework.framework.util.typeinference.constraint.TSubU)1 TSuperU (org.checkerframework.framework.util.typeinference.constraint.TSuperU)1 TUConstraint (org.checkerframework.framework.util.typeinference.constraint.TUConstraint)1