rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionAnnotationTest.java
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.tck;
20 import java.lang.annotation.Annotation;
21 import java.lang.annotation.Retention;
22 import java.lang.annotation.RetentionPolicy;
23 import org.apidesign.bck2brwsr.vmtest.Compare;
24 import org.apidesign.bck2brwsr.vmtest.VMTest;
25 import org.testng.annotations.Factory;
29 * @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
31 public class ReflectionAnnotationTest {
32 @Retention(RetentionPolicy.RUNTIME)
34 int integer() default 33;
35 double dbl() default 33.3;
36 String string() default "Ahoj";
37 E enums() default E.B;
38 Class<?> type() default String.class;
42 Ann ann() default @Ann(
55 public void method() {
59 @Ann(type = String.class)
60 @AnnAnn(ann = @Ann(string = "Ciao"))
66 @Compare public String annoClass() throws Exception {
67 Retention r = Ann.class.getAnnotation(Retention.class);
68 assert r != null : "Annotation is present";
69 assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
70 return r.annotationType().getName();
73 @Compare public boolean isAnnotation() {
74 return Ann.class.isAnnotation();
76 @Compare public boolean isNotAnnotation() {
77 return String.class.isAnnotation();
79 @Compare public boolean isNotAnnotationEnum() {
80 return E.class.isAnnotation();
83 @Compare public int intDefaultAttrIsRead() {
84 return D.class.getAnnotation(Ann.class).integer();
87 @Compare public double doubleDefaultAttrIsRead() {
88 return D.class.getAnnotation(Ann.class).dbl();
91 @Compare public String stringDefaultAttrIsRead() {
92 return D.class.getAnnotation(Ann.class).string();
95 @Compare public String enumDefaultAttrIsRead() {
96 return D.class.getAnnotation(Ann.class).enums().toString();
99 @Compare public String classDefaultAttrIsRead() {
100 return D.class.getAnnotation(Ann.class).type().getName();
103 @Compare public String classAttrIsRead() {
104 return E.class.getAnnotation(Ann.class).type().getName();
107 // @Compare public String defaultAnnotationAttrIsRead() {
108 // final Ann ann = D.class.getAnnotation(AnnAnn.class).ann();
109 // return ann.string() + ann.dbl() + ann.enums() + ann.integer() + ann.type();
112 // @Compare public String annotationAttrIsRead() {
113 // final Ann ann = E.class.getAnnotation(AnnAnn.class).ann();
114 // return ann.string();
117 private static String toClassNames(Object[] arr) {
118 StringBuilder sb = new StringBuilder();
120 for (Object o : arr) {
123 } else if (o instanceof Ann) {
125 } else if (o instanceof AnnAnn) {
130 return sb.toString();
134 public String twoAnnotations() {
135 Annotation[] arr = D.class.getAnnotations();
139 return toClassNames(arr);
143 public String twoDeclaredAnnotations() {
144 Annotation[] arr = D.class.getDeclaredAnnotations();
148 return toClassNames(arr);
152 public String twoAnnotationsOnMethod() throws Exception {
153 Annotation[] arr = D.class.getMethod("method").getAnnotations();
157 return toClassNames(arr);
161 public String noAnnotation() {
162 Annotation[] arr = ReflectionAnnotationTest.class.getAnnotations();
166 return toClassNames(arr);
170 public boolean noAnnotationSingle() {
171 Annotation arr = ReflectionAnnotationTest.class.getAnnotation(Ann.class);
176 public boolean noAnnotationOnMethodSingle() throws Exception {
177 Annotation arr = ReflectionAnnotationTest.class.getMethod("noAnnotationOnMethodSingle").getAnnotation(Ann.class);
182 public String noDeclaredAnnotation() {
183 Annotation[] arr = ReflectionAnnotationTest.class.getDeclaredAnnotations();
187 return toClassNames(arr);
191 public String twoDeclaredAnnotationsOnMethod() throws Exception {
192 Annotation[] arr = D.class.getMethod("method").getAnnotations();
196 return toClassNames(arr);
200 public static Object[] create() {
201 return VMTest.create(ReflectionAnnotationTest.class);