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: Constructor.getAnnotatedReturnType() 27 * @author Charlie Wang 28 * @library ../../../../ 29 * @build AnnotationTest TestCaseGenerator DeclarationGenerator Helper 30 * @run main ConstructorGetAnnotatedReturnTypeTest 31 */ 32 import java.lang.reflect.AnnotatedType; 33 import java.util.LinkedHashMap; 34 import java.util.Map; 35 import java.lang.reflect.Constructor; 36 37 /* 38 * This file is intended to test API: Constructor.getAnnotatedReturnType(). 39 * It can be run directly by java command or by jtreg. 40 * By setting Helper.DEBUG = true/false, one can display/hide the test code 41 * on the command line output, which is constructed for the annotation test. 42 * Here's more details about test code generation: 43 * Construct the test code in a structure specified in TestCase. 44 * In each of the following cases, different combinations of annotation 45 * specified in AnnotationTest.annotationCombinations are tested. The output 46 * annotations must be the same as input both in value and order. 47 * 1. Single constructor, apply all combinations of annotations on it 48 * The program test all the cases in TestCase by looping over each one of it. 49 * Test cases are constructed with small code snippet from Helper.SrcType 50 * using changable annotations, type and name. After these values are specified, 51 * the method GenTestCode() generates corresponding java code, encapsulate them 52 * with a class. Then this class is compiled by Helper.compileCode() into 53 * classes which is later loaded into memory by Helper.loadClass(). 54 * The test fails if any of the cases fail. 55 */ 56 public class ConstructorGetAnnotatedReturnTypeTest extends AnnotationTest { 57 58 enum TestCase implements TestCaseGenerator { 59 60 TESTBASECLS() { 61 /** 62 * generate test base, which is the class name used to compile the code: 63 * import java.io.*; 64 * import java.util.*; 65 * import java.lang.*; 66 * import java.lang.reflect.*; 67 * import java.lang.annotation.*; 68 * class testBaseClsName { } 69 */ 70 public String genTestCase(String str) { 71 if (0 == testInput.size()) { 72 testInput.put(null, null); 73 return AnnotationTest.genBaseClass(testBaseClsName); 74 } else { 75 return ""; 76 } 77 } 78 }, 79 CONSTRUCTOR() { 80 /** 81 * generate test constructor: 82 * class TypeAnnoCls { 83 * @anno String TypeAnnoCls(){} 84 * } 85 * 86 * input should be like [anno] 87 */ 88 public String genTestCase(String str) { 89 if (null == str) { 90 throw new RuntimeException("bad test base."); 91 } 92 String testCode = ""; 93 int clsIdx = testInput.size(); 94 TestCodeGenerator tcg = new TestCodeGenerator(); 95 tcg.put(Helper.Declaration.CLASS, typeAnnoClsName + clsIdx); 96 tcg.put(Helper.Declaration.CLASS_BODY_START, ""); 97 tcg.put(Helper.Declaration.CONSTRUCTOR_HEADER, 98 str + " " + typeAnnoClsName + clsIdx); 99 tcg.put(Helper.Declaration.METHOD_PARAMETER, ""); 100 tcg.put(Helper.Declaration.METHOD_BODY, " {}"); 101 tcg.put(Helper.Declaration.CLASS_BODY_END, ""); 102 testCode += tcg.genTestCode(); 103 testInput.put(typeAnnoClsName + clsIdx, str); 104 return testCode; 105 } 106 }; 107 108 // a place to hold class name -- annotation correspondence 109 public static Map<String, String> testInput = new LinkedHashMap<>(); 110 111 // generate test class of a specific case 112 public String genTestCase(String str) { 113 return ""; 114 } 115 } 116 117 // compare input with result 118 protected boolean checkResult() throws Exception { 119 compileCode(TestCase.class); 120 // Get Class object for the compiled class 121 for (String clsName : TestCase.testInput.keySet()) { 122 if (null == clsName) { 123 continue; 124 } 125 Class cls = Helper.loadClass(clsName); 126 Constructor c = cls.getDeclaredConstructor(); 127 AnnotatedType at = c.getAnnotatedReturnType(); 128 129 if (!Helper.getAT(at).equals(TestCase.testInput.get(clsName))) { 130 Helper.debugPrint(clsName + " failed with faulty annotations."); 131 return false; 132 } 133 } 134 return true; 135 } 136 137 public static void main(String[] args) throws Exception { 138 new ConstructorGetAnnotatedReturnTypeTest().test(); 139 } 140 }