Java Code Examples for java.lang.annotation.Retention

The following are top voted examples for showing how to use java.lang.annotation.Retention. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to product more good examples.

Example 1
Project: resin   File: EventManager.java View source code 6 votes vote down vote up
private void validateEventQualifiers(Annotation []qualifiers)
{
  int length = qualifiers.length;
  for (int i = 0; i < length; i++) {
    Annotation qualifierA = qualifiers[i];

    Class<? extends Annotation> annType = qualifierA.annotationType();
  
    if (! _cdiManager.isQualifier(annType))
      throw new IllegalArgumentException(L.l("'{0}' is an invalid event annotation because it's not a @Qualifier.",
                                             qualifierA));
  
    Retention retention = annType.getAnnotation(Retention.class);
  
    if (retention == null || retention.value() != RetentionPolicy.RUNTIME) {
      throw new IllegalArgumentException(L.l("'{0}' is an invalid event qualifier because it doesn't have RUNTIME retention.",
                                             qualifierA));        
    }
  
    for (int j = i + 1; j < length; j++) {
      if (qualifierA.annotationType() == qualifiers[j].annotationType()) {
        throw new IllegalArgumentException(L.l("fireEvent is invalid because the bindings are duplicate types: {0} and {1}",
                                               qualifiers[i], qualifiers[j]));
      
      }
    }
  }
}
 
Example 2
Project: tapestry-5   File: InternalUtils.java View source code 6 votes vote down vote up
/**
 * Validates that the marker annotation class had a retention policy of runtime.
 *
 * @param markerClass
 *         the marker annotation class
 */
public static void validateMarkerAnnotation(Class markerClass)
{
    Retention policy = (Retention) markerClass.getAnnotation(Retention.class);

    if (policy != null && policy.value() == RetentionPolicy.RUNTIME)
        return;

    throw new IllegalArgumentException(UtilMessages.badMarkerAnnotation(markerClass));
}
 
Example 3
Project: meiyo   File: Filters.java View source code 6 votes vote down vote up
private static void checkForRuntimeRetention( Class<? extends Annotation> annotationType )
{
    Retention retention = annotationType.getAnnotation( Retention.class );
    if ( retention == null || RetentionPolicy.RUNTIME != retention.value() )
    {
        throw new IllegalArgumentException( "Annotation @" + annotationType.getName()
            + " is missing RUNTIME retention" );
    }
}
 
Example 4
Project: classindex   File: ClassFilter.java View source code 6 votes vote down vote up
@Override
public Builder annotatedWith(final Class<? extends Annotation> annotation) {
	Retention retention = annotation.getAnnotation(Retention.class);
	if (retention == null || retention.value() != RetentionPolicy.RUNTIME) {
		throw new IllegalStateException("Cannot filter annotated with annotation without retention policy"
				+ " set to RUNTIME: " + annotation.getName());
	}
	return satisfying(new Predicate() {
		@Override
		public boolean matches(Class<?> klass) {
			return klass.isAnnotationPresent(annotation);
		}
	});
}
 
Example 5
Project: Xpect   File: ResolvedConfiguration.java View source code 6 votes vote down vote up
protected void checkMarkerAnnotation(Class<? extends Annotation> annotation) {
	Retention retention = annotation.getAnnotation(Retention.class);
	if (retention == null)
		throw new IllegalStateException("@Retention annotation missing on @" + annotation.getName());
	if (retention.value() != RetentionPolicy.RUNTIME)
		throw new IllegalStateException("RetentionPolicy on @" + annotation.getName() + " must be RUNTIME");
	XpectStateAnnotation stateAnnotation = annotation.getAnnotation(XpectStateAnnotation.class);
	if (stateAnnotation == null)
		throw new IllegalStateException("@" + XpectStateAnnotation.class.getSimpleName() + " annotation missing on @" + annotation.getName());
}
 
Example 6
Project: VarexJ   File: AnnotationType.java View source code 6 votes vote down vote up
private AnnotationType(final Class<?> annoCls) {
  if (!annoCls.isAnnotation()) {
    throw new IllegalArgumentException("Not an annotation type");
  }

  Method[] methods = annoCls.getDeclaredMethods();

  for (Method m : methods) {
    if (m.getParameterTypes().length == 0) {
      // cache name -> method assoc
      String mname = m.getName();
      members.put(mname, m);

      // cache member type
      Class<?> type = m.getReturnType();
      memberTypes.put(mname, invocationHandlerReturnType(type));

      // cache member default val (if any)
      Object val = m.getDefaultValue();
      if (val != null) {
        memberDefaults.put(mname, val);
      }
    } else {
      // probably an exception
    }
  }

  if ((annoCls != Retention.class) && (annoCls != Inherited.class)) { // don't get recursive
    inherited = annoCls.isAnnotationPresent(Inherited.class);

    Retention r = annoCls.getAnnotation(Retention.class);
    if (r == null) {
      retention = RetentionPolicy.CLASS;
    } else {
      retention = r.value();
    }
  }

  SharedSecrets.getJavaLangAccess().setAnnotationType(annoCls, this);
}
 
Example 7
Project: st-js   File: AnnotationHelper.java View source code 6 votes vote down vote up
public static RetentionPolicy getRetentionType(Tree annotationType) {
	if (!(annotationType instanceof ExpressionTree)) {
		return null;
	}
	Element annotationElement = TreeUtils.elementFromUse((ExpressionTree) annotationType);
	Retention retention = annotationElement.getAnnotation(Retention.class);
	if (retention == null) {
		return null;
	}
	return retention.value();
}
 
Example 8
Project: acteur   File: PageAnnotationHandler.java View source code 6 votes vote down vote up
@SuppressWarnings("unchecked")
/**
 * Create a PageAnnotationHandler
 *
 * @param registry The registry, which this will automatically be registered
 * with
 * @param annotationTypes An array of annotation classes (while generic
 * arrays are illegal in java, an error will be thrown if a passed type is
 * not an annotation type)
 */
protected PageAnnotationHandler(Registry registry, Class<?>... annotationTypes) {
    Set<Class<? extends Annotation>> tps = new HashSet<>();
    for (Class<?> type : annotationTypes) {
        if (!Annotation.class.isAssignableFrom(type)) {
            throw new ConfigurationError("Not an annotation type: " + type.getName());
        }
        Retention retention = type.getAnnotation(Retention.class);
        if (retention == null) {
            throw new ConfigurationError("Not annotated with @Retention: " + type.getName());
        }
        if (retention.value() != RetentionPolicy.RUNTIME) {
            throw new ConfigurationError("Attempting to use " + type.getName()
                    + " as a page annotation, but it does not have @Retention(RUNTIME)");
        }
        tps.add((Class<? extends Annotation>) type);
    }
    this.types = ImmutableSet.<Class<? extends Annotation>>copyOf(tps);
    registry.register(this);
}
 
Example 9
Project: Funcheck   File: AnnotatedTypeFactory.java View source code 6 votes vote down vote up
/**
 * Determines whether the given annotation is a part of the type system
 * under which this type factory operates.
 *
 * @param a any annotation
 * @return true if that annotation is part of the type system under which
 *         this type factory operates, false otherwise
 */
/*package-scope*/ boolean isSupportedQualifier(AnnotationMirror a) {
    if (supportedQuals.isEmpty()) {
        // Only include with retention
        TypeElement elt = (TypeElement)a.getAnnotationType().asElement();
        Retention retention = elt.getAnnotation(Retention.class);
        return (retention == null) || retention.value() != RetentionPolicy.SOURCE;
    }
    Name name = AnnotationUtils.annotationName(a);
    return supportedQuals.contains(name);
}