--- /dev/null Thu Aug 8 00:10:41 2013 +++ new/test/type-annotations/java/lang/reflect/AnnotatedParameterizedType/AnnotatedParameterizedTypeGetAnnotatedActualTypeArgumentsTest.java Thu Aug 8 00:10:40 2013 @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +/** + * @test + * @bug 8013497 + * @summary test for API: AnnotatedParameterizedType.getAnnotatedActualTypeArguments() + * @library ../../../../ + * @build AnnotationTest TestCaseGenerator DeclarationGenerator Helper + * @author Charlie Wang + * @run main AnnotatedParameterizedTypeGetAnnotatedActualTypeArgumentsTest + */ +import java.lang.reflect.AnnotatedParameterizedType; +import java.lang.reflect.AnnotatedType; + +/* + * Construct the test code in a structure specified in TestCase. + * In each of the following cases, different combinations of annotation + * specified in AnnotationTest.annotationCombinations are tested. The output + * annotations must be the same as input both in value and order. + * 1. generics - + * 2. generics - + * The program test all the cases in TestCase by looping over each one of it. + * Test cases are constructed with small code snippet from Helper.SrcType + * using changable annotations, type and name. After these values are specified, + * the method GenTestCode() generates corresponding java code, encapsulate them + * with a class. Then this class is compiled by Helper.compileCode() into + * classes which is later loaded into memory by Helper.loadClass(). + * The test fails if any of the cases fail. + */ +public class AnnotatedParameterizedTypeGetAnnotatedActualTypeArgumentsTest + extends AnnotationTest { + + enum TestCase implements TestCaseGenerator { + + TESTBASECLSSTART() { + /** + * generate test base, which is the class name used to compile the + * code: + * import java.io.*; + * import java.util.*; + * import java.lang.*; + * import java.lang.reflect.*; + * import java.lang.annotation.*; + * class testBaseClsName { } + */ + public String genTestCase(String str) { + String testCode = ""; + lhm.clear(); + lhm.put(Helper.Declaration.IMPORT, null); + lhm.put(Helper.Declaration.CLASS, testBaseClsName); + lhm.put(Helper.Declaration.CLASS_BODY_START, ""); + testCode += Helper.genDeclaration(lhm, + Helper.Declaration.CLASS); + lhm.clear(); + return testCode; + } + }, + GENERICTYPE() { + /** + * generate generic type: + * public Class<[#ANNO] type> f1; + * then append annotations on [#ANNO] + * + * input should be like [@anno] + */ + public String genTestCase(String str) { + if (null == str) { + throw new RuntimeException("bad test case."); + } + String testCode = ""; + lhm.put(Helper.Declaration.FIELD_TYPE, str + " Class"); + lhm.put(Helper.Declaration.GENERICS1, str + " String"); + lhm.put(Helper.Declaration.FIELD_NAME, + testBaseFieldName + clsIdx + "1"); + testInput.put(testBaseFieldName + clsIdx + "1", str); + testCode += Helper.genDeclaration(lhm, + Helper.Declaration.FIELD_ARRAY); + + lhm.put(Helper.Declaration.FIELD_TYPE, str + " HashMap"); + lhm.put(Helper.Declaration.GENERICS1, + new String[]{str + " String", str + " String"}); + lhm.put(Helper.Declaration.FIELD_NAME, + testBaseFieldName + clsIdx + "2"); + testInput.put(testBaseFieldName + clsIdx+ "2", str); + testCode += Helper.genDeclaration(lhm, + Helper.Declaration.FIELD_ARRAY); + lhm.clear(); + clsIdx++; + return testCode; + } + }, + TESTBASECLSEND() { + /** + * generate end of the test base class: } + * + */ + public String genTestCase(String str) { + String testCode = ""; + lhm.put(Helper.Declaration.CLASS_BODY_END, ""); + lhm.put(Helper.Declaration.ANNOTATION_TYPE_1, ""); + lhm.put(Helper.Declaration.ANNOTATION_TYPE_2, ""); + lhm.put(Helper.Declaration.ANNOTATION_TYPE_3, ""); + lhm.put(Helper.Declaration.CONTAINING_ANNOTATION_TYPE_1, ""); + lhm.put(Helper.Declaration.CONTAINING_ANNOTATION_TYPE_2, ""); + lhm.put(Helper.Declaration.CONTAINING_ANNOTATION_TYPE_3, ""); + testCode += Helper.genDeclaration( + lhm, Helper.Declaration.FIELD_ARRAY); + lhm.clear(); + return testCode; + } + }; + + // generate test class of a specific case + public String genTestCase(String str) { + return ""; + } + } + + // generate source code for test according to TestCase + public String GenTestCode(Class tcg) { + String testCode = ""; + testCode += TestCase.TESTBASECLSSTART.genTestCase(null); + for (String anno : annotationCombinations) { + // append annotation on field + testCode += TestCase.GENERICTYPE.genTestCase(anno); + } + testCode += TestCase.TESTBASECLSEND.genTestCase(null); + + return testCode; + } + + // compare input with result + public boolean checkResult() throws Exception { + compileCode(TestCase.class); + + // Get Class object for the compiled class + Class cls = Helper.loadClass(TestCaseGenerator.testBaseClsName); + + for (Object fieldName : testInput.keySet()) { + AnnotatedType[] result = + ((AnnotatedParameterizedType) + cls.getDeclaredField((String) fieldName). + getAnnotatedType()).getAnnotatedActualTypeArguments(); + for (AnnotatedType at : result) { + if (!Helper.getAT(at).equals(testInput.get(fieldName))) { + Helper.debugPrint(fieldName + + " failed with faulty annotations."); + return false; + } + } + } + return true; + } + + public static void main(String[] args) throws Exception { + new AnnotatedParameterizedTypeGetAnnotatedActualTypeArgumentsTest() + .test(); + } +}