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

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Oct 2 12:54:27 EDT 2006


Author: tirelli
Date: 2006-10-02 12:54:15 -0400 (Mon, 02 Oct 2006)
New Revision: 6525

Added:
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/CheeseInterface.java
Modified:
   labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ShadowProxyFactory.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/Cheese.java
   labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/ShadowProxyFactoryTest.java
Log:
JBRULES-44:

  * Adding support to create proxies for interfaces
  * Unit test updated



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-10-02 16:39:02 UTC (rev 6524)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ShadowProxyFactory.java	2006-10-02 16:54:15 UTC (rev 6525)
@@ -119,12 +119,21 @@
     protected static void buildClassHeader(final Class clazz,
                                            final String className,
                                            final ClassWriter cw) {
-        cw.visit( Opcodes.V1_2,
-                  Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER,
-                  className,
-                  null,
-                  Type.getInternalName( clazz ),
-                  new String[]{BASE_INTERFACE} );
+        if( clazz.isInterface() ) {
+            cw.visit( Opcodes.V1_2,
+                      Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER,
+                      className,
+                      null,
+                      Type.getInternalName( Object.class ),
+                      new String[]{BASE_INTERFACE, Type.getInternalName( clazz )} );
+        } else {
+            cw.visit( Opcodes.V1_2,
+                      Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER,
+                      className,
+                      null,
+                      Type.getInternalName( clazz ),
+                      new String[]{BASE_INTERFACE} );
+        }
 
         cw.visitSource( null,
                         null );
@@ -176,11 +185,20 @@
                                 l0 );
             mv.visitVarInsn( Opcodes.ALOAD,
                              0 );
-            mv.visitMethodInsn( Opcodes.INVOKESPECIAL,
-                                Type.getInternalName( clazz ),
-                                "<init>",
-                                Type.getMethodDescriptor( Type.VOID_TYPE,
-                                                          new Type[]{} ) );
+            if(clazz.isInterface()) {
+                mv.visitMethodInsn( Opcodes.INVOKESPECIAL,
+                                    Type.getInternalName( Object.class ),
+                                    "<init>",
+                                    Type.getMethodDescriptor( Type.VOID_TYPE,
+                                                              new Type[]{} ) );
+            } else {
+                mv.visitMethodInsn( Opcodes.INVOKESPECIAL,
+                                    Type.getInternalName( clazz ),
+                                    "<init>",
+                                    Type.getMethodDescriptor( Type.VOID_TYPE,
+                                                              new Type[]{} ) );
+            }
+            
             // this.delegate = delegate
             Label l1 = new Label();
             mv.visitLabel( l1 );
@@ -274,10 +292,17 @@
                            className,
                            DELEGATE_FIELD_NAME,
                            Type.getDescriptor( clazz ) );
-        mv.visitMethodInsn( Opcodes.INVOKEVIRTUAL,
-                            Type.getInternalName( clazz ),
-                            method.getName(),
-                            Type.getMethodDescriptor( method ) );
+        if(clazz.isInterface()) {
+            mv.visitMethodInsn( Opcodes.INVOKEINTERFACE,
+                                Type.getInternalName( clazz ),
+                                method.getName(),
+                                Type.getMethodDescriptor( method ) );
+        } else {
+            mv.visitMethodInsn( Opcodes.INVOKEVIRTUAL,
+                                Type.getInternalName( clazz ),
+                                method.getName(),
+                                Type.getMethodDescriptor( method ) );
+        }
         mv.visitFieldInsn( Opcodes.PUTFIELD,
                            className,
                            fieldName,

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/Cheese.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/Cheese.java	2006-10-02 16:39:02 UTC (rev 6524)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/Cheese.java	2006-10-02 16:54:15 UTC (rev 6525)
@@ -20,7 +20,7 @@
 import java.beans.Introspector;
 import java.beans.PropertyDescriptor;
 
-public class Cheese {
+public class Cheese implements CheeseInterface {
     private String type;
 
     private int    price;
@@ -34,23 +34,29 @@
         this.price = price;
     }
 
+    /* (non-Javadoc)
+     * @see org.drools.CheeseInterface#getType()
+     */
     public String getType() {
         return this.type;
     }
 
+    /* (non-Javadoc)
+     * @see org.drools.CheeseInterface#getPrice()
+     */
     public int getPrice() {
         return this.price;
     }
 
-    /**
-     * @param price the price to set
+    /* (non-Javadoc)
+     * @see org.drools.CheeseInterface#setPrice(int)
      */
     public void setPrice(final int price) {
         this.price = price;
     }
 
-    /**
-     * @param type the type to set
+    /* (non-Javadoc)
+     * @see org.drools.CheeseInterface#setType(java.lang.String)
      */
     public void setType(final String type) {
         this.type = type;

Added: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/CheeseInterface.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/CheeseInterface.java	2006-10-02 16:39:02 UTC (rev 6524)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/CheeseInterface.java	2006-10-02 16:54:15 UTC (rev 6525)
@@ -0,0 +1,19 @@
+package org.drools;
+
+public interface CheeseInterface {
+
+    public String getType();
+
+    public int getPrice();
+
+    /**
+     * @param price the price to set
+     */
+    public void setPrice(final int price);
+
+    /**
+     * @param type the type to set
+     */
+    public void setType(final String type);
+
+}
\ No newline at end of file


Property changes on: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/CheeseInterface.java
___________________________________________________________________
Name: svn:executable
   + *
Name: svn:keywords
   + id author date revision
Name: svn:eol-style
   + native

Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/ShadowProxyFactoryTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/ShadowProxyFactoryTest.java	2006-10-02 16:39:02 UTC (rev 6524)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/ShadowProxyFactoryTest.java	2006-10-02 16:54:15 UTC (rev 6525)
@@ -4,6 +4,7 @@
 import junit.framework.TestCase;
 
 import org.drools.Cheese;
+import org.drools.CheeseInterface;
 
 public class ShadowProxyFactoryTest extends TestCase {
 
@@ -15,7 +16,7 @@
         super.tearDown();
     }
 
-    public void testGetProxy() {
+    public void testProxyForClass() {
         try {
             // creating original object
             String originalType = "stilton";
@@ -59,4 +60,50 @@
             fail("Error: "+e.getMessage());
         }
     }
+    
+    public void testProxyForInterface() {
+        try {
+            // creating original object
+            String originalType = "stilton";
+            int originalPrice = 15;
+            Cheese cheese = new Cheese(originalType, originalPrice);
+            
+            // creating proxy
+            Class proxy = ShadowProxyFactory.getProxy( CheeseInterface.class );
+            CheeseInterface cheeseProxy = (CheeseInterface) proxy.getConstructor( new Class[] { CheeseInterface.class } ).newInstance( new Object[] { cheese } );
+
+            // proxy is proxying the values
+            Assert.assertEquals( originalType, cheeseProxy.getType() );
+            Assert.assertEquals( originalPrice, cheeseProxy.getPrice() );
+            
+            // changing original values
+            String actualType = "rotten stilton";
+            int actualPrice = 1;
+            cheese.setType( actualType );
+            cheese.setPrice( actualPrice );
+            
+            // proxy does not see changes
+            Assert.assertEquals( actualType, cheese.getType() );
+            Assert.assertFalse( actualType.equals( cheeseProxy.getType() ) );
+            Assert.assertEquals( originalType, cheeseProxy.getType() );
+            Assert.assertEquals( actualPrice, cheese.getPrice() );
+            Assert.assertFalse( actualPrice == cheeseProxy.getPrice() );
+            Assert.assertEquals( originalPrice, cheeseProxy.getPrice() );
+            
+            // reseting proxy
+            ((ShadowProxy) cheeseProxy).resetProxy();
+            
+            // now proxy see changes
+            Assert.assertEquals( actualType, cheese.getType() );
+            Assert.assertEquals( actualType, cheeseProxy.getType() );
+            Assert.assertFalse( originalType.equals( cheeseProxy.getType() ) );
+            Assert.assertEquals( actualPrice, cheese.getPrice() );
+            Assert.assertEquals( actualPrice, cheeseProxy.getPrice() );
+            Assert.assertFalse( originalPrice == cheeseProxy.getPrice() );
+            
+        } catch ( Exception e ) {
+            fail("Error: "+e.getMessage());
+        }
+    }
+    
 }




More information about the jboss-svn-commits mailing list