1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 /** 24 * @test 25 * @bug 8013497 26 * @summary test for API: AnnotatedWildcardType.getAnnotatedUpperBounds() 27 * @library ../../../../ 28 * @build AnnotationTest TestCaseGenerator DeclarationGenerator Helper 29 * @author Charlie Wang 30 * @run main AnnotatedWildcardTypeGetAnnotatedUpperBoundsTest 31 */ 32 import java.lang.reflect.AnnotatedParameterizedType; 33 import java.lang.reflect.AnnotatedType; 34 import java.lang.reflect.AnnotatedWildcardType; 35 36 /* 37 * Construct the test code in a structure specified in TestCase. 38 * In each of the following cases, different combinations of annotation 39 * specified in AnnotationTest.annotationCombinations are tested. The output 40 * annotations must be the same as input both in value and order. 41 * 1. generics - <T extends/super Q> 42 * 2. generics - <T extends/super Q, P extends/super S> 43 * The program test all the cases in TestCase by looping over each one of it. 44 * Test cases are constructed with small code snippet from Helper.SrcType 45 * using changable annotations, type and name. After these values are specified, 46 * the method GenTestCode() generates corresponding java code, encapsulate them 47 * with a class. Then this class is compiled by Helper.compileCode() into 48 * classes which is later loaded into memory by Helper.loadClass(). 49 * The test fails if any of the cases fail. 50 */ 51 public class AnnotatedWildcardTypeGetAnnotatedUpperBoundsTest 52 extends AnnotationTest { 53 54 enum TestCase implements TestCaseGenerator { 55 56 TESTBASECLSSTART() { 57 /** 58 * generate test base, which is the class name used to compile the 59 * code: 60 * import java.io.*; 61 * import java.util.*; 62 * import java.lang.*; 63 * import java.lang.reflect.*; 64 * import java.lang.annotation.*; 65 * class testBaseClsName { } 66 */ 67 public String genTestCase(String str) { 68 String testCode = ""; 69 lhm.clear(); 70 lhm.put(Helper.Declaration.IMPORT, null); 71 lhm.put(Helper.Declaration.CLASS, testBaseClsName); 72 lhm.put(Helper.Declaration.CLASS_BODY_START, ""); 73 testCode += Helper.genDeclaration(lhm, 74 Helper.Declaration.CLASS); 75 lhm.clear(); 76 return testCode; 77 } 78 }, 79 GENERICTYPE() { 80 /** 81 * generate generic type: 82 * public Class<[#ANNO] type> f1; 83 * then append annotations on [#ANNO] 84 * 85 * input should be like [@anno] 86 */ 87 public String genTestCase(String str) { 88 if (null == str) { 89 throw new RuntimeException("bad test case."); 90 } 91 String testCode = ""; 92 lhm.put(Helper.Declaration.FIELD_TYPE, str + " Class"); 93 lhm.put(Helper.Declaration.GENERICS1, 94 "? extends " + str + " Object"); 95 lhm.put(Helper.Declaration.FIELD_NAME, 96 testBaseFieldName + clsIdx + "1"); 97 testInput.put(testBaseFieldName + clsIdx + "1", str); 98 testCode += Helper.genDeclaration(lhm, 99 Helper.Declaration.FIELD_ARRAY); 100 101 lhm.put(Helper.Declaration.FIELD_TYPE, str + " HashMap"); 102 lhm.put(Helper.Declaration.GENERICS1, 103 new String[]{"? extends " + str + " Object", 104 "? extends " + str + " Object"}); 105 lhm.put(Helper.Declaration.FIELD_NAME, 106 testBaseFieldName + clsIdx + "2"); 107 testInput.put(testBaseFieldName + clsIdx + "2", str); 108 testCode += Helper.genDeclaration(lhm, 109 Helper.Declaration.FIELD_ARRAY); 110 111 lhm.put(Helper.Declaration.FIELD_TYPE, str + " Class"); 112 lhm.put(Helper.Declaration.GENERICS1, "? super " 113 + str + " Object"); 114 lhm.put(Helper.Declaration.FIELD_NAME, 115 testBaseFieldName + clsIdx + "3"); 116 testInput.put(testBaseFieldName + clsIdx + "3", ""); 117 testCode += Helper.genDeclaration(lhm, 118 Helper.Declaration.FIELD_ARRAY); 119 120 lhm.put(Helper.Declaration.FIELD_TYPE, str + " HashMap"); 121 lhm.put(Helper.Declaration.GENERICS1, 122 new String[]{"? super " + str + " Object", 123 "? super " + str + " Object"}); 124 lhm.put(Helper.Declaration.FIELD_NAME, 125 testBaseFieldName + clsIdx + "4"); 126 testInput.put(testBaseFieldName + clsIdx + "4", ""); 127 testCode += Helper.genDeclaration(lhm, 128 Helper.Declaration.FIELD_ARRAY); 129 lhm.clear(); 130 clsIdx++; 131 return testCode; 132 } 133 }, 134 TESTBASECLSEND() { 135 /** 136 * generate end of the test base class: } 137 * 138 */ 139 public String genTestCase(String str) { 140 String testCode = ""; 141 lhm.put(Helper.Declaration.CLASS_BODY_END, ""); 142 lhm.put(Helper.Declaration.ANNOTATION_TYPE_1, ""); 143 lhm.put(Helper.Declaration.ANNOTATION_TYPE_2, ""); 144 lhm.put(Helper.Declaration.ANNOTATION_TYPE_3, ""); 145 lhm.put(Helper.Declaration.CONTAINING_ANNOTATION_TYPE_1, ""); 146 lhm.put(Helper.Declaration.CONTAINING_ANNOTATION_TYPE_2, ""); 147 lhm.put(Helper.Declaration.CONTAINING_ANNOTATION_TYPE_3, ""); 148 testCode += Helper.genDeclaration(lhm, 149 Helper.Declaration.FIELD_ARRAY); 150 lhm.clear(); 151 return testCode; 152 } 153 }; 154 155 // generate test class of a specific case 156 public String genTestCase(String str) { 157 return ""; 158 } 159 } 160 161 // generate source code for test according to TestCase 162 protected String GenTestCode(Class<?> tcg) { 163 String testCode = ""; 164 testCode += TestCase.TESTBASECLSSTART.genTestCase(null); 165 for (String anno : annotationCombinations) { 166 // append annotation on field 167 testCode += TestCase.GENERICTYPE.genTestCase(anno); 168 } 169 testCode += TestCase.TESTBASECLSEND.genTestCase(null); 170 171 return testCode; 172 } 173 174 // compare input with result 175 protected boolean checkResult() throws Exception { 176 compileCode(TestCase.class); 177 // Get Class object for the compiled class 178 Class cls = Helper.loadClass(TestCaseGenerator.testBaseClsName); 179 for (Object fieldName : testInput.keySet()) { 180 AnnotatedType[] result = 181 ((AnnotatedParameterizedType) cls.getDeclaredField( 182 (String) fieldName).getAnnotatedType()). 183 getAnnotatedActualTypeArguments(); 184 for (AnnotatedType at : result) { 185 for (AnnotatedType as : 186 ((AnnotatedWildcardType) at).getAnnotatedUpperBounds()) { 187 188 if (!Helper.getAT(as).equals(testInput.get(fieldName))) { 189 Helper.debugPrint( 190 fieldName + " failed with faulty annotations."); 191 return false; 192 } 193 } 194 } 195 } 196 return true; 197 } 198 199 public static void main(String[] args) throws Exception { 200 new AnnotatedWildcardTypeGetAnnotatedUpperBoundsTest().test(); 201 } 202 }