undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
86
test/jdk/java/lang/StackTraceElement/ModuleFrames.java
Normal file
86
test/jdk/java/lang/StackTraceElement/ModuleFrames.java
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Stack trace should have module information
|
||||
* @run testng ModuleFrames
|
||||
*/
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class ModuleFrames {
|
||||
|
||||
@Test
|
||||
public void testModuleName() {
|
||||
try {
|
||||
Integer.parseInt("a");
|
||||
} catch (NumberFormatException e) {
|
||||
|
||||
StackTraceElement[] stack = e.getStackTrace();
|
||||
StackTraceElement topFrame = stack[0];
|
||||
StackTraceElement thisFrame = findFrame(this.getClass().getName(), stack);
|
||||
|
||||
assertEquals(topFrame.getModuleName(), "java.base",
|
||||
"Expected top frame to be in module java.base");
|
||||
|
||||
assertTrue(topFrame.toString().startsWith("java.base"),
|
||||
"Expected toString of top frame to omit loader name, start with module name: java.base");
|
||||
|
||||
assertTrue(!topFrame.toString().contains("@"),
|
||||
"Expected toString of top frame not to include module version ('@')");
|
||||
|
||||
Path home = Paths.get(System.getProperty("java.home"));
|
||||
boolean isImage = Files.exists(home.resolve("lib").resolve("modules"));
|
||||
if (isImage) {
|
||||
assertTrue(!topFrame.getModuleVersion().isEmpty(),
|
||||
"Expected non-empty STE.getModuleVersion() for top frame");
|
||||
}
|
||||
|
||||
assertNull(thisFrame.getModuleName(),
|
||||
"Expected frame for test not to have a module name");
|
||||
|
||||
assertTrue(thisFrame.toString().startsWith(this.getClass().getName()),
|
||||
"Expected toString to start with class name (no loader or module name");
|
||||
|
||||
ClassLoader testCL = this.getClass().getClassLoader();
|
||||
if (ClassLoader.getSystemClassLoader().getClass().isInstance(testCL)) {
|
||||
assertEquals(thisFrame.getClassLoaderName(), "app",
|
||||
"Expect STE to have loader name of \"app\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static StackTraceElement findFrame(String cn, StackTraceElement[] stack) {
|
||||
return Arrays.stream(stack)
|
||||
.filter(s -> s.getClassName().equals(cn))
|
||||
.findFirst()
|
||||
.get();
|
||||
}
|
||||
}
|
||||
95
test/jdk/java/lang/StackTraceElement/PublicConstructor.java
Normal file
95
test/jdk/java/lang/StackTraceElement/PublicConstructor.java
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4712607 6479237
|
||||
* @summary Basic test for StackTraceElementPublic constructor
|
||||
* @author Josh Bloch
|
||||
*/
|
||||
|
||||
import java.lang.module.ModuleDescriptor;
|
||||
|
||||
public class PublicConstructor {
|
||||
public static void main(String... args) {
|
||||
testConstructor();
|
||||
testConstructorWithModule();
|
||||
}
|
||||
|
||||
static void testConstructor() {
|
||||
StackTraceElement ste = new StackTraceElement("com.acme.Widget",
|
||||
"frobnicate",
|
||||
"Widget.java", 42);
|
||||
if (!(ste.getClassName().equals("com.acme.Widget") &&
|
||||
ste.getFileName().equals("Widget.java") &&
|
||||
ste.getMethodName().equals("frobnicate") &&
|
||||
ste.getLineNumber() == 42))
|
||||
throw new RuntimeException("1");
|
||||
|
||||
if (ste.isNativeMethod())
|
||||
throw new RuntimeException("2");
|
||||
|
||||
assertEquals(ste.toString(),
|
||||
"com.acme.Widget.frobnicate(Widget.java:42)");
|
||||
|
||||
StackTraceElement ste1 = new StackTraceElement("com.acme.Widget",
|
||||
"frobnicate",
|
||||
"Widget.java",
|
||||
-2);
|
||||
if (!ste1.isNativeMethod())
|
||||
throw new RuntimeException("3");
|
||||
|
||||
assertEquals(ste1.toString(),
|
||||
"com.acme.Widget.frobnicate(Native Method)");
|
||||
}
|
||||
|
||||
static void testConstructorWithModule() {
|
||||
StackTraceElement ste = new StackTraceElement("app",
|
||||
"jdk.module",
|
||||
"9.0",
|
||||
"com.acme.Widget",
|
||||
"frobnicate",
|
||||
"Widget.java",
|
||||
42);
|
||||
if (!(ste.getClassName().equals("com.acme.Widget") &&
|
||||
ste.getModuleName().equals("jdk.module") &&
|
||||
ste.getModuleVersion().equals("9.0") &&
|
||||
ste.getClassLoaderName().equals("app") &&
|
||||
ste.getFileName().equals("Widget.java") &&
|
||||
ste.getMethodName().equals("frobnicate") &&
|
||||
ste.getLineNumber() == 42))
|
||||
throw new RuntimeException("3");
|
||||
|
||||
if (ste.isNativeMethod())
|
||||
throw new RuntimeException("4");
|
||||
|
||||
assertEquals(ste.toString(),
|
||||
"app/jdk.module@9.0/com.acme.Widget.frobnicate(Widget.java:42)");
|
||||
}
|
||||
|
||||
static void assertEquals(String s, String expected) {
|
||||
if (!s.equals(expected)) {
|
||||
throw new RuntimeException("Expected: " + expected + " but found: " + s);
|
||||
}
|
||||
}
|
||||
}
|
||||
249
test/jdk/java/lang/StackTraceElement/SerialTest.java
Normal file
249
test/jdk/java/lang/StackTraceElement/SerialTest.java
Normal file
|
|
@ -0,0 +1,249 @@
|
|||
/*
|
||||
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6479237 8291641
|
||||
* @summary Test the format of StackTraceElement::toString and its serial form
|
||||
* @modules java.logging
|
||||
*
|
||||
* @run main SerialTest
|
||||
*/
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class SerialTest {
|
||||
private static final Path SER_DIR = Paths.get("sers");
|
||||
private static final String JAVA_BASE = "java.base";
|
||||
private static final String JAVA_LOGGING = "java.logging";
|
||||
|
||||
private static boolean isImage;
|
||||
|
||||
public static void main(String... args) throws Exception {
|
||||
Files.createDirectories(SER_DIR);
|
||||
|
||||
// detect if exploded image build
|
||||
Path home = Paths.get(System.getProperty("java.home"));
|
||||
isImage = Files.exists(home.resolve("lib").resolve("modules"));
|
||||
|
||||
// test stack trace from built-in loaders
|
||||
try {
|
||||
Logger.getLogger(null);
|
||||
} catch (NullPointerException e) {
|
||||
Arrays.stream(e.getStackTrace())
|
||||
.filter(ste -> ste.getClassName().startsWith("java.util.logging.") ||
|
||||
ste.getClassName().equals("SerialTest"))
|
||||
.forEach(SerialTest::test);
|
||||
}
|
||||
|
||||
// test stack trace with class loader name from other class loader
|
||||
Loader loader = new Loader("myloader");
|
||||
Class<?> cls = Class.forName("SerialTest", true, loader);
|
||||
Method method = cls.getMethod("throwException");
|
||||
StackTraceElement ste = (StackTraceElement)method.invoke(null);
|
||||
test(ste, loader);
|
||||
|
||||
// verify the class loader name and in the stack trace
|
||||
if (!cls.getClassLoader().getName().equals("myloader.hacked")) {
|
||||
throw new RuntimeException("Unexpected loader name: " +
|
||||
cls.getClassLoader().getName());
|
||||
}
|
||||
if (!ste.getClassLoaderName().equals("myloader")) {
|
||||
throw new RuntimeException("Unexpected loader name: " +
|
||||
ste.getClassLoaderName());
|
||||
}
|
||||
}
|
||||
|
||||
private static void test(StackTraceElement ste) {
|
||||
test(ste, null);
|
||||
}
|
||||
|
||||
private static void test(StackTraceElement ste, ClassLoader loader) {
|
||||
try {
|
||||
SerialTest serialTest = new SerialTest(ste);
|
||||
StackTraceElement ste2 = serialTest.serialize().deserialize();
|
||||
System.out.println(ste2);
|
||||
// verify StackTraceElement::toString returns the same string
|
||||
if (!ste.equals(ste2) || !ste.toString().equals(ste2.toString())) {
|
||||
throw new RuntimeException(ste + " != " + ste2);
|
||||
}
|
||||
|
||||
String mn = ste.getModuleName();
|
||||
if (mn != null) {
|
||||
switch (mn) {
|
||||
case JAVA_BASE:
|
||||
case JAVA_LOGGING:
|
||||
checkNamedModule(ste, loader, false);
|
||||
break;
|
||||
default: // ignore
|
||||
}
|
||||
} else {
|
||||
checkUnnamedModule(ste, loader);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkUnnamedModule(StackTraceElement ste, ClassLoader loader) {
|
||||
String mn = ste.getModuleName();
|
||||
String s = ste.toString();
|
||||
int i = s.indexOf('/');
|
||||
|
||||
if (mn != null) {
|
||||
throw new RuntimeException("expected null but got " + mn);
|
||||
}
|
||||
|
||||
if (loader != null) {
|
||||
// Expect <loader>//<classname>.<method>(<src>:<ln>)
|
||||
if (i <= 0) {
|
||||
throw new RuntimeException("loader name missing: " + s);
|
||||
}
|
||||
if (!getLoaderName(loader).equals(s.substring(0, i))) {
|
||||
throw new RuntimeException("unexpected loader name: " + s);
|
||||
}
|
||||
int j = s.substring(i+1).indexOf('/');
|
||||
if (j != 0) {
|
||||
throw new RuntimeException("unexpected element for unnamed module: " + s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Loader::getName is overridden to return some other name
|
||||
*/
|
||||
private static String getLoaderName(ClassLoader loader) {
|
||||
if (loader == null)
|
||||
return "";
|
||||
|
||||
if (loader instanceof Loader) {
|
||||
return ((Loader) loader).name;
|
||||
} else {
|
||||
return loader.getName();
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkNamedModule(StackTraceElement ste,
|
||||
ClassLoader loader,
|
||||
boolean showVersion) {
|
||||
String loaderName = getLoaderName(loader);
|
||||
String mn = ste.getModuleName();
|
||||
String s = ste.toString();
|
||||
int i = s.indexOf('/');
|
||||
|
||||
if (mn == null) {
|
||||
throw new RuntimeException("expected module name: " + s);
|
||||
}
|
||||
|
||||
if (i <= 0) {
|
||||
throw new RuntimeException("module name missing: " + s);
|
||||
}
|
||||
|
||||
// Expect <module>/<classname>.<method>(<src>:<ln>)
|
||||
if (!loaderName.isEmpty()) {
|
||||
throw new IllegalArgumentException(loaderName);
|
||||
}
|
||||
|
||||
// <module>: name@version
|
||||
int j = s.indexOf('@');
|
||||
if ((showVersion && j <= 0) || (!showVersion && j >= 0)) {
|
||||
throw new RuntimeException("unexpected version: " + s);
|
||||
}
|
||||
|
||||
String name = j < 0 ? s.substring(0, i) : s.substring(0, j);
|
||||
if (!name.equals(mn)) {
|
||||
throw new RuntimeException("unexpected module name: " + s);
|
||||
}
|
||||
}
|
||||
|
||||
private final Path ser;
|
||||
private final StackTraceElement ste;
|
||||
SerialTest(StackTraceElement ste) throws IOException {
|
||||
this.ser = Files.createTempFile(SER_DIR, "SerialTest", ".ser");
|
||||
this.ste = ste;
|
||||
}
|
||||
|
||||
private StackTraceElement deserialize() throws IOException {
|
||||
try (InputStream in = Files.newInputStream(ser);
|
||||
BufferedInputStream bis = new BufferedInputStream(in);
|
||||
ObjectInputStream ois = new ObjectInputStream(bis)) {
|
||||
return (StackTraceElement)ois.readObject();
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private SerialTest serialize() throws IOException {
|
||||
try (OutputStream out = Files.newOutputStream(ser);
|
||||
BufferedOutputStream bos = new BufferedOutputStream(out);
|
||||
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
|
||||
oos.writeObject(ste);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public static StackTraceElement throwException() {
|
||||
try {
|
||||
Integer.parseInt(null);
|
||||
} catch (NumberFormatException e) {
|
||||
return Arrays.stream(e.getStackTrace())
|
||||
.filter(ste -> ste.getMethodName().equals("throwException"))
|
||||
.findFirst().get();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static class Loader extends URLClassLoader {
|
||||
final String name;
|
||||
Loader(String name) throws MalformedURLException {
|
||||
super(name, new URL[] { testClassesURL() } , null);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private static URL testClassesURL() throws MalformedURLException {
|
||||
Path path = Paths.get(System.getProperty("test.classes"));
|
||||
return path.toUri().toURL();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name + ".hacked";
|
||||
}
|
||||
}
|
||||
}
|
||||
125
test/jdk/java/lang/StackTraceElement/WithClassLoaderName.java
Normal file
125
test/jdk/java/lang/StackTraceElement/WithClassLoaderName.java
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
/*
|
||||
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6479237
|
||||
* @summary Basic test StackTraceElement with class loader names
|
||||
* @library lib /lib/testlibrary /test/lib
|
||||
* @modules jdk.compiler
|
||||
* @build jdk.test.lib.compiler.CompilerUtils
|
||||
* m1/* WithClassLoaderName
|
||||
* @run main/othervm m1/com.app.Main
|
||||
* @run main/othervm WithClassLoaderName
|
||||
*/
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import jdk.test.lib.compiler.CompilerUtils;
|
||||
|
||||
import com.app.Utils;
|
||||
|
||||
public class WithClassLoaderName {
|
||||
private static final String TEST_SRC = System.getProperty("test.src");
|
||||
private static final String SRC_FILENAME = "WithClassLoaderName.java";
|
||||
|
||||
private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
|
||||
private static final Path CLASSES_DIR = Paths.get("classes");
|
||||
private static final String THROW_EXCEPTION_CLASS = "p.ThrowException";
|
||||
|
||||
public static void main(String... args) throws Exception {
|
||||
/*
|
||||
* Test the following frames both have the same class loader name "app"
|
||||
* com.app.Test::test
|
||||
* WithClassLoaderName::test
|
||||
*/
|
||||
Utils.verify(WithClassLoaderName.class, "app", "main", SRC_FILENAME);
|
||||
|
||||
/*
|
||||
* Test StackTraceElement for a class loaded by a named URLClassLoader
|
||||
*/
|
||||
compile();
|
||||
testURLClassLoader("myloader");
|
||||
|
||||
// loader name same as application class loader
|
||||
testURLClassLoader("app");
|
||||
}
|
||||
|
||||
private static void compile() throws Exception {
|
||||
boolean rc = CompilerUtils.compile(SRC_DIR, CLASSES_DIR);
|
||||
if (!rc) {
|
||||
throw new RuntimeException("compilation fails");
|
||||
}
|
||||
}
|
||||
|
||||
public static void testURLClassLoader(String loaderName) throws Exception {
|
||||
System.err.println("---- test URLClassLoader name: " + loaderName);
|
||||
|
||||
URL[] urls = new URL[] { CLASSES_DIR.toUri().toURL() };
|
||||
ClassLoader parent = ClassLoader.getSystemClassLoader();
|
||||
URLClassLoader loader = new URLClassLoader(loaderName, urls, parent);
|
||||
|
||||
Class<?> c = Class.forName(THROW_EXCEPTION_CLASS, true, loader);
|
||||
Method method = c.getMethod("throwError");
|
||||
try {
|
||||
// invoke p.ThrowException::throwError
|
||||
method.invoke(null);
|
||||
} catch (InvocationTargetException x) {
|
||||
Throwable e = x.getCause();
|
||||
e.printStackTrace();
|
||||
|
||||
StackTraceElement[] stes = e.getStackTrace();
|
||||
StackWalker.StackFrame[] frames = new StackWalker.StackFrame[] {
|
||||
Utils.makeStackFrame(c, "throwError", "ThrowException.java"),
|
||||
Utils.makeStackFrame(WithClassLoaderName.class, "testURLClassLoader",
|
||||
SRC_FILENAME),
|
||||
Utils.makeStackFrame(WithClassLoaderName.class, "main", SRC_FILENAME),
|
||||
};
|
||||
|
||||
// p.ThrowException.throwError
|
||||
Utils.checkFrame(loaderName, frames[0], stes[0]);
|
||||
// skip reflection frames
|
||||
int i = 1;
|
||||
while (i < stes.length) {
|
||||
String cn = stes[i].getClassName();
|
||||
if (!cn.startsWith("java.lang.reflect.") &&
|
||||
!cn.startsWith("jdk.internal.reflect."))
|
||||
break;
|
||||
i++;
|
||||
}
|
||||
// WithClassLoaderName.testURLClassLoader
|
||||
Utils.checkFrame("app", frames[1], stes[i]);
|
||||
|
||||
// WithClassLoaderName.main
|
||||
Utils.checkFrame("app", frames[2], stes[i+1]);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.app;
|
||||
|
||||
import java.lang.StackWalker.StackFrame;
|
||||
|
||||
public class Main {
|
||||
public static void main(String... args) throws Exception {
|
||||
StackFrame frame = Utils.makeStackFrame(Main.class, "main", "Main.java");
|
||||
Utils.checkFrame("app", frame, caller());
|
||||
}
|
||||
|
||||
private static StackTraceElement caller() {
|
||||
StackTraceElement[] stes = Thread.currentThread().getStackTrace();
|
||||
return stes[2];
|
||||
}
|
||||
}
|
||||
135
test/jdk/java/lang/StackTraceElement/lib/m1/com/app/Utils.java
Normal file
135
test/jdk/java/lang/StackTraceElement/lib/m1/com/app/Utils.java
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
/*
|
||||
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.app;
|
||||
|
||||
import java.lang.StackWalker.StackFrame;
|
||||
import java.lang.module.ModuleDescriptor;
|
||||
import java.util.Objects;
|
||||
|
||||
public class Utils {
|
||||
public static void verify(Class<?> caller, String loaderName,
|
||||
String methodname, String filename) {
|
||||
StackTraceElement[] stes = Thread.currentThread().getStackTrace();
|
||||
StackWalker.StackFrame[] frames = new StackFrame[] {
|
||||
makeStackFrame(Utils.class, "verify", "Utils.java"),
|
||||
makeStackFrame(caller, methodname, filename)
|
||||
};
|
||||
|
||||
checkFrame("app", frames[0], stes[1]);
|
||||
checkFrame(loaderName, frames[1], stes[2]);
|
||||
}
|
||||
|
||||
public static StackFrame makeStackFrame(Class<?> c, String methodname, String filename) {
|
||||
return new StackFrame() {
|
||||
@Override
|
||||
public String getClassName() {
|
||||
return c.getName();
|
||||
}
|
||||
@Override
|
||||
public String getMethodName() {
|
||||
return methodname;
|
||||
}
|
||||
@Override
|
||||
public Class<?> getDeclaringClass() {
|
||||
return c;
|
||||
}
|
||||
@Override
|
||||
public int getByteCodeIndex() {
|
||||
return 0;
|
||||
}
|
||||
@Override
|
||||
public String getFileName() {
|
||||
return filename;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLineNumber() {
|
||||
return 0;
|
||||
}
|
||||
@Override
|
||||
public boolean isNativeMethod() {
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public StackTraceElement toStackTraceElement() {
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getClassLoaderName(Class<?> c) {
|
||||
ClassLoader loader = c.getClassLoader();
|
||||
String name = "";
|
||||
if (loader == null) {
|
||||
name = "boot";
|
||||
} else if (loader.getName() != null) {
|
||||
name = loader.getName();
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String mid = getClassLoaderName(c);
|
||||
Module module = c.getModule();
|
||||
if (module.isNamed()) {
|
||||
ModuleDescriptor md = module.getDescriptor();
|
||||
mid = md.name();
|
||||
if (md.version().isPresent())
|
||||
mid += "@" + md.version().get().toString();
|
||||
mid += "/";
|
||||
}
|
||||
String fileName = getFileName();
|
||||
int lineNumber = getLineNumber();
|
||||
String sourceinfo = "Unknown Source";
|
||||
if (isNativeMethod()) {
|
||||
sourceinfo = "Native Method";
|
||||
} else if (fileName != null && lineNumber >= 0) {
|
||||
sourceinfo = fileName + ":" + lineNumber;
|
||||
}
|
||||
return String.format("%s/%s.%s(%s)", mid, getClassName(), getMethodName(),
|
||||
sourceinfo);
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static void checkFrame(String loaderName, StackFrame frame,
|
||||
StackTraceElement ste) {
|
||||
System.err.println("checking " + ste.toString() + " expected: " + frame.toString());
|
||||
Class<?> c = frame.getDeclaringClass();
|
||||
Module module = c.getModule();
|
||||
assertEquals(ste.getModuleName(), module.getName(), "module name");
|
||||
assertEquals(ste.getClassLoaderName(), loaderName, "class loader name");
|
||||
assertEquals(ste.getClassLoaderName(), c.getClassLoader().getName(),
|
||||
"class loader name");
|
||||
assertEquals(ste.getClassName(), c.getName(), "class name");
|
||||
assertEquals(ste.getMethodName(), frame.getMethodName(), "method name");
|
||||
assertEquals(ste.getFileName(), frame.getFileName(), "file name");
|
||||
|
||||
}
|
||||
private static void assertEquals(String actual, String expected, String msg) {
|
||||
if (!Objects.equals(actual, expected))
|
||||
throw new AssertionError("Actual: " + actual + " Excepted: " +
|
||||
expected + " mismatched " + msg);
|
||||
}
|
||||
}
|
||||
26
test/jdk/java/lang/StackTraceElement/lib/m1/module-info.java
Normal file
26
test/jdk/java/lang/StackTraceElement/lib/m1/module-info.java
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
module m1 {
|
||||
exports com.app;
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package p;
|
||||
|
||||
import java.lang.StackWalker.StackFrame;
|
||||
|
||||
public class ThrowException {
|
||||
public static void throwError() {
|
||||
throw new Error("testing");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue