本文整理匯總了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);
}
示例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();
}
示例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);
}
示例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) {
}
}
示例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);
}
}
示例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));
}
}
};
}
示例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);
}
示例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();
}
}
示例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);
}
}
}
示例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);
}
}
示例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));
}
}
}
}
}
示例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());
}
}
}
}
}
}
}
示例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);
}
示例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);
}
示例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;
}