Path: blob/master/test/jdk/java/lang/StackTraceElement/WithClassLoaderName.java
41149 views
/*1* Copyright (c) 2016, 2017, 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*/2223/*24* @test25* @bug 647923726* @summary Basic test StackTraceElement with class loader names27* @library lib /lib/testlibrary /test/lib28* @modules jdk.compiler29* @build jdk.test.lib.compiler.CompilerUtils30* m1/* WithClassLoaderName31* @run main/othervm m1/com.app.Main32* @run main/othervm WithClassLoaderName33*/3435import java.lang.reflect.InvocationTargetException;36import java.lang.reflect.Method;37import java.net.URL;38import java.net.URLClassLoader;39import java.nio.file.Path;40import java.nio.file.Paths;4142import jdk.test.lib.compiler.CompilerUtils;4344import com.app.Utils;4546public class WithClassLoaderName {47private static final String TEST_SRC = System.getProperty("test.src");48private static final String SRC_FILENAME = "WithClassLoaderName.java";4950private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");51private static final Path CLASSES_DIR = Paths.get("classes");52private static final String THROW_EXCEPTION_CLASS = "p.ThrowException";5354public static void main(String... args) throws Exception {55/*56* Test the following frames both have the same class loader name "app"57* com.app.Test::test58* WithClassLoaderName::test59*/60Utils.verify(WithClassLoaderName.class, "app", "main", SRC_FILENAME);6162/*63* Test StackTraceElement for a class loaded by a named URLClassLoader64*/65compile();66testURLClassLoader("myloader");6768// loader name same as application class loader69testURLClassLoader("app");70}7172private static void compile() throws Exception {73boolean rc = CompilerUtils.compile(SRC_DIR, CLASSES_DIR);74if (!rc) {75throw new RuntimeException("compilation fails");76}77}7879public static void testURLClassLoader(String loaderName) throws Exception {80System.err.println("---- test URLClassLoader name: " + loaderName);8182URL[] urls = new URL[] { CLASSES_DIR.toUri().toURL() };83ClassLoader parent = ClassLoader.getSystemClassLoader();84URLClassLoader loader = new URLClassLoader(loaderName, urls, parent);8586Class<?> c = Class.forName(THROW_EXCEPTION_CLASS, true, loader);87Method method = c.getMethod("throwError");88try {89// invoke p.ThrowException::throwError90method.invoke(null);91} catch (InvocationTargetException x) {92Throwable e = x.getCause();93e.printStackTrace();9495StackTraceElement[] stes = e.getStackTrace();96StackWalker.StackFrame[] frames = new StackWalker.StackFrame[] {97Utils.makeStackFrame(c, "throwError", "ThrowException.java"),98Utils.makeStackFrame(WithClassLoaderName.class, "testURLClassLoader",99SRC_FILENAME),100Utils.makeStackFrame(WithClassLoaderName.class, "main", SRC_FILENAME),101};102103// p.ThrowException.throwError104Utils.checkFrame(loaderName, frames[0], stes[0]);105// skip reflection frames106int i = 1;107while (i < stes.length) {108String cn = stes[i].getClassName();109if (!cn.startsWith("java.lang.reflect.") &&110!cn.startsWith("jdk.internal.reflect."))111break;112i++;113}114// WithClassLoaderName.testURLClassLoader115Utils.checkFrame("app", frames[1], stes[i]);116117// WithClassLoaderName.main118Utils.checkFrame("app", frames[2], stes[i+1]);119120}121}122123}124125126127