commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bali...@apache.org
Subject cvs commit: jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/tools Enhancer.java
Date Mon, 04 Mar 2002 15:42:18 GMT
baliuka     02/03/04 07:42:18

  Modified:    simplestore/src/java/org/apache/commons/simplestore/tools
                        Enhancer.java
  Log:
  Added Byte Code generation for interface and abstract methods
  
  Revision  Changes    Path
  1.5       +76 -24    jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/tools/Enhancer.java
  
  Index: Enhancer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/tools/Enhancer.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Enhancer.java	4 Mar 2002 12:38:17 -0000	1.4
  +++ Enhancer.java	4 Mar 2002 15:42:18 -0000	1.5
  @@ -63,7 +63,7 @@
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: Enhancer.java,v 1.4 2002/03/04 12:38:17 baliuka Exp $
  + *@version    $Id: Enhancer.java,v 1.5 2002/03/04 15:42:18 baliuka Exp $
    */
   public class Enhancer implements org.apache.bcel.Constants{
       
  @@ -107,11 +107,11 @@
       private Enhancer() {
       }
       
  -    public static Object enhance(Class cls , MethodInterceptor ih) throws Throwable{
  +    public static Object enhance(Class cls,Class interfaces [] , MethodInterceptor ih)
throws Throwable{
           
  -        return enhance(cls,ih,Thread.currentThread().getContextClassLoader());
  +        return enhance(cls,interfaces,ih,Thread.currentThread().getContextClassLoader());
       }
  -    public synchronized static Object enhance(Class cls, MethodInterceptor ih,ClassLoader
loader)throws Throwable {
  +    public synchronized static Object enhance(Class cls,Class interfaces[],  MethodInterceptor
ih,ClassLoader loader)throws Throwable {
           
           java.util.Map map = (java.util.Map)cache.get(loader);
           
  @@ -125,8 +125,8 @@
           Class result = (Class)map.get(cls);
           
           if(result == null){
  -            
  -            JavaClass clazz = enhance(cls);
  +            java.util.HashMap methods = new java.util.HashMap();
  +            JavaClass clazz = enhance(cls, interfaces , methods);
               
               byte b [] = clazz.getBytes();
               java.lang.reflect.Method m =
  @@ -139,20 +139,14 @@
               result = (Class)m.invoke(loader,new Object[]{clazz.getClassName(),b,new Integer(0),new
Integer(b.length)});
               m.setAccessible(flag);
               
  -            map.put(cls,result);
               
  -        }
  -        java.lang.reflect.Method methods [] = result.getMethods();
  -        for( int i = 0; i < methods.length; i++ ){
  -            try{
  -                result.getField("mtd_" + methods[i].getName() + "$" +
  -                methods[i].getParameterTypes().length
  -                ).set(null,methods[i]);
  -            }catch( java.lang.NoSuchFieldException nsfe ){
  +            for( java.util.Iterator i =  methods.keySet().iterator(); i.hasNext();  ){
  +                String name = (String)i.next();
  +                result.getField( name ).set(null,methods.get(name));
  +                
               }
  +            map.put(cls,result);
           }
  -        
  -        
           return result.getConstructor(new Class[]{ MethodInterceptor.class} ).
           newInstance(new Object[]{ih});
           
  @@ -160,6 +154,9 @@
           
       }
       
  +    
  +    
  +    
       private static void addConstructor(ClassGen  cg ){
           
           String parentClass = cg.getSuperclassName();
  @@ -200,7 +197,7 @@
           SOURCE_FILE, ACC_PUBLIC , null );
       }
       
  -    private static JavaClass enhance( Class parentClass )  {
  +    private static JavaClass enhance( Class parentClass,Class interfaces [],java.util.HashMap
methodTable )  {
           
           String class_name = parentClass.getName() + ENHAVCED_CLASS_SUFIX;
           ClassGen  cg = getClassGen(class_name,parentClass);
  @@ -210,13 +207,31 @@
           int before =  addBeforeRef(cp);
           int after  =  addAfterRef(cp);
           int invokeSuper  = addInvokeSupperRef(cp);
  -        java.lang.reflect.Method  methods[] = parentClass.getMethods();
  -        for( int i = 0 ; i < methods.length; i++ ){
  -            if( ! java.lang.reflect.Modifier.isStatic(methods[i].getModifiers())  ){
  -                cg.addMethod( generateMethod( methods[i], cg,
  -                before, after, invokeSuper ) );
  +        java.util.Set methodSet = new java.util.HashSet();
  +        
  +        for(int j = 0; j <= (interfaces == null ? 0 : interfaces.length); j++   ) {
  +            java.lang.reflect.Method  methods[];
  +            if( j == 0 ){
  +                methods = parentClass.getMethods();
  +            }else{
  +                methods = interfaces[j - 1].getMethods();
  +            }
  +            for( int i = 0 ; i < methods.length; i++ ){
  +                if( ! java.lang.reflect.Modifier.isStatic(methods[i].getModifiers())  ){
  +                    methodSet.add(methods[i]);
  +                    
  +                }
               }
           }
  +        int cntr = 0;
  +        for( java.util.Iterator i = methodSet.iterator(); i.hasNext();  ){
  +            java.lang.reflect.Method method  = (java.lang.reflect.Method)i.next();
  +            String fieldName = "METHOD_" + (cntr++);
  +            cg.addMethod( generateMethod( method,fieldName,cg, before,after,invokeSuper
) );
  +            methodTable.put(fieldName,method);
  +        }
  +        
  +        
           
           JavaClass jcl = cg.getJavaClass();
           return  jcl;
  @@ -507,6 +522,7 @@
       }
       
       private static  Method  generateMethod( java.lang.reflect.Method method,
  +    String fieldName,
       ClassGen cg,int before,
       int after, int invokeSuper){
           
  @@ -517,7 +533,6 @@
           Type types[] = mg.getArgumentTypes();
           int argCount = types.length;
           
  -        String fieldName = "mtd_" + method.getName() + "$" + argCount;
           addMethodField( fieldName, cg );
           
           boolean returnsValue = !mg.getReturnType().equals( Type.VOID );
  @@ -627,5 +642,42 @@
       }
       
       
  +    static class MethodWrapper{
  +        java.lang.reflect.Method method;
  +        MethodWrapper(java.lang.reflect.Method method){
  +            if( method == null ){
  +                throw new NullPointerException();
  +            }
  +            this.method = method;
  +        }
  +        public  boolean equals(Object obj){
  +            if(obj == null || !( obj instanceof java.lang.reflect.Method) ){
  +                return false;
  +            }
  +            return equalsForEnhancer(method,(java.lang.reflect.Method)obj);
  +        }
  +        public  int hashCode(){
  +            return method.hashCode();
  +        }
  +    }
  +    static  boolean equalsForEnhancer(java.lang.reflect.Method m1,java.lang.reflect.Method
m2 ) {
  +        if( m1 == m2 ){
  +            return true;
  +        }
  +        if( m1.getName().equals(m2.getName()) ){
  +            Class[] params1 = m1.getParameterTypes();
  +            Class[] params2 = m2.getParameterTypes();
  +            if (params1.length == params2.length) {
  +                for (int i = 0; i < params1.length; i++) {
  +                    if ( params1[i] != params2[i] ){
  +                        return false;
  +                    }
  +                }
  +                return true;
  +            }
  +        }
  +        
  +        return false;
  +    }
       
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message