1 /*
   2  * Copyright (c) 2013, 2015, 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 import static com.sun.tools.classfile.TypeAnnotation.TargetType.*;
  25 
  26 /*
  27  * @test
  28  * @bug 8013852 8042451
  29  * @summary Test population of reference info for instance and class initializers
  30  * @author Werner Dietl
  31  * @modules jdk.compiler/com.sun.tools.classfile
  32  * @compile -g Driver.java ReferenceInfoUtil.java Initializers.java
  33  * @run main Driver Initializers
  34  */
  35 public class Initializers {
  36 
  37     @TADescription(annotation = "TA", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
  38     @TADescription(annotation = "TB", type = NEW,
  39                 genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
  40         public String instanceInit1() {
  41         return "class %TEST_CLASS_NAME% { { Object o = new @TA ArrayList<@TB String>(); } }";
  42     }
  43 
  44     @TADescription(annotation = "TA", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
  45     @TADescription(annotation = "TB", type = NEW,
  46                 genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
  47     @TADescription(annotation = "TC", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
  48     @TADescription(annotation = "TD", type = NEW,
  49                 genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
  50         public String instanceInit2() {
  51         return "class %TEST_CLASS_NAME% { Object f = new @TA ArrayList<@TB String>(); " +
  52                 " { Object o = new @TC ArrayList<@TD String>(); } }";
  53     }
  54 
  55     @TADescription(annotation = "TA", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
  56     @TADescription(annotation = "TB", type = NEW,
  57                 genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
  58         public String staticInit1() {
  59         return "class %TEST_CLASS_NAME% { static { Object o = new @TA ArrayList<@TB String>(); } }";
  60     }
  61 
  62     @TADescription(annotation = "TA", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
  63     @TADescription(annotation = "TB", type = NEW,
  64                 genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
  65     @TADescription(annotation = "TC", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
  66     @TADescription(annotation = "TD", type = NEW,
  67                 genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
  68     @TADescription(annotation = "TE", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
  69     @TADescription(annotation = "TF", type = NEW,
  70                 genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
  71         public String staticInit2() {
  72         return "class %TEST_CLASS_NAME% { Object f = new @TA ArrayList<@TB String>(); " +
  73                 " static Object g = new @TC ArrayList<@TD String>(); " +
  74                 " static { Object o = new @TE ArrayList<@TF String>(); } }";
  75     }
  76 
  77     @TADescription(annotation = "TA", type = CAST,
  78                 typeIndex = 0, offset = ReferenceInfoUtil.IGNORE_VALUE)
  79         public String lazyConstantCast1() {
  80         return "class %TEST_CLASS_NAME% { public static final Object o = (@TA Object) null; }";
  81     }
  82 
  83     @TADescription(annotation = "RTAs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
  84     @TADescription(annotation = "RTBs", type = NEW,
  85             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
  86     public String instanceInitRepeatableAnnotation1() {
  87         return "class %TEST_CLASS_NAME% { { Object o = new @RTA @RTA ArrayList<@RTB @RTB String>(); } }";
  88     }
  89 
  90     @TADescription(annotation = "RTAs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
  91     @TADescription(annotation = "RTBs", type = NEW,
  92             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
  93     @TADescription(annotation = "RTCs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
  94     @TADescription(annotation = "RTDs", type = NEW,
  95             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
  96     public String instanceInitRepeatableAnnotation2() {
  97         return "class %TEST_CLASS_NAME% { Object f = new @RTA @RTA ArrayList<@RTB @RTB String>(); " +
  98                 " { Object o = new @RTC @RTC ArrayList<@RTD @RTD String>(); } }";
  99     }
 100 
 101     @TADescription(annotation = "RTAs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
 102     @TADescription(annotation = "RTBs", type = NEW,
 103             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
 104     public String staticInitRepeatableAnnotation1() {
 105         return "class %TEST_CLASS_NAME% { static { Object o = new @RTA @RTA ArrayList<@RTB @RTB String>(); } }";
 106     }
 107 
 108     @TADescription(annotation = "RTAs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
 109     @TADescription(annotation = "RTBs", type = NEW,
 110             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
 111     @TADescription(annotation = "RTCs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
 112     @TADescription(annotation = "RTDs", type = NEW,
 113             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
 114     @TADescription(annotation = "RTEs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
 115     @TADescription(annotation = "RTFs", type = NEW,
 116             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
 117     public String staticInitRepeatableAnnotation2() {
 118         return "class %TEST_CLASS_NAME% { Object f = new @RTA @RTA ArrayList<@RTB @RTB String>(); " +
 119                 " static Object g = new @RTC @RTC ArrayList<@RTD @RTD String>(); " +
 120                 " static { Object o = new @RTE @RTE ArrayList<@RTF @RTF String>(); } }";
 121     }
 122 
 123     // TODO: test interaction with several constructors, especially non-initial constructors.
 124     // I don't think this kind of test is possible here.
 125 
 126     @TADescription(annotation = "RTAs", type = CAST,
 127             typeIndex = 0, offset = ReferenceInfoUtil.IGNORE_VALUE)
 128     public String lazyConstantCastRepeatableAnnotation1() {
 129         return "class %TEST_CLASS_NAME% { public static final Object o = (@RTA @RTA Object) null; }";
 130     }
 131 }