[jboss-svn-commits] JBL Code SVN: r7125 - in labs/jbossrules/trunk/drools-core/src: main/java/org/drools/base test/java/org/drools/base test/java/org/drools/rule test/java/org/drools/spi
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Oct 25 20:48:56 EDT 2006
Author: tirelli
Date: 2006-10-25 20:48:46 -0400 (Wed, 25 Oct 2006)
New Revision: 7125
Removed:
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/spi/MockField.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/base/ShadowProxyFactoryTest.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/TestBean.java
labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/FieldConstraintTest.java
Log:
Fixing tests and dropping eager proxy generation code
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-26 00:23:51 UTC (rev 7124)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/base/ShadowProxyFactory.java 2006-10-26 00:48:46 UTC (rev 7125)
@@ -66,28 +66,6 @@
}
}
- public static Class getEagerProxy(final Class clazz) {
- try {
- String className = null;
- if ( clazz.getPackage().getName().startsWith( "java." ) || clazz.getPackage().getName().startsWith( "javax." ) ) {
- className = "org/drools/shadow/" + Type.getInternalName( clazz ) + "EagerShadowProxy";
- } else {
- className = Type.getInternalName( clazz ) + "EagerShadowProxy";
- }
- // generating byte array to create target class
- final byte[] bytes = dumpEager( clazz,
- className );
- // use bytes to get a class
- final ByteArrayClassLoader classLoader = new ByteArrayClassLoader( Thread.currentThread().getContextClassLoader() );
- final Class newClass = classLoader.defineClass( className.replace( '/',
- '.' ),
- bytes );
- return newClass;
- } catch ( final Exception e ) {
- throw new RuntimeDroolsException( e );
- }
- }
-
protected static byte[] dump(final Class clazz,
final String className) throws Exception {
@@ -114,9 +92,7 @@
final Method[] methods = clazz.getMethods();
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" )) ) {
+ if ( (!methods[i].getReturnType().equals( Void.TYPE )) && (methods[i].getParameterTypes().length == 0) && (!methods[i].getName().equals( "hashCode" )) && (!methods[i].getName().equals( "toString" )) ) {
final String fieldName = methods[i].getName();
@@ -136,8 +112,7 @@
className,
clazz,
cw );
- } else if ( (!methods[i].getName().equals( "hashCode" )) &&
- (!methods[i].getName().equals( "equals" ) ) ) {
+ } else if ( (!methods[i].getName().equals( "hashCode" )) && (!methods[i].getName().equals( "equals" )) ) {
buildDelegateMethod( methods[i],
clazz,
className,
@@ -160,71 +135,13 @@
fieldTypes );
buildHashCode( cw,
- className,
- clazz,
- fieldTypes );
+ className,
+ clazz,
+ fieldTypes );
return cw.toByteArray();
}
- protected static byte[] dumpEager(final Class clazz,
- final String className) throws Exception {
-
- final ClassWriter cw = new ClassWriter( true );
-
- buildClassHeader( clazz,
- className,
- cw );
-
- buildField( ShadowProxyFactory.DELEGATE_FIELD_NAME,
- Type.getDescriptor( clazz ),
- cw );
-
- final Map fieldTypes = new HashMap();
- final Map fieldMethods = new HashMap();
-
- final Method[] methods = clazz.getMethods();
- 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().startsWith( "get" )) || (methods[i].getName().startsWith( "is" ))) ) {
- final String fieldName = methods[i].getName();
-
- buildField( /*FIELD_NAME_PREFIX +*/fieldName,
- Type.getDescriptor( methods[i].getReturnType() ),
- cw );
- fieldMethods.put( /*FIELD_NAME_PREFIX +*/fieldName,
- methods[i] );
- fieldTypes.put( /*FIELD_NAME_PREFIX +*/fieldName,
- methods[i].getReturnType() );
-
- buildSimpleGetMethod( /*FIELD_NAME_PREFIX +*/fieldName,
- methods[i].getReturnType(),
- methods[i],
- className,
- clazz,
- cw );
- } else {
- buildDelegateMethod( methods[i],
- clazz,
- className,
- cw );
- }
- }
- }
-
- buildEagerConstructor( clazz,
- className,
- cw );
-
- buildEagerUpdateProxyMethod( clazz,
- fieldTypes,
- fieldMethods,
- className,
- cw );
-
- return cw.toByteArray();
- }
-
/**
* Builds the shadow proxy class header
*
@@ -357,98 +274,6 @@
}
/**
- * Creates a constructor for the shadow proxy receiving
- * the actual delegate class as parameter
- *
- * @param originalClassName
- * @param className
- * @param cw
- */
- private static void buildEagerConstructor(final Class clazz,
- final String className,
- final ClassWriter cw) {
- MethodVisitor mv;
- {
- mv = cw.visitMethod( Opcodes.ACC_PUBLIC,
- "<init>",
- Type.getMethodDescriptor( Type.VOID_TYPE,
- new Type[]{Type.getType( clazz )} ),
- null,
- null );
- mv.visitCode();
-
- // super();
- final Label l0 = new Label();
- mv.visitLabel( l0 );
- mv.visitLineNumber( 41,
- l0 );
- mv.visitVarInsn( Opcodes.ALOAD,
- 0 );
- 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
- final Label l1 = new Label();
- mv.visitLabel( l1 );
- mv.visitVarInsn( Opcodes.ALOAD,
- 0 );
- mv.visitVarInsn( Opcodes.ALOAD,
- 1 );
- mv.visitFieldInsn( Opcodes.PUTFIELD,
- className,
- ShadowProxyFactory.DELEGATE_FIELD_NAME,
- Type.getDescriptor( clazz ) );
-
- // this.updateProxy();
- final Label l4 = new Label();
- mv.visitLabel( l4 );
- mv.visitVarInsn( Opcodes.ALOAD,
- 0 );
- mv.visitMethodInsn( Opcodes.INVOKEVIRTUAL,
- className,
- ShadowProxyFactory.UPDATE_PROXY,
- Type.getMethodDescriptor( Type.VOID_TYPE,
- new Type[]{} ) );
-
- // return
- final Label l2 = new Label();
- mv.visitLabel( l2 );
- mv.visitLineNumber( 43,
- l2 );
- mv.visitInsn( Opcodes.RETURN );
-
- final Label l3 = new Label();
- mv.visitLabel( l3 );
- mv.visitLocalVariable( "this",
- "L" + className + ";",
- null,
- l0,
- l3,
- 0 );
- mv.visitLocalVariable( ShadowProxyFactory.DELEGATE_FIELD_NAME,
- Type.getDescriptor( clazz ),
- null,
- l0,
- l3,
- 1 );
- mv.visitMaxs( 0,
- 0 );
- mv.visitEnd();
- }
- }
-
- /**
* Creates the proxy reader method for the given method
*
* @param fieldName
@@ -599,70 +424,6 @@
mv.visitEnd();
}
- protected static void buildEagerUpdateProxyMethod(final Class delegate,
- final Map fieldTypes,
- final Map fieldMethods,
- final String className,
- final ClassWriter cw) {
- final MethodVisitor mv = cw.visitMethod( Opcodes.ACC_PUBLIC,
- ShadowProxyFactory.UPDATE_PROXY,
- Type.getMethodDescriptor( Type.VOID_TYPE,
- new Type[]{} ),
- null,
- null );
- mv.visitCode();
- final Label l0 = new Label();
- mv.visitLabel( l0 );
- for ( final Iterator it = fieldTypes.entrySet().iterator(); it.hasNext(); ) {
- final Map.Entry entry = (Map.Entry) it.next();
- final String fieldName = (String) entry.getKey();
- final Class fieldType = (Class) entry.getValue();
- final Method method = (Method) fieldMethods.get( fieldName );
-
- // __field = this.delegate.getField()
- final Label l1 = new Label();
- mv.visitLabel( l1 );
- mv.visitVarInsn( Opcodes.ALOAD,
- 0 );
- mv.visitVarInsn( Opcodes.ALOAD,
- 0 );
- mv.visitFieldInsn( Opcodes.GETFIELD,
- className,
- ShadowProxyFactory.DELEGATE_FIELD_NAME,
- Type.getDescriptor( delegate ) );
- if ( delegate.isInterface() ) {
- mv.visitMethodInsn( Opcodes.INVOKEINTERFACE,
- Type.getInternalName( delegate ),
- method.getName(),
- Type.getMethodDescriptor( method ) );
- } else {
- mv.visitMethodInsn( Opcodes.INVOKEVIRTUAL,
- Type.getInternalName( delegate ),
- method.getName(),
- Type.getMethodDescriptor( method ) );
- }
- mv.visitFieldInsn( Opcodes.PUTFIELD,
- className,
- fieldName,
- Type.getDescriptor( fieldType ) );
-
- }
- final Label l4 = new Label();
- mv.visitLabel( l4 );
- mv.visitInsn( Opcodes.RETURN );
- final Label l5 = new Label();
- mv.visitLabel( l5 );
- mv.visitLocalVariable( "this",
- "L" + className + ";",
- null,
- l0,
- l5,
- 0 );
- mv.visitMaxs( 0,
- 0 );
- mv.visitEnd();
- }
-
protected static void buildUpdateProxyMethod(final Map fieldTypes,
final String className,
final ClassWriter cw) {
@@ -782,7 +543,7 @@
mv.visitEnd();
}
- private static void buildEquals(ClassWriter cw,
+ protected static void buildEquals(ClassWriter cw,
String className,
final Class clazz,
Map fieldTypes) {
@@ -844,10 +605,10 @@
int count = 0;
for ( Iterator it = fieldTypes.entrySet().iterator(); it.hasNext(); ) {
Map.Entry entry = (Map.Entry) it.next();
- String name = (String) entry.getKey();
+ String fieldName = (String) entry.getKey();
Method method = (Method) entry.getValue();
Class fieldType = method.getReturnType();
- String fieldFlag = name + ShadowProxyFactory.FIELD_SET_FLAG;
+ String fieldFlag = fieldName + ShadowProxyFactory.FIELD_SET_FLAG;
count++;
Label goNext = new Label();
@@ -888,7 +649,7 @@
}
mv.visitFieldInsn( Opcodes.PUTFIELD,
className,
- name,
+ fieldName,
Type.getDescriptor( fieldType ) );
// __fieldIsSet = true;
@@ -911,7 +672,7 @@
0 );
mv.visitFieldInsn( Opcodes.GETFIELD,
className,
- name,
+ fieldName,
Type.getDescriptor( fieldType ) );
mv.visitVarInsn( Opcodes.ALOAD,
2 );
@@ -958,7 +719,7 @@
0 );
mv.visitFieldInsn( Opcodes.GETFIELD,
className,
- name,
+ fieldName,
Type.getDescriptor( fieldType ) );
Label secondIfPart = new Label();
mv.visitJumpInsn( Opcodes.IFNONNULL,
@@ -984,7 +745,7 @@
0 );
mv.visitFieldInsn( Opcodes.GETFIELD,
className,
- name,
+ fieldName,
Type.getDescriptor( fieldType ) );
mv.visitJumpInsn( Opcodes.IFNULL,
goNext );
@@ -992,7 +753,7 @@
0 );
mv.visitFieldInsn( Opcodes.GETFIELD,
className,
- name,
+ fieldName,
Type.getDescriptor( fieldType ) );
mv.visitVarInsn( Opcodes.ALOAD,
2 );
@@ -1007,11 +768,19 @@
method.getName(),
Type.getMethodDescriptor( method ) );
}
- mv.visitMethodInsn( Opcodes.INVOKEVIRTUAL,
- Type.getInternalName( fieldType ),
- "equals",
- Type.getMethodDescriptor( Type.BOOLEAN_TYPE,
- new Type[]{Type.getType( Object.class )} ) );
+ if ( fieldType.isInterface() ) {
+ mv.visitMethodInsn( Opcodes.INVOKEINTERFACE,
+ Type.getInternalName( fieldType ),
+ "equals",
+ Type.getMethodDescriptor( Type.BOOLEAN_TYPE,
+ new Type[]{Type.getType( Object.class )} ) );
+ } else {
+ mv.visitMethodInsn( Opcodes.INVOKEVIRTUAL,
+ Type.getInternalName( fieldType ),
+ "equals",
+ Type.getMethodDescriptor( Type.BOOLEAN_TYPE,
+ new Type[]{Type.getType( Object.class )} ) );
+ }
mv.visitJumpInsn( Opcodes.IFNE,
goNext );
// return false;
@@ -1059,11 +828,35 @@
}
}
- private static void buildHashCode(ClassWriter cw,
+ /**
+ * Sample of generated code for all primitive + object types
+ *
+ * public int hashCode() {
+ * final int PRIME = 31;
+ * int result = 1;
+ * result = PRIME * result + (booleanAttr ? 1231 : 1237);
+ * result = PRIME * result + charAttr;
+ * long temp = Double.doubleToLongBits( doubleAttr );
+ * result = PRIME * result + (int) (temp ^ (temp >>> 32));
+ * result = PRIME * result + Float.floatToIntBits( floatAttr );
+ * result = PRIME * result + intAttr;
+ * result = PRIME * result + ((listAttr == null) ? 0 : listAttr.hashCode());
+ * result = PRIME * result + (int) (longAttr ^ (longAttr >>> 32));
+ * result = PRIME * result + shortAttr;
+ * return result;
+ * }
+ *
+ * @param cw
+ * @param className
+ * @param clazz
+ * @param fieldTypes
+ */
+ protected static void buildHashCode(ClassWriter cw,
String className,
final Class clazz,
Map fieldTypes) {
MethodVisitor mv;
+ boolean hasDoubleAttr = false;
// Building hashcode method
{
mv = cw.visitMethod( Opcodes.ACC_PUBLIC,
@@ -1093,13 +886,13 @@
int count = 0;
for ( Iterator it = fieldTypes.entrySet().iterator(); it.hasNext(); ) {
Map.Entry entry = (Map.Entry) it.next();
- String name = (String) entry.getKey();
+ String fieldName = (String) entry.getKey();
Method method = (Method) entry.getValue();
Class fieldType = method.getReturnType();
- String fieldFlag = name + ShadowProxyFactory.FIELD_SET_FLAG;
+ String fieldFlag = fieldName + ShadowProxyFactory.FIELD_SET_FLAG;
count++;
Label goNext = new Label();
-
+
// if ( ! _fieldIsSet ) {
final Label l5 = new Label();
mv.visitLabel( l5 );
@@ -1137,7 +930,7 @@
}
mv.visitFieldInsn( Opcodes.PUTFIELD,
className,
- name,
+ fieldName,
Type.getDescriptor( fieldType ) );
// __fieldIsSet = true;
@@ -1154,35 +947,112 @@
// }
mv.visitLabel( l6 );
- // result = PRIME * result + <att hashcode>
- Label l2 = new Label();
- mv.visitLabel( l2 );
- mv.visitIntInsn( Opcodes.BIPUSH,
- 31 );
- mv.visitVarInsn( Opcodes.ILOAD,
- 2 );
- mv.visitInsn( Opcodes.IMUL );
-
if ( fieldType.isPrimitive() ) {
// for primitive types
- mv.visitVarInsn( Opcodes.ALOAD,
- 0 );
- mv.visitFieldInsn( Opcodes.GETFIELD,
- className,
- name,
- Type.getDescriptor( fieldType ) );
+ // result = PRIME * result + <att hashcode>
+ Label l2 = new Label();
+ if ( fieldType == Double.TYPE ) {
+ hasDoubleAttr = true;
+ mv.visitVarInsn( Opcodes.ALOAD,
+ 0 );
+ mv.visitFieldInsn( Opcodes.GETFIELD,
+ className,
+ fieldName,
+ Type.getDescriptor( fieldType ) );
+ mv.visitMethodInsn( Opcodes.INVOKESTATIC,
+ Type.getInternalName( Double.class ),
+ "doubleToLongBits",
+ "(D)J" );
+ mv.visitVarInsn( Opcodes.LSTORE,
+ 3 );
+ }
- mv.visitInsn( Type.getType( fieldType ).getOpcode( Opcodes.IADD ) );
- mv.visitVarInsn( Type.getType( fieldType ).getOpcode( Opcodes.ISTORE ),
+ mv.visitLabel( l2 );
+ mv.visitIntInsn( Opcodes.BIPUSH,
+ 31 );
+ mv.visitVarInsn( Opcodes.ILOAD,
2 );
+ mv.visitInsn( Opcodes.IMUL );
+ if ( fieldType != Double.TYPE ) {
+ mv.visitVarInsn( Opcodes.ALOAD,
+ 0 );
+ mv.visitFieldInsn( Opcodes.GETFIELD,
+ className,
+ fieldName,
+ Type.getDescriptor( fieldType ) );
+ }
+
+ if ( fieldType == Boolean.TYPE ) {
+ // att_hashcode ::= ( boolean_attribute ) ? 1231 : 1237;
+ Label z1 = new Label();
+ mv.visitJumpInsn( Opcodes.IFEQ,
+ z1 );
+ mv.visitIntInsn( Opcodes.SIPUSH,
+ 1231 );
+ Label z2 = new Label();
+ mv.visitJumpInsn( Opcodes.GOTO,
+ z2 );
+ mv.visitLabel( z1 );
+ mv.visitIntInsn( Opcodes.SIPUSH,
+ 1237 );
+ mv.visitLabel( z2 );
+ } else if ( fieldType == Double.TYPE ) {
+ // long temp = Double.doubleToLongBits( doubleAttr );
+ // att_hashcode ::= (int) (temp ^ ( temp >>> 32 ) );
+ Label d1 = new Label();
+ mv.visitLabel( d1 );
+ mv.visitVarInsn( Opcodes.LLOAD,
+ 3 );
+ mv.visitVarInsn( Opcodes.LLOAD,
+ 3 );
+ mv.visitIntInsn( Opcodes.BIPUSH,
+ 32 );
+ mv.visitInsn( Opcodes.LUSHR );
+ mv.visitInsn( Opcodes.LXOR );
+ mv.visitInsn( Opcodes.L2I );
+ } else if ( fieldType == Float.TYPE ) {
+ // att_hashcode ::= Float.floatToIntBits( floatAttr );
+ mv.visitMethodInsn( Opcodes.INVOKESTATIC,
+ Type.getInternalName( Float.class ),
+ "floatToIntBits",
+ "(F)I" );
+
+ } else if ( fieldType == Long.TYPE ) {
+ // att_hashcode ::= (int) (lontattr ^( longattr >>> 32 ) );
+ mv.visitVarInsn( Opcodes.ALOAD,
+ 0 );
+ mv.visitFieldInsn( Opcodes.GETFIELD,
+ className,
+ fieldName,
+ Type.getDescriptor( fieldType ) );
+ mv.visitIntInsn( Opcodes.BIPUSH,
+ 32 );
+ mv.visitInsn( Opcodes.LUSHR );
+ mv.visitInsn( Opcodes.LXOR );
+ mv.visitInsn( Opcodes.L2I );
+
+ }
+ mv.visitInsn( Opcodes.IADD );
+ mv.visitVarInsn( Opcodes.ISTORE,
+ 2 );
+
} else {
// for non primitive types
+ // result = PRIME * result + <att hashcode>
+ Label l2 = new Label();
+ mv.visitLabel( l2 );
+ mv.visitIntInsn( Opcodes.BIPUSH,
+ 31 );
+ mv.visitVarInsn( Opcodes.ILOAD,
+ 2 );
+ mv.visitInsn( Opcodes.IMUL );
+
mv.visitVarInsn( Opcodes.ALOAD,
0 );
mv.visitFieldInsn( Opcodes.GETFIELD,
className,
- name,
+ fieldName,
Type.getDescriptor( fieldType ) );
Label np1 = new Label();
@@ -1197,12 +1067,19 @@
0 );
mv.visitFieldInsn( Opcodes.GETFIELD,
className,
- name,
+ fieldName,
Type.getDescriptor( fieldType ) );
- mv.visitMethodInsn( Opcodes.INVOKEVIRTUAL,
- Type.getInternalName( fieldType ),
- "hashCode",
- "()I" );
+ if ( fieldType.isInterface() ) {
+ mv.visitMethodInsn( Opcodes.INVOKEINTERFACE,
+ Type.getInternalName( fieldType ),
+ "hashCode",
+ "()I" );
+ } else {
+ mv.visitMethodInsn( Opcodes.INVOKEVIRTUAL,
+ Type.getInternalName( fieldType ),
+ "hashCode",
+ "()I" );
+ }
mv.visitLabel( np2 );
mv.visitInsn( Opcodes.IADD );
mv.visitVarInsn( Opcodes.ISTORE,
@@ -1217,7 +1094,7 @@
mv.visitLabel( lastLabel );
mv.visitLocalVariable( "this",
- "L"+className+";",
+ "L" + className + ";",
null,
l0,
lastLabel,
@@ -1234,6 +1111,14 @@
l1,
lastLabel,
2 );
+ if ( hasDoubleAttr ) {
+ mv.visitLocalVariable( "temp",
+ Type.LONG_TYPE.getDescriptor(),
+ null,
+ l1,
+ lastLabel,
+ 3 );
+ }
mv.visitMaxs( 0,
0 );
mv.visitEnd();
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-26 00:23:51 UTC (rev 7124)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/ShadowProxyFactoryTest.java 2006-10-26 00:48:46 UTC (rev 7125)
@@ -152,136 +152,6 @@
}
}
- public void testEagerProxyForClass() {
- try {
- // creating original object
- final String originalType = "stilton";
- final int originalPrice = 15;
- final Cheese cheese = new Cheese( originalType,
- originalPrice );
-
- // creating proxy
- final Class proxy = ShadowProxyFactory.getEagerProxy( Cheese.class );
- final Cheese cheeseProxy = (Cheese) proxy.getConstructor( new Class[]{Cheese.class} ).newInstance( new Object[]{cheese} );
-
- // proxy is proxying the values
- Assert.assertEquals( originalType,
- cheeseProxy.getType() );
- Assert.assertEquals( originalPrice,
- cheeseProxy.getPrice() );
-
- // changing original values
- final String actualType = "rotten stilton";
- final 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).updateProxy();
-
- // 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 ( final Exception e ) {
- fail( "Error: " + e.getMessage() );
- }
- }
-
- public void testEagerProxyForInterface() {
- try {
- // creating original object
- final String originalType = "stilton";
- final int originalPrice = 15;
- final Cheese cheese = new Cheese( originalType,
- originalPrice );
-
- // creating proxy
- final Class proxy = ShadowProxyFactory.getEagerProxy( CheeseInterface.class );
- final 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
- final String actualType = "rotten stilton";
- final 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).updateProxy();
-
- // 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 ( final Exception e ) {
- fail( "Error: " + e.getMessage() );
- }
- }
-
- public void testEagerProxyForAPIClass() {
- try {
- // creating original object
- final List list = new ArrayList();
-
- // creating proxy
- final Class proxy = ShadowProxyFactory.getEagerProxy( ArrayList.class );
- final List listProxy = (List) proxy.getConstructor( new Class[]{ArrayList.class} ).newInstance( new Object[]{list} );
-
- // proxy is proxying the values
- Assert.assertEquals( list,
- listProxy );
-
- } catch ( final Exception e ) {
- fail( "Error: " + e.getMessage() );
- }
- }
-
public void testEqualsHashCodeForClass() {
try {
// creating original object
@@ -329,6 +199,25 @@
}
}
+ public void testEqualsHashCodeForClass2() {
+ try {
+ // creating original object
+ final TestBean bean = new TestBean();
+
+ // creating proxy
+ final Class proxy = ShadowProxyFactory.getProxy( TestBean.class );
+ final TestBean beanProxy1 = (TestBean) proxy.getConstructor( new Class[]{TestBean.class} ).newInstance( new Object[]{bean} );
+ final TestBean beanProxy2 = (TestBean) proxy.getConstructor( new Class[]{TestBean.class} ).newInstance( new Object[]{bean} );
+
+ Assert.assertEquals( beanProxy1, beanProxy2 );
+ Assert.assertEquals( beanProxy2, beanProxy1 );
+ Assert.assertEquals( -130900686 , beanProxy1.hashCode() );
+
+ } catch ( final Exception e ) {
+ fail( "Error: " + e.getMessage() );
+ }
+ }
+
private int cheeseHashCode(Cheese cheese) {
final int PRIME = 31;
int result = 1;
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/TestBean.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/TestBean.java 2006-10-26 00:23:51 UTC (rev 7124)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/base/TestBean.java 2006-10-26 00:48:46 UTC (rev 7125)
@@ -23,15 +23,15 @@
private final String name = "michael";
private final int age = 42;
- private final boolean booleanAttr = true;
- private final byte byteAttr = 1;
- private final char charAttr = 'a';
- private final short shortAttr = 3;
- private final int intAttr = 4;
- private final long longAttr = 5;
- private final float floatAttr = 6.0f;
- private final double doubleAttr = 7.0;
- private final List listAttr = Collections.EMPTY_LIST;
+ private boolean booleanAttr = true;
+ private byte byteAttr = 1;
+ private char charAttr = 'a';
+ private short shortAttr = 3;
+ private int intAttr = 4;
+ private long longAttr = 5;
+ private float floatAttr = 6.0f;
+ private double doubleAttr = 7.0;
+ private List listAttr = Collections.EMPTY_LIST;
public String getName() {
return this.name;
@@ -76,4 +76,5 @@
public short getShortAttr() {
return this.shortAttr;
}
+
}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/FieldConstraintTest.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/FieldConstraintTest.java 2006-10-26 00:23:51 UTC (rev 7124)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/rule/FieldConstraintTest.java 2006-10-26 00:48:46 UTC (rev 7125)
@@ -26,6 +26,7 @@
import org.drools.WorkingMemory;
import org.drools.base.ClassFieldExtractor;
import org.drools.base.ClassObjectType;
+import org.drools.base.FieldFactory;
import org.drools.base.ValueType;
import org.drools.base.evaluators.Operator;
import org.drools.common.InternalFactHandle;
@@ -35,7 +36,6 @@
import org.drools.spi.Evaluator;
import org.drools.spi.FieldExtractor;
import org.drools.spi.FieldValue;
-import org.drools.spi.MockField;
import org.drools.spi.PredicateExpression;
import org.drools.spi.ReturnValueExpression;
import org.drools.spi.Tuple;
@@ -66,7 +66,7 @@
final ClassFieldExtractor extractor = new ClassFieldExtractor( Cheese.class,
"type" );
- final FieldValue field = new MockField( "cheddar" );
+ final FieldValue field = FieldFactory.getFieldValue( "cheddar" );
final Evaluator evaluator = ValueType.STRING_TYPE.getEvaluator( Operator.EQUAL );
@@ -111,7 +111,7 @@
final ClassFieldExtractor extractor = new ClassFieldExtractor( Cheese.class,
"price" );
- final FieldValue field = new MockField( new Integer( 5 ) );
+ final FieldValue field = FieldFactory.getFieldValue( 5 );
final Evaluator evaluator = ValueType.PINTEGER_TYPE.getEvaluator( Operator.EQUAL );
@@ -152,7 +152,7 @@
*/
public void testPredicateConstraint() throws IntrospectionException {
final ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase();
- final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
+ final InternalWorkingMemory workingMemory = (InternalWorkingMemory) ruleBase.newWorkingMemory();
final FieldExtractor priceExtractor = new ClassFieldExtractor( Cheese.class,
"price" );
@@ -187,8 +187,8 @@
Declaration declaration,
Declaration[] declarations,
WorkingMemory workingMemory) {
- int price1 = ((Integer) declarations[0].getValue( workingMemory.getObject( tuple.get( declarations[0] ) ) )).intValue();
- int price2 = ((Integer) declaration.getValue( object )).intValue();
+ int price1 = declarations[0].getIntValue( workingMemory.getObject( tuple.get( declarations[0] ) ) );
+ int price2 = declaration.getIntValue( object );
return (price2 == (price1 * 2));
@@ -212,7 +212,6 @@
f1 );
assertTrue( constraint1.isAllowed( f1.getObject(),
- tuple,
workingMemory ) );
}
@@ -231,7 +230,7 @@
*/
public void testReturnValueConstraint() throws IntrospectionException {
final ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory.newRuleBase();
- final WorkingMemory workingMemory = ruleBase.newWorkingMemory();
+ final InternalWorkingMemory workingMemory = (InternalWorkingMemory) ruleBase.newWorkingMemory();
final FieldExtractor priceExtractor = new ClassFieldExtractor( Cheese.class,
"price" );
@@ -251,11 +250,11 @@
*/
private static final long serialVersionUID = 5673999834006100045L;
- public Object evaluate(Tuple tuple, // ?price
+ public FieldValue evaluate(Tuple tuple, // ?price
Declaration[] declarations,
WorkingMemory workingMemory) {
int price = ((Integer) declarations[0].getValue( workingMemory.getObject( tuple.get( declarations[0] ) ) )).intValue();
- return new Integer( 2 * price );
+ return FieldFactory.getFieldValue( 2 * price );
}
};
@@ -283,11 +282,9 @@
f1 );
assertTrue( constraint1.isAllowed( f1.getObject(),
- tuple,
workingMemory ) );
assertFalse( constraint2.isAllowed( f1.getObject(),
- tuple,
workingMemory ) );
final Cheese cheddar2 = new Cheese( "cheddar",
@@ -296,7 +293,6 @@
final InternalFactHandle f2 = (InternalFactHandle) workingMemory.assertObject( cheddar2 );
assertTrue( constraint2.isAllowed( f2.getObject(),
- tuple,
workingMemory ) );
}
Deleted: labs/jbossrules/trunk/drools-core/src/test/java/org/drools/spi/MockField.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/test/java/org/drools/spi/MockField.java 2006-10-26 00:23:51 UTC (rev 7124)
+++ labs/jbossrules/trunk/drools-core/src/test/java/org/drools/spi/MockField.java 2006-10-26 00:48:46 UTC (rev 7125)
@@ -1,53 +0,0 @@
-package org.drools.spi;
-
-/*
- * Copyright 2005 JBoss Inc
- *
- * Licensed 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.
- */
-
-public final class MockField
- implements
- FieldValue {
-
- /**
- *
- */
- private static final long serialVersionUID = 6399579773906347733L;
- private final Object value;
-
- public MockField(final Object value) {
- this.value = value;
- }
-
- public Object getValue() {
- return this.value;
- }
-
- public boolean equals(final Object other) {
- if ( this == other ) {
- return true;
- }
- if ( !(other instanceof MockField) ) {
- return false;
- }
- final MockField field = (MockField) other;
-
- return (((this.value == null) && (field.value == null)) || ((this.value != null) && (this.value.equals( field.value ))));
- }
-
- public int hashCode() {
- return this.value.hashCode();
- }
-
-}
\ No newline at end of file
More information about the jboss-svn-commits
mailing list