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 com.sun.tools.classfile.TypeAnnotation.TargetType.*;
  25 
  26 /*
  27  * @test
  28  * @bug 8042451
  29  * @summary Test population of reference info for type casts
  30  * @modules jdk.compiler/com.sun.tools.classfile
  31  * @compile -g Driver.java ReferenceInfoUtil.java TypeCasts.java
  32  * @run main Driver TypeCasts
  33  */
  34 public class TypeCasts {
  35 
  36     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
  37             typeIndex = 0)
  38     public String returnObject() {
  39         return "Object returnObject() { return (@TA String)null; }";
  40     }
  41 
  42     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
  43             typeIndex = 0)
  44     @TADescription(annotation = "TB", type = CAST,
  45             genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
  46             typeIndex = 0)
  47     @TADescription(annotation = "TC", type = CAST,
  48             genericLocation = { 0, 0, 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
  49             typeIndex = 0)
  50     public String returnObjectArray() {
  51         return "Object returnObjectArray() { return (@TC String @TA [] @TB [])null; }";
  52     }
  53 
  54     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
  55             typeIndex = 0)
  56     @TADescription(annotation = "TB", type = CAST,
  57             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
  58             typeIndex = 0)
  59     public String returnObjectGeneric() {
  60         return "Object returnObjectGeneric() { return (@TA List<@TB String>)null; }";
  61     }
  62 
  63     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
  64             typeIndex = 0)
  65     public String returnPrim() {
  66         return "Object returnPrim() { return (@TA int)0; }";
  67     }
  68 
  69     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
  70             typeIndex = 0)
  71     @TADescription(annotation = "TB", type = CAST,
  72             genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
  73             typeIndex = 0)
  74     public String returnPrimArray() {
  75         return "Object returnPrimArray() { return (@TB int @TA [])null; }";
  76     }
  77 
  78     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
  79             typeIndex = 0)
  80     public String initObject() {
  81         return "void initObject() { Object a =  (@TA String)null; }";
  82     }
  83 
  84     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
  85             typeIndex = 0)
  86     @TADescription(annotation = "TB", type = CAST,
  87             genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
  88             typeIndex = 0)
  89     public String initObjectArray() {
  90         return "void initObjectArray() { Object a = (@TB String @TA [])null; }";
  91     }
  92 
  93     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
  94             typeIndex = 0)
  95     @TADescription(annotation = "TB", type = CAST,
  96             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
  97             typeIndex = 0)
  98     public String initObjectGeneric() {
  99         return "void initObjectGeneric() { Object a = (@TA List<@TB String>)null; }";
 100     }
 101 
 102     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 103             typeIndex = 0)
 104     public String initPrim() {
 105         return "void initPrim() { Object a =  (@TA int)0; }";
 106     }
 107 
 108     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 109             typeIndex = 0)
 110     @TADescription(annotation = "TB", type = CAST,
 111             genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
 112             typeIndex = 0)
 113     public String initPrimArray() {
 114         return "void initPrimArray() { Object a = (@TB int @TA [])null; }";
 115     }
 116 
 117     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 118             typeIndex = 0)
 119     public String eqtestObject() {
 120         return "void eqtestObject() { if (null == (@TA String)null); }";
 121     }
 122 
 123     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 124             typeIndex = 0)
 125     @TADescription(annotation = "TB", type = CAST,
 126             genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
 127             typeIndex = 0)
 128     public String eqtestObjectArray() {
 129         return "void eqtestObjectArray() { if (null == (@TB String @TA [])null); }";
 130     }
 131 
 132     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 133             typeIndex = 0)
 134     @TADescription(annotation = "TB", type = CAST,
 135             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
 136             typeIndex = 0)
 137     public String eqtestObjectGeneric() {
 138         return "void eqtestObjectGeneric() { if (null == (@TA List<@TB String >)null); }";
 139     }
 140 
 141     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 142             typeIndex = 0)
 143     // compiler optimizes away compile time constants casts
 144     public String eqtestPrim() {
 145         return "void eqtestPrim(int a) { if (0 == (@TA int)a); }";
 146     }
 147 
 148     @TADescription(annotation = "TA", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 149             typeIndex = 0)
 150     @TADescription(annotation = "TB", type = CAST,
 151             genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
 152             typeIndex = 0)
 153     public String eqtestPrimArray() {
 154         return "void eqtestPrimArray() { if (null == (@TB int @TA [])null); }";
 155     }
 156 
 157     @TADescription(annotation = "TA", type = CAST,
 158             offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 0)
 159     @TADescription(annotation = "TB", type = CAST,
 160             offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 1)
 161     @TADescription(annotation = "TC", type = CAST,
 162             offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 1,
 163             genericLocation = {3, 0})
 164     public String intersection1() {
 165         return "void intersection() { Object o = (@TA String & @TB Comparable<@TC String>) null; }";
 166     }
 167 
 168     @TADescription(annotation = "TA", type = CAST,
 169             offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 0)
 170     @TADescription(annotation = "TB", type = CAST,
 171             offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 1)
 172     @TADescription(annotation = "TC", type = CAST,
 173             offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 1,
 174             genericLocation = {3, 0})
 175     @TADescription(annotation = "TD", type = CAST,
 176             offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 2)
 177     public String intersection2() {
 178         return "void intersection() { Object o = (@TA String & @TB Comparable<@TC String> & @TD CharSequence) null; }";
 179     }
 180 
 181     @TADescription(annotation = "RTAs", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 182             typeIndex = 0)
 183     public String returnObjectRepeatableAnnotation() {
 184         return "Object returnObject() { return (@RTA @RTA String)null; }";
 185     }
 186 
 187     @TADescription(annotation = "RTAs", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 188             typeIndex = 0)
 189     @TADescription(annotation = "RTBs", type = CAST,
 190             genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
 191             typeIndex = 0)
 192     @TADescription(annotation = "RTCs", type = CAST,
 193             genericLocation = { 0, 0, 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
 194             typeIndex = 0)
 195     public String returnObjectArrayRepeatableAnnotation() {
 196         return "Object returnObjectArray() { return (@RTC @RTC String @RTA @RTA [] @RTB @RTB [])null; }";
 197     }
 198 
 199     @TADescription(annotation = "RTAs", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 200             typeIndex = 0)
 201     @TADescription(annotation = "RTBs", type = CAST,
 202             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
 203             typeIndex = 0)
 204     public String returnObjectGenericRepeatableAnnotation() {
 205         return "Object returnObjectGeneric() { return (@RTA @RTA List<@RTB @RTB String>)null; }";
 206     }
 207 
 208     @TADescription(annotation = "RTAs", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 209             typeIndex = 0)
 210     public String returnPrimRepeatableAnnotation() {
 211         return "Object returnPrim() { return (@RTA @RTA int)0; }";
 212     }
 213 
 214     @TADescription(annotation = "RTAs", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 215             typeIndex = 0)
 216     @TADescription(annotation = "RTBs", type = CAST,
 217             genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
 218             typeIndex = 0)
 219     public String returnPrimArrayRepeatableAnnotation() {
 220         return "Object returnPrimArray() { return (@RTB @RTB int @RTA @RTA [])null; }";
 221     }
 222 
 223     @TADescription(annotation = "RTAs", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 224             typeIndex = 0)
 225     public String initObjectRepeatableAnnotation() {
 226         return "void initObject() { Object a =  (@RTA @RTA String)null; }";
 227     }
 228 
 229     @TADescription(annotation = "RTAs", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 230             typeIndex = 0)
 231     @TADescription(annotation = "RTBs", type = CAST,
 232             genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
 233             typeIndex = 0)
 234     public String initObjectArrayRepeatableAnnotation() {
 235         return "void initObjectArray() { Object a = (@RTB @RTB String @RTA @RTA [])null; }";
 236     }
 237 
 238     @TADescription(annotation = "RTAs", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 239             typeIndex = 0)
 240     @TADescription(annotation = "RTBs", type = CAST,
 241             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
 242             typeIndex = 0)
 243     public String initObjectGenericRepeatableAnnotation() {
 244         return "void initObjectGeneric() { Object a = (@RTA @RTA List<@RTB @RTB String>)null; }";
 245     }
 246 
 247     @TADescription(annotation = "RTAs", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 248             typeIndex = 0)
 249     public String initPrimRepeatableAnnotation() {
 250         return "void initPrim() { Object a =  (@RTA @RTA int)0; }";
 251     }
 252 
 253     @TADescription(annotation = "RTAs", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 254             typeIndex = 0)
 255     @TADescription(annotation = "RTBs", type = CAST,
 256             genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
 257             typeIndex = 0)
 258     public String initPrimArrayRepeatableAnnotation() {
 259         return "void initPrimArray() { Object a = (@RTB @RTB int @RTA @RTA [])null; }";
 260     }
 261 
 262     @TADescription(annotation = "RTAs", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 263             typeIndex = 0)
 264     public String eqtestObjectRepeatableAnnotation() {
 265         return "void eqtestObject() { if (null == (@RTA @RTA String)null); }";
 266     }
 267 
 268     @TADescription(annotation = "RTAs", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 269             typeIndex = 0)
 270     @TADescription(annotation = "RTBs", type = CAST,
 271             genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
 272             typeIndex = 0)
 273     public String eqtestObjectArrayRepeatableAnnotation() {
 274         return "void eqtestObjectArray() { if (null == (@RTB @RTB String @RTA @RTA [])null); }";
 275     }
 276 
 277     @TADescription(annotation = "RTAs", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 278             typeIndex = 0)
 279     @TADescription(annotation = "RTBs", type = CAST,
 280             genericLocation = { 3, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
 281             typeIndex = 0)
 282     public String eqtestObjectGenericRepeatableAnnotation() {
 283         return "void eqtestObjectGeneric() { if (null == (@RTA @RTA List<@RTB @RTB String >)null); }";
 284     }
 285 
 286     @TADescription(annotation = "RTAs", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 287             typeIndex = 0)
 288     // compiler optimizes away compile time constants casts
 289     public String eqtestPrimRepeatableAnnotation() {
 290         return "void eqtestPrim(int a) { if (0 == (@RTA @RTA int)a); }";
 291     }
 292 
 293     @TADescription(annotation = "RTAs", type = CAST, offset = ReferenceInfoUtil.IGNORE_VALUE,
 294             typeIndex = 0)
 295     @TADescription(annotation = "RTBs", type = CAST,
 296             genericLocation = { 0, 0 }, offset = ReferenceInfoUtil.IGNORE_VALUE,
 297             typeIndex = 0)
 298     public String eqtestPrimArrayRepeatableAnnotation() {
 299         return "void eqtestPrimArray() { if (null == (@RTB @RTB int @RTA @RTA [])null); }";
 300     }
 301 
 302     @TADescription(annotation = "RTAs", type = CAST,
 303             offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 0)
 304     @TADescription(annotation = "RTBs", type = CAST,
 305             offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 1)
 306     @TADescription(annotation = "RTCs", type = CAST,
 307             offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 1,
 308             genericLocation = {3, 0})
 309     public String intersectionRepeatableAnnotation1() {
 310         return "void intersection() { Object o = (@RTA @RTA String & @RTB @RTB Comparable<@RTC @RTC String>) null; }";
 311     }
 312 
 313     @TADescription(annotation = "RTAs", type = CAST,
 314             offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 0)
 315     @TADescription(annotation = "RTBs", type = CAST,
 316             offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 1)
 317     @TADescription(annotation = "RTCs", type = CAST,
 318             offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 1,
 319             genericLocation = {3, 0})
 320     @TADescription(annotation = "RTDs", type = CAST,
 321             offset = ReferenceInfoUtil.IGNORE_VALUE, typeIndex = 2)
 322     public String intersectionRepeatableAnnotation2() {
 323         return "void intersection() { Object o = (@RTA @RTA String & @RTB @RTB Comparable<@RTC @RTC String> &" +
 324                 " @RTD @RTD CharSequence) null; }";
 325     }
 326 }