Java Code Examples for java.lang.annotation.RetentionPolicy#RUNTIME

The following examples show how to use java.lang.annotation.RetentionPolicy#RUNTIME . 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: 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 2
Source File: NoRuntimeRetention.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
@AstVisitor(nodes = AstNodes.EXPRESSIONS)
public void visit(Expression expr, MethodContext mc, DeclaredAnnotations da) {
    if (expr.getCode() == AstCode.InvokeVirtual && expr.getArguments().size() == 2) {
        MethodReference mr = (MethodReference) expr.getOperand();
        if ((mr.getDeclaringType().getInternalName().startsWith("java/lang/reflect/") || mr.getDeclaringType()
                .getInternalName().equals("java/lang/Class")) && mr.getName().contains("Annotation")) {
            Object constant = Nodes.getConstant(expr.getArguments().get(1));
            if (constant instanceof TypeReference) {
                TypeReference tr = (TypeReference) constant;
                DeclaredAnnotation annot = da.get(tr);
                if (annot != null && annot.getPolicy() != RetentionPolicy.RUNTIME) {
                    mc.report("AnnotationNoRuntimeRetention", 0, expr, ANNOTATION.create(tr));
                }
            }
        }
    }
}
 
Example 3
Source File: AnnotationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Pair(x = 3, y = "foo")
@Full(classValue=Full.class,
      enumValue=RetentionPolicy.RUNTIME,
      booleanValue=false,
      stringArrayValue={"foo", "bar"},
      classArrayValue={Full.class},
      intArrayValue={1, 2},
      enumArrayValue={RetentionPolicy.RUNTIME},
      booleanArrayValue={false, true})
int getReadOnly();
 
Example 4
Source File: AnnotationDef.java    From annotation-tools with MIT License 5 votes vote down vote up
/**
 * The retention policy for annotations of this type.
 * If non-null, this is called a "top-level" annotation definition.
 * It may be null for annotations that are used only as a field of other
 * annotations.
 *
 * @return the retention policy for annotations of this type
 */
public @Nullable RetentionPolicy retention() {
    if (tlAnnotationsHere.contains(Annotations.aRetentionClass)) {
        return RetentionPolicy.CLASS;
    } else if (tlAnnotationsHere.contains(Annotations.aRetentionRuntime)) {
        return RetentionPolicy.RUNTIME;
    } else if (tlAnnotationsHere.contains(Annotations.aRetentionSource)) {
        return RetentionPolicy.SOURCE;
    } else {
        return null;
    }
}
 
Example 5
Source File: AnnotationTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Pair(x = 3, y = "foo")
@Full(classValue=Full.class,
      enumValue=RetentionPolicy.RUNTIME,
      booleanValue=false,
      stringArrayValue={"foo", "bar"},
      classArrayValue={Full.class},
      intArrayValue={1, 2},
      enumArrayValue={RetentionPolicy.RUNTIME},
      booleanArrayValue={false, true})
int getReadOnly();
 
Example 6
Source File: AnnotationTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Pair(x = 3, y = "foo")
@Full(classValue=Full.class,
      enumValue=RetentionPolicy.RUNTIME,
      booleanValue=false,
      stringArrayValue={"foo", "bar"},
      classArrayValue={Full.class},
      intArrayValue={1, 2},
      enumArrayValue={RetentionPolicy.RUNTIME},
      booleanArrayValue={false, true})
int getReadOnly();
 
Example 7
Source File: AnnotationTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Pair(x = 3, y = "foo")
@Full(classValue=Full.class,
      enumValue=RetentionPolicy.RUNTIME,
      booleanValue=false,
      stringArrayValue={"foo", "bar"},
      classArrayValue={Full.class},
      intArrayValue={1, 2},
      enumArrayValue={RetentionPolicy.RUNTIME},
      booleanArrayValue={false, true})
int getReadOnly();
 
Example 8
Source File: AnnotationTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Pair(x = 3, y = "foo")
@Full(classValue=Full.class,
      enumValue=RetentionPolicy.RUNTIME,
      booleanValue=false,
      stringArrayValue={"foo", "bar"},
      classArrayValue={Full.class},
      intArrayValue={1, 2},
      enumArrayValue={RetentionPolicy.RUNTIME},
      booleanArrayValue={false, true})
int getReadOnly();
 
Example 9
Source File: AnnotationTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Pair(x = 3, y = "foo")
@Full(classValue=Full.class,
      enumValue=RetentionPolicy.RUNTIME,
      booleanValue=false,
      stringArrayValue={"foo", "bar"},
      classArrayValue={Full.class},
      intArrayValue={1, 2},
      enumArrayValue={RetentionPolicy.RUNTIME},
      booleanArrayValue={false, true})
int getReadOnly();
 
Example 10
Source File: TomlWriterTest.java    From toml4j with MIT License 5 votes vote down vote up
@Test
public void should_handle_enum() throws Exception {
  class WithEnum {
    RetentionPolicy retentionPolicy = RetentionPolicy.RUNTIME;
  }
  
  assertEquals("retentionPolicy = \"RUNTIME\"\n", new TomlWriter().write(new WithEnum()));
}
 
Example 11
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 12
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 13
Source File: AnnotationTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Pair(x = 3, y = "foo")
@Full(classValue=Full.class,
      enumValue=RetentionPolicy.RUNTIME,
      booleanValue=false,
      stringArrayValue={"foo", "bar"},
      classArrayValue={Full.class},
      intArrayValue={1, 2},
      enumArrayValue={RetentionPolicy.RUNTIME},
      booleanArrayValue={false, true})
int getReadOnly();
 
Example 14
Source File: AnnotationTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Pair(x = 3, y = "foo")
@Full(classValue=Full.class,
      enumValue=RetentionPolicy.RUNTIME,
      booleanValue=false,
      stringArrayValue={"foo", "bar"},
      classArrayValue={Full.class},
      intArrayValue={1, 2},
      enumArrayValue={RetentionPolicy.RUNTIME},
      booleanArrayValue={false, true})
int getReadOnly();
 
Example 15
Source File: AnnotationTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Pair(x = 3, y = "foo")
@Full(classValue=Full.class,
      enumValue=RetentionPolicy.RUNTIME,
      booleanValue=false,
      stringArrayValue={"foo", "bar"},
      classArrayValue={Full.class},
      intArrayValue={1, 2},
      enumArrayValue={RetentionPolicy.RUNTIME},
      booleanArrayValue={false, true})
int getReadOnly();
 
Example 16
Source File: AnnotationTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Pair(x = 3, y = "foo")
@Full(classValue=Full.class,
      enumValue=RetentionPolicy.RUNTIME,
      booleanValue=false,
      stringArrayValue={"foo", "bar"},
      classArrayValue={Full.class},
      intArrayValue={1, 2},
      enumArrayValue={RetentionPolicy.RUNTIME},
      booleanArrayValue={false, true})
int getReadOnly();
 
Example 17
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 18
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 19
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 20
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;
}