1 /*
   2  * Copyright (c) 2018, 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 /*
  26  * @test
  27  * @compile pkg/Grand.java pkg/Parent.java pkg/ClassLoaderForParentFoo.java
  28  * @compile pkg/ClassLoaderForChildGrandFoo.java pkg/Child.jasm
  29  * @run main/othervm LdrCnstrFldMsgTest
  30  */
  31 
  32 import java.lang.reflect.Method;
  33 
  34 // Check that LinkageError loader constraint message for fields contains the
  35 // correct information.
  36 //
  37 // The test creates two class loaders.  The first class loader loads classes
  38 // Child, Foo, and Grand.  The second class loader loads Parent and Foo.  Class
  39 // Parent is a sub-class of Grand and class Child is a sub-class of Parent.
  40 // Class Child tries to load Parent._field1.  This should fail because type Foo
  41 // for Parent._field1 is a different type than Child's Foo.
  42 //
  43 public class LdrCnstrFldMsgTest {
  44     public static void main(String... args) throws Exception {
  45         ClassLoader l = new pkg.ClassLoaderForChildGrandFoo("pkg.Foo", "pkg.Child", "pkg.Grand");
  46         l.loadClass("pkg.Foo");
  47 
  48         // Try to call a public method in Grand.
  49         Runnable r = (Runnable) l.loadClass("pkg.Child").newInstance();
  50         try {
  51             r.run();
  52             throw new RuntimeException("Expected LinkageError exception not thrown");
  53         } catch (java.lang.LinkageError e) {
  54             if (!e.getMessage().contains("for the field's defining type, pkg.Parent,") ||
  55                 !e.getMessage().contains("have different Class objects for type pkg.Foo")) {
  56                 throw new RuntimeException("Wrong LinkageError exception thrown: " + e.toString());
  57             }
  58         }
  59     }
  60 }