java.lang.annotation.RetentionPolicy Java Examples
The following examples show how to use
java.lang.annotation.RetentionPolicy.
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: NoRuntimeRetention.java From huntbugs with Apache License 2.0 | 6 votes |
@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 #2
Source File: FactoryProcessor.java From toothpick with Apache License 2.0 | 6 votes |
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()); } }
Example #3
Source File: AnnotationDefaultTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
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(); } }
Example #4
Source File: IncludeTypes.java From immutables with Apache License 2.0 | 6 votes |
@SuppressWarnings("CheckReturnValue") 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); }
Example #5
Source File: PsiAnnotationExtractor.java From litho with Apache License 2.0 | 6 votes |
/** * We consider an annotation to be valid for extraction if it's not an internal annotation (i.e. * is in the <code>com.facebook.litho</code> package and is not a source-only annotation. * * @return Whether or not to extract the given annotation. */ private static boolean isValidAnnotation(Project project, PsiAnnotation psiAnnotation) { final String text = psiAnnotation.getQualifiedName(); if (text.startsWith("com.facebook.")) { return false; } PsiClass annotationClass = PsiSearchUtils.findClass(project, psiAnnotation.getQualifiedName()); if (annotationClass == null) { throw new RuntimeException("Annotation class not found, text is: " + text); } final Retention retention = PsiAnnotationProxyUtils.findAnnotationInHierarchy(annotationClass, Retention.class); return retention == null || retention.value() != RetentionPolicy.SOURCE; }
Example #6
Source File: DeclaredAnnotations.java From huntbugs with Apache License 2.0 | 6 votes |
@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()); } } } } } } }
Example #7
Source File: RuntimeAnnotationChecker.java From netbeans with Apache License 2.0 | 6 votes |
public boolean check() { List<? extends AnnotationMirror> annotations = getElement() .getAnnotationMirrors(); boolean hasAnnotation = getHelper().hasAnnotation(annotations, getAnnotation()); if (!hasAnnotation) { // this is not subject annotation , just return false return false; } if ( !hasRuntimeRetention() ){ getLogger().log(Level.WARNING, "Annotation " + getElement().getQualifiedName() + " declared as " +getAnnotation()+" but has wrong retention policy." + " Correct retention policy is " + RetentionPolicy.RUNTIME.toString());// NOI18N return false; } return hasTarget(); }
Example #8
Source File: RuntimeRetentionAnalyzer.java From netbeans with Apache License 2.0 | 6 votes |
public boolean hasRuntimeRetention() { Map<String, ? extends AnnotationMirror> types = getHelper() .getAnnotationsByType(getElement().getAnnotationMirrors()); AnnotationMirror retention = types.get(Retention.class.getCanonicalName()); if ( retention == null ) { handleNoRetention(); return false; } AnnotationParser parser = AnnotationParser.create(getHelper()); parser.expectEnumConstant(AnnotationUtil.VALUE, getHelper().resolveType( RetentionPolicy.class.getCanonicalName()), null); String retentionPolicy = parser.parse(retention).get(AnnotationUtil.VALUE, String.class); return RetentionPolicy.RUNTIME.toString().equals(retentionPolicy); }
Example #9
Source File: JdkOnlyTest.java From immutables with Apache License 2.0 | 6 votes |
@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); }
Example #10
Source File: TypeWriterFieldPoolRecordTest.java From byte-buddy with Apache License 2.0 | 6 votes |
@Before @SuppressWarnings("unchecked") public void setUp() throws Exception { when(fieldDescription.getActualModifiers()).thenReturn(MODIFIER); when(fieldDescription.getInternalName()).thenReturn(FOO); when(fieldDescription.getDescriptor()).thenReturn(BAR); when(fieldDescription.getGenericSignature()).thenReturn(QUX); when(fieldDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(annotationDescription)); when(fieldDescription.getType()).thenReturn(TypeDescription.Generic.OBJECT); when(classVisitor.visitField(MODIFIER, FOO, BAR, QUX, defaultValue)).thenReturn(fieldVisitor); when(classVisitor.visitField(MODIFIER, FOO, BAR, QUX, FieldDescription.NO_DEFAULT_VALUE)).thenReturn(fieldVisitor); when(annotationValueFilterFactory.on(fieldDescription)).thenReturn(valueFilter); when(fieldVisitor.visitAnnotation(any(String.class), anyBoolean())).thenReturn(annotationVisitor); when(annotationDescription.getAnnotationType()).thenReturn(annotationType); when(annotationType.getDescriptor()).thenReturn(BAZ); when(annotationType.getDeclaredMethods()).thenReturn(new MethodList.Empty<MethodDescription.InDefinedShape>()); when(annotationDescription.getRetention()).thenReturn(RetentionPolicy.RUNTIME); }
Example #11
Source File: DependencyInjectionUtils.java From panda with Apache License 2.0 | 6 votes |
/** * 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 #12
Source File: AnnotationTest.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
@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 #13
Source File: AnnotationTest.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
@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: RetentionPolicyTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * @throws Exception * @tests java.lang.annotation.RetentionPolicy#values() */ @SuppressWarnings("nls") public void test_values() throws Exception { RetentionPolicy[] values = RetentionPolicy.values(); assertTrue(values.length > 1); Arrays.sort(values); assertTrue(Arrays.binarySearch(values, RetentionPolicy.RUNTIME) >= 0); }
Example #15
Source File: FieldCreatorImpl.java From gizmo with Apache License 2.0 | 5 votes |
@Override public void write(ClassVisitor file) { FieldVisitor fieldVisitor = file.visitField(modifiers, fieldDescriptor.getName(), fieldDescriptor.getType(), signature, null); for(AnnotationCreatorImpl annotation : annotations) { AnnotationVisitor av = fieldVisitor.visitAnnotation(DescriptorUtils.extToInt(annotation.getAnnotationType()), annotation.getRetentionPolicy() == RetentionPolicy.RUNTIME); for(Map.Entry<String, Object> e : annotation.getValues().entrySet()) { AnnotationUtils.visitAnnotationValue(av, e.getKey(), e.getValue()); } av.visitEnd(); } fieldVisitor.visitEnd(); }
Example #16
Source File: Controller.java From Diorite with MIT License | 5 votes |
@Override protected Map<Class<? extends Annotation>, ? extends Annotation> extractRawQualifierAnnotations(AnnotatedCodeElement element) { Annotation[] annotations = AsmUtils.getAnnotations(element, RetentionPolicy.RUNTIME); Map<Class<? extends Annotation>, Annotation> resultMap = new HashMap<>(annotations.length + 1); for (Annotation annotation : annotations) { Class<? extends Annotation> annotationType = annotation.annotationType(); if (annotationType.isAnnotationPresent(Qualifier.class)) { if (resultMap.containsKey(annotationType)) { throw new IllegalStateException("Duplicated qualifier! Found: " + annotation + ", others: " + resultMap); } resultMap.put(annotationType, annotation); } if (annotationType.isAnnotationPresent(ShortcutInject.class)) { this.extractQualifierAnnotations(annotationType.getAnnotation(ShortcutInject.class), annotation, r -> { Class<? extends Annotation> type = r.annotationType(); if (resultMap.containsKey(type)) { throw new IllegalStateException("Duplicated qualifier! Found: " + r + ", others: " + resultMap); } resultMap.put(type, r); }); } } return resultMap; }
Example #17
Source File: ConstBinder.java From turbine with Apache License 2.0 | 5 votes |
static AnnotationMetadata bindAnnotationMetadata( TurbineTyKind kind, Iterable<AnnoInfo> annotations) { if (kind != TurbineTyKind.ANNOTATION) { return null; } RetentionPolicy retention = null; ImmutableSet<TurbineElementType> target = null; ClassSymbol repeatable = null; for (AnnoInfo annotation : annotations) { ClassSymbol sym = annotation.sym(); if (sym == null) { continue; } switch (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); }
Example #18
Source File: AnnotationTest.java From hottub with GNU General Public License v2.0 | 5 votes |
@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 #19
Source File: AnnotationAppenderDefaultTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testSourceRetentionAnnotation() throws Exception { AnnotationVisitor annotationVisitor = mock(AnnotationVisitor.class); when(target.visit(anyString(), anyBoolean())).thenReturn(annotationVisitor); AnnotationDescription annotationDescription = mock(AnnotationDescription.class); when(annotationDescription.getRetention()).thenReturn(RetentionPolicy.SOURCE); annotationAppender.append(annotationDescription, valueFilter); verifyZeroInteractions(valueFilter); verifyZeroInteractions(annotationVisitor); }
Example #20
Source File: AbstractTypeParameterSubstitutorTest.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
@Test public void testResolve_05() { JvmTypeReference declaration = typeReferences.getTypeForName(RetentionPolicy.class, resourceSet); JvmTypeReference resolveMe = typeReferences.getTypeForName(Enum.class, resourceSet); String resolved = resolve(declaration, resolveMe); Assert.assertEquals("Enum<RetentionPolicy>", resolved); }
Example #21
Source File: ClassAnnotationSceneWriter.java From annotation-tools with MIT License | 5 votes |
/** * The following methods are utility methods for accessing * information useful to asm from scene-library data structures. * * @return true iff tla is visible at runtime */ private static boolean isRuntimeRetention(Annotation tla) { if (tla.def.retention() == null) { return false; // TODO: temporary } return tla.def.retention().equals(RetentionPolicy.RUNTIME); }
Example #22
Source File: DolphinUtilsTest.java From dolphin-platform with Apache License 2.0 | 5 votes |
@Test public void testIsAllowedForUnmanaged() { //Basics assertTrue(DolphinUtils.isAllowedForUnmanaged(Double.class)); assertTrue(DolphinUtils.isAllowedForUnmanaged(Double.TYPE)); assertTrue(DolphinUtils.isAllowedForUnmanaged(Long.class)); assertTrue(DolphinUtils.isAllowedForUnmanaged(Long.TYPE)); assertTrue(DolphinUtils.isAllowedForUnmanaged(Float.class)); assertTrue(DolphinUtils.isAllowedForUnmanaged(Float.TYPE)); assertTrue(DolphinUtils.isAllowedForUnmanaged(Integer.class)); assertTrue(DolphinUtils.isAllowedForUnmanaged(Integer.TYPE)); assertTrue(DolphinUtils.isAllowedForUnmanaged(Boolean.class)); assertTrue(DolphinUtils.isAllowedForUnmanaged(Boolean.TYPE)); assertTrue(DolphinUtils.isAllowedForUnmanaged(String.class)); //Enum assertTrue(DolphinUtils.isAllowedForUnmanaged(RetentionPolicy.class)); //Property assertTrue(DolphinUtils.isAllowedForUnmanaged(MockedProperty.class)); //Other assertFalse(DolphinUtils.isAllowedForUnmanaged(Date.class)); assertFalse(DolphinUtils.isAllowedForUnmanaged(LocalDateTime.class)); assertFalse(DolphinUtils.isAllowedForUnmanaged(Locale.class)); try { DolphinUtils.isAllowedForUnmanaged(null); Assert.fail("Null check not working"); } catch (Exception e) { } }
Example #23
Source File: TestCase.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private Set<String> getRuntimeAnnotations(RetentionPolicy policy) { return annotations.values().stream() .filter(e -> e.policy == policy) .map(a -> a.annotationName) .distinct() .collect(Collectors.toSet()); }
Example #24
Source File: TomlWriterTest.java From toml4j with MIT License | 5 votes |
@Test public void should_handle_enum() throws Exception { class WithEnum { RetentionPolicy retentionPolicy = RetentionPolicy.RUNTIME; } assertEquals("retentionPolicy = \"RUNTIME\"\n", new TomlWriter().write(new WithEnum())); }
Example #25
Source File: AdviceTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testUserEnumValue() throws Exception { Class<?> type = new ByteBuddy() .redefine(Sample.class) .visit(Advice.withCustomMapping().bind(Custom.class, RetentionPolicy.CLASS).to(CustomAdvice.class).on(named(FOO))) .make() .load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER) .getLoaded(); assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO)); }
Example #26
Source File: AsmBackedClassGenerator.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
public void addConstructor(Constructor<?> constructor) throws Exception { List<Type> paramTypes = new ArrayList<Type>(); for (Class<?> paramType : constructor.getParameterTypes()) { paramTypes.add(Type.getType(paramType)); } String methodDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE, paramTypes.toArray( new Type[paramTypes.size()])); MethodVisitor methodVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, "<init>", methodDescriptor, signature(constructor), new String[0]); for (Annotation annotation : constructor.getDeclaredAnnotations()) { if (annotation.annotationType().getAnnotation(Inherited.class) != null) { continue; } Retention retention = annotation.annotationType().getAnnotation(Retention.class); AnnotationVisitor annotationVisitor = methodVisitor.visitAnnotation(Type.getType(annotation.annotationType()).getDescriptor(), retention != null && retention.value() == RetentionPolicy.RUNTIME); annotationVisitor.visitEnd(); } methodVisitor.visitCode(); // this.super(p0 .. pn) methodVisitor.visitVarInsn(Opcodes.ALOAD, 0); for (int i = 0; i < constructor.getParameterTypes().length; i++) { methodVisitor.visitVarInsn(Type.getType(constructor.getParameterTypes()[i]).getOpcode(Opcodes.ILOAD), i + 1); } methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), "<init>", methodDescriptor); methodVisitor.visitInsn(Opcodes.RETURN); methodVisitor.visitMaxs(0, 0); methodVisitor.visitEnd(); }
Example #27
Source File: CanBeAnnotated.java From ArchUnit with Apache License 2.0 | 5 votes |
private static void checkAnnotationHasReasonableRetention(Class<? extends Annotation> annotationType) { if (isRetentionSource(annotationType)) { throw new InvalidSyntaxUsageException(String.format( "Annotation type %s has @%s(%s), thus the information is gone after compile. " + "So checking this with ArchUnit is useless.", annotationType.getName(), Retention.class.getSimpleName(), RetentionPolicy.SOURCE)); } }
Example #28
Source File: AnnotationAppenderDefaultTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Test public void testSourceRetentionTypeAnnotation() throws Exception { AnnotationVisitor annotationVisitor = mock(AnnotationVisitor.class); when(target.visit(anyString(), anyBoolean())).thenReturn(annotationVisitor); AnnotationDescription annotationDescription = mock(AnnotationDescription.class); when(annotationDescription.getRetention()).thenReturn(RetentionPolicy.SOURCE); annotationAppender.append(annotationDescription, valueFilter, 0, null); verifyZeroInteractions(valueFilter); verifyZeroInteractions(annotationVisitor); }
Example #29
Source File: AnnotationTest.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
@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 #30
Source File: AnnotationTest.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
@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();