jjg@1521: /* jjg@1521: * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. jjg@1521: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1521: * jjg@1521: * This code is free software; you can redistribute it and/or modify it jjg@1521: * under the terms of the GNU General Public License version 2 only, as jjg@1521: * published by the Free Software Foundation. jjg@1521: * jjg@1521: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1521: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1521: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1521: * version 2 for more details (a copy is included in the LICENSE file that jjg@1521: * accompanied this code). jjg@1521: * jjg@1521: * You should have received a copy of the GNU General Public License version jjg@1521: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1521: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1521: * jjg@1521: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1521: * or visit www.oracle.com if you need additional information or have any jjg@1521: * questions. jjg@1521: */ jjg@1521: jjg@1521: /* jjg@1521: * @test jjg@1521: * @bug 8005085 8005877 8004829 8005681 8006734 8006775 jjg@1521: * @ignore jjg@1521: * @summary Combinations of Target ElementTypes on (repeated)type annotations. jjg@1521: */ jjg@1521: jjg@1521: import com.sun.tools.classfile.*; jjg@1521: import java.io.File; jjg@1521: jjg@1521: public class CombinationsTargetTest2 extends ClassfileTestHelper { jjg@1521: // Helps identify test case in event of failure. jjg@1521: int testcount = 0; jjg@1521: int src3 = 3, src8 = 8, src9 = 9; jjg@1521: jjg@1521: String[] ETypes={"TYPE", "FIELD", "METHOD", "PARAMETER", "CONSTRUCTOR", jjg@1521: "LOCAL_VARIABLE", "ANNOTATION_TYPE", "PACKAGE"}; jjg@1521: jjg@1521: // local class tests will have an inner class. jjg@1521: Boolean hasInnerClass=false; jjg@1521: String innerClassname=""; jjg@1521: jjg@1521: public static void main(String[] args) throws Exception { jjg@1521: new CombinationsTargetTest2().run(); jjg@1521: } jjg@1521: jjg@1521: void run() throws Exception { jjg@1521: // Determines which repeat and order in source(ABMix). jjg@1521: Boolean As= false, BDs=true, ABMix=false; jjg@1521: int testrun=0; jjg@1521: // A repeats and/or B/D repeats, ABMix for order of As and Bs. jjg@1521: Boolean [][] bRepeat = new Boolean[][]{{false,false,false},//no repeats jjg@1521: {true,false,false}, //repeat @A jjg@1521: {false,true,false}, //repeat @B jjg@1521: {true,true,false}, //repeat both jjg@1521: {false,false,true} //repeat mix jjg@1521: }; jjg@1521: for(Boolean[] bCombo : bRepeat) { jjg@1521: As=bCombo[0]; BDs=bCombo[1]; ABMix=bCombo[2]; jjg@1521: for(String et : ETypes) { jjg@1521: switch(et) { jjg@1521: case "METHOD": jjg@1521: test( 8, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, src3); jjg@1521: test( 0, 8, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src3); jjg@1521: break; jjg@1521: case "CONSTRUCTOR": jjg@1521: case "FIELD": jjg@1521: test( 8, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, src3); jjg@1521: test( 8, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, src8); jjg@1521: test( 6, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, src9); jjg@1521: test( 0, 8, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src3); jjg@1521: test( 0, 8, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src8); jjg@1521: test( 0, 6, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src9); jjg@1521: break; jjg@1521: default:/*TYPE,PARAMETER,LOCAL_VARIABLE,ANNOTATION_TYPE,PACKAGE*/ jjg@1521: break; jjg@1521: } jjg@1521: } jjg@1521: } jjg@1521: } jjg@1521: jjg@1521: public void test(int tinv, int tvis, int inv, int vis, Boolean Arepeats, jjg@1521: Boolean BDrepeats, Boolean ABmix, String rtn, String et2, jjg@1521: Integer N, int source) throws Exception { jjg@1521: ++testcount; jjg@1521: expected_tvisibles = tvis; jjg@1521: expected_tinvisibles = tinv; jjg@1521: expected_visibles = vis; jjg@1521: expected_invisibles = inv; jjg@1521: File testFile = null; jjg@1521: String tname="Test" + N.toString(); jjg@1521: hasInnerClass=false; jjg@1521: String testDef = "Test " + testcount + " parameters: tinv=" + tinv + jjg@1521: ", tvis=" + tvis + ", inv=" + inv + ", vis=" + vis + jjg@1521: ", Arepeats=" + Arepeats + ", BDrepeats=" + BDrepeats + jjg@1521: ", ABmix=" + ABmix + ", retention: " + rtn + ", anno2: " + jjg@1521: et2 + ", src=" + source; jjg@1521: jjg@1521: // Uncomment this block to run the tests but skip failing scenarios. jjg@1521: // // 8005681 - skip cases with repeated annotations on new, array, cast. jjg@1521: // if((source==3 || source==8 || source==9) && (ABmix || (Arepeats && BDrepeats))) { jjg@1521: // System.out.println(testDef+"\n8005681-skip repeated annotations on new,array,cast"); jjg@1521: // return; jjg@1521: // } jjg@1521: jjg@1521: println(testDef); jjg@1521: // Create test source and File. jjg@1521: String sourceString = sourceString(tname, rtn, et2, Arepeats, jjg@1521: BDrepeats, ABmix, source); jjg@1521: testFile = writeTestFile(tname+".java", sourceString); jjg@1521: // Compile test source and read classfile. jjg@1521: File classFile = null; jjg@1521: try { jjg@1521: classFile = compile(testFile); jjg@1521: } catch (Error err) { jjg@1521: System.err.println("Failed compile. Source:\n" + sourceString); jjg@1521: throw err; jjg@1521: } jjg@1521: //if sourcString() set hasInnerClass it also set innerClassname. jjg@1521: if(hasInnerClass) { jjg@1521: StringBuffer sb = new StringBuffer(classFile.getAbsolutePath()); jjg@1521: classFile=new File(sb.insert(sb.lastIndexOf(".class"),innerClassname).toString()); jjg@1521: } jjg@1521: ClassFile cf = ClassFile.read(classFile); jjg@1521: jjg@1521: //Test class,fields and method counts. jjg@1521: test(cf); jjg@1521: jjg@1521: for (Field f : cf.fields) { jjg@1521: test(cf, f); jjg@1521: } jjg@1521: for (Method m: cf.methods) { jjg@1521: test(cf, m); jjg@1521: } jjg@1521: countAnnotations(); jjg@1521: if (errors > 0) { jjg@1521: System.err.println( testDef ); jjg@1521: System.err.println( "Source:\n" + sourceString ); jjg@1521: throw new Exception( errors + " errors found" ); jjg@1521: } jjg@1521: println("Pass"); jjg@1521: } jjg@1521: jjg@1521: // jjg@1521: // Source for test cases jjg@1521: // jjg@1521: String sourceString(String testname, String retentn, String annot2, jjg@1521: Boolean Arepeats, Boolean BDrepeats, Boolean ABmix, jjg@1521: int src) { jjg@1521: jjg@1521: String As = "@A", Bs = "@B", Ds = "@D"; jjg@1521: if(Arepeats) As = "@A @A"; jjg@1521: if(BDrepeats) { jjg@1521: Bs = "@B @B"; jjg@1521: Ds = "@D @D"; jjg@1521: } jjg@1521: if(ABmix) { As = "@A @B"; Bs = "@A @B"; Ds = "@D @D"; } jjg@1521: jjg@1521: // Source to check for TYPE_USE and TYPE_PARAMETER annotations. jjg@1521: // Source base (annotations) is same for all test cases. jjg@1521: String source = new String(); jjg@1521: String imports = new String("import java.lang.annotation.*; \n" + jjg@1521: "import static java.lang.annotation.RetentionPolicy.*; \n" + jjg@1521: "import static java.lang.annotation.ElementType.*; \n" + jjg@1521: "import java.util.List; \n" + jjg@1521: "import java.util.HashMap; \n" + jjg@1521: "import java.util.Map; \n\n"); jjg@1521: jjg@1521: String sourceBase = new String("@Retention("+retentn+")\n" + jjg@1521: "@Target({TYPE_USE,_OTHER_})\n" + darcy@1531: "@Repeatable( AC.class )\n" + jjg@1521: "@interface A { }\n\n" + jjg@1521: jjg@1521: "@Retention("+retentn+")\n" + jjg@1521: "@Target({TYPE_USE,_OTHER_})\n" + jjg@1521: "@interface AC { A[] value(); }\n\n" + jjg@1521: jjg@1521: "@Retention("+retentn+")\n" + jjg@1521: "@Target({TYPE_USE,_OTHER_})\n" + darcy@1531: "@Repeatable( BC.class )\n" + jjg@1521: "@interface B { }\n\n" + jjg@1521: jjg@1521: "@Retention("+retentn+")\n" + jjg@1521: "@Target({TYPE_USE,_OTHER_})\n" + jjg@1521: "@interface BC { B[] value(); } \n\n" + jjg@1521: jjg@1521: "@Retention("+retentn+")\n" + jjg@1521: "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" + darcy@1531: "@Repeatable(DC.class)\n" + jjg@1521: "@interface D { }\n\n" + jjg@1521: jjg@1521: "@Retention("+retentn+")\n" + jjg@1521: "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" + jjg@1521: "@interface DC { D[] value(); }\n\n"); jjg@1521: jjg@1521: // Test case sources with sample generated source jjg@1521: switch(src) { jjg@1521: case 3: // (repeating) type annotations on field in method body jjg@1521: /* jjg@1521: * class Test1 { jjg@1521: * Test1(){} jjg@1521: * // type usage in method body jjg@1521: * String test(Test1 this, String param, String ... vararg) { jjg@1521: * @A @B jjg@1521: * Object o = new @A @B String @A @B [3]; jjg@1521: * return (@A @B String) null; jjg@1521: * }} jjg@1521: */ jjg@1521: source = new String( jjg@1521: "class " + testname + " {\n" + jjg@1521: "" + testname +"(){} \n" + jjg@1521: "// type usage in method body \n" + jjg@1521: "String test("+testname+" this, " + jjg@1521: "String param, String ... vararg) { \n" + jjg@1521: " _As_ _Bs_\n Object o = new _As_ _Bs_ String _As_ _Bs_ [3]; \n" + jjg@1521: " return (_As_ _Bs_ String) null; \n" + jjg@1521: "} \n" + jjg@1521: "} \n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) + jjg@1521: "\n\n"; jjg@1521: break; jjg@1521: case 8: // (repeating) annotations on type parameters, bounds and type arguments in new statement. jjg@1521: /* jjg@1521: * class Test2 { jjg@1521: * Map, Integer> map = jjg@1521: * new HashMap<@A @B List<@A @B String>, @A @B Integer>(); jjg@1521: * Map, Integer> map2 = new @A @B HashMap<>(); jjg@1521: * String test(Test2 this) { return null;} jjg@1521: * String genericMethod(T t) { return null; } jjg@1521: * } jjg@1521: */ jjg@1521: source = new String( source + jjg@1521: "// (repeating) annotations on type parameters, bounds and type arguments. \n" + jjg@1521: "class " + testname + " {\n" + jjg@1521: " Map, Integer> map =\n" + jjg@1521: " new HashMap<_As_ _Bs_ List<_As_ _Bs_ String>, _As_ _Bs_ Integer>();\n" + jjg@1521: " Map, Integer> map2 = new _As_ _Bs_ HashMap<>();\n" + jjg@1521: " String test(" + testname + " this) { return null;}\n" + jjg@1521: " String genericMethod(T t) { return null; }\n" + jjg@1521: "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) + jjg@1521: "\n\n"; jjg@1521: break; jjg@1521: case 9: // (repeating)annotations on type parameters of class, method return value in method. jjg@1521: /* jjg@1521: * class Test3{ jjg@1521: * Map, E > foo(E e) { jjg@1521: * class maptest { jjg@1521: * Map,E> getMap() { jjg@1521: * Map,E> Em = new HashMap,@A @B @D E>(); jjg@1521: * return Em; jjg@1521: * } jjg@1521: * } jjg@1521: * return new maptest().getMap(); jjg@1521: * } jjg@1521: * Map,String> shm = foo(new String("hello")); jjg@1521: * } jjg@1521: */ jjg@1521: source = new String( source + jjg@1521: "// (repeating)annotations on type parameters of class, method return value in method. \n" + jjg@1521: "class "+ testname + "{\n" + jjg@1521: " Map, E > foo(E e) {\n" + jjg@1521: " class maptest {\n" + // inner class $1maptest jjg@1521: " Map,E> getMap() { \n" + jjg@1521: " Map,E> Em = new HashMap,_As_ _Bs_ _Ds_ E>();\n" + jjg@1521: " return Em;\n" + jjg@1521: " }\n" + jjg@1521: " }\n" + jjg@1521: " return new maptest().getMap();\n" + jjg@1521: " }\n" + jjg@1521: " Map,String> shm = foo(new String(\"hello\"));\n" + jjg@1521: "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) + jjg@1521: "\n\n"; jjg@1521: hasInnerClass=true; jjg@1521: innerClassname="$1maptest"; jjg@1521: break; jjg@1521: jjg@1521: } jjg@1521: return imports + source; jjg@1521: } jjg@1521: }