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());
}
}
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;
}
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);
}
}
}
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);
}
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;
}
Aggregations