From ad81779bffb43aaa5e0b55454d0ae765fc89c793 Mon Sep 17 00:00:00 2001 From: Andy Clement Date: Thu, 5 Mar 2015 11:03:03 -0800 Subject: [PATCH] possible fix for https://github.com/spring-projects/spring-loaded/issues/98 --- .../java/org/springsource/loaded/jvm/JVM.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/springloaded/src/main/java/org/springsource/loaded/jvm/JVM.java b/springloaded/src/main/java/org/springsource/loaded/jvm/JVM.java index eee08f7..e96be04 100644 --- a/springloaded/src/main/java/org/springsource/loaded/jvm/JVM.java +++ b/springloaded/src/main/java/org/springsource/loaded/jvm/JVM.java @@ -38,6 +38,8 @@ public class JVM { private static Constructor jlrMethodCtor = (Constructor) Method.class.getDeclaredConstructors()[0]; private static Method jlrMethodCopy; + private static Method jlrMethodGetRoot; + static { try { jlrMethodCopy = Method.class.getDeclaredMethod("copy"); @@ -45,6 +47,12 @@ public class JVM { } catch (Exception e) { log.log(Level.SEVERE, "Problems getting 'Method.copy()' method. Incompatible JVM?", e); } + try { + jlrMethodGetRoot = Method.class.getDeclaredMethod("getRoot"); + jlrMethodGetRoot.setAccessible(true); + } catch (Exception e) { + // Possibly on a JDK level that didn't have this? + } } private static Method jlrFieldCopy; @@ -57,14 +65,22 @@ public class JVM { } } + private static Method jlrConstructorGetRoot; private static Method jlrConstructorCopy; static { try { + jlrConstructorCopy = Constructor.class.getDeclaredMethod("copy"); jlrConstructorCopy.setAccessible(true); } catch (Exception e) { log.log(Level.SEVERE, "Problems getting 'Constructor.copy()' method. Incompatible JVM?", e); } + try { + jlrConstructorGetRoot = Constructor.class.getDeclaredMethod("getRoot"); + jlrConstructorGetRoot.setAccessible(true); + } catch (Exception e) { + // Possibly on a JDK level that didn't have this? + } } private static Field jlrMethodModifiers; @@ -131,6 +147,12 @@ public class JVM { */ public static Method copyMethod(Method method) { try { + if (jlrMethodGetRoot != null) { + Method possibleRoot = (Method) jlrMethodGetRoot.invoke(method); + if (possibleRoot != null) { + method = possibleRoot; + } + } return (Method) jlrMethodCopy.invoke(method); } catch (Exception e) { log.log(Level.SEVERE, "Problems copying method. Incompatible JVM?", e); @@ -149,6 +171,12 @@ public class JVM { public static Constructor copyConstructor(Constructor c) { try { + if (jlrConstructorGetRoot != null) { + Constructor possibleRoot = (Constructor) jlrConstructorGetRoot.invoke(c); + if (possibleRoot != null) { + c = possibleRoot; + } + } return (Constructor) jlrConstructorCopy.invoke(c); } catch (Exception e) { log.log(Level.SEVERE, "Problems copying constructor. Incompatible JVM?", e);