1 /*
   2  * Copyright (c) 2016, 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 com.sun.tools.classfile.*;
  25 import com.sun.tools.classfile.ConstantPool.*;
  26 
  27 import java.io.File;
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 
  31 /*
  32  * @test
  33  * @bug     8151223
  34  * @summary String concatenation fails with implicit toString() on package-private class
  35  * @modules jdk.jdeps/com.sun.tools.classfile
  36  *
  37  * @clean *
  38  * @compile -XDstringConcat=indy              Holder.java PublicClass.java PublicInterface.java Public_PublicClass.java Public_PublicInterface.java Public_PrivateInterface1.java Public_PrivateInterface2.java Test.java
  39  * @run main Test
  40  *
  41  * @clean *
  42  * @compile -XDstringConcat=indyWithConstants Holder.java PublicClass.java PublicInterface.java Public_PublicClass.java Public_PublicInterface.java Public_PrivateInterface1.java Public_PrivateInterface2.java Test.java
  43  * @run main Test
  44  */
  45 
  46 public class Test {
  47     static List<String> actualTypes;
  48 
  49     public static void main(String[] argv) throws Exception {
  50         readIndyTypes();
  51 
  52         p1.Holder holder = new p1.Holder();
  53 
  54         int idx = 0;
  55 
  56         // ----------------------------------------------------------------------------
  57 
  58         // public Private_PublicClass            c1 = new Private_PublicClass();
  59         test("" + holder.c1, idx++, "(Lp1/PublicClass;)Ljava/lang/String;");
  60 
  61         // public Private_PublicInterface        c2 = new Private_PublicInterface();
  62         test("" + holder.c2, idx++, "(Ljava/lang/Object;)Ljava/lang/String;");
  63 
  64         // public Private_PrivateInterface1      c3 = new Private_PrivateInterface1();
  65         test("" + holder.c3, idx++, "(Ljava/lang/Object;)Ljava/lang/String;");
  66 
  67         // public Private_PrivateInterface2      c4 = new Private_PrivateInterface2();
  68         test("" + holder.c4, idx++, "(Ljava/lang/Object;)Ljava/lang/String;");
  69 
  70         // public Public_PublicClass             c5 = new Public_PublicClass();
  71         test("" + holder.c5, idx++, "(Lp1/Public_PublicClass;)Ljava/lang/String;");
  72 
  73         // public Public_PublicInterface         c6 = new Public_PublicInterface();
  74         test("" + holder.c6, idx++, "(Lp1/Public_PublicInterface;)Ljava/lang/String;");
  75 
  76         // public Public_PrivateInterface1       c7 = new Public_PrivateInterface1();
  77         test("" + holder.c7, idx++, "(Lp1/Public_PrivateInterface1;)Ljava/lang/String;");
  78 
  79         // public Public_PrivateInterface2       c8 = new Public_PrivateInterface2();
  80         test("" + holder.c8, idx++, "(Lp1/Public_PrivateInterface2;)Ljava/lang/String;");
  81 
  82         // ----------------------------------------------------------------------------
  83 
  84         // public Private_PublicClass[]          ac1 = new Private_PublicClass[0];
  85         test("" + holder.ac1, idx++, "([Lp1/PublicClass;)Ljava/lang/String;");
  86 
  87         // public Private_PublicInterface[]      ac2 = new Private_PublicInterface[0];
  88         test("" + holder.ac2, idx++, "([Ljava/lang/Object;)Ljava/lang/String;");
  89 
  90         // public Private_PrivateInterface1[]    ac3 = new Private_PrivateInterface1[0];
  91         test("" + holder.ac3, idx++, "([Ljava/lang/Object;)Ljava/lang/String;");
  92 
  93         // public Private_PrivateInterface2[]    ac4 = new Private_PrivateInterface2[0];
  94         test("" + holder.ac4, idx++, "([Ljava/lang/Object;)Ljava/lang/String;");
  95 
  96         // public Public_PublicClass[]           ac5 = new Public_PublicClass[0];
  97         test("" + holder.ac5, idx++, "([Lp1/Public_PublicClass;)Ljava/lang/String;");
  98 
  99         // public Public_PublicInterface[]       ac6 = new Public_PublicInterface[0];
 100         test("" + holder.ac6, idx++, "([Lp1/Public_PublicInterface;)Ljava/lang/String;");
 101 
 102         // public Public_PrivateInterface1[]     ac7 = new Public_PrivateInterface1[0];
 103         test("" + holder.ac7, idx++, "([Lp1/Public_PrivateInterface1;)Ljava/lang/String;");
 104 
 105         // public Public_PrivateInterface2[]     ac8 = new Public_PrivateInterface2[0];
 106         test("" + holder.ac8, idx++, "([Lp1/Public_PrivateInterface2;)Ljava/lang/String;");
 107 
 108         // ----------------------------------------------------------------------------
 109 
 110         // public Private_PublicClass[][]       aac1 = new Private_PublicClass[0][];
 111         test("" + holder.aac1, idx++, "([[Lp1/PublicClass;)Ljava/lang/String;");
 112 
 113         // public Private_PublicInterface[][]   aac2 = new Private_PublicInterface[0][];
 114         test("" + holder.aac2, idx++, "([[Ljava/lang/Object;)Ljava/lang/String;");
 115 
 116         // public Private_PrivateInterface1[][] aac3 = new Private_PrivateInterface1[0][];
 117         test("" + holder.aac3, idx++, "([[Ljava/lang/Object;)Ljava/lang/String;");
 118 
 119         // public Private_PrivateInterface2[][] aac4 = new Private_PrivateInterface2[0][];
 120         test("" + holder.aac4, idx++, "([[Ljava/lang/Object;)Ljava/lang/String;");
 121 
 122         // public Public_PublicClass[][]        aac5 = new Public_PublicClass[0][];
 123         test("" + holder.aac5, idx++, "([[Lp1/Public_PublicClass;)Ljava/lang/String;");
 124 
 125         // public Public_PublicInterface[][]    aac6 = new Public_PublicInterface[0][];
 126         test("" + holder.aac6, idx++, "([[Lp1/Public_PublicInterface;)Ljava/lang/String;");
 127 
 128         // public Public_PrivateInterface1[][]  aac7 = new Public_PrivateInterface1[0][];
 129         test("" + holder.aac7, idx++, "([[Lp1/Public_PrivateInterface1;)Ljava/lang/String;");
 130 
 131         // public Public_PrivateInterface2[][]  aac8 = new Public_PrivateInterface2[0][];
 132         test("" + holder.aac8, idx++, "([[Lp1/Public_PrivateInterface2;)Ljava/lang/String;");
 133 
 134         // ----------------------------------------------------------------------------
 135 
 136         // public PublicInterface                i1 = new Private_PublicInterface();
 137         test("" + holder.i1, idx++, "(Lp1/PublicInterface;)Ljava/lang/String;");
 138 
 139         // public PrivateInterface1              i2 = new Private_PrivateInterface1();
 140         test("" + holder.i2, idx++, "(Ljava/lang/Object;)Ljava/lang/String;");
 141 
 142         // public PrivateInterface2              i3 = new Private_PrivateInterface2();
 143         test("" + holder.i3, idx++, "(Ljava/lang/Object;)Ljava/lang/String;");
 144 
 145         // public PublicInterface[]              ai1 = new Private_PublicInterface[0];
 146         test("" + holder.ai1, idx++, "([Lp1/PublicInterface;)Ljava/lang/String;");
 147 
 148         // public PrivateInterface1[]            ai2 = new Private_PrivateInterface1[0];
 149         test("" + holder.ai2, idx++, "([Ljava/lang/Object;)Ljava/lang/String;");
 150 
 151         // public PrivateInterface2[]            ai3 = new Private_PrivateInterface2[0];
 152         test("" + holder.ai3, idx++, "([Ljava/lang/Object;)Ljava/lang/String;");
 153 
 154         // public PublicInterface[][]           aai1 = new Private_PublicInterface[0][];
 155         test("" + holder.aai1, idx++, "([[Lp1/PublicInterface;)Ljava/lang/String;");
 156 
 157         // public PrivateInterface1[][]         aai2 = new Private_PrivateInterface1[0][];
 158         test("" + holder.aai2, idx++, "([[Ljava/lang/Object;)Ljava/lang/String;");
 159 
 160         // public PrivateInterface2[][]         aai3 = new Private_PrivateInterface2[0][];
 161         test("" + holder.aai3, idx++, "([[Ljava/lang/Object;)Ljava/lang/String;");
 162 
 163     }
 164 
 165     public static void test(String actual, int index, String expectedType) {
 166         if (!"passed".equals(actual) && !actual.startsWith("[")) {
 167             throw new IllegalStateException("Unexpected result: " + actual);
 168         }
 169         String actualType = actualTypes.get(index);
 170         if (!actualType.equals(expectedType)) {
 171             throw new IllegalStateException("Unexpected type: expected = " + expectedType + ", actual = " + actualType);
 172         }
 173     }
 174 
 175     public static void readIndyTypes() throws Exception {
 176         actualTypes = new ArrayList<String>();
 177 
 178         ClassFile classFile = ClassFile.read(new File(System.getProperty("test.classes", "."),
 179                     Test.class.getName() + ".class"));
 180         ConstantPool constantPool = classFile.constant_pool;
 181 
 182         for (Method method : classFile.methods) {
 183             if (method.getName(constantPool).equals("main")) {
 184                 Code_attribute code = (Code_attribute) method.attributes
 185                         .get(Attribute.Code);
 186                 for (Instruction i : code.getInstructions()) {
 187                     if (i.getOpcode() == Opcode.INVOKEDYNAMIC) {
 188                         CONSTANT_InvokeDynamic_info indyInfo = (CONSTANT_InvokeDynamic_info) constantPool.get(i.getUnsignedShort(1));
 189                         CONSTANT_NameAndType_info natInfo = indyInfo.getNameAndTypeInfo();
 190                         actualTypes.add(natInfo.getType());
 191                     }
 192                 }
 193             }
 194         }
 195     }
 196 }