test/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest1.java

changeset 1521
71f35e4b93a5
child 1531
8e4c22acebeb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest1.java	Wed Jan 23 13:27:24 2013 -0800
     1.3 @@ -0,0 +1,369 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8005085 8005877 8004829 8005681 8006734 8006775
    1.30 + * @ignore
    1.31 + * @summary Combinations of Target ElementTypes on (repeated)type annotations.
    1.32 + */
    1.33 +
    1.34 +import com.sun.tools.classfile.*;
    1.35 +import java.io.File;
    1.36 +
    1.37 +public class CombinationsTargetTest1 extends ClassfileTestHelper {
    1.38 +    // Helps identify test case in event of failure.
    1.39 +    int testcount = 0;
    1.40 +    int src1 = 1, src2 = 2, src4 = 4,
    1.41 +        src5 = 5, src6 = 6, src7 = 7;
    1.42 +
    1.43 +    String[] ETypes={"TYPE", "FIELD", "METHOD", "PARAMETER", "CONSTRUCTOR",
    1.44 +                     "LOCAL_VARIABLE", "ANNOTATION_TYPE", "PACKAGE"};
    1.45 +
    1.46 +    // local class tests will have an inner class.
    1.47 +    Boolean hasInnerClass=false;
    1.48 +    String innerClassname="";
    1.49 +
    1.50 +    public static void main(String[] args) throws Exception {
    1.51 +        new CombinationsTargetTest1().run();
    1.52 +    }
    1.53 +
    1.54 +    void run() throws Exception {
    1.55 +        // Determines which repeat and order in source(ABMix).
    1.56 +        Boolean As= false, BDs=true, ABMix=false;
    1.57 +        int testrun=0;
    1.58 +        // A repeats and/or B/D repeats, ABMix for order of As and Bs.
    1.59 +        Boolean [][] bRepeat = new Boolean[][]{{false,false,false},//no repeats
    1.60 +                                               {true,false,false}, //repeat @A
    1.61 +                                               {false,true,false}, //repeat @B
    1.62 +                                               {true,true,false},  //repeat both
    1.63 +                                               {false,false,true}  //repeat mix
    1.64 +        };
    1.65 +        for(Boolean[] bCombo : bRepeat) {
    1.66 +            As=bCombo[0]; BDs=bCombo[1]; ABMix=bCombo[2];
    1.67 +            for(String et : ETypes) {
    1.68 +               switch(et) {
    1.69 +                   case "METHOD":
    1.70 +                       test( 8,  0, 2, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src1);
    1.71 +                       test(10,  0, 2, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src2);
    1.72 +                       test( 8,  0, 0, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src4);
    1.73 +                       test(10,  0, 2, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src6);
    1.74 +                       test( 0,  8, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, src1);
    1.75 +                       test( 0, 10, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, src2);
    1.76 +                       test( 0,  8, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src4);
    1.77 +                       test( 0, 10, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, src6);
    1.78 +                       break;
    1.79 +                   case "CONSTRUCTOR":
    1.80 +                   case "FIELD":
    1.81 +                       test( 8,  0, 4, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src1);
    1.82 +                       test( 6,  0, 3, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src5);
    1.83 +                       test( 9,  0, 0, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src7);
    1.84 +                       test( 0,  8, 0, 4, As, BDs, ABMix, "RUNTIME", et, ++testrun, src1);
    1.85 +                       test( 0,  6, 0, 3, As, BDs, ABMix, "RUNTIME", et, ++testrun, src5);
    1.86 +                       test( 0,  9, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, src7);
    1.87 +                       break;
    1.88 +                   default:/*TYPE,PARAMETER,LOCAL_VARIABLE,ANNOTATION_TYPE,PACKAGE*/
    1.89 +                       test( 8,  0, 2, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src1);
    1.90 +                       test( 6,  0, 3, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, src5);
    1.91 +                       test( 0,  8, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, src1);
    1.92 +                       test( 0,  6, 0, 3, As, BDs, ABMix, "RUNTIME", et, ++testrun, src5);
    1.93 +               }
    1.94 +            }
    1.95 +        }
    1.96 +    }
    1.97 +
    1.98 +    public void test(int tinv, int tvis, int inv, int vis, Boolean Arepeats,
    1.99 +                     Boolean BDrepeats, Boolean ABmix, String rtn, String et2,
   1.100 +                     Integer N, int source) throws Exception {
   1.101 +        ++testcount;
   1.102 +        expected_tvisibles = tvis;
   1.103 +        expected_tinvisibles = tinv;
   1.104 +        expected_visibles = vis;
   1.105 +        expected_invisibles = inv;
   1.106 +        File testFile = null;
   1.107 +        String tname="Test" + N.toString();
   1.108 +        hasInnerClass=false;
   1.109 +        String testDef = "Test " + testcount + " parameters: tinv=" + tinv +
   1.110 +                ", tvis=" + tvis + ", inv=" + inv + ", vis=" + vis +
   1.111 +                ", Arepeats=" + Arepeats + ", BDrepeats=" + BDrepeats +
   1.112 +                ", ABmix=" + ABmix + ", retention: " + rtn + ", anno2: " +
   1.113 +                et2 + ", src=" + source;
   1.114 +
   1.115 +        println(testDef);
   1.116 +        // Create test source and File.
   1.117 +        String sourceString = sourceString(tname, rtn, et2, Arepeats,
   1.118 +                                           BDrepeats, ABmix, source);
   1.119 +        testFile = writeTestFile(tname+".java", sourceString);
   1.120 +        // Compile test source and read classfile.
   1.121 +        File classFile = null;
   1.122 +        try {
   1.123 +            classFile = compile(testFile);
   1.124 +        } catch (Error err) {
   1.125 +            System.err.println("Failed compile. Source:\n" + sourceString);
   1.126 +            throw err;
   1.127 +        }
   1.128 +        //if sourcString() set hasInnerClass it also set innerClassname.
   1.129 +        if(hasInnerClass) {
   1.130 +            StringBuffer sb = new StringBuffer(classFile.getAbsolutePath());
   1.131 +            classFile=new File(sb.insert(sb.lastIndexOf(".class"),innerClassname).toString());
   1.132 +        }
   1.133 +        ClassFile cf = ClassFile.read(classFile);
   1.134 +
   1.135 +        //Test class,fields and method counts.
   1.136 +        test(cf);
   1.137 +
   1.138 +        for (Field f : cf.fields) {
   1.139 +            test(cf, f);
   1.140 +        }
   1.141 +        for (Method m: cf.methods) {
   1.142 +            test(cf, m);
   1.143 +        }
   1.144 +        countAnnotations();
   1.145 +        if (errors > 0) {
   1.146 +            System.err.println( testDef );
   1.147 +            System.err.println( "Source:\n" + sourceString );
   1.148 +            throw new Exception( errors + " errors found" );
   1.149 +        }
   1.150 +        println("Pass");
   1.151 +    }
   1.152 +
   1.153 +    //
   1.154 +    // Source for test cases
   1.155 +    //
   1.156 +    String sourceString(String testname, String retentn, String annot2,
   1.157 +                        Boolean Arepeats, Boolean BDrepeats, Boolean ABmix,
   1.158 +                        int src) {
   1.159 +
   1.160 +        String As = "@A", Bs = "@B", Ds = "@D";
   1.161 +        if(Arepeats) As = "@A @A";
   1.162 +        if(BDrepeats) {
   1.163 +            Bs = "@B @B";
   1.164 +            Ds = "@D @D";
   1.165 +        }
   1.166 +        if(ABmix) { As = "@A @B"; Bs = "@A @B"; Ds = "@D @D"; }
   1.167 +
   1.168 +        // Source to check for TYPE_USE and TYPE_PARAMETER annotations.
   1.169 +        // Source base (annotations) is same for all test cases.
   1.170 +        String source = new String();
   1.171 +        String imports = new String("import java.lang.annotation.*; \n" +
   1.172 +            "import static java.lang.annotation.RetentionPolicy.*; \n" +
   1.173 +            "import static java.lang.annotation.ElementType.*; \n" +
   1.174 +            "import java.util.List; \n" +
   1.175 +            "import java.util.HashMap; \n" +
   1.176 +            "import java.util.Map; \n\n");
   1.177 +
   1.178 +            String sourceBase = new String("@Retention("+retentn+")\n" +
   1.179 +            "@Target({TYPE_USE,_OTHER_})\n" +
   1.180 +            "@ContainedBy( AC.class )\n" +
   1.181 +            "@interface A { }\n\n" +
   1.182 +
   1.183 +            "@Retention("+retentn+")\n" +
   1.184 +            "@Target({TYPE_USE,_OTHER_})\n" +
   1.185 +            "@ContainerFor(A.class)\n" +
   1.186 +            "@interface AC { A[] value(); }\n\n" +
   1.187 +
   1.188 +            "@Retention("+retentn+")\n" +
   1.189 +            "@Target({TYPE_USE,_OTHER_})\n" +
   1.190 +            "@ContainedBy( BC.class )\n" +
   1.191 +            "@interface B { }\n\n" +
   1.192 +
   1.193 +            "@Retention("+retentn+")\n" +
   1.194 +            "@Target({TYPE_USE,_OTHER_})\n" +
   1.195 +            "@ContainerFor(B.class)\n" +
   1.196 +            "@interface BC { B[] value(); } \n\n" +
   1.197 +
   1.198 +            "@Retention("+retentn+")\n" +
   1.199 +            "@Target({TYPE_PARAMETER,_OTHER_})\n" +
   1.200 +            "@interface C { }\n\n" +
   1.201 +
   1.202 +            "@Retention("+retentn+")\n" +
   1.203 +            "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" +
   1.204 +            "@ContainedBy(DC.class)\n" +
   1.205 +            "@interface D { }\n\n" +
   1.206 +
   1.207 +            "@Retention("+retentn+")\n" +
   1.208 +            "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" +
   1.209 +            "@ContainerFor(D.class) \n" +
   1.210 +            "@interface DC { D[] value(); }\n\n");
   1.211 +
   1.212 +        // Test case sources with sample generated source.
   1.213 +        switch(src) {
   1.214 +            case 1: // repeating type annotations at class level
   1.215 +                    /*
   1.216 +                     * @A @B class Test1 {
   1.217 +                     * @A @B Test1(){}
   1.218 +                     * @A @B Integer i1 = 0;
   1.219 +                     * String @A @B [] @A @B [] sa = null;
   1.220 +                     * // type usage in method body
   1.221 +                     * String test(Test1 this, String param, String ... vararg) {
   1.222 +                     *     Object o = new  String  [3];
   1.223 +                     *     return (String) null;
   1.224 +                     * }}
   1.225 +                     */
   1.226 +                source = new String(
   1.227 +                "// (repeating) type annotations at class level. \n" +
   1.228 +                "_As_ _Bs_ class " + testname + " {\n" +
   1.229 +                "_As_ _Bs_ " + testname +"(){} \n" +
   1.230 +                "_As_ _Bs_ Integer i1 = 0; \n" +
   1.231 +                "String _As_ _Bs_ [] _As_ _Bs_ [] sa = null; \n" +
   1.232 +                "// type usage in method body \n" +
   1.233 +                "String test("+testname+" this, " +
   1.234 +                   "String param, String ... vararg) { \n" +
   1.235 +                "    Object o = new  String  [3]; \n" +
   1.236 +                "    return (String) null; \n" +
   1.237 +                "} \n" +
   1.238 +                "} \n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
   1.239 +                "\n\n";
   1.240 +                break;
   1.241 +            case 2: // (repeating) type annotations on method.
   1.242 +                    /*
   1.243 +                     * class Test12 {
   1.244 +                     * Test12(){}
   1.245 +                     * // type usage on method
   1.246 +                     * @A @B String test(@A @B  Test12 this, @A @B  String param, @A @B  String @A @B  ... vararg) {
   1.247 +                     *     Object o = new String [3];
   1.248 +                     *     return (String) null;
   1.249 +                     * }}
   1.250 +                     */
   1.251 +                source = new String(
   1.252 +                "// (repeating) type annotations on method. \n" +
   1.253 +                "class " + testname + " {\n" +
   1.254 +                testname +"(){} \n" +
   1.255 +                "// type usage on method \n" +
   1.256 +                "_As_ _Bs_ String test(_As_ _Bs_  "+testname+" this, " +
   1.257 +                   "_As_ _Bs_  String param, _As_ _Bs_  String _As_ _Bs_  ... vararg) { \n" +
   1.258 +                "    Object o = new String [3]; \n" +
   1.259 +                "    return (String) null; \n" +
   1.260 +                "} \n" +
   1.261 +                "} \n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
   1.262 +                "\n\n";
   1.263 +                break;
   1.264 +            case 4: //(repeating) annotations on wildcard, type arguments in anonymous class.
   1.265 +                    /*
   1.266 +                     * class Test13<T extends Object> {
   1.267 +                     *     public T data = null;
   1.268 +                     *     T getData() { return data;}
   1.269 +                     *     String mtest( Test13<String> t){ return t.getData(); }
   1.270 +                     *     public void test() {
   1.271 +                     *         mtest( new Test13<@A @B String>() {
   1.272 +                     *                  void m1(List<@A @B ? extends @A @B  Object> lst) {}
   1.273 +                     *                  void m2() throws@A @B Exception { }
   1.274 +                     *                });
   1.275 +                     *     }
   1.276 +                     * }
   1.277 +                     */
   1.278 +                source = new String( source +
   1.279 +                "// (repeating) annotations on wildcard, type arguments in anonymous class. \n" +
   1.280 +                "class " + testname + "<T extends Object> {\n" +
   1.281 +                "    public T data = null;\n" +
   1.282 +                "    T getData() { return data;}\n" +
   1.283 +                "    String mtest( " + testname + "<String> t){ return t.getData(); }\n" +
   1.284 +                "    public void test() {\n" +
   1.285 +                "        mtest( new " + testname + "<_As_ _Bs_ String>() {\n" +
   1.286 +                "                 void m1(List<_As_ _Bs_ ? extends _As_ _Bs_  Object> lst) {}\n" +
   1.287 +                "                 void m2() throws_As_ _Bs_ Exception { }\n" +
   1.288 +                "               });\n" +
   1.289 +                "    }\n" +
   1.290 +                "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) + "\n\n";
   1.291 +                hasInnerClass=true;
   1.292 +                innerClassname="$1";
   1.293 +            break;
   1.294 +            case 5: // (repeating)annotations on type parameters, bounds and  type arguments on class decl.
   1.295 +                    /*
   1.296 +                     * @A @B @D
   1.297 +                     * class Test2<@A @B @C @D T extends @A @B Object> {
   1.298 +                     *     Map<List<String>, Integer> map =
   1.299 +                     *         new HashMap<List< String>, Integer>();
   1.300 +                     *     Map<List<String>,Integer> map2 = new HashMap<>();
   1.301 +                     *     String test(Test2<T> this) { return null;}
   1.302 +                     *     <T> String genericMethod(T t) { return null; }
   1.303 +                     * }
   1.304 +                     */
   1.305 +                source = new String( source +
   1.306 +                "// (repeating)annotations on type parameters, bounds and  type arguments on class decl. \n" +
   1.307 +                "_As_ _Bs_ _Ds_\n" +  //8004829: A and B on type parameter below.
   1.308 +                "class " + testname + "<_As_ _Bs_ @C _Ds_ T extends _As_ _Bs_ Object> {\n" +
   1.309 +                "    Map<List<String>, Integer> map =\n" +
   1.310 +                "        new HashMap<List< String>, Integer>();\n" +
   1.311 +                "    Map<List<String>,Integer> map2 = new HashMap<>();\n" +
   1.312 +                "    String test(" + testname + "<T> this) { return null;}\n" +
   1.313 +                "    <T> String genericMethod(T t) { return null; }\n" +
   1.314 +                "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) +
   1.315 +                "\n\n";
   1.316 +            break;
   1.317 +            case 6: // (repeating) annotations on type parameters, bounds and  type arguments on method.
   1.318 +                    /*
   1.319 +                     * class Test14<T extends Object> {
   1.320 +                     *     Map<List<String>, Integer> map =
   1.321 +                     *         new HashMap<List<String>, Integer>();
   1.322 +                     *     Map<List<String>, Integer> map2 = new HashMap<>();
   1.323 +                     *     String test(@A @B Test14<@D T> this) { return null;}
   1.324 +                     *     <@C @D T> @A @B String genericMethod(@A @B @D T t) { return null; }
   1.325 +                     * }
   1.326 +                     */
   1.327 +                source = new String( source +
   1.328 +                "// (repeating) annotations on type parameters, bounds and  type arguments on method. \n" +
   1.329 +                "class " + testname + "<T extends Object> {\n" +
   1.330 +                "    Map<List<String>, Integer> map =\n" +
   1.331 +                "        new HashMap<List<String>, Integer>();\n" +
   1.332 +                "    Map<List<String>, Integer> map2 = new HashMap<>();\n" +
   1.333 +                "    String test(_As_ _Bs_ " + testname + "<_Ds_ T> this) { return null;}\n" +
   1.334 +                "    <@C _Ds_ T> _As_ _Bs_ String genericMethod(_As_ _Bs_ _Ds_ T t) { return null; }\n" +
   1.335 +                "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) +
   1.336 +                "\n\n";
   1.337 +            break;
   1.338 +            case 7: // repeating annotations on type parameters, bounds and  type arguments in method.
   1.339 +                    /*
   1.340 +                     * class Test7{
   1.341 +                     *     <E extends Comparable> Map<List<E>, E > foo(E e) {
   1.342 +                     *         class maptest <@A @B @D E> {
   1.343 +                     *             Map<List<@A @B @D E>,@A @B @D E> getMap() {
   1.344 +                     *                 return new HashMap<List<E>,E>();
   1.345 +                     *             }
   1.346 +                     *         }
   1.347 +                     *         return new maptest<E>().getMap();
   1.348 +                     *    }
   1.349 +                     *    Map<List<String>,String> shm = foo(new String("hello"));
   1.350 +                     * }
   1.351 +                     */
   1.352 +                source = new String( source +
   1.353 +                "// (repeating)annotations on type parameters of class, method return value in method. \n" +
   1.354 +                "class "+ testname + "{\n" +
   1.355 +                "    <E extends Comparable> Map<List<E>, E > foo(E e) {\n" +
   1.356 +                "        class maptest <_As_ _Bs_ _Ds_ E> {\n" +                  // inner class $1maptest
   1.357 +                "            Map<List<_As_ _Bs_ _Ds_ E>,_As_ _Bs_ _Ds_ E> getMap() { \n" +
   1.358 +                "                return new HashMap<List<E>,E>();\n" +
   1.359 +                "            }\n" +
   1.360 +                "        }\n" +
   1.361 +                "        return new maptest<E>().getMap();\n" +
   1.362 +                "   }\n" +
   1.363 +                "   Map<List<String>,String> shm = foo(new String(\"hello\"));\n" +
   1.364 +                "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) +
   1.365 +                "\n\n";
   1.366 +                hasInnerClass=true;
   1.367 +                innerClassname="$1maptest";
   1.368 +            break;
   1.369 +        }
   1.370 +        return imports + source;
   1.371 +    }
   1.372 +}

mercurial