You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2008/05/28 03:02:44 UTC

svn commit: r660768 - in /openejb/trunk/openejb3/container/openejb-core/src/main: java/org/apache/openejb/config/rules/ java/org/apache/openejb/core/ java/org/apache/openejb/core/cmp/cmp2/ resources/org/apache/openejb/config/rules/

Author: dblevins
Date: Tue May 27 18:02:43 2008
New Revision: 660768

URL: http://svn.apache.org/viewvc?rev=660768&view=rev
Log:
OPENEJB-810: CMP ejbPostCreate methods made optional
OPENEJB-808: Validation: Unused ejbCreate methods
OPENEJB-809: Validation: Unused ejbPostCreate methods

Added:
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/cmp2/PostCreateGenerator.java
Modified:
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckMethods.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/CoreDeploymentInfo.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/cmp2/Cmp1Generator.java
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/cmp2/Cmp2Generator.java
    openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckMethods.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckMethods.java?rev=660768&r1=660767&r2=660768&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckMethods.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckMethods.java Tue May 27 18:02:43 2008
@@ -17,11 +17,11 @@
 package org.apache.openejb.config.rules;
 
 import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.config.EjbModule;
 import org.apache.openejb.jee.EnterpriseBean;
-import org.apache.openejb.jee.RemoteBean;
 import org.apache.openejb.jee.EntityBean;
+import org.apache.openejb.jee.RemoteBean;
 import org.apache.openejb.jee.SessionBean;
-import org.apache.openejb.config.EjbModule;
 
 import javax.ejb.EJBLocalObject;
 import java.lang.reflect.Method;
@@ -42,6 +42,10 @@
                 check_localInterfaceMethods(b);
                 check_localHomeInterfaceMethods(b);
             }
+
+            check_unusedCreateMethods(b);
+            check_unusedPostCreateMethods(b);
+
         }
     }
 
@@ -57,10 +61,8 @@
 
         if (check_hasCreateMethod(b, bean, home)) {
             check_createMethodsAreImplemented(b, bean, home);
-            check_postCreateMethodsAreImplemented(b, bean, home);
+//            check_postCreateMethodsAreImplemented(b, bean, home);
         }
-
-        check_unusedCreateMethods(b, bean, home);
     }
 
     private void check_localInterfaceMethods(RemoteBean b) {
@@ -131,24 +133,18 @@
 
         if (check_hasCreateMethod(b, bean, home)) {
             check_createMethodsAreImplemented(b, bean, home);
-            check_postCreateMethodsAreImplemented(b, bean, home);
+            // ejbPostCreate methods are now automatically generated
+//            check_postCreateMethodsAreImplemented(b, bean, home);
         }
-
-        check_unusedCreateMethods(b, bean, home);
     }
 
     public boolean check_hasCreateMethod(RemoteBean b, Class bean, Class home) {
 
-        if (b instanceof SessionBean && !javax.ejb.SessionBean.class.isAssignableFrom(bean)){
+        if (b instanceof SessionBean && !javax.ejb.SessionBean.class.isAssignableFrom(bean)) {
             // This is a pojo-style bean
             return false;
         }
 
-        if (b instanceof EntityBean) {
-            // entity beans are not required to have a create method
-            return false;
-        }
-
         Method[] homeMethods = home.getMethods();
 
         boolean hasCreateMethod = false;
@@ -157,7 +153,7 @@
             hasCreateMethod = homeMethods[i].getName().startsWith("create");
         }
 
-        if (!hasCreateMethod) {
+        if (!hasCreateMethod && !(b instanceof EntityBean)) {
 
             fail(b, "no.home.create", b.getHome(), b.getRemote());
 
@@ -177,7 +173,7 @@
             Method create = homeMethods[i];
 
             StringBuilder ejbCreateName = new StringBuilder(create.getName());
-            ejbCreateName.replace(0,1, "ejbC");
+            ejbCreateName.replace(0, 1, "ejbC");
 
             try {
                 if (EnterpriseBean.class.isAssignableFrom(bean)) {
@@ -218,7 +214,7 @@
             if (!homeMethods[i].getName().startsWith("create")) continue;
             Method create = homeMethods[i];
             StringBuilder ejbPostCreateName = new StringBuilder(create.getName());
-            ejbPostCreateName.replace(0,1, "ejbPostC");
+            ejbPostCreateName.replace(0, 1, "ejbPostC");
             try {
                 bean.getMethod(ejbPostCreateName.toString(), create.getParameterTypes());
             } catch (NoSuchMethodException e) {
@@ -234,31 +230,86 @@
         return result;
     }
 
-    public boolean check_unusedCreateMethods(RemoteBean b, Class bean, Class home) {
-        boolean result = true;
+    public void check_unusedCreateMethods(RemoteBean b) {
 
-        Method[] homeMethods = home.getMethods();
-        Method[] beanMethods = bean.getMethods();
+        Class home = null;
+        Class localHome = null;
+        Class bean = null;
+        try {
+            if (b.getLocalHome() != null) {
+                localHome = loadClass(b.getLocalHome());
+            }
+
+            if (b.getHome() != null) {
+                home = loadClass(b.getHome());
+            }
+
+            bean = loadClass(b.getEjbClass());
+        } catch (OpenEJBException e) {
+            return;
+        }
+
+        for (Method ejbCreate : bean.getMethods()) {
+
+            if (!ejbCreate.getName().startsWith("ejbCreate")) continue;
 
-        for (int i = 0; i < homeMethods.length; i++) {
-            if (!beanMethods[i].getName().startsWith("ejbCreate")) continue;
-            Method ejbCreate = beanMethods[i];
             StringBuilder create = new StringBuilder(ejbCreate.getName());
-            create.replace(0,4, "c");
+            create.replace(0, "ejbC".length(), "c");
+
+
+            boolean inLocalHome = false;
+            boolean inHome = false;
+
             try {
-                // TODO, don't just check the remote home interface, there is a local too
-                home.getMethod(create.toString(), ejbCreate.getParameterTypes());
+                if (localHome != null) {
+                    localHome.getMethod(create.toString(), ejbCreate.getParameterTypes());
+                    inLocalHome = true;
+                }
             } catch (NoSuchMethodException e) {
-                result = false;
+            }
 
-                String paramString = getParameters(ejbCreate);
+            try {
+                if (home != null) {
+                    home.getMethod(create.toString(), ejbCreate.getParameterTypes());
+                    inHome = true;
+                }
+            } catch (NoSuchMethodException e) {
+            }
 
-                warn(b, "unused.ejb.create", b.getEjbClass(), ejbCreate.getName(), create.toString(), paramString, home.getName());
+            if (!inLocalHome && !inHome){
+                String paramString = getParameters(ejbCreate);
 
+                warn(b, "unused.ejb.create", b.getEjbClass(), ejbCreate.getName(),  paramString, create.toString());
             }
         }
+    }
 
-        return result;
+    public void check_unusedPostCreateMethods(RemoteBean b) {
+
+        Class bean = null;
+        try {
+            bean = loadClass(b.getEjbClass());
+        } catch (OpenEJBException e) {
+            return;
+        }
+
+        for (Method postCreate : bean.getMethods()) {
+
+            if (!postCreate.getName().startsWith("ejbPostCreate")) continue;
+
+            StringBuilder ejbCreate = new StringBuilder(postCreate.getName());
+            ejbCreate.replace(0, "ejbPostCreate".length(), "ejbCreate");
+
+            try {
+                bean.getMethod(ejbCreate.toString(), postCreate.getParameterTypes());
+            } catch (NoSuchMethodException e) {
+
+                String paramString = getParameters(postCreate);
+
+                warn(b, "unused.ejbPostCreate", b.getEjbClass(), postCreate.getName(), paramString, ejbCreate.toString());
+
+            }
+        }
     }
 
 /// public void check_findMethods(){

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/CoreDeploymentInfo.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/CoreDeploymentInfo.java?rev=660768&r1=660767&r2=660768&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/CoreDeploymentInfo.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/CoreDeploymentInfo.java Tue May 27 18:02:43 2008
@@ -849,7 +849,9 @@
                     */
                     if (this.componentType == BeanType.BMP_ENTITY || this.componentType == BeanType.CMP_ENTITY) {
                         ejbCreateName.insert(3, "Post");
-                        Method postCreateMethod = beanClass.getMethod(ejbCreateName.toString(), method.getParameterTypes());
+                        Class clazz = beanClass;
+                        if (cmpImplClass != null) clazz = cmpImplClass;
+                        Method postCreateMethod = clazz.getMethod(ejbCreateName.toString(), method.getParameterTypes());
                         postCreateMethodMap.put(createMethod, postCreateMethod);
                     }
                     /*

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/cmp2/Cmp1Generator.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/cmp2/Cmp1Generator.java?rev=660768&r1=660767&r2=660768&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/cmp2/Cmp1Generator.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/cmp2/Cmp1Generator.java Tue May 27 18:02:43 2008
@@ -27,12 +27,15 @@
     private String beanClassName;
     private ClassWriter cw;
     private boolean unknownPk;
+    private final PostCreateGenerator postCreateGenerator;
 
     public Cmp1Generator(String cmpImplClass, Class beanClass) {
         beanClassName = Type.getInternalName(beanClass);
         implClassName = cmpImplClass.replace('.', '/');
 
         cw = new ClassWriter(true);
+
+        postCreateGenerator = new PostCreateGenerator(beanClass, cw);
     }
 
     public byte[] generate() {
@@ -47,6 +50,8 @@
 
         createConstructor();
 
+        postCreateGenerator.generate();
+        
         cw.visitEnd();
 
         return cw.toByteArray();

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/cmp2/Cmp2Generator.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/cmp2/Cmp2Generator.java?rev=660768&r1=660767&r2=660768&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/cmp2/Cmp2Generator.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/cmp2/Cmp2Generator.java Tue May 27 18:02:43 2008
@@ -56,6 +56,7 @@
     private final Class primKeyClass;
     private final List<Method> selectMethods = new ArrayList<Method>();
     private final Class beanClass;
+    private final PostCreateGenerator postCreateGenerator;
 
     public Cmp2Generator(String cmpImplClass, Class beanClass, String pkField, Class<?> primKeyClass, String[] cmpFields) {
         if (pkField == null && primKeyClass == null) throw new NullPointerException("Both pkField and primKeyClass are null");
@@ -93,6 +94,8 @@
         this.beanClass = beanClass;
 
         cw = new ClassWriter(true);
+
+        postCreateGenerator = new PostCreateGenerator(beanClass, cw);
     }
 
     public void addCmrField(CmrField cmrField) {
@@ -176,6 +179,8 @@
         if (!hasMethod(beanClass, "setEntityContext", EntityContext.class)) createSetEntityContext();
         if (!hasMethod(beanClass, "unsetEntityContext")) createUnsetEntityContext();
 
+        postCreateGenerator.generate();
+
         cw.visitEnd();
 
         return cw.toByteArray();
@@ -855,14 +860,6 @@
 
     }
 
-//    public void createEjbPostCreate() {
-//        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "ejbPostCreate", "(Ljava/lang/String;Ljava/lang/String;I)V", null, null);
-//        mv.visitCode();
-//        mv.visitInsn(RETURN);
-//        mv.visitMaxs(0, 4);
-//        mv.visitEnd();
-//    }
-
     public void createEjbActivate() {
         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "ejbActivate", "()V", null, null);
         mv.visitCode();

Added: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/cmp2/PostCreateGenerator.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/cmp2/PostCreateGenerator.java?rev=660768&view=auto
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/cmp2/PostCreateGenerator.java (added)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/cmp2/PostCreateGenerator.java Tue May 27 18:02:43 2008
@@ -0,0 +1,70 @@
+/**
+ * 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.openejb.core.cmp.cmp2;
+
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.Type;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Opcodes;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class PostCreateGenerator {
+    private final Class beanClass;
+    private final ClassWriter cw;
+
+    public PostCreateGenerator(Class beanClass, ClassWriter cw) {
+        this.beanClass = beanClass;
+        this.cw = cw;
+    }
+
+    public void generate() {
+        for (Method ejbCreate : beanClass.getMethods()) {
+
+            if (!ejbCreate.getName().startsWith("ejbCreate")) continue;
+
+            StringBuilder ejbPostCreateName = new StringBuilder(ejbCreate.getName());
+            ejbPostCreateName.replace(0, "ejbC".length(), "ejbPostC");
+
+            if (hasMethod(beanClass, ejbPostCreateName.toString(), ejbCreate.getParameterTypes())) continue;
+
+            createEjbPostCreate(ejbPostCreateName.toString(), ejbCreate);
+        }
+    }
+
+    private boolean hasMethod(Class beanClass, String name, Class... args) {
+        try {
+            Method method = beanClass.getMethod(name, args);
+            return !Modifier.isAbstract(method.getModifiers());
+        } catch (NoSuchMethodException e) {
+            return false;
+        }
+    }
+
+    public void createEjbPostCreate(String ejbPostCreateName, Method ejbCreate) {
+        String methodDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE, Type.getArgumentTypes(ejbCreate));
+        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, ejbPostCreateName, methodDescriptor, null, null);
+        mv.visitCode();
+        mv.visitInsn(Opcodes.RETURN);
+        mv.visitMaxs(0, ejbCreate.getParameterTypes().length + 1);
+        mv.visitEnd();
+    }
+}

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties?rev=660768&r1=660767&r2=660768&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/resources/org/apache/openejb/config/rules/Messages.properties Tue May 27 18:02:43 2008
@@ -92,12 +92,15 @@
 2.no.ejb.post.create      Missing create method: {1}({2})
 3.no.ejb.post.create      Entity create method with no matching ejbPostCreate.  There should be an ejbPostCreate method in the bean class {0} with the following signature:\n\n\tpublic void {1}({2}) throws javax.ejb.CreateException
 
-# 1 - bean class
-# 2 - create params
-# 3 - home interface
+# warn(b, "unused.ejb.create", b.getEjbClass(), ejbCreate.getName(), paramString, create.toString());
 1.unused.ejb.create       Unused ejbCreate method
-2.unused.ejb.create       Unused ejbCreate method: {2}({3})
-3.unused.ejb.create       Create method will never be called.  The bean class {0} defines the create method {1}({3}), but there is no matching {2}({3}) method in the home interface {2}
+2.unused.ejb.create       Unused ejbCreate method: {1}({2})
+3.unused.ejb.create       Create method will never be called.  The bean class {0} defines the create method {1}({2}), but there is no matching {3}({2}) method in the home or local-home interfaces.
+
+# warn(b, "unused.ejbPostCreate", b.getEjbClass(), postCreate.getName(), paramString, ejbCreate.toString());
+1.unused.ejbPostCreate       Unused ejbPostCreate method
+2.unused.ejbPostCreate       Unused ejbPostCreate method: {1}({2})
+3.unused.ejbPostCreate       PostCreate method will never be called.  The bean class {0} defines the create method {1}({2}), but there is no matching {3}({2}) method defined in the bean class.
 
 
 # 0 - Class name