java类java.lang.annotation.RetentionPolicy的实例源码

AnnotationsHighlightUtil.java 文件源码 项目:tools-idea 阅读 27 收藏 0 点赞 0 评论 0
@Nullable
private static RetentionPolicy getRetentionPolicy(PsiClass annotation) {
  PsiModifierList modifierList = annotation.getModifierList();
  if (modifierList != null) {
    PsiAnnotation retentionAnno = modifierList.findAnnotation(CommonClassNames.JAVA_LANG_ANNOTATION_RETENTION);
    if (retentionAnno == null) return RetentionPolicy.CLASS;

    PsiAnnotationMemberValue policyRef = PsiImplUtil.findAttributeValue(retentionAnno, null);
    if (policyRef instanceof PsiReference) {
      PsiElement field = ((PsiReference)policyRef).resolve();
      if (field instanceof PsiEnumConstant) {
        String name = ((PsiEnumConstant)field).getName();
        try {
          return RetentionPolicy.valueOf(name);
        }
        catch (Exception e) {
          LOG.warn("Unknown policy: " + name);
        }
      }
    }
  }

  return null;
}
AnnotationMetadata.java 文件源码 项目:Dyvil 阅读 29 收藏 0 点赞 0 评论 0
private void readRetention()
{
    final Annotation retention = this.theClass.getAnnotation(Annotation.LazyFields.RETENTION_CLASS);
    if (retention == null)
    {
        return;
    }

    final IValue value = retention.getArguments().get(0, Names.value);
    if (!(value instanceof EnumValue))
    {
        return;
    }

    try
    {
        final String name = ((EnumValue) value).getName().qualified;
        this.retention = RetentionPolicy.valueOf(name);
    }
    catch (IllegalArgumentException ignored)
    {
        // Problematic RetentionPolicy annotation - do not handle this
    }
}
RetentionPolicyTest.java 文件源码 项目:freeVM 阅读 24 收藏 0 点赞 0 评论 0
/**
 * @throws Exception
 * @tests java.lang.annotation.RetentionPolicy#valueOf(String)
 */
@SuppressWarnings("nls")
public void test_valueOfLjava_lang_String() throws Exception {
    assertSame(RetentionPolicy.CLASS, RetentionPolicy
            .valueOf("CLASS"));
    assertSame(RetentionPolicy.RUNTIME, RetentionPolicy
            .valueOf("RUNTIME"));
    assertSame(RetentionPolicy.SOURCE, RetentionPolicy
            .valueOf("SOURCE"));
    try {
        RetentionPolicy.valueOf("OTHER");
        fail("Should throw an IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        // expected
    }
}
RetentionPolicyTest.java 文件源码 项目:freeVM 阅读 26 收藏 0 点赞 0 评论 0
/**
 * @throws Exception
 * @tests java.lang.annotation.RetentionPolicy#valueOf(String)
 */
@SuppressWarnings("nls")
public void test_valueOfLjava_lang_String() throws Exception {
    assertSame(RetentionPolicy.CLASS, RetentionPolicy
            .valueOf("CLASS"));
    assertSame(RetentionPolicy.RUNTIME, RetentionPolicy
            .valueOf("RUNTIME"));
    assertSame(RetentionPolicy.SOURCE, RetentionPolicy
            .valueOf("SOURCE"));
    try {
        RetentionPolicy.valueOf("OTHER");
        fail("Should throw an IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        // expected
    }
}
MethodAttributeAppenderForInstrumentedMethodTest.java 文件源码 项目:byte-buddy 阅读 23 收藏 0 点赞 0 评论 0
@Test
@SuppressWarnings("unchecked")
public void testJdkTypeIsFiltered() throws Exception {
    when(annotationValueFilter.isRelevant(any(AnnotationDescription.class), any(MethodDescription.InDefinedShape.class))).thenReturn(true);
    AnnotationDescription annotationDescription = mock(AnnotationDescription.class);
    TypeDescription annotationType = mock(TypeDescription.class);
    when(annotationType.getDeclaredMethods()).thenReturn(new MethodList.Empty<MethodDescription.InDefinedShape>());
    when(annotationDescription.getRetention()).thenReturn(RetentionPolicy.RUNTIME);
    when(annotationDescription.getAnnotationType()).thenReturn(annotationType);
    when(annotationType.getActualName()).thenReturn("jdk.internal.Sample");
    when(methodDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Explicit(annotationDescription));
    when(methodDescription.getParameters()).thenReturn((ParameterList) new ParameterList.Empty<ParameterDescription>());
    when(methodDescription.getReturnType()).thenReturn(TypeDescription.Generic.VOID);
    when(methodDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
    when(methodDescription.getExceptionTypes()).thenReturn(new TypeList.Generic.Empty());
    methodAttributeAppender.apply(methodVisitor, methodDescription, annotationValueFilter);
    verifyZeroInteractions(methodVisitor);
}
TypeWriterFieldPoolRecordTest.java 文件源码 项目:byte-buddy 阅读 26 收藏 0 点赞 0 评论 0
@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);
}
ClassPredicatesTest.java 文件源码 项目:ajunit 阅读 24 收藏 0 点赞 0 评论 0
@Test
public void isSubclassOfInterfacePredicate() throws Exception {
    // arrange / given
    final Predicate predicate = ClassPredicates.isSubclassOf(AnyInterface.class);

    // assert / then
    assertClassType(predicate, int.class, false);
    assertClassType(predicate, void.class, false);
    assertClassType(predicate, int[].class, false);
    assertClassType(predicate, Object.class, false);
    assertClassType(predicate, Serializable.class, false);
    assertClassType(predicate, RetentionPolicy.class, false);
    assertClassType(predicate, Retention.class, false);
    assertClassType(predicate, AnyInterface.class, true);
    assertClassType(predicate, AnyBaseClass.class, true);
    assertClassType(predicate, AnyClass.class, true);
}
ClassPredicatesTest.java 文件源码 项目:ajunit 阅读 29 收藏 0 点赞 0 评论 0
@Test
public void isSubclassOfBaseClassPredicate() throws Exception {
    // arrange / given
    final Predicate predicate = ClassPredicates.isSubclassOf(AnyBaseClass.class);

    // assert / then
    assertClassType(predicate, int.class, false);
    assertClassType(predicate, void.class, false);
    assertClassType(predicate, int[].class, false);
    assertClassType(predicate, Object.class, false);
    assertClassType(predicate, Serializable.class, false);
    assertClassType(predicate, RetentionPolicy.class, false);
    assertClassType(predicate, Retention.class, false);
    assertClassType(predicate, AnyInterface.class, false);
    assertClassType(predicate, AnyBaseClass.class, true);
    assertClassType(predicate, AnyClass.class, true);
}
ClassTest.java 文件源码 项目:teavm 阅读 38 收藏 0 点赞 0 评论 0
@Test
public void annotationFieldTypesSupported() {
    AnnotWithVariousFields annot = D.class.getAnnotation(AnnotWithVariousFields.class);
    assertEquals(true, annot.a());
    assertEquals((byte) 2, annot.b());
    assertEquals((short) 3, annot.c());
    assertEquals(4, annot.d());
    assertEquals(5L, annot.e());
    assertEquals(6.5, annot.f(), 0.01);
    assertEquals(7.2, annot.g(), 0.01);
    assertArrayEquals(new int[] { 2, 3 }, annot.h());
    assertEquals(RetentionPolicy.CLASS, annot.i());
    assertEquals(Retention.class, annot.j().annotationType());
    assertEquals(1, annot.k().length);
    assertEquals(RetentionPolicy.RUNTIME, annot.k()[0].value());
    assertEquals("foo", annot.l());
    assertArrayEquals(new String[] { "bar" }, annot.m());
    assertEquals(Integer.class, annot.n());
}
IncludeTypes.java 文件源码 项目:immutables 阅读 29 收藏 0 点赞 0 评论 0
@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);
}


问题


面经


文章

微信
公众号

扫码关注公众号