1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /**
  26  * @test
  27  * @summary Test exception messages of LinkageError.
  28  * @library /test/lib
  29  * @compile D_ambgs.jasm
  30  * @run driver ClassFileInstaller test.D_ambgs
  31  * @compile  ../common/PreemptingClassLoader.java
  32  *           test/D_ambgs.java Test.java test/B.java
  33  * @run driver ClassFileInstaller test.B
  34  * @run main/othervm Test
  35  */
  36 
  37 import test.*;
  38 
  39 import java.io.ByteArrayOutputStream;
  40 import java.io.InputStream;
  41 
  42 import java.io.*;
  43 import java.nio.file.Files;
  44 import java.nio.file.Path;
  45 import java.nio.file.Paths;
  46 
  47 public class Test {
  48 
  49     //  Force LinkageError.
  50     //
  51     //  Derived from test runtime/6626217.
  52     //
  53     //  Uses the specialized class loader PreemptingClassLoader.
  54     //  PreemptingClassLoader only loads files with names passed to it in its
  55     //  constructor. If it does not find it, it delegates to the super class loader.
  56     //
  57     //  A    // interface
  58     //  |
  59     //  B    // Compiled to the current working directory so that it is found by our
  60     //       // special class loader. B uses D, so that loading B triggers loading D.
  61     //
  62     //  C    // An abstract class.
  63     //  |
  64     //  D    // Class with two different implementations D1 and D2. D2 is
  65     //       // compiled to the current working directory so that it is found by our
  66     //       // special class loader.
  67     //
  68     // First, the bootstrap loader will load D1. It already has loaded interface A.
  69     // Then, the second class loader PreemptingClassLoader will load B. Recursive,
  70     // it tries to load interface A. As it does not find it (there is no A.impl2),
  71     // it asks the super classloader for A.
  72     // Then it loads the D2 variant of D from the current working directory and it's
  73     // superclass C. This fails as D1 is already loaded with the same superclass.
  74 
  75     static String expectedErrorMessage =
  76         "loader constraint violation: loader instance of PreemptingClassLoader " +
  77         "(parent: \"app\" jdk.internal.loader.ClassLoaders$AppClassLoader) wants to load " +
  78         "class test.D_ambgs. A different class with the same name was previously loaded " +
  79         "by \"app\" (instance of jdk.internal.loader.ClassLoaders$AppClassLoader, " +
  80         "parent: \"platform\" jdk.internal.loader.ClassLoaders$PlatformClassLoader).";
  81 
  82     public static void test_access() throws Exception {
  83         try {
  84             // Make a Class 'D_ambgs' under the default loader.
  85             // This uses the implementation from the .java file.
  86             C c_1 = new D_ambgs();
  87 
  88             // Some classes under a new Loader, loader2, including, indirectly,
  89             // another version of 'D_ambgs'
  90             String[] classNames = {"test.B", "test.D_ambgs"};
  91 
  92             ClassLoader loader2 = new PreemptingClassLoader(null, classNames, false);
  93             Class       class2  = loader2.loadClass("test.B");
  94             A           iface   = (A)class2.newInstance();
  95 
  96             // Call D1.make() loaded by bootstrap loader with B loaded by Loader2.
  97             D_ambgs[] x2 = c_1.make(iface);
  98 
  99             throw new RuntimeException("Expected LinkageError was not thrown.");
 100         } catch (LinkageError jle) {
 101             String errorMsg = jle.getMessage();
 102             if (!errorMsg.equals(expectedErrorMessage)) {
 103                 System.out.println("Expected: " + expectedErrorMessage + "\n" +
 104                                    "but got:  " + errorMsg);
 105                 throw new RuntimeException("Wrong error message of LinkageError.");
 106             } else {
 107                 System.out.println("Passed with message: " + errorMsg);
 108             }
 109         }
 110     }
 111 
 112     public static void main(String[] args) throws Exception {
 113         test_access();
 114     }
 115 }
 116