--- old/src/java.base/share/classes/java/lang/System.java 2017-03-21 18:39:04.000000000 +0000 +++ new/src/java.base/share/classes/java/lang/System.java 2017-03-21 18:39:04.000000000 +0000 @@ -1579,6 +1579,14 @@ * @return an instance of {@link Logger} that can be used by the calling * class. * @throws NullPointerException if {@code name} is {@code null}. + * @throws IllegalCallerException if there is no {@linkplain + * StackWalker#getCallerClass() caller} frame, i.e. this method + * is called from JNI and there is no Java frame on the stack.
+ * Note: To obtain a logger in such a context, use an auxiliary + * class that will implicitly be identified as the caller, or use + * the system {@link LoggerFinder#getLoggerFinder() LoggerFinder} + * to obtain a logger instead. Note however that doing the latter + * may eagerly initialize the underlying logging system. * * @since 9 */ @@ -1586,6 +1594,9 @@ public static Logger getLogger(String name) { Objects.requireNonNull(name); final Class caller = Reflection.getCallerClass(); + if (caller == null) { + throw new IllegalCallerException("no caller frame"); + } return LazyLoggers.getLogger(name, caller.getModule()); } @@ -1599,7 +1610,7 @@ * The returned logger will perform message localization as specified * by {@link LoggerFinder#getLocalizedLogger(java.lang.String, * java.util.ResourceBundle, java.lang.reflect.Module) - * LoggerFinder.getLocalizedLogger(name, bundle, module}, where + * LoggerFinder.getLocalizedLogger(name, bundle, module)}, where * {@code module} is the caller's module. * * @apiNote @@ -1619,6 +1630,14 @@ * resource bundle for message localization. * @throws NullPointerException if {@code name} is {@code null} or * {@code bundle} is {@code null}. + * @throws IllegalCallerException if there is no {@linkplain + * StackWalker#getCallerClass() caller} frame, i.e. this method + * is called from JNI and there is no Java frame on the stack.
+ * Note: To obtain a logger in such a context, use an auxiliary + * class that will implicitly be identified as the caller, or use + * the system {@link LoggerFinder#getLoggerFinder() LoggerFinder} + * to obtain a logger instead. Note however that doing the latter + * may eagerly initialize the underlying logging system. * * @since 9 */ @@ -1627,6 +1646,9 @@ final ResourceBundle rb = Objects.requireNonNull(bundle); Objects.requireNonNull(name); final Class caller = Reflection.getCallerClass(); + if (caller == null) { + throw new IllegalCallerException("no caller frame"); + } final SecurityManager sm = System.getSecurityManager(); // We don't use LazyLoggers if a resource bundle is specified. // Bootstrap sensitive classes in the JDK do not use resource bundles