[jboss-svn-commits] JBL Code SVN: r8332 - labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Dec 14 17:03:01 EST 2006


Author: tirelli
Date: 2006-12-14 17:02:58 -0500 (Thu, 14 Dec 2006)
New Revision: 8332

Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ShadowProxyFactory.java
Log:
JBRULES-575: filtering overriden methods based on the return type

Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ShadowProxyFactory.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ShadowProxyFactory.java	2006-12-14 20:28:13 UTC (rev 8331)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ShadowProxyFactory.java	2006-12-14 22:02:58 UTC (rev 8332)
@@ -18,8 +18,10 @@
 
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import org.drools.RuntimeDroolsException;
@@ -128,7 +130,7 @@
                               cw );
 
         final Map fieldTypes = new HashMap();
-        final Method[] methods = clazz.getMethods();
+        final Method[] methods = getMethods( clazz );
         for ( int i = 0; i < methods.length; i++ ) {
             if ( (!Modifier.isFinal( methods[i].getModifiers() )) && Modifier.isPublic( methods[i].getModifiers() ) ) {
                 if ( (!methods[i].getReturnType().equals( Void.TYPE )) && (methods[i].getParameterTypes().length == 0) && (!methods[i].getName().equals( "hashCode" )) && (!methods[i].getName().equals( "toString" )) ) {
@@ -182,6 +184,72 @@
     }
 
     /**
+     * Filter out any method we are not interested in
+     * @param clazz
+     * @return
+     */
+    private static Method[] getMethods(final Class clazz) {
+        // to help filtering process, we will create a map of maps:
+        // Map< String methodName, Map< Class[] parameterTypes, Method method > >
+        Map map = new HashMap();
+        List helperList = new ArrayList();
+        final Method[] methods = clazz.getMethods();
+        for( int i = 0; i < methods.length; i++ ) {
+            Method previous = null; 
+            Map signatures = (Map) map.get( methods[i].getName() );
+            ParametersWrapper key = new ParametersWrapper( methods[i].getParameterTypes() );
+            if( signatures != null ) {
+                previous = (Method) signatures.get( key );
+            }
+            // if no previous method with the same name and parameter types is found
+            // or if the previous method's return type is a super class of the 
+            // current method's return type, add current to the map
+            // overriding previous if it exists
+            if( ( previous == null ) ||
+                ( previous.getReturnType().isAssignableFrom( methods[i].getReturnType() ) ) ) {
+                if( signatures == null ) {
+                    signatures = new HashMap();
+                    map.put( methods[i].getName(), signatures );
+                }
+                if( signatures.put( key, methods[i] ) != null ) {
+                    helperList.remove( previous );
+                }
+                helperList.add( methods[i] );
+            }
+        }
+        return (Method[]) helperList.toArray( new Method[helperList.size()] );
+    }
+    
+    private static class ParametersWrapper {
+        private Class[] parameters;
+        public ParametersWrapper( Class[] parameters ) {
+            this.parameters = parameters;
+        }
+        
+        public int hashCode() {
+            return this.parameters.length;
+        }
+        
+        public boolean equals( Object o ) {
+            if( !( o instanceof ParametersWrapper ) ) {
+                return false;
+            }
+            ParametersWrapper other = (ParametersWrapper) o;
+            
+            if( this.parameters.length != other.parameters.length ) {
+                return false;
+            }
+            
+            for( int i = 0; i < this.parameters.length; i++ ) {
+                if( ! this.parameters[i].equals( other.parameters[i] )) {
+                    return false;
+                }
+            }
+            return true;
+        }
+    }
+
+    /**
      * Builds the shadow proxy class header
      *  
      * @param clazz The class to build shadow proxy for




More information about the jboss-svn-commits mailing list