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