Path: blob/master/test/hotspot/jtreg/runtime/LoaderConstraints/ldrCnstrFldMsg/LdrCnstrFldMsgTest.java
40948 views
/*1* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/*25* @test26* @compile pkg/Grand.java pkg/Parent.java pkg/ClassLoaderForParentFoo.java27* @compile pkg/ClassLoaderForChildGrandFoo.java pkg/Child.jasm28* @run main/othervm LdrCnstrFldMsgTest29*/3031import java.lang.reflect.Method;3233// Check that the LinkageError loader constraint message for fields contains the34// correct information.35//36// The test creates two class loaders. The first class loader loads classes37// Child, Foo, and Grand. The second class loader loads Parent and Foo. Class38// Parent is a sub-class of Grand and class Child is a sub-class of Parent.39// Class Child tries to load Parent._field1. This should fail because type Foo40// for Parent._field1 is a different type than Child's Foo.41//42// Grand (ldr1) has field _field1 of type Foo(ldr1)43// |44// Parent (ldr2) has field _field1 of type Foo(ldr2)45// |46// Child (ldr1)47//48// java.lang.LinkageError: loader constraint violation:49// when resolving field "_field1" of type pkg.Foo,50// the class loader pkg.ClassLoaderForChildGrandFoo @42b2e259 of the current class, pkg.Child,51// and the class loader pkg.ClassLoaderForParentFoo @4b55c90f for the field's defining class, pkg.Parent,52// have different Class objects for type pkg.Foo53// (pkg.Child is in unnamed module of loader pkg.ClassLoaderForChildGrandFoo @42b2e259, parent loader 'app';54// pkg.Parent is in unnamed module of loader pkg.ClassLoaderForParentFoo @4b55c90f, parent loader 'app')55//56public class LdrCnstrFldMsgTest {57public static void main(String... args) throws Exception {58ClassLoader l = new pkg.ClassLoaderForChildGrandFoo("pkg.Foo", "pkg.Child", "pkg.Grand");59l.loadClass("pkg.Foo");6061// Try to call a public method in Grand.62Runnable r = (Runnable) l.loadClass("pkg.Child").newInstance();63try {64r.run();65throw new RuntimeException("Expected LinkageError exception not thrown");66} catch (java.lang.LinkageError e) {67if (!e.getMessage().contains("for the field's defining class, pkg.Parent,") ||68!e.getMessage().contains("have different Class objects for type pkg.Foo")) {69throw new RuntimeException("Wrong LinkageError exception thrown: " + e.toString());70}71}72}73}747576