openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From allee8...@apache.org
Subject svn commit: r1209373 - in /openjpa/trunk: openjpa-kernel/ openjpa-kernel/src/main/java/org/apache/openjpa/ee/ openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ openjpa-kernel/src/main/java/org/apache/openjpa/util/ openjpa-lib/src/main/java/org/a...
Date Fri, 02 Dec 2011 06:43:02 GMT
Author: allee8285
Date: Fri Dec  2 06:42:55 2011
New Revision: 1209373

URL: http://svn.apache.org/viewvc?rev=1209373&view=rev
Log:
OPENJPA-2085 Use ASM to reconstruct the missing StackMapTable attribute in class file after
entity enhancement. BCClassWriter is contributed by Brett Kail.

Added:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/AsmAdaptor.java
  (with props)
Modified:
    openjpa/trunk/openjpa-kernel/pom.xml
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/ee/WASManagedRuntime.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ManagedClassSubclasser.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCClassFileTransformer.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ProxyManagerImpl.java
    openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java
    openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedDataSource.java

Modified: openjpa/trunk/openjpa-kernel/pom.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/pom.xml?rev=1209373&r1=1209372&r2=1209373&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/pom.xml (original)
+++ openjpa/trunk/openjpa-kernel/pom.xml Fri Dec  2 06:42:55 2011
@@ -7,18 +7,18 @@
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at
- 
+
  http://www.apache.org/licenses/LICENSE-2.0
- 
+
  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied.  See the License for the
  specific language governing permissions and limitations
- under the License.   
+ under the License.
 -->
-<!-- 
-    Maven release plugin requires the project tag to be on a single line. 
+<!--
+    Maven release plugin requires the project tag to be on a single line.
 -->
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 
@@ -64,7 +64,7 @@
             <groupId>org.apache.ant</groupId>
             <artifactId>ant</artifactId>
             <scope>provided</scope>
-        </dependency>        
+        </dependency>
         <!-- for osgi ManagedRuntime implementation, -->
         <!-- we pull in OSGi core at the same level as Apache Aries -->
         <dependency>
@@ -73,6 +73,11 @@
             <version>4.2.0</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>asm</groupId>
+            <artifactId>asm</artifactId>
+            <version>3.2</version>
+        </dependency>
     </dependencies>
 
     <build>
@@ -160,7 +165,7 @@
     </build>
 
     <repositories>
-        <!-- 
+        <!--
             Internal (in SVN) repository which contains jar files that are not
             publicly available and cannot be re-distributed.
          -->

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/ee/WASManagedRuntime.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/ee/WASManagedRuntime.java?rev=1209373&r1=1209372&r2=1209373&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/ee/WASManagedRuntime.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/ee/WASManagedRuntime.java
Fri Dec  2 06:42:55 2011
@@ -36,6 +36,7 @@ import javax.transaction.Transaction;
 import javax.transaction.xa.XAResource;
 
 import org.apache.openjpa.conf.OpenJPAConfiguration;
+import org.apache.openjpa.enhance.AsmAdaptor;
 import org.apache.openjpa.lib.conf.Configurable;
 import org.apache.openjpa.lib.conf.Configuration;
 import org.apache.openjpa.lib.log.Log;
@@ -381,7 +382,7 @@ public class WASManagedRuntime extends A
         	}
         }
         bcClass.declareInterface(INTERFACE);
-        bcClass.write();
+        AsmAdaptor.write(bcClass);
     }
 
     public void setRollbackOnly(Throwable cause)

Added: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/AsmAdaptor.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/AsmAdaptor.java?rev=1209373&view=auto
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/AsmAdaptor.java
(added)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/AsmAdaptor.java
Fri Dec  2 06:42:55 2011
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.enhance;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.URLDecoder;
+
+import serp.bytecode.BCClass;
+
+import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassWriter;
+
+/**
+ * Use ASM to add required StackMapTable attribute to the byte code generated by
+ * Serp.
+ */
+public final class AsmAdaptor {
+    private static final int Java7_MajorVersion = 51;
+
+    @SuppressWarnings("deprecation")
+    public static void write(BCClass bc) throws IOException {
+        if (bc.getMajorVersion() < Java7_MajorVersion) {
+            bc.write();
+        } else {
+            String name = bc.getName();
+            int dotIndex = name.lastIndexOf('.') + 1;
+            name = name.substring(dotIndex);
+            Class<?> type = bc.getType();
+
+            OutputStream out = new FileOutputStream(
+                    URLDecoder.decode(type.getResource(name + ".class").getFile()));
+            try {
+                writeJava7(bc, out);
+            } finally {
+                out.flush();
+                out.close();
+            }
+        }
+    }
+
+    public static void write(BCClass bc, File outFile) throws IOException {
+        if (bc.getMajorVersion() < Java7_MajorVersion) {
+            bc.write(outFile);
+        } else {
+            OutputStream out = new FileOutputStream(outFile);
+            try {
+                writeJava7(bc, out);
+            } finally {
+                out.flush();
+                out.close();
+            }
+        }
+    }
+
+    public static byte[] toByteArray(BCClass bc, byte[] returnBytes) throws IOException {
+        if (returnBytes.length >= 8 && (returnBytes[6] << 8 + returnBytes[7])
>= Java7_MajorVersion) {
+            returnBytes = toJava7ByteArray(bc, returnBytes);
+        }
+        return returnBytes;
+    }
+
+    private static void writeJava7(BCClass bc, OutputStream out) throws IOException {
+        byte[] java7Bytes = toJava7ByteArray(bc, bc.toByteArray());
+        out.write(java7Bytes);
+    }
+
+    private static byte[] toJava7ByteArray(BCClass bc, byte[] classBytes) throws IOException
{
+        ByteArrayInputStream bais = new ByteArrayInputStream(classBytes);
+        BufferedInputStream bis = new BufferedInputStream(bais);
+
+        ClassWriter cw = new BCClassWriter(ClassWriter.COMPUTE_FRAMES, bc.getClassLoader());
+        ClassReader cr = new ClassReader(bis);
+        cr.accept(cw, 0);
+        return cw.toByteArray();
+    }
+
+    private static class BCClassWriter extends ClassWriter {
+        private final ClassLoader _loader;
+
+        BCClassWriter(int flags, ClassLoader loader) {
+            super(flags);
+            _loader = loader;
+        }
+
+        @Override
+        protected String getCommonSuperClass(String type1, String type2) {
+            Class<?> class1;
+            Class<?> class2;
+            try {
+                class1 = _loader.loadClass(type1.replace('/', '.'));
+                class2 = _loader.loadClass(type2.replace('/', '.'));
+            } catch (ClassNotFoundException ex) {
+                throw new RuntimeException(ex);
+            }
+            if (class1.isAssignableFrom(class2)) {
+                return type1;
+            }
+            if (class2.isAssignableFrom(class1)) {
+                return type2;
+            }
+            if (class1.isInterface() || class2.isInterface()) {
+                return "java/lang/Object";
+            }
+            do {
+                class1 = class1.getSuperclass();
+            } while (!class1.isAssignableFrom(class2));
+            return class1.getName().replace('.', '/');
+        }
+    }
+}

Propchange: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/AsmAdaptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ManagedClassSubclasser.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ManagedClassSubclasser.java?rev=1209373&r1=1209372&r2=1209373&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ManagedClassSubclasser.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/ManagedClassSubclasser.java
Fri Dec  2 06:42:55 2011
@@ -32,7 +32,6 @@ import java.util.Set;
 import org.apache.openjpa.conf.OpenJPAConfiguration;
 import org.apache.openjpa.lib.log.Log;
 import org.apache.openjpa.lib.util.BytecodeWriter;
-import org.apache.openjpa.lib.util.JavaVersions;
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.lib.util.Files;
 import org.apache.openjpa.lib.util.Localizer.Message;
@@ -302,7 +301,7 @@ public class ManagedClassSubclasser {
             dir = Files.getPackageFile(dir, bc.getPackageName(), true);
             File f = new File(dir, bc.getClassName() + ".class");
             System.err.println("Writing to " + f);
-            bc.write(f);
+            AsmAdaptor.write(bc, f);
         }
     }
 

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCClassFileTransformer.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCClassFileTransformer.java?rev=1209373&r1=1209372&r2=1209373&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCClassFileTransformer.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCClassFileTransformer.java
Fri Dec  2 06:42:55 2011
@@ -151,7 +151,7 @@ public class PCClassFileTransformer
             if (enhancer.run() == PCEnhancer.ENHANCE_NONE)
                 return null;
             returnBytes = enhancer.getPCBytecode().toByteArray();
-            return returnBytes;
+            return AsmAdaptor.toByteArray(enhancer.getPCBytecode(), returnBytes);
         } catch (Throwable t) {
             _log.warn(_loc.get("cft-exception-thrown", className), t);
             if (t instanceof RuntimeException)

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java?rev=1209373&r1=1209372&r2=1209373&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/enhance/PCEnhancer.java
Fri Dec  2 06:42:55 2011
@@ -52,7 +52,6 @@ import java.util.Set;
 import org.apache.commons.lang.StringUtils;
 import org.apache.openjpa.conf.OpenJPAConfiguration;
 import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
-import org.apache.openjpa.conf.OpenJPAVersion;
 import org.apache.openjpa.lib.conf.Configurations;
 import org.apache.openjpa.lib.log.Log;
 import org.apache.openjpa.lib.meta.ClassArgParser;
@@ -625,10 +624,10 @@ public class PCEnhancer { 
         if (_writer != null)
             _writer.write(bc);
         else if (_dir == null)
-            bc.write();
+            AsmAdaptor.write(bc);
         else {
             File dir = Files.getPackageFile(_dir, bc.getPackageName(), true);
-            bc.write(new File(dir, bc.getClassName() + ".class"));
+            AsmAdaptor.write(bc, new File(dir, bc.getClassName() + ".class"));
         }
     }
 

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ProxyManagerImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ProxyManagerImpl.java?rev=1209373&r1=1209372&r2=1209373&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ProxyManagerImpl.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/ProxyManagerImpl.java
Fri Dec  2 06:42:55 2011
@@ -50,15 +50,14 @@ import java.util.TreeSet;
 import java.util.Queue;
 
 import org.apache.commons.lang.StringUtils;
+import org.apache.openjpa.enhance.AsmAdaptor;
 import org.apache.openjpa.kernel.OpenJPAStateManager;
 import org.apache.openjpa.lib.util.Files;
 import org.apache.openjpa.lib.util.J2DoPrivHelper;
-import org.apache.openjpa.lib.util.JavaVersions;
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.lib.util.Options;
 import org.apache.openjpa.lib.util.concurrent.NullSafeConcurrentHashMap;
 
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
 import serp.bytecode.BCClass;
@@ -1664,7 +1663,7 @@ public class ProxyManagerImpl
             }
 
             System.out.println(bc.getName());
-            bc.write(new File(dir, bc.getClassName() + ".class"));
+            AsmAdaptor.write(bc, new File(dir, bc.getClassName() + ".class"));
         }
     }
 }

Modified: openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java?rev=1209373&r1=1209372&r2=1209373&view=diff
==============================================================================
--- openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java
(original)
+++ openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/J2DoPrivHelper.java
Fri Dec  2 06:42:55 2011
@@ -1040,25 +1040,6 @@ public abstract class J2DoPrivHelper {
     }
     
     /**
-     * Return a PrivilegedExceptionAction object for BCClass.write().
-     * 
-     * Requires security policy:
-     *   'permission java.io.FilePermission "write";'
-     * 
-     * @return null
-     * @throws IOException
-     */
-    public static final PrivilegedExceptionAction<Object> bCClassWrite(
-        final BCClass bc, final File f) throws IOException {
-        return new PrivilegedExceptionAction<Object>() {
-            public Object run() throws IOException {
-                bc.write(f);
-                return null;
-            }
-        };
-    }
-
-    /**
      * Return a PrivilegeAction object for BCClass.getFields().
      * 
      * Requires security policy:

Modified: openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedDataSource.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedDataSource.java?rev=1209373&r1=1209372&r2=1209373&view=diff
==============================================================================
--- openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedDataSource.java
(original)
+++ openjpa/trunk/openjpa-slice/src/main/java/org/apache/openjpa/slice/jdbc/DistributedDataSource.java
Fri Dec  2 06:42:55 2011
@@ -21,9 +21,11 @@ package org.apache.openjpa.slice.jdbc;
 import java.io.PrintWriter;
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.logging.Logger;
 
 import javax.sql.DataSource;
 import javax.sql.XADataSource;
@@ -112,4 +114,7 @@ public class DistributedDataSource exten
         
     }
 
+    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
+        throw new SQLFeatureNotSupportedException();
+    }
 }



Mime
View raw message