Java Code Examples for java.lang.annotation.Retention#value()

The following examples show how to use java.lang.annotation.Retention#value() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PsiAnnotationExtractor.java    From litho with Apache License 2.0 6 votes vote down vote up
/**
 * We consider an annotation to be valid for extraction if it's not an internal annotation (i.e.
 * is in the <code>com.facebook.litho</code> package and is not a source-only annotation.
 *
 * @return Whether or not to extract the given annotation.
 */
private static boolean isValidAnnotation(Project project, PsiAnnotation psiAnnotation) {
  final String text = psiAnnotation.getQualifiedName();
  if (text.startsWith("com.facebook.")) {
    return false;
  }
  PsiClass annotationClass = PsiSearchUtils.findClass(project, psiAnnotation.getQualifiedName());
  if (annotationClass == null) {
    throw new RuntimeException("Annotation class not found, text is: " + text);
  }

  final Retention retention =
      PsiAnnotationProxyUtils.findAnnotationInHierarchy(annotationClass, Retention.class);

  return retention == null || retention.value() != RetentionPolicy.SOURCE;
}
 
Example 2
Source File: FactoryProcessor.java    From toothpick with Apache License 2.0 6 votes vote down vote up
private void checkScopeAnnotationValidity(TypeElement annotation) {
  if (annotation.getAnnotation(Scope.class) == null) {
    error(
        annotation,
        "Scope Annotation %s does not contain Scope annotation.",
        annotation.getQualifiedName());
    return;
  }

  Retention retention = annotation.getAnnotation(Retention.class);
  if (retention == null || retention.value() != RetentionPolicy.RUNTIME) {
    error(
        annotation,
        "Scope Annotation %s does not have RUNTIME retention policy.",
        annotation.getQualifiedName());
  }
}
 
Example 3
Source File: DependencyInjectionUtils.java    From panda with Apache License 2.0 6 votes vote down vote up
/**
 * Check if annotation is available at runtime and it is annotated by @Injectable annotation
 *
 * @param annotation the annotation to check
 * @param <T> annotation type
 * @return the tested annotation
 * @throws org.panda_lang.utilities.inject.DependencyInjectionException when:
 *  <ul>
 *      <li>the given class is not an annotation</li>
 *      <li>annotation is not marked as @{@link org.panda_lang.utilities.inject.annotations.Injectable}</li>
 *      <li>retention policy is not defined or its value is other than the {@link java.lang.annotation.RetentionPolicy#RUNTIME} </li>
 *  </ul>
 */
public static <T> Class<T> testAnnotation(Class<T> annotation) throws DependencyInjectionException {
    if (!annotation.isAnnotation()) {
        throw new DependencyInjectionException(annotation + " is not an annotation");
    }

    @Nullable Retention retention = annotation.getAnnotation(Retention.class);

    if (retention == null) {
        throw new DependencyInjectionException(annotation + " has no specified retention policy");
    }

    if (retention.value() != RetentionPolicy.RUNTIME) {
        throw new DependencyInjectionException(annotation + " is not marked as runtime annotation");
    }

    if (annotation.getAnnotation(Injectable.class) == null) {
        throw new DependencyInjectionException(annotation + " is not marked as @Injectable");
    }

    return annotation;
}
 
Example 4
Source File: AnnotationExtractor.java    From litho with Apache License 2.0 5 votes vote down vote up
/**
 * We consider an annotation to be valid for extraction if it's not an internal annotation (i.e.
 * is in the <code>com.facebook.litho</code> package and is not a source-only annotation.
 *
 * <p>We also do not consider the kotlin.Metadata annotation to be valid as it represents the
 * metadata of the Spec class and not of the class that we are generating.
 *
 * @return Whether or not to extract the given annotation.
 */
private static boolean isValidAnnotation(AnnotationMirror annotation) {
  final Retention retention =
      annotation.getAnnotationType().asElement().getAnnotation(Retention.class);

  if (retention != null && retention.value() == RetentionPolicy.SOURCE) {
    return false;
  }

  String annotationName = annotation.getAnnotationType().toString();

  return !annotationName.startsWith("com.facebook.") && !annotationName.equals("kotlin.Metadata");
}
 
Example 5
Source File: AnnotationUsageCache.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor
 *
 * @param aAnnotationClass
 *        The annotation class to store the existence of. It must have the
 *        {@link RetentionPolicy#RUNTIME} to be usable within this class!
 */
public AnnotationUsageCache (@Nonnull final Class <? extends Annotation> aAnnotationClass)
{
  ValueEnforcer.notNull (aAnnotationClass, "AnnotationClass");

  // Check retention policy
  final Retention aRetention = aAnnotationClass.getAnnotation (Retention.class);
  final RetentionPolicy eRetentionPolicy = aRetention == null ? RetentionPolicy.CLASS : aRetention.value ();
  if (eRetentionPolicy != RetentionPolicy.RUNTIME)
    throw new IllegalArgumentException ("RetentionPolicy must be of type RUNTIME to be used within this cache. The current value ist " +
                                        eRetentionPolicy);

  // Save to members
  m_aAnnotationClass = aAnnotationClass;
}
 
Example 6
Source File: Matchers.java    From crate with Apache License 2.0 5 votes vote down vote up
private static void checkForRuntimeRetention(
        Class<? extends Annotation> annotationType) {
    Retention retention = annotationType.getAnnotation(Retention.class);
    if (retention == null || retention.value() != RetentionPolicy.RUNTIME) {
        throw new IllegalArgumentException("Annotation " + annotationType.getSimpleName() + " is missing RUNTIME retention");
    }
}
 
Example 7
Source File: Methods.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
private static boolean isRuntimeRetained(Class<? extends Annotation> annotation) {
   Retention retention = annotation.getAnnotation(Retention.class);
   return retention != null && retention.value() == RetentionPolicy.RUNTIME; 
}
 
Example 8
Source File: Annotations.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Returns true if the given annotation is retained at runtime.
 */
public static boolean isRetainedAtRuntime(Class<? extends Annotation> annotationType) {
    Retention retention = annotationType.getAnnotation(Retention.class);
    return retention != null && retention.value() == RetentionPolicy.RUNTIME;
}
 
Example 9
Source File: Annotations.java    From businessworks with Apache License 2.0 4 votes vote down vote up
/**
 * Returns true if the given annotation is retained at runtime.
 */
public static boolean isRetainedAtRuntime(Class<? extends Annotation> annotationType) {
  Retention retention = annotationType.getAnnotation(Retention.class);
  return retention != null && retention.value() == RetentionPolicy.RUNTIME;
}
 
Example 10
Source File: AnnotationGenerator.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Applies a Java 1.5-style annotation to a given Host. The Host must be of type {@link SootClass}, {@link SootMethod}
 * or {@link SootField}.
 * 
 * @param h a method, field, or class
 * @param klass the class of the annotation to apply to <code>h</code>
 * @param elems a (possibly empty) sequence of AnnotationElem objects corresponding to the elements that should be contained in this annotation
 */
public void annotate(Host h, Class<? extends Annotation> klass, List<AnnotationElem> elems) {
	//error-checking -- is this annotation appropriate for the target Host?
	Target t = klass.getAnnotation(Target.class);
	Collection<ElementType> elementTypes = Arrays.asList(t.value());		
	final String ERR = "Annotation class "+klass+" not applicable to host of type "+h.getClass()+".";
	if(h instanceof SootClass) {
		if(!elementTypes.contains(ElementType.TYPE)) {
			throw new RuntimeException(ERR);
		}
	} else if(h instanceof SootMethod) {
		if(!elementTypes.contains(ElementType.METHOD)) {
			throw new RuntimeException(ERR);
		}
	} else if(h instanceof SootField) {
		if(!elementTypes.contains(ElementType.FIELD)) {
			throw new RuntimeException(ERR);
		}
	} else {
		throw new RuntimeException("Tried to attach annotation to host of type "+h.getClass()+".");
	}
	
	//get the retention type of the class
	Retention r = klass.getAnnotation(Retention.class);
	
	// CLASS (runtime invisible) retention is the default
	int retPolicy = AnnotationConstants.RUNTIME_INVISIBLE;
	if(r!=null) {
		//TODO why actually do we have AnnotationConstants at all and don't use
		//     RetentionPolicy directly? (Eric Bodden 20/05/2008)
		switch(r.value()) {
		case CLASS:
			retPolicy = AnnotationConstants.RUNTIME_INVISIBLE;
			break;
		case RUNTIME:
			retPolicy = AnnotationConstants.RUNTIME_VISIBLE;
			break;
		default:
			throw new RuntimeException("Unexpected retention policy: "+retPolicy);
		}
	} 

	annotate(h, klass.getCanonicalName(), retPolicy , elems);	
}
 
Example 11
Source File: TestFromListOf.java    From jfixture with MIT License 4 votes vote down vote up
@Test
public void Annotation_is_retained_at_runtime() {
    Retention retention = FromListOf.class.getAnnotation(Retention.class);
    RetentionPolicy retentionPolicy = retention.value();
    assertEquals(RetentionPolicy.RUNTIME, retentionPolicy);
}
 
Example 12
Source File: TestRange.java    From jfixture with MIT License 4 votes vote down vote up
@Test
public void Annotation_is_retained_at_runtime() {
    Retention retention = Range.class.getAnnotation(Retention.class);
    RetentionPolicy retentionPolicy = retention.value();
    assertEquals(RetentionPolicy.RUNTIME, retentionPolicy);
}
 
Example 13
Source File: TestFixture.java    From jfixture with MIT License 4 votes vote down vote up
@Test
public void Annotation_is_retained_at_runtime() {
    Retention retention = Fixture.class.getAnnotation(Retention.class);
    RetentionPolicy retentionPolicy = retention.value();
    assertEquals(RetentionPolicy.RUNTIME, retentionPolicy);
}
 
Example 14
Source File: Annotations.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Returns true if the given annotation is retained at runtime.
 */
public static boolean isRetainedAtRuntime(Class<? extends Annotation> annotationType) {
    Retention retention = annotationType.getAnnotation(Retention.class);
    return retention != null && retention.value() == RetentionPolicy.RUNTIME;
}