< prev index next >
src/java.logging/share/classes/java/util/logging/Logger.java
Print this page
@@ -644,14 +644,22 @@
});
return Boolean.valueOf(s);
}
}
- private static Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
+ private static <T> Class<T> checkCaller(Class<T> caller) {
+ if (caller == null) {
+ throw new IllegalCallerException("no caller frame");
+ }
+ return caller;
+ }
+
+ private static Logger demandLogger(String name, String resourceBundleName,
+ Class<?> caller) {
LogManager manager = LogManager.getLogManager();
if (!SystemLoggerHelper.disableCallerCheck) {
- if (isSystem(caller.getModule())) {
+ if (isSystem(checkCaller(caller).getModule())) {
return manager.demandSystemLogger(name, resourceBundleName, caller);
}
}
return manager.demandLogger(name, resourceBundleName, caller);
// ends up calling new Logger(name, resourceBundleName, caller)
@@ -682,10 +690,13 @@
* be based on the package name or class name
* of the subsystem, such as java.net
* or javax.swing
* @return a suitable Logger
* @throws NullPointerException if the name is null.
+ * @throws IllegalCallerException if there is no caller frame, i.e.
+ * when this {@code getLogger} method is called from JNI
+ * and there is no Java frame on the stack.
*/
// Synchronization is not required here. All synchronization for
// adding a new Logger object is handled by LogManager.addLogger().
@CallerSensitive
@@ -698,11 +709,11 @@
// getLogger("Foo");
//
// would throw an IllegalArgumentException in the second call
// because the wrapper would result in an attempt to replace
// the existing "resourceBundleForFoo" with null.
- return Logger.getLogger(name, Reflection.getCallerClass());
+ return Logger.getLogger(name, checkCaller(Reflection.getCallerClass()));
}
/**
* Find or create a logger for a named subsystem on behalf
* of the given caller.
@@ -759,17 +770,21 @@
* @throws IllegalArgumentException if the Logger already exists and uses
* a different resource bundle name; or if
* {@code resourceBundleName} is {@code null} but the named
* logger has a resource bundle set.
* @throws NullPointerException if the name is null.
+ * @throws IllegalCallerException if there is no caller frame, i.e.
+ * when this {@code getLogger} method is called from JNI
+ * and there is no Java frame on the stack.
*/
// Synchronization is not required here. All synchronization for
// adding a new Logger object is handled by LogManager.addLogger().
@CallerSensitive
public static Logger getLogger(String name, String resourceBundleName) {
- return Logger.getLogger(name, resourceBundleName, Reflection.getCallerClass());
+ return Logger.getLogger(name, resourceBundleName,
+ checkCaller(Reflection.getCallerClass()));
}
/**
* Find or create a logger for a named subsystem on behalf
* of the given caller.
@@ -867,20 +882,23 @@
* messages for this logger.
* May be null if none of the messages require localization.
* @return a newly created private Logger
* @throws MissingResourceException if the resourceBundleName is non-null and
* no corresponding resource can be found.
+ * @throws IllegalCallerException if there is no caller frame, i.e.
+ * when this {@code getLogger} method is called from JNI
+ * and there is no Java frame on the stack.
*/
// Synchronization is not required here. All synchronization for
// adding a new anonymous Logger object is handled by doSetParent().
@CallerSensitive
public static Logger getAnonymousLogger(String resourceBundleName) {
LogManager manager = LogManager.getLogManager();
// cleanup some Loggers that have been GC'ed
manager.drainLoggerRefQueueBounded();
- final Class<?> callerClass = Reflection.getCallerClass();
+ final Class<?> callerClass = checkCaller(Reflection.getCallerClass());
final Module module = callerClass.getModule();
Logger result = new Logger(null, resourceBundleName,
module, manager, false);
result.anonymous = true;
Logger root = manager.getLogger("");
< prev index next >