Repository: logging-log4j2
Updated Branches:
  refs/heads/master d95dc7dc5 -> 69f5da463


Revert "[LOG4J2-1763]: Use MethodHandle in ReflectionUtil"

This reverts commit 90e8e60530df52b005560f1aa5116602fe1503f9.

JDK 7 has some weird call stack bug with MethodHandle and cannot be used
for a caller sensitive method.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/69f5da46
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/69f5da46
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/69f5da46

Branch: refs/heads/master
Commit: 69f5da4639f4bf53f78ca01f213f0a83e2d23742
Parents: d95dc7d
Author: Matt Sicker <[email protected]>
Authored: Tue Jan 3 23:24:57 2017 -0600
Committer: Matt Sicker <[email protected]>
Committed: Tue Jan 3 23:24:57 2017 -0600

----------------------------------------------------------------------
 .../logging/log4j/util/ReflectionUtil.java      | 45 +++++++++-----------
 src/changes/changes.xml                         |  3 --
 2 files changed, 19 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/69f5da46/log4j-api/src/main/java/org/apache/logging/log4j/util/ReflectionUtil.java
----------------------------------------------------------------------
diff --git 
a/log4j-api/src/main/java/org/apache/logging/log4j/util/ReflectionUtil.java 
b/log4j-api/src/main/java/org/apache/logging/log4j/util/ReflectionUtil.java
index 7506e75..52f171e 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/ReflectionUtil.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/ReflectionUtil.java
@@ -16,9 +16,7 @@
  */
 package org.apache.logging.log4j.util;
 
-import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodType;
+import java.lang.reflect.Method;
 import java.util.Stack;
 
 import org.apache.logging.log4j.Logger;
@@ -34,7 +32,6 @@ import org.apache.logging.log4j.status.StatusLogger;
  * {@code Class} using the slow {@link Class#forName} (which can add an extra 
microsecond or more for each invocation
  * depending on the runtime ClassLoader hierarchy).
  * </p>
- * <h3>History</h3>
  * <p>
  * During Java 8 development, the {@code 
sun.reflect.Reflection.getCallerClass(int)} was removed from OpenJDK, and this
  * change was back-ported to Java 7 in version 1.7.0_25 which changed the 
behavior of the call and caused it to be off
@@ -43,9 +40,8 @@ import org.apache.logging.log4j.status.StatusLogger;
  * </p>
  * <p>
  * After much community backlash, the JDK team agreed to restore {@code 
getCallerClass(int)} and keep its existing
- * behavior for the rest of Java 7. However, the method is deprecated in Java 
8, and Java 9 provides a new API
- * ({@code StackWalker}).
- * Therefore, the functionality of this class cannot be relied upon for all 
future versions of Java.
+ * behavior for the rest of Java 7. However, the method is deprecated in Java 
8, and current Java 9 development has not
+ * addressed this API. Therefore, the functionality of this class cannot be 
relied upon for all future versions of Java.
  * It does, however, work just fine in Sun JDK 1.6, OpenJDK 1.6, 
Oracle/OpenJDK 1.7, and Oracle/OpenJDK 1.8. Other Java
  * environments may fall back to using {@link Throwable#getStackTrace()} which 
is significantly slower due to
  * examination of every virtual frame of execution.
@@ -56,31 +52,29 @@ public final class ReflectionUtil {
     // CHECKSTYLE:OFF
     static final int JDK_7u25_OFFSET;
     // CHECKSTYLE:OFF
-
+    
     private static final Logger LOGGER = StatusLogger.getLogger();
-
     private static final boolean SUN_REFLECTION_SUPPORTED;
-    private static final MethodHandle GET_CALLER_CLASS;
+    private static final Method GET_CALLER_CLASS;
     private static final PrivateSecurityManager SECURITY_MANAGER;
 
     static {
-        MethodHandle getCallerClass;
+        Method getCallerClass;
         int java7u25CompensationOffset = 0;
         try {
             final Class<?> sunReflectionClass = 
LoaderUtil.loadClass("sun.reflect.Reflection");
-            getCallerClass = 
MethodHandles.lookup().findStatic(sunReflectionClass, "getCallerClass",
-                MethodType.methodType(Class.class, int.class));
-            Class<?> o = (Class<?>) getCallerClass.invokeExact(0);
-            final Class<?> test1 = (Class<?>) getCallerClass.invokeExact(0);
+            getCallerClass = 
sunReflectionClass.getDeclaredMethod("getCallerClass", int.class);
+            Object o = getCallerClass.invoke(null, 0);
+            final Object test1 = getCallerClass.invoke(null, 0);
             if (o == null || o != sunReflectionClass) {
                 LOGGER.warn("Unexpected return value from 
Reflection.getCallerClass(): {}", test1);
                 getCallerClass = null;
                 java7u25CompensationOffset = -1;
             } else {
-                o = (Class<?>) getCallerClass.invokeExact(1);
+                o = getCallerClass.invoke(null, 1);
                 if (o == sunReflectionClass) {
                     LOGGER.warn("You are using Java 1.7.0_25 which has a 
broken implementation of "
-                        + "Reflection.getCallerClass.");
+                            + "Reflection.getCallerClass.");
                     LOGGER.warn("You should upgrade to at least Java 1.7.0_40 
or later.");
                     LOGGER.debug("Using stack depth compensation offset of 1 
due to Java 7u25.");
                     java7u25CompensationOffset = 1;
@@ -88,11 +82,7 @@ public final class ReflectionUtil {
             }
         } catch (final Exception | LinkageError e) {
             LOGGER.info("sun.reflect.Reflection.getCallerClass is not 
supported. "
-                + "ReflectionUtil.getCallerClass will be much slower due to 
this.", e);
-            getCallerClass = null;
-            java7u25CompensationOffset = -1;
-        } catch (final Throwable throwable) {
-            LOGGER.warn("Error looking up 
sun.reflect.Reflection.getCallerClass", throwable);
+                    + "ReflectionUtil.getCallerClass will be much slower due 
to this.", e);
             getCallerClass = null;
             java7u25CompensationOffset = -1;
         }
@@ -110,7 +100,7 @@ public final class ReflectionUtil {
             psm = new PrivateSecurityManager();
         } catch (final SecurityException ignored) {
             LOGGER.debug("Not allowed to create SecurityManager. "
-                + "Falling back to slowest ReflectionUtil implementation.");
+                    + "Falling back to slowest ReflectionUtil 
implementation.");
             psm = null;
         }
         SECURITY_MANAGER = psm;
@@ -136,8 +126,8 @@ public final class ReflectionUtil {
         // since Reflection.getCallerClass ignores the call to Method.invoke()
         if (supportsFastReflection()) {
             try {
-                return (Class<?>) GET_CALLER_CLASS.invokeExact(depth + 1 + 
JDK_7u25_OFFSET);
-            } catch (final Throwable e) {
+                return (Class<?>) GET_CALLER_CLASS.invoke(null, depth + 1 + 
JDK_7u25_OFFSET);
+            } catch (final Exception e) {
                 // theoretically this could happen if the caller class were 
native code
                 LOGGER.error("Error in ReflectionUtil.getCallerClass({}).", 
depth, e);
                 // TODO: return Object.class
@@ -265,7 +255,7 @@ public final class ReflectionUtil {
         }
         try {
             return LoaderUtil.loadClass(getCallerClassName(anchor.getName(), 
Strings.EMPTY,
-                new Throwable().getStackTrace()));
+                    new Throwable().getStackTrace()));
         } catch (final ClassNotFoundException ignored) {
             // no problem really
         }
@@ -312,6 +302,9 @@ public final class ReflectionUtil {
         return new Stack<>();
     }
 
+    /**
+     * 
+     */
     static final class PrivateSecurityManager extends SecurityManager {
 
         @Override

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/69f5da46/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index d7bb884..4ef9cf3 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -225,9 +225,6 @@
       <action issue="LOG4J2-1764" dev="mattsicker" type="add">
         Use MethodHandle in ContextDataFactory cached constructor.
       </action>
-      <action issue="LOG4J2-1763" dev="mattsicker" type="add">
-        Use MethodHandle instead of Method in ReflectionUtil.
-      </action>
       <action issue="LOG4J2-1730" dev="mattsicker" type="add">
         Add Apache Cassandra appender.
       </action>

Reply via email to