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