1 /*
2  * Copyright (c) 2009, 2013, 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  * @summary Test population of reference info for type casts
29  * @compile -g Driver.java ReferenceInfoUtil.java TypeCasts.java
30  * @run main Driver TypeCasts
31  */
32 public class TypeCasts {
33 
34     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
35             typeIndex = 0)
returnObject()36     public String returnObject() {
37         return "Object returnObject() { return (@TA String)null; }";
38     }
39 
40     @TADescriptions({
41         @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
42                 typeIndex = 0),
43         @TADescription(annotation = "TB", type = CAST,
44                 genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
45                 typeIndex = 0),
46         @TADescription(annotation = "TC", type = CAST,
47                 genericLocation = { 0, 0, 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
48                 typeIndex = 0)
49     })
returnObjectArray()50     public String returnObjectArray() {
51         return "Object returnObjectArray() { return (@TC String @TA [] @TB [])null; }";
52     }
53 
54     @TADescriptions({
55         @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
56                 typeIndex = 0),
57         @TADescription(annotation = "TB", type = CAST,
58                 genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
59                 typeIndex = 0)
60     })
returnObjectGeneric()61     public String returnObjectGeneric() {
62         return "Object returnObjectGeneric() { return (@TA List<@TB String>)null; }";
63     }
64 
65     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
66             typeIndex = 0)
returnPrim()67     public String returnPrim() {
68         return "Object returnPrim() { return (@TA int)0; }";
69     }
70 
71     @TADescriptions({
72         @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
73                 typeIndex = 0),
74         @TADescription(annotation = "TB", type = CAST,
75                 genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
76                 typeIndex = 0)
77     })
returnPrimArray()78     public String returnPrimArray() {
79         return "Object returnPrimArray() { return (@TB int @TA [])null; }";
80     }
81 
82     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
83             typeIndex = 0)
initObject()84     public String initObject() {
85         return "void initObject() { Object a =  (@TA String)null; }";
86     }
87 
88     @TADescriptions({
89         @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
90                 typeIndex = 0),
91         @TADescription(annotation = "TB", type = CAST,
92                 genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
93                 typeIndex = 0)
94     })
initObjectArray()95     public String initObjectArray() {
96         return "void initObjectArray() { Object a = (@TB String @TA [])null; }";
97     }
98 
99     @TADescriptions({
100         @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
101                 typeIndex = 0),
102         @TADescription(annotation = "TB", type = CAST,
103                 genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
104                 typeIndex = 0)
105     })
initObjectGeneric()106     public String initObjectGeneric() {
107         return "void initObjectGeneric() { Object a = (@TA List<@TB String>)null; }";
108     }
109 
110     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
111             typeIndex = 0)
initPrim()112     public String initPrim() {
113         return "void initPrim() { Object a =  (@TA int)0; }";
114     }
115 
116     @TADescriptions({
117         @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
118                 typeIndex = 0),
119         @TADescription(annotation = "TB", type = CAST,
120                 genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
121                 typeIndex = 0)
122     })
initPrimArray()123     public String initPrimArray() {
124         return "void initPrimArray() { Object a = (@TB int @TA [])null; }";
125     }
126 
127     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
128             typeIndex = 0)
eqtestObject()129     public String eqtestObject() {
130         return "void eqtestObject() { if (null == (@TA String)null); }";
131     }
132 
133     @TADescriptions({
134         @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
135                 typeIndex = 0),
136         @TADescription(annotation = "TB", type = CAST,
137                 genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
138                 typeIndex = 0)
139     })
eqtestObjectArray()140     public String eqtestObjectArray() {
141         return "void eqtestObjectArray() { if (null == (@TB String @TA [])null); }";
142     }
143 
144     @TADescriptions({
145         @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
146                 typeIndex = 0),
147         @TADescription(annotation = "TB", type = CAST,
148                 genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
149                 typeIndex = 0)
150     })
eqtestObjectGeneric()151     public String eqtestObjectGeneric() {
152         return "void eqtestObjectGeneric() { if (null == (@TA List<@TB String >)null); }";
153     }
154 
155     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
156             typeIndex = 0)
157     // compiler optimizes away compile time constants casts
eqtestPrim()158     public String eqtestPrim() {
159         return "void eqtestPrim(int a) { if (0 == (@TA int)a); }";
160     }
161 
162     @TADescriptions({
163         @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
164                 typeIndex = 0),
165         @TADescription(annotation = "TB", type = CAST,
166                 genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
167                 typeIndex = 0)
168     })
eqtestPrimArray()169     public String eqtestPrimArray() {
170         return "void eqtestPrimArray() { if (null == (@TB int @TA [])null); }";
171     }
172 
173     @TADescriptions({
174         @TADescription(annotation = "TA", type = CAST,
175                 offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 0),
176         @TADescription(annotation = "TB", type = CAST,
177                 offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 1),
178         @TADescription(annotation = "TC", type = CAST,
179                 offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 1,
180                 genericLocation = {3, 0})
181     })
intersection1()182     public String intersection1() {
183         return "void intersection() { Object o = (@TA String & @TB Comparable<@TC String>) null; }";
184     }
185 
186     @TADescriptions({
187         @TADescription(annotation = "TA", type = CAST,
188                 offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 0),
189         @TADescription(annotation = "TB", type = CAST,
190                 offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 1),
191         @TADescription(annotation = "TC", type = CAST,
192                 offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 1,
193                 genericLocation = {3, 0}),
194         @TADescription(annotation = "TD", type = CAST,
195                 offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 2),
196     })
intersection2()197     public String intersection2() {
198         return "void intersection() { Object o = (@TA String & @TB Comparable<@TC String> & @TD CharSequence) null; }";
199     }
200 }
201