use of java.lang.annotation.Target in project checker-framework by typetools.
the class TypeAnnotationMover method isDeclarationAnnotation.
/**
* Returns whether the annotation represented by {@code annotationDeclaration} might be a
* declaration annotation for {@code declarationType}. This holds if the TypeElement has no
* {@code @Target} meta-annotation, or if {@code declarationType} is a target of the annotation.
*
* @param annotationDeclaration declaration for an annotation
* @param declarationType the declaration type to check if the annotation might be a declaration
* annotation for
* @return true if {@code annotationDeclaration} contains {@code declarationType} as a target or
* doesn't contain {@code ElementType.TYPE_USE} as a target
*/
private boolean isDeclarationAnnotation(TypeElement annotationDeclaration, ElementType declarationType) {
Target target = annotationDeclaration.getAnnotation(Target.class);
if (target == null) {
return true;
}
boolean hasTypeUse = false;
for (ElementType elementType : target.value()) {
if (elementType == declarationType) {
return true;
}
if (elementType == ElementType.TYPE_USE) {
hasTypeUse = true;
}
}
if (!hasTypeUse) {
throw new Error(String.format("Annotation %s cannot be used on declaration with type %s", annotationDeclaration.getQualifiedName(), declarationType));
}
return false;
}
use of java.lang.annotation.Target in project checker-framework by typetools.
the class AnnotationUtils method isDeclarationAnnotation.
/**
* Returns true if anno is a declaration annotation. In other words, returns true if anno cannot
* be written on uses of types.
*
* @param anno the AnnotationMirror
* @return true if anno is a declaration annotation
*/
public static boolean isDeclarationAnnotation(AnnotationMirror anno) {
TypeElement elem = (TypeElement) anno.getAnnotationType().asElement();
Target t = elem.getAnnotation(Target.class);
if (t == null) {
return true;
}
for (ElementType elementType : t.value()) {
if (elementType == ElementType.TYPE_USE) {
return false;
}
}
return true;
}
use of java.lang.annotation.Target in project checker-framework by typetools.
the class AnnotatedTypeFactory method checkSupportedQuals.
/**
* @throws BugInCF If supportedQuals is empty or if any of the support qualifiers has a @Target
* meta-annotation that contain something besides TYPE_USE or TYPE_PARAMETER. (@Target({}) is
* allowed.)
*/
private void checkSupportedQuals() {
if (supportedQuals.isEmpty()) {
throw new TypeSystemError("Found no supported qualifiers.");
}
for (Class<? extends Annotation> annotationClass : supportedQuals) {
// Check @Target values
ElementType[] targetValues = annotationClass.getAnnotation(Target.class).value();
List<ElementType> badTargetValues = new ArrayList<>();
for (ElementType element : targetValues) {
if (!(element == ElementType.TYPE_USE || element == ElementType.TYPE_PARAMETER)) {
// if there's an ElementType with an enumerated value of something other
// than TYPE_USE or TYPE_PARAMETER then it isn't a valid qualifier
badTargetValues.add(element);
}
}
if (!badTargetValues.isEmpty()) {
String msg = "The @Target meta-annotation on type qualifier " + annotationClass.toString() + " must not contain " + StringsPlume.conjunction("or", badTargetValues) + ".";
throw new TypeSystemError(msg);
}
}
}
use of java.lang.annotation.Target in project kie-wb-common by kiegroup.
the class DriverUtils method copyAnnotationTarget.
public static void copyAnnotationTarget(Class annotationClass, AnnotationDefinition annotationDefinition) {
if (annotationClass.isAnnotationPresent(Target.class)) {
Target targetAnnotation = (Target) annotationClass.getAnnotation(Target.class);
java.lang.annotation.ElementType[] targets = targetAnnotation.value();
if (targets != null && targets.length > 0) {
for (int i = 0; i < targets.length; i++) {
annotationDefinition.addTarget(buildElementType(targets[i]));
}
} else {
// added to avoid an errai unmarshalling error in broser side, when an annotation has no targets, e.g.
// javax.persistence.UniqueConstraint
annotationDefinition.addTarget(ElementType.UNDEFINED);
}
}
}
use of java.lang.annotation.Target in project geronimo-xbean by apache.
the class MetaAnnotatedElement method validTarget.
private static boolean validTarget(Class<? extends Annotation> type) {
final Target target = type.getAnnotation(Target.class);
if (target == null)
return false;
final ElementType[] targets = target.value();
return targets.length == 1 && targets[0] == ElementType.ANNOTATION_TYPE;
}
Aggregations