rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionAnnotationTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 19 Mar 2016 10:31:13 +0100
changeset 1889 e1953d8b8338
parent 1838 rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java@493d5960d520
child 1899 d729cfa77fa7
permissions -rw-r--r--
Support for default attributes of annotations
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.tck;
    19 
    20 import java.lang.annotation.Retention;
    21 import java.lang.annotation.RetentionPolicy;
    22 import org.apidesign.bck2brwsr.vmtest.Compare;
    23 import org.apidesign.bck2brwsr.vmtest.VMTest;
    24 import org.testng.annotations.Factory;
    25 
    26 /**
    27  *
    28  * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
    29  */
    30 public class ReflectionAnnotationTest {
    31     @Retention(RetentionPolicy.RUNTIME)
    32     @interface Ann {
    33         int integer() default 33;
    34         double dbl() default 33.3;
    35         String string() default "Ahoj";
    36         E enums() default E.B;
    37         Class<?> type() default String.class;
    38     }
    39 
    40     @interface AnnAnn {
    41         Ann ann() default @Ann(
    42             dbl = 44.4,
    43             enums = E.A,
    44             string = "Hi",
    45             integer = 44
    46         );
    47     }
    48 
    49     @Ann
    50     @AnnAnn
    51     class D {
    52     }
    53 
    54     @Ann(type = String.class)
    55     @AnnAnn(ann = @Ann(string = "Ciao"))
    56     enum E {
    57         A, B
    58     };
    59 
    60     
    61     @Compare public String annoClass() throws Exception {
    62         Retention r = Ann.class.getAnnotation(Retention.class);
    63         assert r != null : "Annotation is present";
    64         assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
    65         return r.annotationType().getName();
    66     }
    67 
    68     @Compare public boolean isAnnotation() {
    69         return Ann.class.isAnnotation();
    70     }
    71     @Compare public boolean isNotAnnotation() {
    72         return String.class.isAnnotation();
    73     }
    74     @Compare public boolean isNotAnnotationEnum() {
    75         return E.class.isAnnotation();
    76     }
    77 
    78     @Compare public int intDefaultAttrIsRead() {
    79         return D.class.getAnnotation(Ann.class).integer();
    80     }
    81 
    82     @Compare public double doubleDefaultAttrIsRead() {
    83         return D.class.getAnnotation(Ann.class).dbl();
    84     }
    85 
    86     @Compare public String stringDefaultAttrIsRead() {
    87         return D.class.getAnnotation(Ann.class).string();
    88     }
    89 
    90     @Compare public String enumDefaultAttrIsRead() {
    91         return D.class.getAnnotation(Ann.class).enums().toString();
    92     }
    93 
    94     @Compare public String classDefaultAttrIsRead() {
    95         return D.class.getAnnotation(Ann.class).type().getName();
    96     }
    97 
    98     @Compare public String classAttrIsRead() {
    99         return E.class.getAnnotation(Ann.class).type().getName();
   100     }
   101 
   102 //    @Compare public String defaultAnnotationAttrIsRead() {
   103 //        final Ann ann = D.class.getAnnotation(AnnAnn.class).ann();
   104 //        return ann.string() + ann.dbl() + ann.enums() + ann.integer() + ann.type();
   105 //    }
   106 //
   107 //    @Compare public String annotationAttrIsRead() {
   108 //        final Ann ann = E.class.getAnnotation(AnnAnn.class).ann();
   109 //        return ann.string();
   110 //    }
   111 
   112     @Factory
   113     public static Object[] create() {
   114         return VMTest.create(ReflectionAnnotationTest.class);
   115     }
   116     
   117 }