Skip to content

Commit

Permalink
JBRULES-3224: fixing ClassCastException when comparing class type att…
Browse files Browse the repository at this point in the history
…ributes to null. Removing Java 6 API use.

(cherry picked from commit 32bb1ed)
  • Loading branch information
etirelli committed Sep 26, 2011
1 parent 1ffcd95 commit 540920e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
16 changes: 16 additions & 0 deletions drools-compiler/src/test/java/org/drools/Primitives.java
Expand Up @@ -52,6 +52,8 @@ public class Primitives

private Object object;

private Class<?> classAttr;

public boolean isBooleanPrimitive() {
return this.booleanPrimitive;
}
Expand Down Expand Up @@ -280,4 +282,18 @@ public boolean equals(Object obj) {
return true;
}

/**
* @return the classAttr
*/
public Class< ? > getClassAttr() {
return classAttr;
}

/**
* @param classAttr the classAttr to set
*/
public void setClassAttr( Class< ? > classAttr ) {
this.classAttr = classAttr;
}

}
Expand Up @@ -9122,6 +9122,24 @@ public void testEventsInDifferentPackages() {
rules );
}

@Test
public void testClassTypeAttributes() {
String str = "package org.drools\n" +
"rule r1\n" +
"when\n" +
" Primitives( classAttr == null )" +
"then\n" +
"end\n";

KnowledgeBase kbase = loadKnowledgeBaseFromString( str );
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

ksession.insert( new Primitives() );
int rules = ksession.fireAllRules();
assertEquals( 1,
rules );
}

@Test
public void testJBRULES2872() {
String str = "package org.drools.test\n" +
Expand Down
Expand Up @@ -1768,7 +1768,7 @@ public boolean evaluate(InternalWorkingMemory workingMemory,
final Object object1, final FieldValue object2) {
Object value1 = extractor.getValue( workingMemory, object1 );
Object value2 = object2.getValue();
if ( value2 == null ) {
if ( !object2.isNull() && value2 == null ) {
ClassFieldImpl classField = (ClassFieldImpl) object2;
value2 = classField.resolve( workingMemory );
}
Expand Down Expand Up @@ -1820,7 +1820,7 @@ public boolean evaluate(InternalWorkingMemory workingMemory,
final Object object1, final FieldValue object2) {
Object value1 = extractor.getValue( workingMemory, object1 );
Object value2 = object2.getValue();
if ( value2 == null ) {
if ( !object2.isNull() && value2 == null ) {
ClassFieldImpl classField = (ClassFieldImpl) object2;
value2 = classField.resolve( workingMemory );
}
Expand Down
Expand Up @@ -16,18 +16,17 @@

package org.drools.base.field;

import org.drools.base.TypeResolver;
import org.drools.common.InternalWorkingMemory;
import org.drools.reteoo.ReteooRuleBase;
import org.drools.spi.FieldValue;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.math.BigDecimal;
import java.math.BigInteger;

import org.drools.common.InternalWorkingMemory;
import org.drools.reteoo.ReteooRuleBase;
import org.drools.spi.FieldValue;

public class ClassFieldImpl implements FieldValue, Externalizable {

private Class type;
Expand Down
Expand Up @@ -41,13 +41,12 @@ public byte[] buildClass( ClassDefinition classDef ) {
if ( Object.class.getName().equals( classDef.getSuperClass() ) ) {
intfaces = BuildUtils.getInternalTypes( classDef.getInterfaces() );
} else {
intfaces = BuildUtils.getInternalTypes( classDef.getInterfaces() );
intfaces = Arrays.copyOf(intfaces, intfaces.length + 1);
String[] tmp = BuildUtils.getInternalTypes( classDef.getInterfaces() );
intfaces = new String[ tmp.length + 1 ];
System.arraycopy( tmp, 0, intfaces, 0, tmp.length );
intfaces[ intfaces.length - 1 ] = BuildUtils.getInternalType( classDef.getSuperClass() );

}


cw.visit(V1_5, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE,
cName,
genericTypes,
Expand Down

0 comments on commit 540920e

Please sign in to comment.