-
Enhancement
-
Resolution: Duplicate
-
P4
-
None
-
1.4.1
-
x86
-
windows_xp
Name: rmT116609 Date: 04/21/2003
A DESCRIPTION OF THE REQUEST :
Many Java technologies are relying on a dynamic factory pattern, where a factory class looks for the actual implementation by locating the implementation class through META-INF/services file or by similar means.
JAXP and JAXB are typical examples.
Essentially, those are doing Class.forName(className), but here's the problem.
The caller of Class.forName() is JAXB or JAXP itself, and those API jars
does NOT typically reside in the domain of the application class loader. Instead, it often lives in the app server's own lib directory, or the endorsed lib directory. Therefore, when those factory classes try to locate a service description file or when it calls the Class.forName(), the class loader used for it is the class loader that loaded JAXP/JAXB.
This prevents the factory from looking at the application's class loader, and causing a problem that is hard for average users to fix.
Since the details of class loading is usually not understood by average users, it would be nice if those factory classes can automatically use the caller's class loader, just like Class.forName(String).
I found out that the sun.reflect.Reflection.getCallerClass(int) method is used inside the Class.forName() method to find the caller's class loader, but unfortunately this method is not publicly available, so it cannot be reliably used, hence this RFE.
One possibility seems to be to add the Class getClass() method to the java.lang.StackTraceElement class, so that I can access a Class object (not just a class name). Then I can do:
try {
throw new Exception();
} catch( Exception e ) {
// examine the stack trace elements to find out the caller's class loader
}
If I could obtain the list of the current StackTraceElement[] without throwing a dummy exception, that would be even better.
JUSTIFICATION :
Technologies like JAXB (Java Architecture for XML Binding) would benefit from this enhancement.
It will in turn improve the user experience of those technologies, and make Java less error-prone, easier to user
CUSTOMER SUBMITTED WORKAROUND :
Currently those technologies may allow users to pass in a reference to a class loader. But many users don't understand the details of class loading and therefore it tends to be ignored.
(Review ID: 184519)
======================================================================
- duplicates
-
JDK-8140450 Implement JEP 259: Stack-Walking API
- Resolved
- relates to
-
JDK-8014925 Disable sun.reflect.Reflection.getCallerClass(int) with a temporary switch to re-enable it
- Closed
-
JDK-8020968 Provide a replacement for sun.reflect.Reflection.getCallerClass
- Closed