openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject [openjpa] 03/05: OPENJPA-2822 get rid of tricks < java8
Date Thu, 23 Jul 2020 12:07:27 GMT
This is an automated email from the ASF dual-hosted git repository.

struberg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openjpa.git

commit bf03bdb314d99502046543e2e904235f87f46bb3
Author: Mark Struberg <struberg@apache.org>
AuthorDate: Thu Jul 23 14:00:10 2020 +0200

    OPENJPA-2822 get rid of tricks < java8
    
    we can now rely on transform() being available on all JVMs.
---
 .../org/apache/openjpa/enhance/ClassRedefiner.java | 44 ++++++----------------
 .../openjpa/enhance/InstrumentationFactory.java    |  9 -----
 2 files changed, 12 insertions(+), 41 deletions(-)

diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ClassRedefiner.java b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ClassRedefiner.java
index 339dc2e..0b2e11e 100644
--- a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ClassRedefiner.java
+++ b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ClassRedefiner.java
@@ -18,10 +18,8 @@
  */
 package org.apache.openjpa.enhance;
 
-import java.lang.instrument.ClassDefinition;
 import java.lang.instrument.ClassFileTransformer;
 import java.lang.instrument.Instrumentation;
-import java.lang.reflect.Method;
 import java.security.ProtectionDomain;
 import java.util.Map;
 
@@ -47,7 +45,7 @@ public class ClassRedefiner {
     /**
      * For each element in <code>classes</code>, this method will redefine
      * all the element's methods such that field accesses are intercepted
-     * in-line. If {@link #canRedefineClasses()} returns <code>false</code>,
+     * in-line. If {@link #canRedefineClasses(Log)} returns <code>false</code>,
      * this method is a no-op.
      */
     public static void redefineClasses(OpenJPAConfiguration conf,
@@ -62,37 +60,19 @@ public class ClassRedefiner {
             inst = InstrumentationFactory.getInstrumentation(log);
 
             Class<?>[] array = classes.keySet().toArray(new Class[classes.size()]);
-            if (JavaVersions.VERSION >= 6) {
-                log.trace(_loc.get("retransform-types", classes.keySet()));
+            log.trace(_loc.get("retransform-types", classes.keySet()));
 
-                t = new ClassFileTransformer() {
-                    @Override
-                    public byte[] transform(ClassLoader loader, String clsName,
-                        Class<?> classBeingRedefined, ProtectionDomain pd,
-                        byte[] classfileBuffer) {
-                        return classes.get(classBeingRedefined);
-                    }
-                };
+            t = new ClassFileTransformer() {
+                @Override
+                public byte[] transform(ClassLoader loader, String clsName,
+                    Class<?> classBeingRedefined, ProtectionDomain pd,
+                    byte[] classfileBuffer) {
+                    return classes.get(classBeingRedefined);
+                }
+            };
 
-                // these are Java 6 methods, and we don't have a Java 6 build
-                // module yet. The cost of reflection here is negligible
-                // compared to the redefinition / enhancement costs in total,
-                // so this should not be a big problem.
-                Method meth = inst.getClass().getMethod("addTransformer",
-                    new Class[] { ClassFileTransformer.class, boolean.class });
-                meth.invoke(inst, new Object[] { t, true });
-                meth = inst.getClass().getMethod("retransformClasses",
-                    new Class[] { array.getClass() });
-                meth.invoke(inst, new Object[] { array });
-            } else {
-                log.trace(_loc.get("redefine-types", classes.keySet()));
-                // in a Java 5 context, we can use class redefinition instead
-                ClassDefinition[] defs = new ClassDefinition[array.length];
-                for (int i = 0; i < defs.length; i++)
-                    defs[i] = new ClassDefinition(array[i],
-                        classes.get(array[i]));
-                inst.redefineClasses(defs);
-            }
+            inst.addTransformer(t, true);
+            inst.retransformClasses(array);
         } catch (Exception e) {
             throw new InternalException(e);
         } finally {
diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/InstrumentationFactory.java
b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/InstrumentationFactory.java
index feeb982..3516750 100644
--- a/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/InstrumentationFactory.java
+++ b/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/InstrumentationFactory.java
@@ -40,7 +40,6 @@ import java.util.zip.ZipOutputStream;
 
 import org.apache.openjpa.lib.log.Log;
 import org.apache.openjpa.lib.util.JavaVendors;
-import org.apache.openjpa.lib.util.JavaVersions;
 import org.apache.openjpa.lib.util.Localizer;
 
 
@@ -89,14 +88,6 @@ public class InstrumentationFactory {
         if ( _inst != null || !_dynamicallyInstall)
             return _inst;
 
-        // dynamic loading of the agent is only available in JDK 1.6+
-        if (JavaVersions.VERSION < 6) {
-            if (log.isTraceEnabled() == true) {
-                log.trace(_name + ".getInstrumentation() Dynamic loading only supported on
Java SE 6 or later");
-            }
-            return null;
-        }
-
         AccessController.doPrivileged(new PrivilegedAction<Object>() {
             @Override
             public Object run() {


Mime
View raw message