本文整理汇总了Java中java.lang.annotation.RetentionPolicy.RUNTIME属性的典型用法代码示例。如果您正苦于以下问题:Java RetentionPolicy.RUNTIME属性的具体用法?Java RetentionPolicy.RUNTIME怎么用?Java RetentionPolicy.RUNTIME使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类java.lang.annotation.RetentionPolicy
的用法示例。
在下文中一共展示了RetentionPolicy.RUNTIME属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: annotationTest
@Test(dataProvider = "retentionPolicyTestCase")
public void annotationTest(RetentionPolicy policy) {
assertEval("import java.lang.annotation.*;");
String annotationSource =
"@Retention(RetentionPolicy." + policy.toString() + ")\n" +
"@interface A {}";
assertEval(annotationSource);
String classSource =
"@A class C {\n" +
" @A C() {}\n" +
" @A void f() {}\n" +
" @A int f;\n" +
" @A class Inner {}\n" +
"}";
assertEval(classSource);
String isRuntimeVisible = policy == RetentionPolicy.RUNTIME ? "true" : "false";
assertEval("C.class.getAnnotationsByType(A.class).length > 0;", isRuntimeVisible);
assertEval("C.class.getDeclaredConstructor().getAnnotationsByType(A.class).length > 0;", isRuntimeVisible);
assertEval("C.class.getDeclaredMethod(\"f\").getAnnotationsByType(A.class).length > 0;", isRuntimeVisible);
assertEval("C.class.getDeclaredField(\"f\").getAnnotationsByType(A.class).length > 0;", isRuntimeVisible);
assertEval("C.Inner.class.getAnnotationsByType(A.class).length > 0;", isRuntimeVisible);
}
示例2: annotationTest
@Test(enabled = false) // TODO 8080354
public void annotationTest() {
assertEval("import java.lang.annotation.*;");
for (RetentionPolicy policy : RetentionPolicy.values()) {
String annotationSource =
"@Retention(RetentionPolicy." + policy.toString() + ")\n" +
"@interface A {}";
assertEval(annotationSource);
String classSource =
"@A class C {\n" +
" @A C() {}\n" +
" @A void f() {}\n" +
" @A int f;\n" +
" @A class Inner {}\n" +
"}";
assertEval(classSource);
String isRuntimeVisible = policy == RetentionPolicy.RUNTIME ? "true" : "false";
assertEval("C.class.getAnnotationsByType(A.class).length > 0;", isRuntimeVisible);
assertEval("C.class.getDeclaredConstructor().getAnnotationsByType(A.class).length > 0;", isRuntimeVisible);
assertEval("C.class.getDeclaredMethod(\"f\").getAnnotationsByType(A.class).length > 0;", isRuntimeVisible);
assertEval("C.class.getDeclaredField(\"f\").getAnnotationsByType(A.class).length > 0;", isRuntimeVisible);
assertEval("C.Inner.class.getAnnotationsByType(A.class).length > 0;", isRuntimeVisible);
}
}
示例3: visit
@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));
}
}
}
}
}
示例4: getDeclaredAnnotationClass
@Nullable
private static Class<Annotation> getDeclaredAnnotationClass(AnnotationMirror mirror) throws ClassNotFoundException {
TypeElement element = (TypeElement) mirror.getAnnotationType().asElement();
// Ensure the annotation has the correct retention and targets.
Retention retention = element.getAnnotation(Retention.class);
if (retention != null && retention.value() != RetentionPolicy.RUNTIME) {
return null;
}
Target target = element.getAnnotation(Target.class);
if (target != null) {
if (target.value().length < 2) {
return null;
}
List<ElementType> targets = Arrays.asList(target.value());
if (!(targets.contains(ElementType.TYPE) && targets.contains(ElementType.ANNOTATION_TYPE))) {
return null;
}
}
return (Class<Annotation>) Class.forName(element.getQualifiedName().toString());
}
示例5: includeNotInheritedAnnotations
private void includeNotInheritedAnnotations() {
for (Annotation annotation : type.getDeclaredAnnotations()) {
if (annotation.annotationType().getAnnotation(Inherited.class) != null) {
continue;
}
Retention retention = annotation.annotationType().getAnnotation(Retention.class);
boolean visible = retention != null && retention.value() == RetentionPolicy.RUNTIME;
AnnotationVisitor annotationVisitor = visitor.visitAnnotation(Type.getType(annotation.annotationType()).getDescriptor(), visible);
visitAnnotationValues(annotation, annotationVisitor);
annotationVisitor.visitEnd();
}
}
示例6: checkForRuntimeRetention
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");
}
}
示例7:
@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();
示例8: getRetentionPolicy
protected RetentionPolicy getRetentionPolicy(String name) {
if (name.contains("Visible")) {
return RetentionPolicy.RUNTIME;
} else if (name.contains("Invisible")) {
return RetentionPolicy.CLASS;
}
throw new IllegalArgumentException(name);
}
示例9: checkScopeAnnotationValidity
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());
}
}
示例10: isRetainedAtRuntime
/**
* 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;
}
示例11: retention
/**
* 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.
*/
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;
}
}
示例12: annotatedWith
@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);
}
});
}
示例13: _isValidElement
private boolean _isValidElement(Element pElement)
{
Retention retention = pElement.getAnnotation(Retention.class);
if (retention == null || retention.value() != RetentionPolicy.RUNTIME)
{
processingEnv.getMessager().printMessage(Diagnostic.Kind.MANDATORY_WARNING, "Retention should be RUNTIME", pElement);
return false;
}
Target target = pElement.getAnnotation(Target.class);
if (target == null || target.value() == null || target.value().length == 0)
{
processingEnv.getMessager().printMessage(Diagnostic.Kind.MANDATORY_WARNING, "Target has to be defined", pElement);
return false;
}
else
{
for (ElementType elementType : target.value())
{
if (elementType != ElementType.TYPE)
{
processingEnv.getMessager().printMessage(Diagnostic.Kind.MANDATORY_WARNING, "Unsupported type: " + elementType, pElement);
return false;
}
}
}
return true;
}
示例14: AnnotationUsageCache
/**
* 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;
}
示例15: AnnotationRef
private AnnotationRef(Class<T> annType) {
this.annType = annType;
Retention retention = annType.getAnnotation(Retention.class);
this.isRuntimeVisible = retention != null && retention.value() == RetentionPolicy.RUNTIME;
this.typeDescriptor = Type.getDescriptor(annType);
// we need to ensure that our writers are in a consistent ordering. Otherwise we will generate
// bytecode non deterministically. getDeclaredMethods() internally uses a hashMap for storing
// objects so the order of methods returned from it is non deterministic
ImmutableMap.Builder<Method, FieldWriter> writersBuilder = ImmutableMap.builder();
for (Method method : METHOD_ORDERING.sortedCopy(Arrays.asList(annType.getDeclaredMethods()))) {
if (method.getParameterTypes().length == 0 && !Modifier.isStatic(method.getModifiers())) {
Class<?> returnType = method.getReturnType();
if (returnType.isArray()) {
if (returnType.getComponentType().isAnnotation()) {
// These could be supported, but we don't have a usecase yet.
throw new UnsupportedOperationException("Arrays of annotations are not supported");
}
writersBuilder.put(method, arrayFieldWriter(method.getName()));
} else if (returnType.isAnnotation()) {
// N.B. this is recursive and will fail if we encounter recursive annotations
// (StackOverflowError). This could be resolved when we have a usecase, but the failure
// will be obvious if it every pops up.
@SuppressWarnings("unchecked") // we just checked above
AnnotationRef<?> forType = forType((Class<? extends Annotation>) returnType);
writersBuilder.put(method, annotationFieldWriter(method.getName(), forType));
} else {
// simple primitive
writersBuilder.put(method, simpleFieldWriter(method.getName()));
}
}
}
this.writers = writersBuilder.build();
}