use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.
the class AnnotatedTypes method containsModifierImpl.
/*
* For type variables we might hit the same type again. We keep a list of visited types.
*/
private static boolean containsModifierImpl(AnnotatedTypeMirror type, AnnotationMirror modifier, List<AnnotatedTypeMirror> visited) {
boolean found = type.hasAnnotation(modifier);
boolean vis = visited.contains(type);
visited.add(type);
if (!found && !vis) {
if (type.getKind() == TypeKind.DECLARED) {
AnnotatedDeclaredType declaredType = (AnnotatedDeclaredType) type;
for (AnnotatedTypeMirror typeMirror : declaredType.getTypeArguments()) {
found |= containsModifierImpl(typeMirror, modifier, visited);
if (found) {
break;
}
}
} else if (type.getKind() == TypeKind.ARRAY) {
AnnotatedArrayType arrayType = (AnnotatedArrayType) type;
found = containsModifierImpl(arrayType.getComponentType(), modifier, visited);
} else if (type.getKind() == TypeKind.TYPEVAR) {
AnnotatedTypeVariable atv = (AnnotatedTypeVariable) type;
if (atv.getUpperBound() != null) {
found = containsModifierImpl(atv.getUpperBound(), modifier, visited);
}
if (!found && atv.getLowerBound() != null) {
found = containsModifierImpl(atv.getLowerBound(), modifier, visited);
}
} else if (type.getKind() == TypeKind.WILDCARD) {
AnnotatedWildcardType awc = (AnnotatedWildcardType) type;
if (awc.getExtendsBound() != null) {
found = containsModifierImpl(awc.getExtendsBound(), modifier, visited);
}
if (!found && awc.getSuperBound() != null) {
found = containsModifierImpl(awc.getSuperBound(), modifier, visited);
}
}
}
return found;
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.
the class AnnotatedTypes method implementsAnnotation.
/**
* @return true if atm is an Annotation interface, i.e. an implementation of
* java.lang.annotation.Annotation e.g. @interface MyAnno - implementsAnnotation would
* return true when called on an AnnotatedDeclaredType representing a use of MyAnno
*/
public static boolean implementsAnnotation(final AnnotatedTypeMirror atm) {
if (atm.getKind() != TypeKind.DECLARED) {
return false;
}
final AnnotatedTypeMirror.AnnotatedDeclaredType declaredType = (AnnotatedTypeMirror.AnnotatedDeclaredType) atm;
Symbol.ClassSymbol classSymbol = (Symbol.ClassSymbol) declaredType.getUnderlyingType().asElement();
for (final Type iface : classSymbol.getInterfaces()) {
if (TypesUtils.isDeclaredOfName(iface, annotationClassName)) {
return true;
}
}
return false;
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.
the class AnnotatedTypes method getIteratedType.
/**
* Returns the iterated type of the passed iterable type, and throws {@link
* IllegalArgumentException} if the passed type is not iterable.
*
* <p>The iterated type is the component type of an array, and the type argument of {@link
* Iterable} for declared types.
*
* @param iterableType the iterable type (either array or declared)
* @return the types of elements in the iterable type
*/
public static AnnotatedTypeMirror getIteratedType(ProcessingEnvironment processingEnv, AnnotatedTypeFactory atypeFactory, AnnotatedTypeMirror iterableType) {
if (iterableType.getKind() == TypeKind.ARRAY) {
return ((AnnotatedArrayType) iterableType).getComponentType();
}
// For type variables and wildcards take the effective upper bound.
if (iterableType.getKind() == TypeKind.WILDCARD) {
return getIteratedType(processingEnv, atypeFactory, ((AnnotatedWildcardType) iterableType).getExtendsBound().deepCopy());
}
if (iterableType.getKind() == TypeKind.TYPEVAR) {
return getIteratedType(processingEnv, atypeFactory, ((AnnotatedTypeVariable) iterableType).getUpperBound());
}
if (iterableType.getKind() != TypeKind.DECLARED) {
ErrorReporter.errorAbort("AnnotatedTypes.getIteratedType: not iterable type: " + iterableType);
// dead code
return null;
}
TypeElement iterableElement = processingEnv.getElementUtils().getTypeElement("java.lang.Iterable");
AnnotatedDeclaredType iterableElmType = atypeFactory.getAnnotatedType(iterableElement);
AnnotatedDeclaredType dt = asSuper(atypeFactory, iterableType, iterableElmType);
if (dt.getTypeArguments().isEmpty()) {
TypeElement e = processingEnv.getElementUtils().getTypeElement("java.lang.Object");
AnnotatedDeclaredType t = atypeFactory.getAnnotatedType(e);
return t;
} else {
return dt.getTypeArguments().get(0);
}
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.
the class AtmLubVisitor method visitDeclared_Declared.
@Override
public Void visitDeclared_Declared(AnnotatedDeclaredType type1, AnnotatedDeclaredType type2, AnnotatedTypeMirror lub) {
AnnotatedDeclaredType castedLub = castLub(type1, lub);
lubPrimaryAnnotations(type1, type2, lub);
List<AnnotatedTypeMirror> lubTypArgs = new ArrayList<>();
for (int i = 0; i < type1.getTypeArguments().size(); i++) {
AnnotatedTypeMirror type1TypeArg = type1.getTypeArguments().get(i);
AnnotatedTypeMirror type2TypeArg = type2.getTypeArguments().get(i);
AnnotatedTypeMirror lubTypeArg = castedLub.getTypeArguments().get(i);
lubTypeArgument(type1TypeArg, type2TypeArg, lubTypeArg);
}
if (lubTypArgs.size() > 0) {
castedLub.setTypeArguments(lubTypArgs);
}
return null;
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.
the class AtmLubVisitor method visitUnion_Union.
@Override
public Void visitUnion_Union(AnnotatedUnionType type1, AnnotatedUnionType type2, AnnotatedTypeMirror lub) {
AnnotatedUnionType castedLub = castLub(type1, lub);
lubPrimaryAnnotations(type1, type2, lub);
for (int i = 0; i < castedLub.getAlternatives().size(); i++) {
AnnotatedDeclaredType lubAltern = castedLub.getAlternatives().get(i);
visit(type1.getAlternatives().get(i), type2.getAlternatives().get(i), lubAltern);
}
return null;
}
Aggregations