Search in sources :

Example 1 with TypeCompound

use of com.sun.tools.javac.code.Attribute.TypeCompound in project checker-framework by typetools.

the class MethodApplier method applyThrowsAnnotations.

/**
 * For each thrown type, collect all the annotations for that type and apply them.
 */
private void applyThrowsAnnotations(final List<Attribute.TypeCompound> annos) throws UnexpectedAnnotationLocationException {
    final List<AnnotatedTypeMirror> thrown = methodType.getThrownTypes();
    if (thrown.isEmpty()) {
        return;
    }
    Map<AnnotatedTypeMirror, List<TypeCompound>> typeToAnnos = new LinkedHashMap<>();
    for (final AnnotatedTypeMirror thrownType : thrown) {
        typeToAnnos.put(thrownType, new ArrayList<>());
    }
    for (TypeCompound anno : annos) {
        final TypeAnnotationPosition annoPos = anno.position;
        if (annoPos.type_index >= 0 && annoPos.type_index < thrown.size()) {
            final AnnotatedTypeMirror thrownType = thrown.get(annoPos.type_index);
            typeToAnnos.get(thrownType).add(anno);
        } else {
            throw new BugInCF("MethodApplier.applyThrowsAnnotation: " + "invalid throws index " + annoPos.type_index + " for annotation: " + anno + " for element: " + ElementUtils.getQualifiedName(element));
        }
    }
    for (final Map.Entry<AnnotatedTypeMirror, List<TypeCompound>> typeToAnno : typeToAnnos.entrySet()) {
        ElementAnnotationUtil.annotateViaTypeAnnoPosition(typeToAnno.getKey(), typeToAnno.getValue());
    }
}
Also used : TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound) ArrayList(java.util.ArrayList) List(java.util.List) TypeAnnotationPosition(com.sun.tools.javac.code.TypeAnnotationPosition) BugInCF(org.checkerframework.javacutil.BugInCF) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with TypeCompound

use of com.sun.tools.javac.code.Attribute.TypeCompound in project checker-framework by typetools.

the class TargetedElementAnnotationApplier method sift.

/**
 * Separate the input annotations into a Map of TargetClass (TARGETED, VALID, INVALID) to the
 * annotations that fall into each of those categories.
 *
 * @param typeCompounds annotations to sift through, should be those returned by
 *     getRawTypeAttributes
 * @return a {@literal Map<TargetClass => Annotations>.}
 */
protected Map<TargetClass, List<Attribute.TypeCompound>> sift(final Iterable<Attribute.TypeCompound> typeCompounds) {
    final Map<TargetClass, List<Attribute.TypeCompound>> targetClassToCompound = new EnumMap<>(TargetClass.class);
    for (TargetClass targetClass : TargetClass.values()) {
        targetClassToCompound.put(targetClass, new ArrayList<>());
    }
    for (final Attribute.TypeCompound typeCompound : typeCompounds) {
        final TargetType typeCompoundTarget = typeCompound.position.type;
        final List<Attribute.TypeCompound> destList;
        if (ElementAnnotationUtil.contains(typeCompoundTarget, annotatedTargets())) {
            destList = targetClassToCompound.get(TargetClass.TARGETED);
        } else if (ElementAnnotationUtil.contains(typeCompoundTarget, validTargets())) {
            destList = targetClassToCompound.get(TargetClass.VALID);
        } else {
            destList = targetClassToCompound.get(TargetClass.INVALID);
        }
        destList.add(typeCompound);
    }
    return targetClassToCompound;
}
Also used : TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound) Attribute(com.sun.tools.javac.code.Attribute) TargetType(com.sun.tools.javac.code.TargetType) ArrayList(java.util.ArrayList) List(java.util.List) TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound) EnumMap(java.util.EnumMap)

Example 3 with TypeCompound

use of com.sun.tools.javac.code.Attribute.TypeCompound in project checker-framework by typetools.

the class TypeParamElementAnnotationApplier method applyUpperBounds.

/**
 * Applies a list of annotations to the upperBound of the type parameter. If the type of the upper
 * bound is an intersection we must first find the correct location for each annotation.
 */
private void applyUpperBounds(final List<TypeCompound> upperBounds) {
    if (!upperBounds.isEmpty()) {
        final AnnotatedTypeMirror upperBoundType = typeParam.getUpperBound();
        if (upperBoundType.getKind() == TypeKind.INTERSECTION) {
            final List<AnnotatedTypeMirror> bounds = ((AnnotatedIntersectionType) upperBoundType).getBounds();
            final int boundIndexOffset = ElementAnnotationUtil.getBoundIndexOffset(bounds);
            for (final TypeCompound anno : upperBounds) {
                final int boundIndex = anno.position.bound_index + boundIndexOffset;
                if (boundIndex < 0 || boundIndex > bounds.size()) {
                    throw new BugInCF("Invalid bound index on element annotation ( " + anno + " ) " + "for type ( " + typeParam + " ) with " + "upper bound ( " + typeParam.getUpperBound() + " ) " + "and boundIndex( " + boundIndex + " ) ");
                }
                // TODO: WHY NOT ADD?
                bounds.get(boundIndex).replaceAnnotation(anno);
            }
            ((AnnotatedIntersectionType) upperBoundType).copyIntersectionBoundAnnotations();
        } else {
            upperBoundType.addAnnotations(upperBounds);
        }
    }
}
Also used : TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound) AnnotatedIntersectionType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedIntersectionType) BugInCF(org.checkerframework.javacutil.BugInCF) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 4 with TypeCompound

use of com.sun.tools.javac.code.Attribute.TypeCompound in project checker-framework by typetools.

the class TypesIntoElements method addUniqueTypeCompounds.

private static void addUniqueTypeCompounds(Types types, Symbol sym, List<TypeCompound> tcs) {
    List<TypeCompound> raw = sym.getRawTypeAttributes();
    List<Attribute.TypeCompound> res = List.nil();
    for (Attribute.TypeCompound tc : tcs) {
        if (!TypeAnnotationUtils.isTypeCompoundContained(raw, tc, types)) {
            res = res.append(tc);
        }
    }
    // That method only uses reference equality. isTypeCompoundContained does a deep comparison.
    sym.appendUniqueTypeAttributes(res);
}
Also used : TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound) Attribute(com.sun.tools.javac.code.Attribute) TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound)

Example 5 with TypeCompound

use of com.sun.tools.javac.code.Attribute.TypeCompound in project checker-framework by typetools.

the class ParamApplier method sift.

@Override
protected Map<TargetClass, List<TypeCompound>> sift(Iterable<Attribute.TypeCompound> typeCompounds) {
    // this will sift out the annotations that do not have the right position index
    final Map<TargetClass, List<Attribute.TypeCompound>> targetClassToAnnos = super.sift(typeCompounds);
    final List<Attribute.TypeCompound> targeted = targetClassToAnnos.get(TargetClass.TARGETED);
    final List<Attribute.TypeCompound> valid = targetClassToAnnos.get(TargetClass.VALID);
    // if this is a lambdaParam, filter out from targeted those annos that apply to method
    // formal parameters if this is a method formal param, filter out from targeted those annos
    // that apply to lambdas
    int i = 0;
    while (i < targeted.size()) {
        final Tree onLambda = targeted.get(i).position.onLambda;
        if (onLambda == null) {
            if (!isLambdaParam) {
                ++i;
            } else {
                valid.add(targeted.remove(i));
            }
        } else {
            if (onLambda.equals(this.lambdaTree)) {
                ++i;
            } else {
                valid.add(targeted.remove(i));
            }
        }
    }
    return targetClassToAnnos;
}
Also used : TypeCompound(com.sun.tools.javac.code.Attribute.TypeCompound) Attribute(com.sun.tools.javac.code.Attribute) VariableTree(com.sun.source.tree.VariableTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) Tree(com.sun.source.tree.Tree) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

TypeCompound (com.sun.tools.javac.code.Attribute.TypeCompound)6 ArrayList (java.util.ArrayList)4 Attribute (com.sun.tools.javac.code.Attribute)3 List (java.util.List)3 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)2 BugInCF (org.checkerframework.javacutil.BugInCF)2 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)1 Tree (com.sun.source.tree.Tree)1 VariableTree (com.sun.source.tree.VariableTree)1 TargetType (com.sun.tools.javac.code.TargetType)1 TypeAnnotationPosition (com.sun.tools.javac.code.TypeAnnotationPosition)1 EnumMap (java.util.EnumMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 AnnotatedIntersectionType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedIntersectionType)1