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/test/org/apache/commons/simplestore TestEnhancer.java
Date Tue, 05 Mar 2002 20:03:49 GMT
baliuka     02/03/05 12:03:49

  Modified:    simplestore/src/java/org/apache/commons/simplestore/tools
                        Enhancer.java
               simplestore/src/test/org/apache/commons/simplestore
                        TestEnhancer.java
  Log:
  Added class_name parameter to enhance method for
  ARMI ProxyGenerator implementation
  
  Revision  Changes    Path
  1.9       +35 -15    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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Enhancer.java	4 Mar 2002 22:51:16 -0000	1.8
  +++ Enhancer.java	5 Mar 2002 20:03:49 -0000	1.9
  @@ -63,7 +63,7 @@
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: Enhancer.java,v 1.8 2002/03/04 22:51:16 baliuka Exp $
  + *@version    $Id: Enhancer.java,v 1.9 2002/03/05 20:03:49 baliuka Exp $
    */
   public class Enhancer implements org.apache.bcel.Constants{
       
  @@ -110,9 +110,37 @@
       
       public static Object enhance(Class cls,Class interfaces [] , MethodInterceptor ih)
throws Throwable{
           
  -        return enhance(cls,interfaces,ih,Thread.currentThread().getContextClassLoader());
  +        return enhance(cls,null,interfaces,ih,Thread.currentThread().getContextClassLoader());
       }
  -    public synchronized static Object enhance(Class cls,Class interfaces[],  MethodInterceptor
ih,ClassLoader loader)throws Throwable {
  +    
  +    static class Handler implements java.lang.reflect.InvocationHandler{
  +        MethodInterceptor ih;
  +        Handler(MethodInterceptor ih){
  +            this.ih = ih;
  +        }
  +        
  +        public Object invoke(Object obj, java.lang.reflect.Method method, Object[] args)
throws java.lang.Throwable {
  +            return ih.afterReturn( obj, method, args, null, false, null, null );
  +        }
  +        
  +    }
  +    
  +    public synchronized static Object enhance(Class cls,String class_name,Class interfaces[],
 MethodInterceptor ih,ClassLoader loader)throws Throwable {
  +        // Use Standard way if we don't need to enhance any Class
  +        if( ( Object.class.equals(cls) || cls == null ) && class_name == null ){
  +            return java.lang.reflect.Proxy.newProxyInstance(loader,interfaces, new Handler(ih)
);
  +        }
  +        
  +        if( cls == null ){
  +            cls = Object.class;
  +        }
  +        
  +        if( class_name == null ){
  +            class_name = cls.getName() + ENHAVCED_CLASS_SUFIX;
  +            if(class_name.startsWith("java")){
  +                class_name =  ENHAVCED_CLASS_PREFIX + class_name;
  +            }
  +        }
           
           java.util.Map map = (java.util.Map)cache.get(loader);
           
  @@ -122,13 +150,11 @@
               
           }
           
  -        
           Class result = (Class)map.get(cls);
           
           if(result == null){
               java.util.HashMap methods = new java.util.HashMap();
  -            JavaClass clazz = enhance(cls, interfaces , methods);
  -            
  +            JavaClass clazz = enhance(cls, class_name, interfaces , methods);
               byte b [] = clazz.getBytes();
               java.lang.reflect.Method m =
               ClassLoader.class.getDeclaredMethod("defineClass",
  @@ -139,7 +165,6 @@
               m.setAccessible(true);
               result = (Class)m.invoke(loader,new Object[]{clazz.getClassName(),b,new Integer(0),new
Integer(b.length)});
               m.setAccessible(flag);
  -            
               for( java.util.Iterator i =  methods.keySet().iterator(); i.hasNext();  ){
                   String name = (String)i.next();
                   result.getField( name ).set(null,methods.get(name));
  @@ -157,7 +182,6 @@
           String parentClass = cg.getSuperclassName();
           InstructionFactory factory = new InstructionFactory(cg);
           ConstantPoolGen cp = cg.getConstantPool(); // cg creates constant pool
  -        
           InstructionList il = new InstructionList();
           MethodGen costructor  = new MethodGen(ACC_PUBLIC,// access flags
           Type.VOID,// return type
  @@ -198,12 +222,8 @@
           return gen;
       }
       
  -    private static JavaClass enhance( Class parentClass,Class interfaces [],java.util.HashMap
methodTable )  {
  +    private static JavaClass enhance( Class parentClass,String class_name,Class interfaces
[],java.util.HashMap methodTable )  {
           
  -        String class_name = parentClass.getName() + ENHAVCED_CLASS_SUFIX;
  -        if(class_name.startsWith("java")){
  -            class_name =  ENHAVCED_CLASS_PREFIX + class_name;
  -        }
           ClassGen  cg = getClassGen(class_name,parentClass,interfaces);
           ConstantPoolGen cp = cg.getConstantPool(); // cg creates constant pool
           addHandlerField(cg);
  @@ -226,14 +246,14 @@
                   !java.lang.reflect.Modifier.isFinal(mod) &&
                   ( java.lang.reflect.Modifier.isPublic(mod)||
                   java.lang.reflect.Modifier.isProtected(mod)) ){
  -                    methodSet.add(methods[i]);
  +                    methodSet.add( new  MethodWrapper( 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();
  +            java.lang.reflect.Method method  = ((MethodWrapper)i.next()).method;
               String fieldName = "METHOD_" + (cntr++);
               cg.addMethod( generateMethod( method,fieldName,cg, before,after,invokeSuper
) );
               methodTable.put(fieldName,method);
  
  
  
  1.3       +32 -13    jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore/TestEnhancer.java
  
  Index: TestEnhancer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/test/org/apache/commons/simplestore/TestEnhancer.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestEnhancer.java	4 Mar 2002 22:11:40 -0000	1.2
  +++ TestEnhancer.java	5 Mar 2002 20:03:49 -0000	1.3
  @@ -64,7 +64,7 @@
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: TestEnhancer.java,v 1.2 2002/03/04 22:11:40 baliuka Exp $
  + *@version    $Id: TestEnhancer.java,v 1.3 2002/03/05 20:03:49 baliuka Exp $
    */
   public class TestEnhancer  extends TestCase {
       
  @@ -82,6 +82,17 @@
           junit.textui.TestRunner.main(testCaseName);
       }
       
  +    private static void printArgs(Object args[]){
  +        System.err.print("  args:");
  +        if( args == null ){
  +           System.err.print( args);
  +        }else{
  +         for ( int i = 0; i < args.length; i++ ){
  +           System.err.print( " [ " + args[i] + " ] ");
  +         }
  +        }
  +      System.err.println();
  +    }
       public void testEnhance()throws Throwable{
           //test enchance vector:
           java.util.Vector vector = (java.util.Vector)Enhancer.enhance(
  @@ -94,7 +105,8 @@
               public Object beforeInvoke( Object obj,java.lang.reflect.Method method,
               Object args[] )
               throws java.lang.Throwable{
  -               System.err.println("beforeInvoke:" + method);
  +               System.err.print("beforeInvoke:" + method);
  +               printArgs(args);
                return null;
               }
               
  @@ -102,8 +114,9 @@
               Object args[], Object retValFromBefore )
               throws java.lang.Throwable{
                  
  -                System.err.println("invokeSuper:" + method);
  -                
  +                System.err.print("invokeSuper:" + method);
  +                printArgs(args); 
  +               if( "remove".equals(method.getName()) ) return false;//owerride size 
                return true;//let super to call this
               }
               
  @@ -111,24 +124,30 @@
               Object args[],  Object retValFromBefore,
               boolean invokedSuper, Object retValFromSuper,
               java.lang.Throwable e )throws java.lang.Throwable{
  -                
  -               System.err.println("afterReturn:" + method + " returned:" + retValFromSuper);
  -               
  +                if( e != null ){
  +                  System.err.println("Cauth Exeption from super " + obj.getClass().getSuperclass().getName());
 
  +                  e.printStackTrace();
  +                  System.err.println();
  +                }
  +               System.err.print("afterReturn:" + method + " returned:" + retValFromSuper);
  +               printArgs(args);
  +              if( !invokedSuper )return new Boolean(false); //it was "remove"
                 return retValFromSuper;//return the same as supper
               }
               
           });
         //TODO : Add meanigful asserts  
  +      String value = "VALUE";  
         vector.add(null);
         vector.elements();  
         vector.size();  
         vector.add(this);
  -      vector.add("VALUE");
  -      vector.add(vector);
  -      vector.remove("VALUE");  
  -      vector.remove(vector);  
  -      vector.contains("VALUE");
  -      
  +      vector.add(value);
  +      vector.remove(value);  
  +      vector.remove(value);  
  +      vector.contains(value);
  +      vector.get(vector.indexOf("NOTHING"));//must catch exeption in afterReturn
  +      System.err.println("Enchanced Vector " + vector);
       }
         
   }
  
  
  

--
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