6479237: (cl) Add support for classloader names

Reviewed-by: alanb, bchristi, coleenp, dfuchs, lfoltan, psandoz, sspitsyn
This commit is contained in:
Mandy Chung 2016-11-03 18:08:28 -07:00
parent c535c8a446
commit d74e2a09ac
26 changed files with 1196 additions and 174 deletions

View File

@ -140,7 +140,6 @@ SUNWprivate_1.1 {
Java_java_lang_Double_doubleToRawLongBits;
Java_java_lang_Float_intBitsToFloat;
Java_java_lang_Float_floatToRawIntBits;
Java_java_lang_StackFrameInfo_toStackTraceElement0;
Java_java_lang_StackStreamFactory_checkStackWalkModes;
Java_java_lang_StackStreamFactory_00024AbstractStackWalker_callStackWalk;
Java_java_lang_StackStreamFactory_00024AbstractStackWalker_fetchStackFrames;
@ -215,6 +214,8 @@ SUNWprivate_1.1 {
Java_java_lang_SecurityManager_currentLoadedClass0;
Java_java_lang_SecurityManager_getClassContext;
Java_java_lang_Shutdown_halt0;
Java_java_lang_StackTraceElement_initStackTraceElement;
Java_java_lang_StackTraceElement_initStackTraceElements;
Java_java_lang_String_intern;
Java_java_lang_StringCoding_err;
Java_java_lang_StringUTF16_isBigEndian;
@ -227,7 +228,6 @@ SUNWprivate_1.1 {
Java_java_lang_System_setOut0;
Java_java_lang_Thread_registerNatives;
Java_java_lang_Throwable_fillInStackTrace;
Java_java_lang_Throwable_getStackTraceElements;
Java_java_security_AccessController_doPrivileged__Ljava_security_PrivilegedAction_2;
Java_java_security_AccessController_doPrivileged__Ljava_security_PrivilegedAction_2Ljava_security_AccessControlContext_2;
Java_java_security_AccessController_doPrivileged__Ljava_security_PrivilegedExceptionAction_2;

View File

@ -78,7 +78,7 @@ text: .text%Java_java_security_AccessController_doPrivileged__Ljava_security_Pri
text: .text%JNU_GetEnv;
text: .text%Java_java_io_UnixFileSystem_checkAccess;
text: .text%Java_java_lang_reflect_Array_newArray;
text: .text%Java_java_lang_Throwable_getStackTraceElements;
text: .text%Java_java_lang_StackTraceElement_initStackTraceElements;
text: .text%throwFileNotFoundException;
text: .text%JNU_NotifyAll;
# Test LoadFrame

View File

@ -74,7 +74,7 @@ text: .text%Java_java_security_AccessController_doPrivileged__Ljava_security_Pri
text: .text%JNU_GetEnv;
text: .text%Java_java_io_UnixFileSystem_checkAccess;
text: .text%Java_java_lang_reflect_Array_newArray;
text: .text%Java_java_lang_Throwable_getStackTraceElements;
text: .text%Java_java_lang_StackTraceElement_initStackTraceElements;
text: .text%throwFileNotFoundException: OUTPUTDIR/io_util.o;
text: .text%JNU_NotifyAll;
# Test LoadFrame

View File

@ -78,7 +78,7 @@ text: .text%Java_java_io_UnixFileSystem_checkAccess;
text: .text%Java_sun_reflect_NativeMethodAccessorImpl_invoke0;
text: .text%Java_java_io_FileInputStream_available;
text: .text%Java_java_lang_reflect_Array_newArray;
text: .text%Java_java_lang_Throwable_getStackTraceElements;
text: .text%Java_java_lang_StackTraceElement_initStackTraceElements;
text: .text%Java_java_lang_System_identityHashCode;
text: .text%JNU_NotifyAll;
# Test LoadFrame

View File

@ -222,6 +222,9 @@ public abstract class ClassLoader {
// must be added *after* it.
private final ClassLoader parent;
// class loader name
private final String name;
// the unnamed module for this ClassLoader
private final Module unnamedModule;
@ -331,6 +334,14 @@ public abstract class ClassLoader {
}
private static Void checkCreateClassLoader() {
return checkCreateClassLoader(null);
}
private static Void checkCreateClassLoader(String name) {
if (name != null && name.isEmpty()) {
throw new IllegalArgumentException("name must be non-empty or null");
}
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkCreateClassLoader();
@ -338,7 +349,8 @@ public abstract class ClassLoader {
return null;
}
private ClassLoader(Void unused, ClassLoader parent) {
private ClassLoader(Void unused, String name, ClassLoader parent) {
this.name = name;
this.parent = parent;
this.unnamedModule
= SharedSecrets.getJavaLangReflectModuleAccess()
@ -355,6 +367,27 @@ public abstract class ClassLoader {
}
}
/**
* Creates a new class loader of the specified name and using the
* specified parent class loader for delegation.
*
* @param name class loader name; or {@code null} if not named
* @param parent the parent class loader
*
* @throws IllegalArgumentException if the given name is empty.
*
* @throws SecurityException
* If a security manager exists and its
* {@link SecurityManager#checkCreateClassLoader()}
* method doesn't allow creation of a new class loader.
*
* @since 9
*/
protected ClassLoader(String name, ClassLoader parent) {
this(checkCreateClassLoader(name), name, parent);
}
/**
* Creates a new class loader using the specified parent class loader for
* delegation.
@ -375,9 +408,10 @@ public abstract class ClassLoader {
* @since 1.2
*/
protected ClassLoader(ClassLoader parent) {
this(checkCreateClassLoader(), parent);
this(checkCreateClassLoader(), null, parent);
}
/**
* Creates a new class loader using the <tt>ClassLoader</tt> returned by
* the method {@link #getSystemClassLoader()
@ -394,7 +428,31 @@ public abstract class ClassLoader {
* of a new class loader.
*/
protected ClassLoader() {
this(checkCreateClassLoader(), getSystemClassLoader());
this(checkCreateClassLoader(), null, getSystemClassLoader());
}
/**
* Returns the name of this class loader or {@code null} if
* this class loader is not named.
*
* @apiNote This method is non-final for compatibility. If this
* method is overridden, this method must return the same name
* as specified when this class loader was instantiated.
*
* @return name of this class loader; or {@code null} if
* this class loader is not named.
*
* @since 9
*/
public String getName() {
return name;
}
// package-private used by StackTraceElement to avoid
// calling the overrideable getName method
final String name() {
return name;
}
// -- Class --
@ -1628,6 +1686,9 @@ public abstract class ClassLoader {
* <a href="#builtinLoaders">platform classes</a> are visible to
* the platform class loader.
*
* @implNote The name of the builtin platform class loader is
* {@code "platform"}.
*
* @return The platform {@code ClassLoader}.
*
* @throws SecurityException
@ -1681,7 +1742,8 @@ public abstract class ClassLoader {
* this method during startup should take care not to cache the return
* value until the system is fully initialized.
*
* <p> The class path used by the built-in system class loader is determined
* <p> The name of the built-in system class loader is {@code "app"}.
* The class path used by the built-in system class loader is determined
* by the system property "{@code java.class.path}" during early
* initialization of the VM. If the system property is not defined,
* or its value is an empty string, then there is no class path

View File

@ -112,11 +112,6 @@ class StackFrameInfo implements StackFrame {
return toStackTraceElement().toString();
}
/**
* Fill in the fields of the given StackTraceElement
*/
private native void toStackTraceElement0(StackTraceElement ste);
@Override
public StackTraceElement toStackTraceElement() {
StackTraceElement s = ste;
@ -124,9 +119,7 @@ class StackFrameInfo implements StackFrame {
synchronized (this) {
s = ste;
if (s == null) {
s = new StackTraceElement();
toStackTraceElement0(s);
ste = s;
ste = s = StackTraceElement.of(this);
}
}
}

View File

@ -25,7 +25,18 @@
package java.lang;
import jdk.internal.loader.BuiltinClassLoader;
import jdk.internal.misc.SharedSecrets;
import jdk.internal.misc.VM;
import jdk.internal.module.ModuleHashes;
import java.lang.module.ModuleDescriptor.Version;
import java.lang.reflect.Layer;
import java.lang.reflect.Module;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
/**
* An element in a stack trace, as returned by {@link
@ -40,7 +51,15 @@ import java.util.Objects;
* @author Josh Bloch
*/
public final class StackTraceElement implements java.io.Serializable {
// Normally initialized by VM (public constructor added in 1.5)
// This field is set to the compacted String representation used
// by StackTraceElement::toString and stored in serial form.
//
// This field is of Object type. VM initially sets this field to
// the Class object of the declaring class to build the compacted string.
private Object classOrLoaderModuleClassName;
// Normally initialized by VM
private String classLoaderName;
private String moduleName;
private String moduleVersion;
private String declaringClass;
@ -72,19 +91,22 @@ public final class StackTraceElement implements java.io.Serializable {
*/
public StackTraceElement(String declaringClass, String methodName,
String fileName, int lineNumber) {
this(null, null, declaringClass, methodName, fileName, lineNumber);
this(null, null, null, declaringClass, methodName, fileName, lineNumber);
}
/**
* Creates a stack trace element representing the specified execution
* point.
*
* @param classLoaderName the class loader name if the class loader of
* the class containing the execution point represented by
* the stack trace is named; otherwise {@code null}
* @param moduleName the module name if the class containing the
* execution point represented by the stack trace is in a named
* module; can be {@code null}
* module; otherwise {@code null}
* @param moduleVersion the module version if the class containing the
* execution point represented by the stack trace is in a named
* module that has a version; can be {@code null}
* module that has a version; otherwise {@code null}
* @param declaringClass the fully qualified name of the class containing
* the execution point represented by the stack trace element
* @param methodName the name of the method containing the execution point
@ -97,26 +119,30 @@ public final class StackTraceElement implements java.io.Serializable {
* a negative number if this information is unavailable. A value
* of -2 indicates that the method containing the execution point
* is a native method
*
* @throws NullPointerException if {@code declaringClass} is {@code null}
* or {@code methodName} is {@code null}
*
* @since 9
*/
public StackTraceElement(String moduleName, String moduleVersion,
public StackTraceElement(String classLoaderName,
String moduleName, String moduleVersion,
String declaringClass, String methodName,
String fileName, int lineNumber) {
this.moduleName = moduleName;
this.moduleVersion = moduleVersion;
this.declaringClass = Objects.requireNonNull(declaringClass, "Declaring class is null");
this.methodName = Objects.requireNonNull(methodName, "Method name is null");
this.fileName = fileName;
this.lineNumber = lineNumber;
this.classLoaderName = classLoaderName;
this.moduleName = moduleName;
this.moduleVersion = moduleVersion;
this.declaringClass = Objects.requireNonNull(declaringClass, "Declaring class is null");
this.methodName = Objects.requireNonNull(methodName, "Method name is null");
this.fileName = fileName;
this.lineNumber = lineNumber;
}
/**
* Creates an empty stack frame element to be filled in by Throwable.
/*
* Private constructor for the factory methods to create StackTraceElement
* for Throwable and StackFrameInfo
*/
StackTraceElement() { }
private StackTraceElement() {}
/**
* Returns the name of the source file containing the execution point
@ -177,6 +203,21 @@ public final class StackTraceElement implements java.io.Serializable {
return moduleVersion;
}
/**
* Returns the name of the class loader of the class containing the
* execution point represented by this stack trace element.
*
* @return the name of the class loader of the class containing the execution
* point represented by this stack trace element; {@code null}
* if the class loader is not named.
*
* @since 9
* @see java.lang.ClassLoader#getName()
*/
public String getClassLoaderName() {
return classLoaderName;
}
/**
* Returns the fully qualified name of the class containing the
* execution point represented by this stack trace element.
@ -220,38 +261,83 @@ public final class StackTraceElement implements java.io.Serializable {
* examples may be regarded as typical:
* <ul>
* <li>
* {@code "MyClass.mash(my.module@9.0/MyClass.java:101)"} - Here,
* {@code "MyClass"} is the <i>fully-qualified name</i> of the class
* containing the execution point represented by this stack trace element,
* {@code "mash"} is the name of the method containing the execution
* point, {@code "my.module"} is the module name, {@code "9.0"} is the
* module version, and {@code "101"} is the line number of the source
* line containing the execution point.
* "{@code com.foo.loader/foo@9.0/com.foo.Main.run(Main.java:101)}"
* - See the description below.
* </li>
* <li>
* {@code "MyClass.mash(my.module@9.0/MyClass.java)"} - As above, but the
* line number is unavailable.
* "{@code com.foo.loader/foo@9.0/com.foo.Main.run(Main.java)}"
* - The line number is unavailable.
* </li>
* <li>
* {@code "MyClass.mash(my.module@9.0/Unknown Source)"} - As above, but
* neither the file name nor the line number are available.
* "{@code com.foo.loader/foo@9.0/com.foo.Main.run(Unknown Source)}"
* - Neither the file name nor the line number is available.
* </li>
* <li>
* {@code "MyClass.mash(my.module@9.0/Native Method)"} - As above, but
* neither the file name nor the line number are available, and the
* method containing the execution point is known to be a native method.
* "{@code com.foo.loader/foo@9.0/com.foo.Main.run(Native Method)}"
* - The method containing the execution point is a native method.
* </li>
* <li>
* "{@code com.foo.loader//com.foo.bar.App.run(App.java:12)}"
* - The class of the execution point is defined in the unnamed module of
* the class loader named {@code com.foo.loader}.
* </li>
* <li>
* "{@code acme@2.1/org.acme.Lib.test(Lib.java:80)}"
* - The class of the execution point is defined in {@code acme} module
* loaded by by a built-in class loader such as the application class loader.
* </li>
* <li>
* "{@code MyClass.mash(MyClass.java:9)}"
* - {@code MyClass} class is on the application class path.
* </li>
* </ul>
* If the execution point is not in a named module, {@code "my.module@9.0/"}
* will be omitted from the above.
*
* <p> The first example shows a stack trace element consisting of
* three elements, each separated by {@code "/"} followed with
* the source file name and the line number of the source line
* containing the execution point.
*
* The first element "{@code com.foo.loader}" is
* the name of the class loader. The second element "{@code foo@9.0}"
* is the module name and version. The third element is the method
* containing the execution point; "{@code com.foo.Main"}" is the
* fully-qualified class name and "{@code run}" is the name of the method.
* "{@code Main.java}" is the source file name and "{@code 101}" is
* the line number.
*
* <p> If a class is defined in an <em>unnamed module</em>
* then the second element is omitted as shown in
* "{@code com.foo.loader//com.foo.bar.App.run(App.java:12)}".
*
* If the class loader is a <a href="ClassLoader.html#builtinLoaders">
* built-in class loader</a> or is not named then the first element
* and its following {@code "/"} are omitted as shown in
* "{@code acme@2.1/org.acme.Lib.test(Lib.java:80)}".
* If the first element is omitted and the module is an unnamed module,
* the second element and its following {@code "/"} are also omitted
* as shown in "{@code MyClass.mash(MyClass.java:9)}".
*
* @see Throwable#printStackTrace()
*/
public String toString() {
String mid = "";
if (moduleName != null) {
mid = moduleName;
if (moduleVersion != null)
mid += "@" + moduleVersion;
mid += "/";
String s = buildLoaderModuleClassName();
if (s == null) {
// all elements will be included
s = "";
if (classLoaderName != null && !classLoaderName.isEmpty()) {
s += classLoaderName + "/";
}
if (moduleName != null && !moduleName.isEmpty()) {
s += moduleName;
if (moduleVersion != null && !moduleVersion.isEmpty()) {
s += "@" + moduleVersion;
}
}
s = s.isEmpty() ? declaringClass : s + "/" + declaringClass;
}
return getClassName() + "." + methodName + "(" + mid +
return s + "." + methodName + "(" +
(isNativeMethod() ? "Native Method)" :
(fileName != null && lineNumber >= 0 ?
fileName + ":" + lineNumber + ")" :
@ -264,12 +350,14 @@ public final class StackTraceElement implements java.io.Serializable {
* point as this instance. Two stack trace elements {@code a} and
* {@code b} are equal if and only if:
* <pre>{@code
* equals(a.getFileName(), b.getFileName()) &&
* a.getLineNumber() == b.getLineNumber()) &&
* equals(a.getClassLoaderName(), b.getClassLoaderName()) &&
* equals(a.getModuleName(), b.getModuleName()) &&
* equals(a.getModuleVersion(), b.getModuleVersion()) &&
* equals(a.getClassName(), b.getClassName()) &&
* equals(a.getMethodName(), b.getMethodName())
* equals(a.getFileName(), b.getFileName()) &&
* a.getLineNumber() == b.getLineNumber()
*
* }</pre>
* where {@code equals} has the semantics of {@link
* java.util.Objects#equals(Object, Object) Objects.equals}.
@ -285,9 +373,10 @@ public final class StackTraceElement implements java.io.Serializable {
if (!(obj instanceof StackTraceElement))
return false;
StackTraceElement e = (StackTraceElement)obj;
return e.declaringClass.equals(declaringClass) &&
return Objects.equals(classLoaderName, e.classLoaderName) &&
Objects.equals(moduleName, e.moduleName) &&
Objects.equals(moduleVersion, e.moduleVersion) &&
e.declaringClass.equals(declaringClass) &&
e.lineNumber == lineNumber &&
Objects.equals(methodName, e.methodName) &&
Objects.equals(fileName, e.fileName);
@ -298,6 +387,7 @@ public final class StackTraceElement implements java.io.Serializable {
*/
public int hashCode() {
int result = 31*declaringClass.hashCode() + methodName.hashCode();
result = 31*result + Objects.hashCode(classLoaderName);
result = 31*result + Objects.hashCode(moduleName);
result = 31*result + Objects.hashCode(moduleVersion);
result = 31*result + Objects.hashCode(fileName);
@ -305,5 +395,157 @@ public final class StackTraceElement implements java.io.Serializable {
return result;
}
/**
* Build the compacted String representation to be returned by
* toString method from the declaring Class object.
*/
synchronized String buildLoaderModuleClassName() {
if (classOrLoaderModuleClassName == null)
return null;
if (classOrLoaderModuleClassName instanceof Class) {
Class<?> cls = (Class<?>)classOrLoaderModuleClassName;
classOrLoaderModuleClassName = toLoaderModuleClassName(cls);
}
return (String)classOrLoaderModuleClassName;
}
/**
* Returns <loader>/<module>/<fully-qualified-classname> string
* representation of the given class.
* <p>
* If the module is a non-upgradeable JDK module then omit
* its version string.
* <p>
* If the loader has no name, or if the loader is one of the built-in
* loaders (`boot`, `platform`, or `app`) then drop the first element
* (`<loader>/`).
* <p>
* If the first element has been dropped and the module is unnamed
* then drop the second element (`<module>/`).
* <p>
* If the first element is not dropped and the module is unnamed
* then drop `<module>`.
*/
private static String toLoaderModuleClassName(Class<?> cls) {
ClassLoader loader = cls.getClassLoader0();
Module m = cls.getModule();
// First element - class loader name
// Call package-private ClassLoader::name method
String s = "";
if (loader != null && loader.name() != null &&
!(loader instanceof BuiltinClassLoader)) {
s = loader.name() + "/";
}
// Second element - module name and version
if (m != null && m.isNamed()) {
s += m.getName();
// Include version if it is a user module or upgradeable module
//
// If it is JDK non-upgradeable module which is recorded
// in the hashes in java.base, omit the version.
if (!isHashedInJavaBase(m)) {
Optional<Version> ov = m.getDescriptor().version();
if (ov.isPresent()) {
String version = "@" + ov.get().toString();
s += version;
}
}
}
// fully-qualified class name
return s.isEmpty() ? cls.getName() : s + "/" + cls.getName();
}
/**
* Returns true if the module is hashed with java.base.
* <p>
* This method returns false when running on the exploded image
* since JDK modules are not hashed. They have no Version attribute
* and so "@<version>" part will be omitted anyway.
*/
private static boolean isHashedInJavaBase(Module m) {
// return true if module system is not initialized as the code
// must be in java.base
if (!VM.isModuleSystemInited())
return true;
return Layer.boot() == m.getLayer() && HashedModules.contains(m);
}
/*
* Finds JDK non-upgradeable modules, i.e. the modules that are
* included in the hashes in java.base.
*/
private static class HashedModules {
static Set<String> HASHED_MODULES = hashedModules();
static Set<String> hashedModules() {
Module javaBase = Layer.boot().findModule("java.base").get();
Optional<ModuleHashes> ohashes =
SharedSecrets.getJavaLangModuleAccess()
.hashes(javaBase.getDescriptor());
if (ohashes.isPresent()) {
Set<String> names = new HashSet<>(ohashes.get().names());
names.add("java.base");
return names;
}
return Set.of();
}
static boolean contains(Module m) {
return HASHED_MODULES.contains(m.getName());
}
}
/*
* Returns an array of StackTraceElements of the given depth
* filled from the backtrace of a given Throwable.
*/
static StackTraceElement[] of(Throwable x, int depth) {
StackTraceElement[] stackTrace = new StackTraceElement[depth];
for (int i = 0; i < depth; i++) {
stackTrace[i] = new StackTraceElement();
}
// VM to fill in StackTraceElement
initStackTraceElements(stackTrace, x);
// ensure the proper StackTraceElement initialization
for (StackTraceElement ste : stackTrace) {
ste.buildLoaderModuleClassName();
}
return stackTrace;
}
/*
* Returns a StackTraceElement from a given StackFrameInfo.
*/
static StackTraceElement of(StackFrameInfo sfi) {
StackTraceElement ste = new StackTraceElement();
initStackTraceElement(ste, sfi);
ste.buildLoaderModuleClassName();
return ste;
}
/*
* Sets the given stack trace elements with the backtrace
* of the given Throwable.
*/
private static native void initStackTraceElements(StackTraceElement[] elements,
Throwable x);
/*
* Sets the given stack trace element with the given StackFrameInfo
*/
private static native void initStackTraceElement(StackTraceElement element,
StackFrameInfo sfi);
private static final long serialVersionUID = 6992337162326171013L;
}

View File

@ -24,7 +24,6 @@
*/
package java.lang;
import jdk.internal.misc.VM;
import java.io.*;
import java.util.*;
@ -826,11 +825,7 @@ public class Throwable implements Serializable {
// backtrace if this is the first call to this method
if (stackTrace == UNASSIGNED_STACK ||
(stackTrace == null && backtrace != null) /* Out of protocol state */) {
stackTrace = new StackTraceElement[depth];
for (int i = 0; i < depth; i++) {
stackTrace[i] = new StackTraceElement();
}
getStackTraceElements(stackTrace);
stackTrace = StackTraceElement.of(this, depth);
} else if (stackTrace == null) {
return UNASSIGNED_STACK;
}
@ -881,13 +876,6 @@ public class Throwable implements Serializable {
}
}
/**
* Gets the stack trace elements.
* @param elements
* @throws IndexOutOfBoundsException if {@code elements.length != depth }
*/
private native void getStackTraceElements(StackTraceElement[] elements);
/**
* Reads a {@code Throwable} from a stream, enforcing
* well-formedness constraints on fields. Null entries and

View File

@ -110,19 +110,19 @@ public class URLClassLoader extends SecureClassLoader implements Closeable {
if (security != null) {
security.checkCreateClassLoader();
}
ucp = new URLClassPath(urls);
this.ucp = new URLClassPath(urls);
this.acc = AccessController.getContext();
}
URLClassLoader(URL[] urls, ClassLoader parent,
URLClassLoader(String name, URL[] urls, ClassLoader parent,
AccessControlContext acc) {
super(parent);
super(name, parent);
// this is to make the stack depth consistent with 1.1
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkCreateClassLoader();
}
ucp = new URLClassPath(urls);
this.ucp = new URLClassPath(urls);
this.acc = acc;
}
@ -154,7 +154,7 @@ public class URLClassLoader extends SecureClassLoader implements Closeable {
if (security != null) {
security.checkCreateClassLoader();
}
ucp = new URLClassPath(urls);
this.ucp = new URLClassPath(urls);
this.acc = AccessController.getContext();
}
@ -165,7 +165,7 @@ public class URLClassLoader extends SecureClassLoader implements Closeable {
if (security != null) {
security.checkCreateClassLoader();
}
ucp = new URLClassPath(urls);
this.ucp = new URLClassPath(urls);
this.acc = acc;
}
@ -198,8 +198,76 @@ public class URLClassLoader extends SecureClassLoader implements Closeable {
if (security != null) {
security.checkCreateClassLoader();
}
ucp = new URLClassPath(urls, factory);
acc = AccessController.getContext();
this.ucp = new URLClassPath(urls, factory);
this.acc = AccessController.getContext();
}
/**
* Constructs a new named {@code URLClassLoader} for the specified URLs.
* The URLs will be searched in the order specified for classes
* and resources after first searching in the specified parent class loader.
* Any URL that ends with a '/' is assumed to refer to a directory.
* Otherwise, the URL is assumed to refer to a JAR file which will be
* downloaded and opened as needed.
*
* @param name class loader name; or {@code null} if not named
* @param urls the URLs from which to load classes and resources
* @param parent the parent class loader for delegation
*
* @throws IllegalArgumentException if the given name is empty.
* @throws NullPointerException if {@code urls} is {@code null}.
*
* @throws SecurityException if a security manager exists and its
* {@link SecurityManager#checkCreateClassLoader()} method doesn't
* allow creation of a class loader.
*
* @since 9
*/
public URLClassLoader(String name,
URL[] urls,
ClassLoader parent) {
super(name, parent);
// this is to make the stack depth consistent with 1.1
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkCreateClassLoader();
}
this.ucp = new URLClassPath(urls);
this.acc = AccessController.getContext();
}
/**
* Constructs a new named {@code URLClassLoader} for the specified URLs,
* parent class loader, and URLStreamHandlerFactory.
* The parent argument will be used as the parent class loader for delegation.
* The factory argument will be used as the stream handler factory to
* obtain protocol handlers when creating new jar URLs.
*
* @param name class loader name; or {@code null} if not named
* @param urls the URLs from which to load classes and resources
* @param parent the parent class loader for delegation
* @param factory the URLStreamHandlerFactory to use when creating URLs
*
* @throws IllegalArgumentException if the given name is empty.
* @throws NullPointerException if {@code urls} is {@code null}.
*
* @throws SecurityException if a security manager exists and its
* {@code checkCreateClassLoader} method doesn't allow
* creation of a class loader.
*
* @since 9
*/
public URLClassLoader(String name, URL[] urls, ClassLoader parent,
URLStreamHandlerFactory factory) {
super(name, parent);
// this is to make the stack depth consistent with 1.1
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkCreateClassLoader();
}
this.ucp = new URLClassPath(urls, factory);
this.acc = AccessController.getContext();
}
/* A map (used as a set) to keep track of closeable local resources
@ -735,7 +803,7 @@ public class URLClassLoader extends SecureClassLoader implements Closeable {
URLClassLoader ucl = AccessController.doPrivileged(
new PrivilegedAction<>() {
public URLClassLoader run() {
return new FactoryURLClassLoader(urls, parent, acc);
return new FactoryURLClassLoader(null, urls, parent, acc);
}
});
return ucl;
@ -785,9 +853,9 @@ final class FactoryURLClassLoader extends URLClassLoader {
ClassLoader.registerAsParallelCapable();
}
FactoryURLClassLoader(URL[] urls, ClassLoader parent,
FactoryURLClassLoader(String name, URL[] urls, ClassLoader parent,
AccessControlContext acc) {
super(urls, parent, acc);
super(name, urls, parent, acc);
}
FactoryURLClassLoader(URL[] urls, AccessControlContext acc) {

View File

@ -25,8 +25,6 @@
package java.security;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
@ -113,6 +111,30 @@ public class SecureClassLoader extends ClassLoader {
initialized = true;
}
/**
* Creates a new {@code SecureClassLoader} of the specified name and
* using the specified parent class loader for delegation.
*
* @param name class loader name; or {@code null} if not named
* @param parent the parent class loader
*
* @throws IllegalArgumentException if the given name is empty.
*
* @throws SecurityException if a security manager exists and its
* {@link SecurityManager#checkCreateClassLoader()} method
* doesn't allow creation of a class loader.
*
* @since 9
*/
protected SecureClassLoader(String name, ClassLoader parent) {
super(name, parent);
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkCreateClassLoader();
}
initialized = true;
}
/**
* Converts an array of bytes into an instance of class Class,
* with an optional CodeSource. Before the

View File

@ -145,9 +145,9 @@ public class BuiltinClassLoader
/**
* Create a new instance.
*/
BuiltinClassLoader(BuiltinClassLoader parent, URLClassPath ucp) {
BuiltinClassLoader(String name, BuiltinClassLoader parent, URLClassPath ucp) {
// ensure getParent() returns null when the parent is the boot loader
super(parent == null || parent == ClassLoaders.bootLoader() ? null : parent);
super(name, parent == null || parent == ClassLoaders.bootLoader() ? null : parent);
this.parent = parent;
this.ucp = ucp;

View File

@ -118,7 +118,7 @@ public class ClassLoaders {
*/
private static class BootClassLoader extends BuiltinClassLoader {
BootClassLoader(URLClassPath bcp) {
super(null, bcp);
super(null, null, bcp);
}
@Override
@ -138,7 +138,7 @@ public class ClassLoaders {
}
PlatformClassLoader(BootClassLoader parent) {
super(parent, null);
super("platform", parent, null);
}
/**
@ -165,7 +165,7 @@ public class ClassLoaders {
final URLClassPath ucp;
AppClassLoader(PlatformClassLoader parent, URLClassPath ucp) {
super(parent, ucp);
super("app", parent, ucp);
this.ucp = ucp;
}

View File

@ -165,14 +165,24 @@ JVM_FindLibraryEntry(void *handle, const char *name);
JNIEXPORT jboolean JNICALL
JVM_IsSupportedJNIVersion(jint version);
JNIEXPORT jobjectArray JNICALL
JVM_GetVmArguments(JNIEnv *env);
/*
* java.lang.Throwable
*/
JNIEXPORT void JNICALL
JVM_FillInStackTrace(JNIEnv *env, jobject throwable);
/*
* java.lang.StackTraceElement
*/
JNIEXPORT void JNICALL
JVM_GetStackTraceElements(JNIEnv *env, jobject throwable, jobjectArray elements);
JVM_InitStackTraceElementArray(JNIEnv *env, jobjectArray elements, jobject throwable);
JNIEXPORT void JNICALL
JVM_InitStackTraceElement(JNIEnv* env, jobject element, jobject stackFrameInfo);
/*
* java.lang.StackWalker
@ -194,12 +204,6 @@ JVM_MoreStackWalk(JNIEnv *env, jobject stackStream, jlong mode, jlong anchor,
jint frame_count, jint start_index,
jobjectArray frames);
JNIEXPORT void JNICALL
JVM_ToStackTraceElement(JNIEnv* env, jobject frame, jobject stackElement);
JNIEXPORT jobjectArray JNICALL
JVM_GetVmArguments(JNIEnv *env);
/*
* java.lang.Thread
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* 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
@ -23,25 +23,21 @@
* questions.
*/
/*
* Implementation of class StackFrameInfo
*/
#include <stdio.h>
#include <signal.h>
#include "jni.h"
#include "jvm.h"
#include "java_lang_StackFrameInfo.h"
#include "java_lang_StackTraceElement.h"
/*
* Class: java_lang_StackFrameInfo
* Method: toStackTraceElement0
* Signature: (Ljava/lang/StackTraceElement;)V
*/
JNIEXPORT void JNICALL Java_java_lang_StackFrameInfo_toStackTraceElement0
(JNIEnv *env, jobject stackframeinfo, jobject stacktraceinfo) {
JVM_ToStackTraceElement(env, stackframeinfo, stacktraceinfo);
JNIEXPORT void JNICALL Java_java_lang_StackTraceElement_initStackTraceElement
(JNIEnv *env, jobject dummy, jobject element, jobject stackframeinfo) {
JVM_InitStackTraceElement(env, element, stackframeinfo);
}
JNIEXPORT void JNICALL Java_java_lang_StackTraceElement_initStackTraceElements
(JNIEnv *env, jobject dummy, jobjectArray elements, jobject throwable)
{
JVM_InitStackTraceElementArray(env, elements, throwable);
}

View File

@ -49,10 +49,3 @@ Java_java_lang_Throwable_fillInStackTrace(JNIEnv *env, jobject throwable, jint d
JVM_FillInStackTrace(env, throwable);
return throwable;
}
JNIEXPORT void JNICALL
Java_java_lang_Throwable_getStackTraceElements(JNIEnv *env,
jobject throwable, jobjectArray elements)
{
JVM_GetStackTraceElements(env, throwable, elements);
}

View File

@ -58,7 +58,8 @@ public class StackTraceElementCompositeData extends LazyCompositeData {
getString(cd, FILE_NAME),
getInt(cd, LINE_NUMBER));
} else {
return new StackTraceElement(getString(cd, MODULE_NAME),
return new StackTraceElement(getString(cd, CLASS_LOADER_NAME),
getString(cd, MODULE_NAME),
getString(cd, MODULE_VERSION),
getString(cd, CLASS_NAME),
getString(cd, METHOD_NAME),
@ -76,13 +77,14 @@ public class StackTraceElementCompositeData extends LazyCompositeData {
// CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
// stackTraceElementItemNames!
final Object[] stackTraceElementItemValues = {
ste.getClassLoaderName(),
ste.getModuleName(),
ste.getModuleVersion(),
ste.getClassName(),
ste.getMethodName(),
ste.getFileName(),
ste.getLineNumber(),
ste.isNativeMethod(),
ste.getModuleName(),
ste.getModuleVersion(),
};
try {
return new CompositeDataSupport(stackTraceElementCompositeType,
@ -95,25 +97,29 @@ public class StackTraceElementCompositeData extends LazyCompositeData {
}
// Attribute names
private static final String CLASS_NAME = "className";
private static final String METHOD_NAME = "methodName";
private static final String FILE_NAME = "fileName";
private static final String LINE_NUMBER = "lineNumber";
private static final String NATIVE_METHOD = "nativeMethod";
private static final String MODULE_NAME = "moduleName";
private static final String MODULE_VERSION = "moduleVersion";
private static final String CLASS_LOADER_NAME = "classLoaderName";
private static final String MODULE_NAME = "moduleName";
private static final String MODULE_VERSION = "moduleVersion";
private static final String CLASS_NAME = "className";
private static final String METHOD_NAME = "methodName";
private static final String FILE_NAME = "fileName";
private static final String LINE_NUMBER = "lineNumber";
private static final String NATIVE_METHOD = "nativeMethod";
private static final String[] stackTraceElementItemNames = {
CLASS_LOADER_NAME,
MODULE_NAME,
MODULE_VERSION,
CLASS_NAME,
METHOD_NAME,
FILE_NAME,
LINE_NUMBER,
NATIVE_METHOD,
MODULE_NAME,
MODULE_VERSION,
};
private static final String[] stackTraceElementV9ItemNames = {
CLASS_LOADER_NAME,
MODULE_NAME,
MODULE_VERSION,
};

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
* 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
@ -23,43 +23,74 @@
/*
* @test
* @bug 4712607
* @bug 4712607 6479237
* @summary Basic test for StackTraceElementPublic constructor
* @author Josh Bloch
*/
import java.util.*;
import java.lang.module.ModuleDescriptor;
import java.lang.reflect.Module;
public class PublicConstructor {
public static void main(String args[]) {
public static void main(String... args) {
testConstructor();
testConstructorWithModule();
}
static void testConstructor() {
StackTraceElement ste = new StackTraceElement("com.acme.Widget",
"frobnicate", "Widget.java", 42);
"frobnicate",
"Widget.java", 42);
if (!(ste.getClassName().equals("com.acme.Widget") &&
ste.getFileName().equals("Widget.java") &&
ste.getMethodName().equals("frobnicate") &&
ste.getLineNumber() == 42))
ste.getFileName().equals("Widget.java") &&
ste.getMethodName().equals("frobnicate") &&
ste.getLineNumber() == 42))
throw new RuntimeException("1");
if (ste.isNativeMethod())
throw new RuntimeException("2");
StackTraceElement ste2
= new StackTraceElement("jdk.module",
"9.0",
"com.acme.Widget",
"frobnicate",
"Widget.java",
42);
if (!(ste2.getClassName().equals("com.acme.Widget") &&
ste2.getModuleName().equals("jdk.module") &&
ste2.getModuleVersion().equals("9.0") &&
ste2.getFileName().equals("Widget.java") &&
ste2.getMethodName().equals("frobnicate") &&
ste2.getLineNumber() == 42))
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");
if (ste2.isNativeMethod())
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");
StackTraceElement ste3 = new StackTraceElement("com.acme.Widget",
"frobnicate", "Widget.java", -2);
if (!ste3.isNativeMethod())
throw new RuntimeException("5");
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);
}
}
}

View File

@ -0,0 +1,264 @@
/*
* 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.
*/
/*
* @test
* @bug 6479237
* @summary Test the format of StackTraceElement::toString and its serial form
* @modules java.logging
* java.xml.bind
* @run main SerialTest
*/
import javax.xml.bind.JAXBElement;
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 final String JAVA_XML_BIND = "java.xml.bind";
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 upgradeable module
try {
new JAXBElement(null, null, null);
} catch (IllegalArgumentException e) {
Arrays.stream(e.getStackTrace())
.filter(ste -> ste.getModuleName() != null)
.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;
case JAVA_XML_BIND:
// for exploded build, no version is shown
checkNamedModule(ste, loader, isImage);
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";
}
}
}

View File

@ -0,0 +1,121 @@
/*
* 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.
*/
/*
* @test
* @bug 6479237
* @summary Basic test StackTraceElement with class loader names
* @library lib /lib/testlibrary
* @build 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 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]);
}
}
}

View File

@ -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];
}
}

View File

@ -0,0 +1,136 @@
/*
* 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.lang.reflect.Module;
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);
}
}

View 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;
}

View File

@ -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");
}
}

View File

@ -71,7 +71,7 @@ public class VerifyStackTrace {
"3: VerifyStackTrace$Handle.run(VerifyStackTrace.java:158)\n" +
"4: VerifyStackTrace.invoke(VerifyStackTrace.java:188)\n" +
"5: VerifyStackTrace$1.run(VerifyStackTrace.java:218)\n" +
"6: java.security.AccessController.doPrivileged(java.base/Native Method)\n" +
"6: java.base/java.security.AccessController.doPrivileged(Native Method)\n" +
"7: VerifyStackTrace.test(VerifyStackTrace.java:227)\n" +
"8: VerifyStackTrace.main(VerifyStackTrace.java:182)\n";
@ -100,12 +100,12 @@ public class VerifyStackTrace {
"2: VerifyStackTrace$Handle.execute(VerifyStackTrace.java:147)\n" +
"3: VerifyStackTrace$Handle.run(VerifyStackTrace.java:160)\n" +
"4: VerifyStackTrace.invoke(VerifyStackTrace.java:190)\n" +
"5: jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(java.base/Native Method)\n" +
"6: jdk.internal.reflect.NativeMethodAccessorImpl.invoke(java.base/NativeMethodAccessorImpl.java:62)\n" +
"7: jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(java.base/DelegatingMethodAccessorImpl.java:43)\n" +
"8: java.lang.reflect.Method.invoke(java.base/Method.java:520)\n" +
"5: java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
"6: java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n" +
"7: java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n" +
"8: java.base/java.lang.reflect.Method.invoke(Method.java:520)\n" +
"9: VerifyStackTrace$1.run(VerifyStackTrace.java:220)\n" +
"10: java.security.AccessController.doPrivileged(java.base/Native Method)\n" +
"10: java.base/java.security.AccessController.doPrivileged(Native Method)\n" +
"11: VerifyStackTrace.test(VerifyStackTrace.java:229)\n" +
"12: VerifyStackTrace.main(VerifyStackTrace.java:185)\n";
@ -133,16 +133,16 @@ public class VerifyStackTrace {
"1: VerifyStackTrace.lambda$test$1(VerifyStackTrace.java:213)\n" +
"2: VerifyStackTrace$$Lambda$1/662441761.run(Unknown Source)\n" +
"3: VerifyStackTrace$Handle.execute(VerifyStackTrace.java:149)\n" +
"4: java.lang.invoke.LambdaForm$DMH/2008017533.invokeVirtual_LL_V(java.base/LambdaForm$DMH)\n" +
"5: java.lang.invoke.LambdaForm$MH/1395089624.invoke_MT(java.base/LambdaForm$MH)\n" +
"4: java.base/java.lang.invoke.LambdaForm$DMH/2008017533.invokeVirtual_LL_V(LambdaForm$DMH)\n" +
"5: java.base/java.lang.invoke.LambdaForm$MH/1395089624.invoke_MT(LambdaForm$MH)\n" +
"6: VerifyStackTrace$Handle.run(VerifyStackTrace.java:162)\n" +
"7: VerifyStackTrace.invoke(VerifyStackTrace.java:192)\n" +
"8: jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(java.base/Native Method)\n" +
"9: jdk.internal.reflect.NativeMethodAccessorImpl.invoke(java.base/NativeMethodAccessorImpl.java:62)\n" +
"10: jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(java.base/DelegatingMethodAccessorImpl.java:43)\n" +
"11: java.lang.reflect.Method.invoke(java.base/Method.java:520)\n" +
"8: java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" +
"9: java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n" +
"10: java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n" +
"11: java.base/java.lang.reflect.Method.invoke(Method.java:520)\n" +
"12: VerifyStackTrace$1.run(VerifyStackTrace.java:222)\n" +
"13: java.security.AccessController.doPrivileged(java.base/Native Method)\n" +
"13: java.base/java.security.AccessController.doPrivileged(Native Method)\n" +
"14: VerifyStackTrace.test(VerifyStackTrace.java:231)\n" +
"15: VerifyStackTrace.main(VerifyStackTrace.java:188)\n";
@ -201,8 +201,6 @@ public class VerifyStackTrace {
// out before comparing. We also erase the hash-like names of
// synthetic frames introduced by lambdas & method handles
return produced.replaceAll(":[1-9][0-9]*\\)", ":00)")
.replaceAll("-internal/", "/").replaceAll("-ea/", "/")
.replaceAll("java.base@(\\d+\\.){0,3}(\\d+)/", "java.base/")
.replaceAll("/[0-9]+\\.run", "/xxxxxxxx.run")
.replaceAll("/[0-9]+\\.invoke", "/xxxxxxxx.invoke")
// LFs may or may not be pre-generated, making frames differ

View File

@ -337,9 +337,10 @@ public class ThreadInfoCompositeData {
};
private static final String[] steItemNames = {
"className",
"classLoaderName",
"moduleName",
"moduleVersion",
"className",
"methodName",
"fileName",
"lineNumber",
@ -362,9 +363,10 @@ public class ThreadInfoCompositeData {
validItemTypes[STACK_TRACE] = new ArrayType(1, steCType);
final Object[] steValue = {
ste[0].getClassName(),
ste[0].getClassLoaderName(),
ste[0].getModuleName(),
ste[0].getModuleVersion(),
ste[0].getClassName(),
ste[0].getMethodName(),
ste[0].getFileName(),
new Integer(ste[0].getLineNumber()),

View File

@ -109,7 +109,7 @@ public class NullURLTest {
failures++;
}
try {
loader = new URLClassLoader(null, null, null);
loader = new URLClassLoader((URL[])null, null, null);
System.err.println("URLClassLoader(null, null, null) did not throw NPE");
failures++;
} catch (NullPointerException e) {