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


Java RetentionPolicy类代码示例

本文整理汇总了Java中java.lang.annotation.RetentionPolicy的典型用法代码示例。如果您正苦于以下问题:Java RetentionPolicy类的具体用法?Java RetentionPolicy怎么用?Java RetentionPolicy使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


RetentionPolicy类属于java.lang.annotation包,在下文中一共展示了RetentionPolicy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: use

import java.lang.annotation.RetentionPolicy; //导入依赖的package包/类
void use() {
  // this immutable type (package style used)
  ImIncludeTypes.builder().build();
  // included on this type (package style used)
  ImSerializable.builder().build();
  // included on package (package style used)
  ImTicker.builder().read(1).build();

  // included in IncludeNestedTypes
  ImmutableIncludeNestedTypes.Retention retention =
      ImmutableIncludeNestedTypes.Retention.builder()
          .value(RetentionPolicy.CLASS)
          .build();

  // included in IncludeNestedTypes
  ImmutableIncludeNestedTypes.Target target =
      ImmutableIncludeNestedTypes.Target.builder()
          .value(ElementType.CONSTRUCTOR, ElementType.ANNOTATION_TYPE)
          .build();

  // package applied style "copyWith*" test
  // see PackageStyle
  retention.copyWithValue(RetentionPolicy.RUNTIME);
  target.copyWithValue(ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:26,代码来源:IncludeTypes.java

示例2: use

import java.lang.annotation.RetentionPolicy; //导入依赖的package包/类
default void use() {

    ImmutableUseImmutableCollections.builder()
        .addList("a", "b")
        .addSet(1, 2)
        .addEnumSet(RetentionPolicy.CLASS)
        .addSortedSet(RetentionPolicy.RUNTIME)
        .addMultiset("c", "c")
        .putMap("d", 1)
        .putEnumMap(RetentionPolicy.RUNTIME, 2)
        .putSortedMap(RetentionPolicy.SOURCE, 3)
        .putMultimap("e", 2)
        .putSetMultimap("f", 5)
        .putListMultimap("g", 6)
        .build();
  }
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:17,代码来源:UseImmutableCollections.java

示例3: collections

import java.lang.annotation.RetentionPolicy; //导入依赖的package包/类
@Test
public void collections() {
  ImmutableJdkColl coll = ImmutableJdkColl.builder()
      .addInts(1)
      .addInts(2, 3)
      .addAllInts(Arrays.asList(4, 5, 6))
      .addNavs(1, 2, 3)
      .addOrds(4, 6, 5)
      .addAllOrds(Arrays.asList(8, 7, 9))
      .addPols(RetentionPolicy.RUNTIME, RetentionPolicy.RUNTIME)
      .build();

  check(coll.ints()).isOf(1, 2, 3, 4, 5, 6);
  check(coll.navs()).isOf(3, 2, 1);
  check(coll.ords()).isOf(4, 5, 6, 7, 8, 9);
  check(coll.pols()).isOf(RetentionPolicy.RUNTIME);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:18,代码来源:JdkOnlyTest.java

示例4: switcher

import java.lang.annotation.RetentionPolicy; //导入依赖的package包/类
@Test
public void switcher() {

  check(Factory4Builder.newBuilder(4)
      .runtimePolicy()
      .sourcePolicy()
      .classPolicy()
      .build()).is("" + RetentionPolicy.CLASS + 4);

  check(Factory4Builder.newBuilder(42)
      .sourcePolicy()
      .runtimePolicy()
      .build()).is("" + RetentionPolicy.RUNTIME + 42);

  try {
    Factory4Builder.newBuilder(44).build();
    check(false);
  } catch (IllegalStateException ex) {
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:21,代码来源:FactoryParametersAndSwitcherTest.java

示例5: buildTemplateConstraint

import java.lang.annotation.RetentionPolicy; //导入依赖的package包/类
private JDefinedClass buildTemplateConstraint(String name) {
    try {
        JDefinedClass tplConstraint = codeModel._class(Config.CFG.getBasePackageName() + ".annot."+name, ClassType.ANNOTATION_TYPE_DECL);
        tplConstraint.annotate(Documented.class);
        tplConstraint.annotate(Retention.class).param("value", RetentionPolicy.RUNTIME);
        tplConstraint.annotate(Target.class).paramArray("value").param(ElementType.TYPE).param(ElementType.ANNOTATION_TYPE).param(ElementType.FIELD).param(ElementType.METHOD);
        
        // Using direct as I don't know how to build default { } with code model
        tplConstraint.direct("\n" + "    Class<?>[] groups() default {};\n" + "    String message() default \"Invalid value\";\n" + "    Class<? extends Payload>[] payload() default {};\n");
        
        // Hack to force the import of javax.validation.Payload
        tplConstraint.javadoc().addThrows((JClass) codeModel._ref(Payload.class)).add("Force import");
        
        return tplConstraint;
    } catch (JClassAlreadyExistsException e) {
        throw new RuntimeException("Tried to create an already existing class: " + name, e);
    }
}
 
开发者ID:hibernate,项目名称:beanvalidation-benchmark,代码行数:19,代码来源:Jsr303Annotator.java

示例6: retention

import java.lang.annotation.RetentionPolicy; //导入依赖的package包/类
private static ArchCondition<JavaClass> retention(final RetentionPolicy expected) {
	return new ArchCondition<JavaClass>("retention " + expected.name()) {
		@Override
		public void check(JavaClass item, ConditionEvents events) {
			Optional<Retention> annotation = item.tryGetAnnotationOfType(Retention.class);
			if (annotation.isPresent()) {
				RetentionPolicy actual = annotation.get().value();
				boolean equals = expected.equals(actual);
				String message = String.format("class %s is annotated with %s with value = '%s' which %s with required '%s'",
						item.getName(), Retention.class.getSimpleName(), actual.name(), equals ? "equals" : "not equals", expected.name()
				);
				events.add(equals ? SimpleConditionEvent.satisfied(item, message) : SimpleConditionEvent.violated(item, message));
			}
		}
	};
}
 
开发者ID:valery1707,项目名称:russian-requisites-validator,代码行数:17,代码来源:ArchitectureTest.java

示例7: annotationTest

import java.lang.annotation.RetentionPolicy; //导入依赖的package包/类
@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,代码行数:23,代码来源:ClassMembersTest.java

示例8: test

import java.lang.annotation.RetentionPolicy; //导入依赖的package包/类
public void test() throws TestFailedException {
    try {
        String template = getSource(getSourceFile(templateFileName));
        for (int i = 0; i < 2; ++i) {
            for (String repeatable : new String[] {"", "@Repeatable(Container.class)"}) {
                for (RetentionPolicy policy : RetentionPolicy.values()) {
                    final int finalI = i;
                    Map<String, String> replacements = new HashMap<String, String>(){{
                        put("%POLICY%", policy.toString());
                        if (finalI != 0) {
                            put("default.*\n", ";\n");
                        }
                        put("%REPEATABLE%", repeatable);
                    }};
                    test(template, replacements, i == 0);
                }
            }
        }
    } catch (Throwable e) {
        addFailure(e);
    } finally {
        checkStatus();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:AnnotationDefaultTest.java

示例9: testAttributes

import java.lang.annotation.RetentionPolicy; //导入依赖的package包/类
private void testAttributes(
        TestCase.TestMemberInfo member,
        ClassFile classFile,
        Supplier<Attributes> attributes)
        throws ConstantPoolException {
    Map<String, Annotation> actualInvisible = collectAnnotations(
            classFile,
            member,
            attributes.get(),
            Attribute.RuntimeInvisibleAnnotations);
    Map<String, Annotation> actualVisible = collectAnnotations(
            classFile,
            member,
            attributes.get(),
            Attribute.RuntimeVisibleAnnotations);

    checkEquals(actualInvisible.keySet(),
            member.getRuntimeInvisibleAnnotations(), "RuntimeInvisibleAnnotations");
    checkEquals(actualVisible.keySet(),
            member.getRuntimeVisibleAnnotations(), "RuntimeVisibleAnnotations");

    for (TestAnnotationInfo expectedAnnotation : member.annotations.values()) {
        RetentionPolicy policy = getRetentionPolicy(expectedAnnotation.annotationName);
        if (policy == RetentionPolicy.SOURCE) {
            continue;
        }
        printf("Testing: isVisible: %s %s%n", policy.toString(), expectedAnnotation.annotationName);
        Annotation actualAnnotation =
                (policy == RetentionPolicy.RUNTIME ? actualVisible : actualInvisible)
                        .get(expectedAnnotation.annotationName);
        if (checkNotNull(actualAnnotation, "Annotation is found : "
                + expectedAnnotation.annotationName)) {
            expectedAnnotation.testAnnotation(this, classFile, actualAnnotation);
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:37,代码来源:RuntimeAnnotationsTestBase.java

示例10: annotationTest

import java.lang.annotation.RetentionPolicy; //导入依赖的package包/类
@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,代码行数:25,代码来源:ClassMembersTest.java

示例11: visit

import java.lang.annotation.RetentionPolicy; //导入依赖的package包/类
@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,代码行数:18,代码来源:NoRuntimeRetention.java

示例12: visitType

import java.lang.annotation.RetentionPolicy; //导入依赖的package包/类
@Override
protected void visitType(TypeDefinition td) {
    if (!td.isAnnotation())
        return;
    DeclaredAnnotation da = getOrCreate(td);
    for (CustomAnnotation ca : td.getAnnotations()) {
        if (Types.is(ca.getAnnotationType(), Retention.class)) {
            for (AnnotationParameter ap : ca.getParameters()) {
                if (ap.getMember().equals("value")) {
                    AnnotationElement value = ap.getValue();
                    if (value instanceof EnumAnnotationElement) {
                        EnumAnnotationElement enumValue = (EnumAnnotationElement) value;
                        if (Types.is(enumValue.getEnumType(), RetentionPolicy.class)) {
                            da.policy = RetentionPolicy.valueOf(enumValue.getEnumConstantName());
                        }
                    }
                }
            }
        }
    }
}
 
开发者ID:amaembo,项目名称:huntbugs,代码行数:22,代码来源:DeclaredAnnotations.java

示例13: bindAnnotationMetadata

import java.lang.annotation.RetentionPolicy; //导入依赖的package包/类
static AnnotationMetadata bindAnnotationMetadata(
    TurbineTyKind kind, Iterable<AnnoInfo> annotations) {
  if (kind != TurbineTyKind.ANNOTATION) {
    return null;
  }
  RetentionPolicy retention = null;
  ImmutableSet<ElementType> target = null;
  ClassSymbol repeatable = null;
  for (AnnoInfo annotation : annotations) {
    switch (annotation.sym().binaryName()) {
      case "java/lang/annotation/Retention":
        retention = bindRetention(annotation);
        break;
      case "java/lang/annotation/Target":
        target = bindTarget(annotation);
        break;
      case "java/lang/annotation/Repeatable":
        repeatable = bindRepeatable(annotation);
        break;
      default:
        break;
    }
  }
  return new AnnotationMetadata(retention, target, repeatable);
}
 
开发者ID:google,项目名称:turbine,代码行数:26,代码来源:ConstBinder.java

示例14: get

import java.lang.annotation.RetentionPolicy; //导入依赖的package包/类
@Override
public AnnotationMetadata get() {
  if ((access() & TurbineFlag.ACC_ANNOTATION) != TurbineFlag.ACC_ANNOTATION) {
    return null;
  }
  RetentionPolicy retention = null;
  ImmutableSet<ElementType> target = null;
  ClassSymbol repeatable = null;
  for (ClassFile.AnnotationInfo annotation : classFile.get().annotations()) {
    switch (annotation.typeName()) {
      case "Ljava/lang/annotation/Retention;":
        retention = bindRetention(annotation);
        break;
      case "Ljava/lang/annotation/Target;":
        target = bindTarget(annotation);
        break;
      case "Ljava/lang/annotation/Repeatable;":
        repeatable = bindRepeatable(annotation);
        break;
      default:
        break;
    }
  }
  return new AnnotationMetadata(retention, target, repeatable);
}
 
开发者ID:google,项目名称:turbine,代码行数:26,代码来源:BytecodeBoundClass.java

示例15: TypeModel

import java.lang.annotation.RetentionPolicy; //导入依赖的package包/类
public TypeModel(Type type, Modifier[] modifiers, VariableModel extendsType, Set<VariableModel> interfaces, List<GenericVariableModel> genericVariables, List<AnnotationModel> annotations, Map<String, FieldModel> fields, Map<String, MethodModel> methods, InclusionType inclusion, List<String> values, Map<String, Object[]> valuesWithFields, List<String> fieldOrder, SubTypeModel subTypes, RetentionPolicy retention, ElementType[] elementTypes, List<String> customConstructorFields) {
    this.type = type;
    this.modifiers = modifiers;
    this.extendsType = extendsType;
    this.interfaces = interfaces;
    this.genericVariables = genericVariables;
    this.annotations = annotations;
    this.fields = fields;
    this.methods = methods;
    this.inclusion = inclusion;
    this.values = values;
    this.valuesWithFields = valuesWithFields;
    this.fieldOrder = fieldOrder;
    this.subTypes = subTypes;
    this.retention = retention;
    this.elementTypes = elementTypes;
    this.customConstructorFields = customConstructorFields;
}
 
开发者ID:flipkart-incubator,项目名称:Lyrics,代码行数:19,代码来源:TypeModel.java


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