1 /*
  2  * Copyright (c) 2009, 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 java.lang.classfile.TypeAnnotation.TargetType.*;
 25 
 26 /*
 27  * @test
 28  * @bug 8042451
 29  * @summary Test population of reference info for new object creations
 30  * @enablePreview
 31  * @modules java.base/jdk.internal.classfile.impl
 32  * @compile -g Driver.java ReferenceInfoUtil.java NewObjects.java
 33  * @run main Driver NewObjects
 34  */
 35 public class NewObjects {
 36 
 37     @TADescription(annotation = "TA", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
 38     public String returnObject() {
 39         return "Object returnObject() { return new @TA String(); }";
 40     }
 41 
 42     @TADescription(annotation = "TA", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
 43     @TADescription(annotation = "TB", type = NEW,
 44             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
 45     public String returnObjectGeneric() {
 46         return "Object returnObjectGeneric() { return new @TA ArrayList<@TB String>(); }";
 47     }
 48 
 49     @TADescription(annotation = "TA", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
 50     public String initObject() {
 51         return "void initObject() { Object a =  new @TA String(); }";
 52     }
 53 
 54     @TADescription(annotation = "TA", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
 55     @TADescription(annotation = "TB", type = NEW,
 56             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
 57     @TADescription(annotation = "TC", type = NEW,
 58             genericLocation = { 3, 1 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
 59     public String initObjectGeneric() {
 60         return "void initObjectGeneric() { Object a = new @TA HashMap<@TB String, @TC String>(); }";
 61     }
 62 
 63     @TADescription(annotation = "TA", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
 64     public String eqtestObject() {
 65         return "void eqtestObject(String s) { if (s == new @TA String()); }";
 66     }
 67 
 68     @TADescription(annotation = "TA", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
 69     @TADescription(annotation = "TB", type = NEW,
 70             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
 71     public String eqtestObjectGeneric() {
 72         return "void eqtestObjectGeneric(ArrayList<String> as) { if (as == new @TA ArrayList<@TB String >()); }";
 73     }
 74 
 75     @TADescription(annotation = "TA", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
 76             genericLocation = {0, 0})
 77     @TADescription(annotation = "TB", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
 78     public String returnNewArray1() {
 79         return "Object returnNewArray1() { return new @TA String @TB[1]; }";
 80     }
 81 
 82     @TADescription(annotation = "TA", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
 83             genericLocation = {0, 0, 0, 0})
 84     @TADescription(annotation = "TB", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
 85     @TADescription(annotation = "TC", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
 86             genericLocation = {0, 0})
 87     public String returnNewArray2() {
 88         return "Object returnNewArray2() { return new @TA String @TB [1] @TC [2]; }";
 89     }
 90 
 91     @TADescription(annotation = "TA", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
 92             genericLocation = {0, 0, 0, 0})
 93     @TADescription(annotation = "TB", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
 94             genericLocation = {0, 0, 0, 0, 1, 0})
 95     @TADescription(annotation = "TC", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
 96     @TADescription(annotation = "TD", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
 97             genericLocation = {0, 0})
 98     public String returnNewArray3() {
 99         return "Object returnNewArray3() { return new @TA Outer. @TB Inner @TC [1] @TD [2]; }";
100     }
101 
102     @TADescription(annotation = "TA", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
103             genericLocation = {0, 0, 0, 0})
104     @TADescription(annotation = "TB", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
105             genericLocation = {0, 0, 0, 0, 1, 0})
106     @TADescription(annotation = "TC", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
107             genericLocation = {0, 0, 0, 0, 1, 0, 1, 0})
108     @TADescription(annotation = "TD", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
109     @TADescription(annotation = "TE", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
110             genericLocation = {0, 0})
111     public String returnNewArray4() {
112         return "Object returnNewArray4() { return new @TA Outer. @TB Middle. @TC MInner @TD [1] @TE [2]; }";
113     }
114 
115     @TADescription(annotation = "TA", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
116     @TADescription(annotation = "TB", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
117             genericLocation = {3, 0, 0, 0, 0, 0})
118     @TADescription(annotation = "TC", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
119             genericLocation = {3, 0, 0, 0, 0, 0, 1, 0})
120     @TADescription(annotation = "TD", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
121             genericLocation = {3, 0, 0, 0, 0, 0, 1, 0, 1, 0})
122     @TADescription(annotation = "TE", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
123             genericLocation = {3, 0})
124     @TADescription(annotation = "TF", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
125             genericLocation = {3, 0, 0, 0})
126     public String returnNewArray5() {
127         return "Object returnNewArray5() { return new @TA ArrayList<@TB Outer. @TC Middle. @TD MInner @TE [] @TF []>(); }";
128     }
129 
130     @TADescription(annotation = "TA", type = FIELD, offset = ReferenceInfoUtil.IGNORE_VALUE,
131             genericLocation = {0, 0, 0, 0})
132     @TADescription(annotation = "TB", type = FIELD, offset = ReferenceInfoUtil.IGNORE_VALUE,
133             genericLocation = {0, 0, 0, 0, 1, 0})
134     @TADescription(annotation = "TC", type = FIELD, offset = ReferenceInfoUtil.IGNORE_VALUE)
135     @TADescription(annotation = "TD", type = FIELD, offset = ReferenceInfoUtil.IGNORE_VALUE,
136     genericLocation = {0, 0})
137     public String arrayField() {
138         return "@TA Outer. @TB Inner @TC [] @TD [] f;";
139     }
140 
141     @TADescription(annotation = "RTAs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
142     public String returnObjectRepeatableAnnotation() {
143         return "Object returnObject() { return new @RTA @RTA String(); }";
144     }
145 
146     @TADescription(annotation = "RTAs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
147     @TADescription(annotation = "RTBs", type = NEW,
148             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
149     public String returnObjectGenericRepeatableAnnotation() {
150         return "Object returnObjectGeneric() { return new @RTA @RTA ArrayList<@RTB @RTB String>(); }";
151     }
152 
153     @TADescription(annotation = "RTAs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
154     public String initObjectRepeatableAnnotation() {
155         return "void initObject() { Object a =  new @RTA @RTA String(); }";
156     }
157 
158     @TADescription(annotation = "RTAs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
159     @TADescription(annotation = "RTBs", type = NEW,
160             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
161     @TADescription(annotation = "RTCs", type = NEW,
162             genericLocation = { 3, 1 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
163     public String initObjectGenericRepeatableAnnotation() {
164         return "void initObjectGeneric() { Object a = new @RTA @RTA HashMap<@RTB @RTB String, @RTC @RTC String>(); }";
165     }
166 
167     @TADescription(annotation = "RTAs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
168     public String eqtestObjectRepeatableAnnotation() {
169         return "void eqtestObject(String s) { if (s == new @RTA @RTA String()); }";
170     }
171 
172     @TADescription(annotation = "RTAs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
173     @TADescription(annotation = "RTBs", type = NEW,
174             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE)
175     public String eqtestObjectGenericRepeatableAnnotation() {
176         return "void eqtestObjectGeneric(ArrayList<String> as) { if (as == new @RTA @RTA ArrayList<@RTB @RTB String >()); }";
177     }
178 
179     @TADescription(annotation = "RTAs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
180             genericLocation = {0, 0})
181     @TADescription(annotation = "RTBs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
182     public String returnNewArrayRepeatableAnnotation1() {
183         return "Object returnNewArray1() { return new @RTA @RTA String @RTB @RTB[1]; }";
184     }
185 
186     @TADescription(annotation = "RTAs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
187             genericLocation = {0, 0, 0, 0})
188     @TADescription(annotation = "RTBs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
189     @TADescription(annotation = "RTCs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
190             genericLocation = {0, 0})
191     public String returnNewArrayRepeatableAnnotation2() {
192         return "Object returnNewArray2() { return new @RTA @RTA String @RTB @RTB [1] @RTC @RTC [2]; }";
193     }
194 
195     @TADescription(annotation = "RTAs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
196             genericLocation = {0, 0, 0, 0})
197     @TADescription(annotation = "RTBs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
198             genericLocation = {0, 0, 0, 0, 1, 0})
199     @TADescription(annotation = "RTCs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
200     @TADescription(annotation = "RTDs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
201             genericLocation = {0, 0})
202     public String returnNewArrayRepeatableAnnotation3() {
203         return "Object returnNewArray3() { return new @RTA @RTA Outer. @RTB @RTB Inner @RTC @RTC [1] @RTD @RTD [2]; }";
204     }
205 
206     @TADescription(annotation = "RTAs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
207             genericLocation = {0, 0, 0, 0})
208     @TADescription(annotation = "RTBs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
209             genericLocation = {0, 0, 0, 0, 1, 0})
210     @TADescription(annotation = "RTCs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
211             genericLocation = {0, 0, 0, 0, 1, 0, 1, 0})
212     @TADescription(annotation = "RTDs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
213     @TADescription(annotation = "RTEs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
214             genericLocation = {0, 0})
215     public String returnNewArrayRepeatableAnnotation4() {
216         return "Object returnNewArray4() { return new @RTA @RTA Outer." +
217                 " @RTB @RTB Middle. @RTC @RTC MInner @RTD @RTD [1] @RTE @RTE [2]; }";
218     }
219 
220     @TADescription(annotation = "RTAs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE)
221     @TADescription(annotation = "RTBs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
222             genericLocation = {3, 0, 0, 0, 0, 0})
223     @TADescription(annotation = "RTCs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
224             genericLocation = {3, 0, 0, 0, 0, 0, 1, 0})
225     @TADescription(annotation = "RTDs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
226             genericLocation = {3, 0, 0, 0, 0, 0, 1, 0, 1, 0})
227     @TADescription(annotation = "RTEs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
228             genericLocation = {3, 0})
229     @TADescription(annotation = "RTFs", type = NEW, offset = ReferenceInfoUtil.IGNORE_VALUE,
230             genericLocation = {3, 0, 0, 0})
231     public String returnNewArrayRepeatableAnnotation5() {
232         return "Object returnNewArray5() { return new @RTA @RTA ArrayList<@RTB @RTB Outer." +
233                 " @RTC @RTC Middle. @RTD @RTD MInner @RTE @RTE [] @RTF @RTF []>(); }";
234     }
235 
236     @TADescription(annotation = "RTAs", type = FIELD, offset = ReferenceInfoUtil.IGNORE_VALUE,
237             genericLocation = {0, 0, 0, 0})
238     @TADescription(annotation = "RTBs", type = FIELD, offset = ReferenceInfoUtil.IGNORE_VALUE,
239             genericLocation = {0, 0, 0, 0, 1, 0})
240     @TADescription(annotation = "RTCs", type = FIELD, offset = ReferenceInfoUtil.IGNORE_VALUE)
241     @TADescription(annotation = "RTDs", type = FIELD, offset = ReferenceInfoUtil.IGNORE_VALUE,
242             genericLocation = {0, 0})
243     public String arrayFieldRepeatableAnnotation() {
244         return "@RTA @RTA Outer. @RTB @RTB Inner @RTC @RTC [] @RTD @RTD [] f;";
245     }
246 }