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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/annotations/typeAnnotations/classfile/CombinationsTargetTest2.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,417 @@
     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 8006507
    1.30 + * @summary Combinations of Target ElementTypes on (repeated)type annotations.
    1.31 + */
    1.32 +
    1.33 +import com.sun.tools.classfile.*;
    1.34 +import java.io.File;
    1.35 +
    1.36 +public class CombinationsTargetTest2 extends ClassfileTestHelper {
    1.37 +
    1.38 +    // Test count helps identify test case in event of failure.
    1.39 +    int testcount = 0;
    1.40 +
    1.41 +    // Base test case template descriptions;true==annotations in code attribute.
    1.42 +    enum srce  {
    1.43 +        src1("(repeating) type annotations on on field in method body",true),
    1.44 +        src2("(repeating) type annotations on type parameters, bounds and  type arguments", true),
    1.45 +        src3("(repeating) type annotations on type parameters of class, method return value in method", true),
    1.46 +        src4("(repeating) type annotations on field in anonymous class", false),
    1.47 +        src5("(repeating) type annotations on field in anonymous class", false),
    1.48 +        src6("(repeating) type annotations on void method declaration", false),
    1.49 +        src7("(repeating) type annotations in use of instanceof", true),
    1.50 +        src8("(repeating) type annotations in use of instanceof in method", true);
    1.51 +
    1.52 +        String description;
    1.53 +        Boolean local;
    1.54 +
    1.55 +        srce(String desc, Boolean b) {
    1.56 +            this.description = this + ": " +desc;
    1.57 +            this.local = b;
    1.58 +        }
    1.59 +    }
    1.60 +
    1.61 +
    1.62 +    String[] ETypes={"TYPE", "FIELD", "METHOD", "PARAMETER", "CONSTRUCTOR",
    1.63 +                     "LOCAL_VARIABLE", "ANNOTATION_TYPE", "PACKAGE"};
    1.64 +
    1.65 +    // local class tests will have an inner class.
    1.66 +    Boolean hasInnerClass=false;
    1.67 +    String innerClassname="";
    1.68 +
    1.69 +    public static void main(String[] args) throws Exception {
    1.70 +        new CombinationsTargetTest2().run();
    1.71 +    }
    1.72 +
    1.73 +    void run() throws Exception {
    1.74 +        // Determines which repeat and order in source(ABMix).
    1.75 +        Boolean As= false, BDs=true, ABMix=false;
    1.76 +        int testrun=0;
    1.77 +        Boolean [][] bRepeat = new Boolean[][]{{false,false,false},//no repeats
    1.78 +                                               {true,false,false}, //repeat @A
    1.79 +                                               {false,true,false}, //repeat @B
    1.80 +                                               {true,true,false},  //repeat both
    1.81 +                                               {false,false,true}  //repeat mix
    1.82 +        };
    1.83 +
    1.84 +        for(Boolean[] bCombo : bRepeat) {
    1.85 +            As=bCombo[0]; BDs=bCombo[1]; ABMix=bCombo[2];
    1.86 +            for(String et : ETypes) {
    1.87 +               switch(et) {
    1.88 +                   case "METHOD":
    1.89 +                       test( 8, 0, 0, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, srce.src1);
    1.90 +                       test( 0, 8, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src1);
    1.91 +                       test( 2, 0, 2, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, srce.src5);
    1.92 +                       test( 0, 2, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src5);
    1.93 +                       test( 0, 0, 2, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src6);
    1.94 +                       test( 0, 0, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src6);
    1.95 +                       test( 2, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src7);
    1.96 +                       test( 0, 2, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src7);
    1.97 +                       test( 4, 0, 0, 0, As, BDs, ABMix, "CLASS", et, ++testrun, srce.src8);
    1.98 +                       test( 0, 4, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src8);
    1.99 +                       break;
   1.100 +                   case "FIELD":
   1.101 +                       test( 8, 0, 0, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, srce.src1);
   1.102 +                       test( 8, 0, 0, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, srce.src2);
   1.103 +                       test( 6, 0, 0, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, srce.src3);
   1.104 +                       test( 2, 0, 2, 0, As, BDs, ABMix, "CLASS",   et, ++testrun, srce.src4);
   1.105 +                       test( 0, 8, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src1);
   1.106 +                       test( 0, 8, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src2);
   1.107 +                       test( 0, 6, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src3);
   1.108 +                       test( 0, 2, 0, 2, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src4);
   1.109 +                       break;
   1.110 +                   default:/*TYPE,PARAMETER,LOCAL_VARIABLE,ANNOTATION_TYPE,PACKAGE*/
   1.111 +                       test( 0, 2, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src4);
   1.112 +                       test( 0, 2, 0, 0, As, BDs, ABMix, "RUNTIME", et, ++testrun, srce.src5);
   1.113 +                       break;
   1.114 +               }
   1.115 +            }
   1.116 +        }
   1.117 +    }
   1.118 +
   1.119 +    public void test(int tinv, int tvis, int inv, int vis, Boolean Arepeats,
   1.120 +                     Boolean BDrepeats, Boolean ABmix, String rtn, String et2,
   1.121 +                     Integer N, srce source) throws Exception {
   1.122 +        ++testcount;
   1.123 +        expected_tvisibles = tvis;
   1.124 +        expected_tinvisibles = tinv;
   1.125 +        expected_visibles = vis;
   1.126 +        expected_invisibles = inv;
   1.127 +        File testFile = null;
   1.128 +        String tname="Test" + N.toString();
   1.129 +        hasInnerClass=false;
   1.130 +        String testDef = "Test " + testcount + " parameters: tinv=" + tinv +
   1.131 +                ", tvis=" + tvis + ", inv=" + inv + ", vis=" + vis +
   1.132 +                ", Arepeats=" + Arepeats + ", BDrepeats=" + BDrepeats +
   1.133 +                ", ABmix=" + ABmix + ", retention: " + rtn + ", anno2: " +
   1.134 +                et2 + ", src=" + source + "\n    " + source.description;
   1.135 +
   1.136 +        println(testDef);
   1.137 +        // Create test source and File.
   1.138 +        String sourceString = sourceString(tname, rtn, et2, Arepeats,
   1.139 +                                           BDrepeats, ABmix, source);
   1.140 +        testFile = writeTestFile(tname+".java", sourceString);
   1.141 +        // Compile test source and read classfile.
   1.142 +        File classFile = null;
   1.143 +        try {
   1.144 +            classFile = compile(testFile);
   1.145 +        } catch (Error err) {
   1.146 +            System.err.println("Failed compile. Source:\n" + sourceString);
   1.147 +            throw err;
   1.148 +        }
   1.149 +        //if sourcString() set hasInnerClass it also set innerClassname.
   1.150 +        if(hasInnerClass) {
   1.151 +            StringBuffer sb = new StringBuffer(classFile.getAbsolutePath());
   1.152 +            classFile=new File(sb.insert(sb.lastIndexOf(".class"),innerClassname).toString());
   1.153 +            println("classfile: " + classFile.getAbsolutePath());
   1.154 +        }
   1.155 +        ClassFile cf = ClassFile.read(classFile);
   1.156 +
   1.157 +        //Test class,fields and method counts.
   1.158 +        test(cf);
   1.159 +
   1.160 +        for (Field f : cf.fields) {
   1.161 +            if(source.local)
   1.162 +                test(cf, f, true);
   1.163 +            else
   1.164 +                test(cf,f);
   1.165 +        }
   1.166 +        for (Method m: cf.methods) {
   1.167 +            if(source.local)
   1.168 +                test(cf, m, true);
   1.169 +            else
   1.170 +                test(cf, m);
   1.171 +        }
   1.172 +        countAnnotations();
   1.173 +        if (errors > 0) {
   1.174 +            System.err.println( testDef );
   1.175 +            System.err.println( "Source:\n" + sourceString );
   1.176 +            throw new Exception( errors + " errors found" );
   1.177 +        }
   1.178 +        println("Pass");
   1.179 +    }
   1.180 +
   1.181 +    // Source for test cases
   1.182 +    String sourceString(String testname, String retentn, String annot2,
   1.183 +                        Boolean Arepeats, Boolean BDrepeats, Boolean ABmix,
   1.184 +                        srce src) {
   1.185 +
   1.186 +        String As = "@A", Bs = "@B", Ds = "@D";
   1.187 +        if(Arepeats) As = "@A @A";
   1.188 +        if(BDrepeats) {
   1.189 +            Bs = "@B @B";
   1.190 +            Ds = "@D @D";
   1.191 +        }
   1.192 +        if(ABmix) { As = "@A @B"; Bs = "@A @B"; Ds = "@D @D"; }
   1.193 +
   1.194 +        // Source to check for TYPE_USE and TYPE_PARAMETER annotations.
   1.195 +        // Source base (annotations) is same for all test cases.
   1.196 +        String source = new String();
   1.197 +        String imports = new String("import java.lang.annotation.*; \n" +
   1.198 +            "import static java.lang.annotation.RetentionPolicy.*; \n" +
   1.199 +            "import static java.lang.annotation.ElementType.*; \n" +
   1.200 +            "import java.util.List; \n" +
   1.201 +            "import java.util.HashMap; \n" +
   1.202 +            "import java.util.Map; \n\n");
   1.203 +
   1.204 +            String sourceBase = new String("@Retention("+retentn+")\n" +
   1.205 +            "@Target({TYPE_USE,_OTHER_})\n" +
   1.206 +            "@Repeatable( AC.class )\n" +
   1.207 +            "@interface A { }\n\n" +
   1.208 +
   1.209 +            "@Retention("+retentn+")\n" +
   1.210 +            "@Target({TYPE_USE,_OTHER_})\n" +
   1.211 +            "@interface AC { A[] value(); }\n\n" +
   1.212 +
   1.213 +            "@Retention("+retentn+")\n" +
   1.214 +            "@Target({TYPE_USE,_OTHER_})\n" +
   1.215 +            "@Repeatable( BC.class )\n" +
   1.216 +            "@interface B { }\n\n" +
   1.217 +
   1.218 +            "@Retention("+retentn+")\n" +
   1.219 +            "@Target({TYPE_USE,_OTHER_})\n" +
   1.220 +            "@interface BC { B[] value(); } \n\n" +
   1.221 +
   1.222 +            "@Retention("+retentn+")\n" +
   1.223 +            "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" +
   1.224 +            "@Repeatable(DC.class)\n" +
   1.225 +            "@interface D { }\n\n" +
   1.226 +
   1.227 +            "@Retention("+retentn+")\n" +
   1.228 +            "@Target({TYPE_USE,TYPE_PARAMETER,_OTHER_})\n" +
   1.229 +            "@interface DC { D[] value(); }\n\n");
   1.230 +
   1.231 +        // Test case sources with sample generated source
   1.232 +        switch(src) {
   1.233 +            case src1: // (repeating) type annotations on field in method body
   1.234 +                    /*
   1.235 +                     * class Test1 {
   1.236 +                     * Test1(){}
   1.237 +                     * // type usage in method body
   1.238 +                     * String test(Test1 this, String param, String ... vararg) {
   1.239 +                     *     @A @B
   1.240 +                     *     Object o = new @A @B  String @A @B  [3];
   1.241 +                     *         return (@A @B  String) null;
   1.242 +                     * }}
   1.243 +                      */
   1.244 +                source = new String(
   1.245 +                    "// " + src.description + "\n" +
   1.246 +                    "class " + testname + " {\n" +
   1.247 +                    "" + testname +"(){} \n" +
   1.248 +                    "// type usage in method body \n" +
   1.249 +                    "String test("+testname+" this, " +
   1.250 +                       "String param, String ... vararg) { \n" +
   1.251 +                    "    _As_ _Bs_\n    Object o = new _As_ _Bs_  String _As_ _Bs_  [3]; \n" +
   1.252 +                    "        return (_As_ _Bs_  String) null; \n" +
   1.253 +                    "} \n" +
   1.254 +                    "} \n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
   1.255 +                    "\n\n";
   1.256 +                    break;
   1.257 +            case src2: // (repeating) annotations on type parameters, bounds and  type arguments in new statement.
   1.258 +                    /*
   1.259 +                     * class Test2<T extends Object> {
   1.260 +                     *     Map<List<String>, Integer> map =
   1.261 +                     *         new HashMap<@A @B List<@A @B String>, @A @B Integer>();
   1.262 +                     *     Map<List<String>, Integer> map2 = new @A @B HashMap<>();
   1.263 +                     *     String test(Test2<T> this) { return null;}
   1.264 +                     *     <T> String genericMethod(T t) { return null; }
   1.265 +                     * }
   1.266 +                     */
   1.267 +                source = new String( source +
   1.268 +                    "// " + src.description + "\n" +
   1.269 +                    "class " + testname + "<T extends Object> {\n" +
   1.270 +                    "    Map<List<String>, Integer> map =\n" +
   1.271 +                    "        new HashMap<_As_ _Bs_ List<_As_ _Bs_ String>, _As_ _Bs_ Integer>();\n" +
   1.272 +                    "    Map<List<String>, Integer> map2 = new _As_ _Bs_ HashMap<>();\n" +
   1.273 +                    "    String test(" + testname + "<T> this) { return null;}\n" +
   1.274 +                    "    <T> String genericMethod(T t) { return null; }\n" +
   1.275 +                    "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
   1.276 +                    "\n\n";
   1.277 +                break;
   1.278 +            case src3: // (repeating)annotations on type parameters of class, method return value in method.
   1.279 +                    /*
   1.280 +                     * class Test3{
   1.281 +                     *     <E extends Comparable> Map<List<E>, E > foo(E e) {
   1.282 +                     *         class maptest <E> {
   1.283 +                     *             Map<List<E>,E> getMap() {
   1.284 +                     *                 Map<List<E>,E> Em = new HashMap<List<@A @B @D E>,@A @B @D E>();
   1.285 +                     *                 return Em;
   1.286 +                     *             }
   1.287 +                     *         }
   1.288 +                     *         return new maptest<E>().getMap();
   1.289 +                     *    }
   1.290 +                     *    Map<List<String>,String> shm = foo(new String("hello"));
   1.291 +                     * }
   1.292 +                     */
   1.293 +                source = new String( source +
   1.294 +                    "// " + src.description + "\n" +
   1.295 +                    "class "+ testname + "{\n" +
   1.296 +                    "    <E extends Comparable> Map<List<E>, E > foo(E e) {\n" +
   1.297 +                    "        class maptest <E> {\n" +                  // inner class $1maptest
   1.298 +                    "            Map<List<E>,E> getMap() { \n" +
   1.299 +                    "                Map<List<E>,E> Em = new HashMap<List<_As_ _Bs_ _Ds_ E>,_As_ _Bs_ _Ds_ E>();\n" +
   1.300 +                    "                return Em;\n" +
   1.301 +                    "            }\n" +
   1.302 +                    "        }\n" +
   1.303 +                    "        return new maptest<E>().getMap();\n" +
   1.304 +                    "   }\n" +
   1.305 +                    "   Map<List<String>,String> shm = foo(new String(\"hello\"));\n" +
   1.306 +                    "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs).replace("_Ds_",Ds) +
   1.307 +                    "\n\n";
   1.308 +                    hasInnerClass=true;
   1.309 +                    innerClassname="$1maptest";
   1.310 +                break;
   1.311 +            case src4: // (repeating)annotations on field in anonymous class
   1.312 +                    /*
   1.313 +                     * class Test95{
   1.314 +                     *     void mtest( Test95 t){  }
   1.315 +                     *     public void test() {
   1.316 +                     *         mtest( new Test95() {
   1.317 +                     *             @A @A @B @B String data2 = "test";
   1.318 +                     *         });
   1.319 +                     *     }
   1.320 +                     * }
   1.321 +                     */
   1.322 +                source = new String( source +
   1.323 +                    "// " + src.description + "\n" +
   1.324 +                    "class "+ testname + "{\n" +
   1.325 +                    "    void mtest( "+ testname + " t){  }\n" +
   1.326 +                    "    public void test() {\n" +
   1.327 +                    "        mtest( new "+ testname + "() {\n" +
   1.328 +                    "            _As_ _Bs_ String data2 = \"test\";\n" +
   1.329 +                    "        });\n" +
   1.330 +                    "    }\n" +
   1.331 +                    "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
   1.332 +                    "\n\n";
   1.333 +                    hasInnerClass=true;
   1.334 +                    innerClassname="$1";
   1.335 +                break;
   1.336 +            case src5: // (repeating)annotations on method in anonymous class
   1.337 +                    /*
   1.338 +                     * class Test120{
   1.339 +                     *     void mtest( Test120 t){  }
   1.340 +                     *     public void test() {
   1.341 +                     *         mtest( new Test120() {
   1.342 +                     *             @A @B @A @B String m2(){return null;};
   1.343 +                     *         });
   1.344 +                     *     }
   1.345 +                     */
   1.346 +                source = new String( source +
   1.347 +                    "// " + src.description + "\n" +
   1.348 +                    "class "+ testname + "{\n" +
   1.349 +                    "    void mtest( "+ testname + " t){  }\n" +
   1.350 +                    "    public void test() {\n" +
   1.351 +                    "        mtest( new "+ testname + "() {\n" +
   1.352 +                    "            _As_ _Bs_ String m2(){return null;};\n" +
   1.353 +                    "        });\n" +
   1.354 +                    "    }\n" +
   1.355 +                    "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
   1.356 +                    "\n\n";
   1.357 +                    hasInnerClass=true;
   1.358 +                    innerClassname="$1";
   1.359 +                break;
   1.360 +            case src6: // (repeating)annotations on void method declaration
   1.361 +                    /*
   1.362 +                     * class Test95{
   1.363 +                     *     @A @A @B @B public void test() { };
   1.364 +                     * }
   1.365 +                     */
   1.366 +                source = new String( source +
   1.367 +                    "// " + src.description + "\n" +
   1.368 +                    "class "+ testname + "{\n" +
   1.369 +                    "    _As_ _Bs_ public void test() { }\n" +
   1.370 +                    "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
   1.371 +                    "\n\n";
   1.372 +                    hasInnerClass=false;
   1.373 +                break;
   1.374 +            case src7: // (repeating) type annotations in use of instanceof
   1.375 +                    /*
   1.376 +                     *   class Test10{
   1.377 +                     *       String data = "test";
   1.378 +                     *       boolean dataIsString = ( data instanceof @A @B @A @B String);
   1.379 +                     *   }
   1.380 +                     */
   1.381 +                source = new String( source +
   1.382 +                    "// " + src.description + "\n" +
   1.383 +                    "class "+ testname + "{\n" +
   1.384 +                    "    String data = \"test\";\n" +
   1.385 +                    "    boolean dataIsString = ( data instanceof _As_ _Bs_ String);\n" +
   1.386 +                    "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
   1.387 +                    "\n\n";
   1.388 +                    hasInnerClass=false;
   1.389 +                break;
   1.390 +            case src8: // (repeating) type annotations in use of instanceof
   1.391 +                    /*
   1.392 +                     *   class Test20{
   1.393 +                     *       String data = "test";
   1.394 +                     *       Boolean isString() {
   1.395 +                     *           if( data instanceof @A @B @A @B String )
   1.396 +                     *               return true;
   1.397 +                     *           else
   1.398 +                     *               return( data instanceof @A @B @A @B String );
   1.399 +                     *       }
   1.400 +                     *   }
   1.401 +                     */
   1.402 +                source = new String( source +
   1.403 +                    "// " + src.description + "\n" +
   1.404 +                    "class "+ testname + "{\n" +
   1.405 +                    "    String data = \"test\";\n" +
   1.406 +                    "    Boolean isString() { \n" +
   1.407 +                    "        if( data instanceof _As_ _Bs_ String )\n" +
   1.408 +                    "            return true;\n" +
   1.409 +                    "        else\n" +
   1.410 +                    "            return( data instanceof _As_ _Bs_ String );\n" +
   1.411 +                    "    }\n" +
   1.412 +                    "}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
   1.413 +                    "\n\n";
   1.414 +                    hasInnerClass=false;
   1.415 +                break;
   1.416 +
   1.417 +        }
   1.418 +        return imports + source;
   1.419 +    }
   1.420 +}

mercurial