use of com.sun.tools.javac.code.TypeAnnotationPosition 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) {
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 {
ErrorReporter.errorAbort("MethodApplier.applyThrowsAnnotation: " + "invalid throws index " + annoPos.type_index + " for annotation: " + anno + " for element: " + ElementUtils.getVerboseName(element));
}
}
for (final Entry<AnnotatedTypeMirror, List<TypeCompound>> typeToAnno : typeToAnnos.entrySet()) {
ElementAnnotationUtil.annotateViaTypeAnnoPosition(typeToAnno.getKey(), typeToAnno.getValue());
}
}
use of com.sun.tools.javac.code.TypeAnnotationPosition in project checker-framework by typetools.
the class TypeVarUseApplier method getVariableAnnos.
/**
* @return annotations on an element that apply to variable declarations
*/
private static List<Attribute.TypeCompound> getVariableAnnos(final Element variableElem) {
final VarSymbol varSymbol = (VarSymbol) variableElem;
final List<Attribute.TypeCompound> annotations = new ArrayList<>();
for (Attribute.TypeCompound anno : varSymbol.getRawTypeAttributes()) {
TypeAnnotationPosition pos = anno.position;
switch(pos.type) {
case FIELD:
case LOCAL_VARIABLE:
case RESOURCE_VARIABLE:
case EXCEPTION_PARAMETER:
annotations.add(anno);
break;
default:
}
}
return annotations;
}
use of com.sun.tools.javac.code.TypeAnnotationPosition in project checker-framework by typetools.
the class TypesIntoElements method storeVariable.
private static void storeVariable(ProcessingEnvironment processingEnv, Types types, AnnotatedTypeFactory atypeFactory, VariableTree var) {
VarSymbol sym = (VarSymbol) TreeUtils.elementFromDeclaration(var);
AnnotatedTypeMirror type;
if (atypeFactory instanceof GenericAnnotatedTypeFactory) {
// TODO: this is rather ugly: we do not want refinement from the
// initializer of the field. We need a general way to get
// the "defaulted" type of a variable.
type = ((GenericAnnotatedTypeFactory<?, ?, ?, ?>) atypeFactory).getAnnotatedTypeLhs(var);
} else {
type = atypeFactory.getAnnotatedType(var);
}
TypeAnnotationPosition tapos = TypeAnnotationUtils.fieldTAPosition(((JCTree) var).pos);
List<Attribute.TypeCompound> tcs;
tcs = generateTypeCompounds(processingEnv, type, tapos);
addUniqueTypeCompounds(types, sym, tcs);
}
use of com.sun.tools.javac.code.TypeAnnotationPosition in project checker-framework by typetools.
the class TypesIntoElements method storeTypeParameters.
private static void storeTypeParameters(ProcessingEnvironment processingEnv, Types types, AnnotatedTypeFactory atypeFactory, java.util.List<? extends TypeParameterTree> tps, Symbol sym) {
boolean isClassOrInterface = sym.getKind().isClass() || sym.getKind().isInterface();
List<Attribute.TypeCompound> tcs = List.nil();
int tpidx = 0;
for (TypeParameterTree tp : tps) {
AnnotatedTypeVariable typeVar = (AnnotatedTypeVariable) atypeFactory.getAnnotatedTypeFromTypeTree(tp);
// System.out.println("The Type for type parameter " + tp + " is " + type);
TypeAnnotationPosition tapos;
// the bounds may not be explicit and we couldn't look up separate pos.
if (isClassOrInterface) {
tapos = TypeAnnotationUtils.typeParameterTAPosition(tpidx, ((JCTree) tp).pos);
} else {
tapos = TypeAnnotationUtils.methodTypeParameterTAPosition(tpidx, ((JCTree) tp).pos);
}
{
// This block is essentially direct annotations, perhaps we should refactor that
// method out
List<Attribute.TypeCompound> res = List.nil();
for (AnnotationMirror am : typeVar.getLowerBound().getAnnotations()) {
Attribute.TypeCompound tc = TypeAnnotationUtils.createTypeCompoundFromAnnotationMirror(am, tapos, processingEnv);
res = res.prepend(tc);
}
tcs = tcs.appendList(res);
}
AnnotatedTypeMirror tpbound = typeVar.getUpperBound();
java.util.List<? extends AnnotatedTypeMirror> bounds;
if (tpbound.getKind() == TypeKind.INTERSECTION) {
bounds = ((AnnotatedTypeMirror.AnnotatedIntersectionType) tpbound).directSuperTypes();
} else {
bounds = List.of(tpbound);
}
int bndidx = 0;
for (AnnotatedTypeMirror bound : bounds) {
if (bndidx == 0 && ((Type) bound.getUnderlyingType()).isInterface()) {
// If the first bound is an interface, there is an implicit java.lang.Object
++bndidx;
}
if (isClassOrInterface) {
tapos = TypeAnnotationUtils.typeParameterBoundTAPosition(tpidx, bndidx, ((JCTree) tp).pos);
} else {
tapos = TypeAnnotationUtils.methodTypeParameterBoundTAPosition(tpidx, bndidx, ((JCTree) tp).pos);
}
tcs = tcs.appendList(generateTypeCompounds(processingEnv, bound, tapos));
++bndidx;
}
++tpidx;
}
// System.out.println("Adding " + tcs + " to " + sym);
addUniqueTypeCompounds(types, sym, tcs);
}
use of com.sun.tools.javac.code.TypeAnnotationPosition in project bazel by bazelbuild.
the class TypeAnnotationUtils method copyTAPosition8.
private static TypeAnnotationPosition copyTAPosition8(TypeAnnotationPosition tapos) throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
TypeAnnotationPosition res = TypeAnnotationPosition.class.newInstance();
res.isValidOffset = tapos.isValidOffset;
TypeAnnotationPosition.class.getField("bound_index").set(res, tapos.bound_index);
res.exception_index = tapos.exception_index;
res.location = List.from(tapos.location);
if (tapos.lvarIndex != null)
res.lvarIndex = Arrays.copyOf(tapos.lvarIndex, tapos.lvarIndex.length);
if (tapos.lvarLength != null)
res.lvarLength = Arrays.copyOf(tapos.lvarLength, tapos.lvarLength.length);
if (tapos.lvarOffset != null)
res.lvarOffset = Arrays.copyOf(tapos.lvarOffset, tapos.lvarOffset.length);
res.offset = tapos.offset;
TypeAnnotationPosition.class.getField("onLambda").set(res, tapos.onLambda);
TypeAnnotationPosition.class.getField("parameter_index").set(res, tapos.parameter_index);
TypeAnnotationPosition.class.getField("pos").set(res, tapos.pos);
TypeAnnotationPosition.class.getField("type").set(res, tapos.type);
TypeAnnotationPosition.class.getField("type_index").set(res, tapos.type_index);
return res;
}
Aggregations