Java Code Examples for java.lang.annotation.Target

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project geronimo-xbean, under directory /xbean-finder/src/main/java/org/apache/xbean/finder/.

Source file: AnnotationFinder.java

  18 
vote

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;
}
 

Example 2

From project geronimo-xbean, under directory /xbean-finder/src/main/java/org/apache/xbean/finder/.

Source file: MetaAnnotatedElement.java

  18 
vote

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;
}
 

Example 3

From project hibernate-validator, under directory /annotation-processor/src/main/java/org/hibernate/validator/ap/checks/.

Source file: TargetCheck.java

  18 
vote

@Override public Set<ConstraintCheckError> checkAnnotationType(TypeElement element,AnnotationMirror annotation){
  Target target=element.getAnnotation(Target.class);
  if (target == null) {
    return Collections.emptySet();
  }
  if (!containsAtLeastOneSupportedElementType(target)) {
    return CollectionHelper.asSet(new ConstraintCheckError(element,annotationApiHelper.getMirror(element.getAnnotationMirrors(),Target.class),"CONSTRAINT_TYPE_WITH_WRONG_TARGET"));
  }
  return Collections.emptySet();
}
 

Example 4

From project metatypes, under directory /metatype-impl/src/main/java/org/metatype/.

Source file: MetaAnnotatedObject.java

  18 
vote

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;
}
 

Example 5

From project openwebbeans, under directory /webbeans-impl/src/main/java/org/apache/webbeans/intercept/.

Source file: InterceptorUtil.java

  18 
vote

/** 
 * @param clazz AUTSCH! we should use the AnnotatedType for all that stuff!
 */
public <T>void checkLifecycleConditions(Class<T> clazz,Annotation[] annots,String errorMessage){
  Asserts.nullCheckForClass(clazz);
  if (isLifecycleMethodInterceptor(clazz) && !isBusinessMethodInterceptor(clazz)) {
    Annotation[] anns=webBeansContext.getAnnotationManager().getInterceptorBindingMetaAnnotations(annots);
    for (    Annotation annotation : anns) {
      Target target=annotation.annotationType().getAnnotation(Target.class);
      ElementType[] elementTypes=target.value();
      if (!(elementTypes.length == 1 && elementTypes[0].equals(ElementType.TYPE))) {
        throw new WebBeansConfigurationException(errorMessage);
      }
    }
  }
}
 

Example 6

From project openwebbeans, under directory /webbeans-impl/src/main/java/org/apache/webbeans/intercept/.

Source file: InterceptorUtil.java

  18 
vote

public <T>void checkLifecycleConditions(AnnotatedType<T> annotatedType,Annotation[] annots,String errorMessage){
  if (isLifecycleMethodInterceptor(annotatedType) && !isBusinessMethodInterceptor(annotatedType)) {
    Annotation[] anns=webBeansContext.getAnnotationManager().getInterceptorBindingMetaAnnotations(annots);
    for (    Annotation annotation : anns) {
      Target target=annotation.annotationType().getAnnotation(Target.class);
      ElementType[] elementTypes=target.value();
      if (!(elementTypes.length == 1 && elementTypes[0].equals(ElementType.TYPE))) {
        throw new WebBeansConfigurationException(errorMessage);
      }
    }
  }
}
 

Example 7

From project cdk, under directory /generator/src/main/java/org/richfaces/cdk/apt/.

Source file: JavaSourceProcessor.java

  17 
vote

protected void processAnnotation(CdkAnnotationProcessor processor,RoundEnvironment environment){
  Class<? extends Annotation> processedAnnotation=processor.getProcessedAnnotation();
  log.debug("Process all elements annotated with " + processedAnnotation.getName());
  Target target=processedAnnotation.getAnnotation(Target.class);
  Set<? extends Element> rootElements=environment.getRootElements();
  for (  Element element : rootElements) {
    if (!sourceCache.isChanged(element)) {
      continue;
    }
    if (isAppropriateTarget(element,target)) {
      processElement(processor,processedAnnotation,element);
    }
 else {
      for (      Element enclosedElement : element.getEnclosedElements()) {
        if (!sourceCache.isChanged(enclosedElement)) {
          continue;
        }
        if (isAppropriateTarget(enclosedElement,target)) {
          processElement(processor,processedAnnotation,enclosedElement);
        }
      }
    }
  }
}
 

Example 8

From project cdk, under directory /generator/src/main/java/org/richfaces/cdk/apt/.

Source file: JavaSourceProcessor.java

  15 
vote

private boolean isAppropriateTarget(Element element,Target target){
  boolean match=false;
  ElementKind kind=element.getKind();
  if (null != target) {
    for (    ElementType targetType : target.value()) {
switch (targetType) {
case TYPE:
        match|=ElementKind.CLASS.equals(kind) || ElementKind.INTERFACE.equals(kind) || ElementKind.ENUM.equals(kind);
      break;
case PACKAGE:
    match|=ElementKind.PACKAGE.equals(kind);
  break;
case METHOD:
match|=ElementKind.METHOD.equals(kind);
break;
case FIELD:
match|=ElementKind.FIELD.equals(kind);
break;
default :
break;
}
}
}
 else {
match=ElementKind.CLASS.equals(kind) || ElementKind.INTERFACE.equals(kind) || ElementKind.ENUM.equals(kind)|| ElementKind.PACKAGE.equals(kind)|| ElementKind.METHOD.equals(kind)|| ElementKind.FIELD.equals(kind);
}
return match;
}
 

Example 9

From project hibernate-validator, under directory /annotation-processor/src/main/java/org/hibernate/validator/ap/checks/.

Source file: TargetCheck.java

  15 
vote

private boolean containsAtLeastOneSupportedElementType(Target target){
  ElementType[] elementTypes=target.value();
  for (  ElementType oneElementType : elementTypes) {
    if (supportedTypes.contains(oneElementType)) {
      return true;
    }
  }
  return false;
}
 

Example 10

From project spring-retry, under directory /src/main/java/org/springframework/classify/util/.

Source file: MethodInvokerUtils.java

  15 
vote

/** 
 * Create  {@link MethodInvoker} for the method with the provided annotationon the provided object. Annotations that cannot be applied to methods (i.e. that aren't annotated with an element type of METHOD) will cause an exception to be thrown.
 * @param annotationType to be searched for
 * @param target to be invoked
 * @return MethodInvoker for the provided annotation, null if none is found.
 */
public static MethodInvoker getMethodInvokerByAnnotation(final Class<? extends Annotation> annotationType,final Object target){
  Assert.notNull(target,"Target must not be null");
  Assert.notNull(annotationType,"AnnotationType must not be null");
  Assert.isTrue(ObjectUtils.containsElement(annotationType.getAnnotation(Target.class).value(),ElementType.METHOD),"Annotation [" + annotationType + "] is not a Method-level annotation.");
  final Class<?> targetClass=(target instanceof Advised) ? ((Advised)target).getTargetSource().getTargetClass() : target.getClass();
  if (targetClass == null) {
    return null;
  }
  final AtomicReference<Method> annotatedMethod=new AtomicReference<Method>();
  ReflectionUtils.doWithMethods(targetClass,new ReflectionUtils.MethodCallback(){
    public void doWith(    Method method) throws IllegalArgumentException, IllegalAccessException {
      Annotation annotation=AnnotationUtils.findAnnotation(method,annotationType);
      if (annotation != null) {
        Assert.isNull(annotatedMethod.get(),"found more than one method on target class [" + targetClass.getSimpleName() + "] with the annotation type ["+ annotationType.getSimpleName()+ "].");
        annotatedMethod.set(method);
      }
    }
  }
);
  Method method=annotatedMethod.get();
  if (method == null) {
    return null;
  }
 else {
    return new SimpleMethodInvoker(target,annotatedMethod.get());
  }
}