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 D_ambgs
  31  * @compile  ../common/PreemptingClassLoader.java
  32  *           D_ambgs.java Test.java B.java
  33  * @run driver ClassFileInstaller B
  34  * @run main/othervm Test
  35  */
  36 
  37 import java.io.ByteArrayOutputStream;
  38 import java.io.InputStream;
  39 
  40 import java.io.*;
  41 import java.nio.file.Files;
  42 import java.nio.file.Path;
  43 import java.nio.file.Paths;
  44 
  45 public class Test {
  46 
  47     //  Force LinkageError.
  48     //
  49     //  Derived from test runtime/6626217.
  50     //
  51     //  Uses the specialized class loader PreemptingClassLoader.
  52     //  PreemptingClassLoader only loads files with names passed to it in its
  53     //  constructor. If it does not find it, it delegates to the super class loader.
  54     //
  55     //  A    // interface
  56     //  |
  57     //  B    // Compiled to the current working directory so that it is found by our
  58     //       // special class loader. B uses D, so that loading B triggers loading D.
  59     //
  60     //  C    // An abstract class.
  61     //  |
  62     //  D    // Class with two different implementations D1 and D2. D2 is
  63     //       // compiled to the current working directory so that it is found by our 
  64     //       // special class loader.
  65     //
  66     // First, the bootstrap loader will load D1. It already has loaded interface A.
  67     // Then, the second class loader PreemptingClassLoader will load B. Recursive,
  68     // it tries to load interface A. As it does not find it (there is no A.impl2),
  69     // it asks the super classloader for A.
  70     // Then it loads the D2 variant of D from the current working directory and it's
  71     // superclass C. This fails as D1 is already loaded with the same superclass.
  72 
  73     static String expectedErrorMessage =
  74         "loader constraint violation: loader instance of PreemptingClassLoader " +
  75         "(parent: \"app\" jdk.internal.loader.ClassLoaders$AppClassLoader) wants to load " +
  76         "class D_ambgs. A different class with the same name was previously loaded " +
  77         "by \"app\" (instance of jdk.internal.loader.ClassLoaders$AppClassLoader, " +
  78         "parent: \"platform\" jdk.internal.loader.ClassLoaders$PlatformClassLoader).";
  79 
  80     public static void test_access() throws Exception {
  81         try {
  82             // Make a Class 'D_ambgs' under the default loader.
  83             // This uses the implementation from the .java file.
  84             C c_1 = new D_ambgs();
  85 
  86             // Some classes under a new Loader, loader2, including, indirectly,
  87             // another version of 'D_ambgs'
  88             String[] classNames = {"B", "D_ambgs"};
  89             
  90             ClassLoader loader2 = new PreemptingClassLoader(null, classNames, false);
  91             Class       class2  = loader2.loadClass("B");
  92             A           iface   = (A)class2.newInstance();
  93 
  94             // Call D1.make() loaded by bootstrap loader with B loaded by Loader2.
  95             D_ambgs[] x2 = c_1.make(iface);
  96 
  97             throw new RuntimeException("Expected LinkageError was not thrown.");
  98         } catch (LinkageError jle) {
  99             String errorMsg = jle.getMessage();
 100             if (!errorMsg.equals(expectedErrorMessage)) {
 101                 System.out.println("Expected: " + expectedErrorMessage + "\n" +
 102                                    "but got:  " + errorMsg);
 103                 throw new RuntimeException("Wrong error message of LinkageError.");
 104             } else {
 105                 System.out.println("Passed with message: " + errorMsg);
 106             }
 107         }
 108     }
 109 
 110     public static void main(String[] args) throws Exception {
 111         test_access();
 112     }
 113 }
 114