当前位置: 首页>>代码示例>>Java>>正文


Java RetentionPolicy.RUNTIME属性代码示例

本文整理汇总了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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:ClassMembersTest.java

示例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);
    }
}
 
开发者ID:campolake,项目名称:openjdk9,代码行数:24,代码来源:ClassMembersTest.java

示例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));
                }
            }
        }
    }
}
 
开发者ID:amaembo,项目名称:huntbugs,代码行数:17,代码来源:NoRuntimeRetention.java

示例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());
}
 
开发者ID:evant,项目名称:autodata,代码行数:20,代码来源:AutoDataAnnotationProcessor.java

示例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();
    }
}
 
开发者ID:lxxlxx888,项目名称:Reer,代码行数:12,代码来源:AsmBackedClassGenerator.java

示例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");
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:7,代码来源:Matchers.java

示例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();
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:10,代码来源:AnnotationTest.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:AnnotationsTestBase.java

示例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());
  }
}
 
开发者ID:stephanenicolas,项目名称:toothpick,代码行数:11,代码来源:FactoryProcessor.java

示例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;
}
 
开发者ID:ruediste,项目名称:salta,代码行数:9,代码来源:Annotations.java

示例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;
    }
}
 
开发者ID:typetools,项目名称:annotation-tools,代码行数:17,代码来源:AnnotationDef.java

示例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);
		}
	});
}
 
开发者ID:soklet,项目名称:soklet,代码行数:14,代码来源:ClassFilter.java

示例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;
}
 
开发者ID:aditosoftware,项目名称:picoservice,代码行数:27,代码来源:AnnotationProcessorPico.java

示例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;
}
 
开发者ID:phax,项目名称:ph-commons,代码行数:21,代码来源:AnnotationUsageCache.java

示例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();
}
 
开发者ID:google,项目名称:closure-templates,代码行数:33,代码来源:AnnotationRef.java


注:本文中的java.lang.annotation.RetentionPolicy.RUNTIME属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。