AnnotationFieldTests.java 文件源码

java
阅读 31 收藏 0 点赞 0 评论 0

项目:openjdk-systemtest 作者:
public void testMetaAnnotation() throws NoSuchFieldException
{
    Field myMetaField = AnnotatedElements.class.getDeclaredField("field4");

    // The @A_Meta annotation has an annotation called @MetaAnnotation2
    A_Meta am = myMetaField.getAnnotation(A_Meta.class);
    assertTrue( am.annotationType().isAnnotationPresent(MetaAnnotation2.class));
    assertTrue( am.annotationType().isAnnotationPresent(MetaAnnotation3.class));

    Annotation[] annot1 = am.annotationType().getAnnotations();
    // 3 annotations should be present: @MetaAnnotation2,@MetaAnnotation3,@Retention
    assertTrue (annot1.length == 3);

    boolean[] annot_count = new boolean[] {false, false, false};        
    for (int i=0; i<annot1.length; i++)
    {
        if (annot1[i] instanceof Retention)
            annot_count[0] = true;
        else if (annot1[i] instanceof MetaAnnotation2)
            annot_count[1] = true;              
        else if (annot1[i] instanceof MetaAnnotation3)
            annot_count[2] = true;              
        else
            fail("Error! Unknown annotation instance detected in field2!");                         
    }
    // Make sure all three annotations were found
    assertTrue(annot_count[0] && annot_count[1] && annot_count[2]);     

    // @MetaAnnotation2 has an annotation called @MetaAnnotation
    Annotation annot2 = MetaAnnotation2.class.getAnnotation(MetaAnnotation.class);
    assertTrue(annot2 != null);
    assertTrue(annot2 instanceof MetaAnnotation);       
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号