Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DROOLS-209 / 5.6.x #229

Merged
merged 2 commits into from Aug 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -4407,4 +4407,67 @@ public void traitLogicalRemovalSimple( ) {
}



public static class TraitableFoo {

private String id;

public TraitableFoo( String id, int x, Object k ) {
setId( id );
}

public String getId() {
return id;
}

public void setId( String id ) {
this.id = id;
}
}

public static class XYZ extends TraitableFoo {

public XYZ() {
super( null, 0, null );
}

}


@Test
public void traitDonLegacyClassWithoutEmptyConstructor( ) {
String drl = "package org.drools.compiler.trait.test;\n" +
"\n" +
"import org.drools.factmodel.traits.TraitTest.TraitableFoo;\n" +
"import org.drools.factmodel.traits.Traitable;\n" +
"\n" +
"" +
"declare trait Bar\n" +
"end\n" +
"\n" +
"rule \"Don\"\n" +
"no-loop \n" +
"when\n" +
" $f : TraitableFoo( )\n" +
"then\n" +
" Bar b = don( $f, Bar.class );\n" +
"end";


StatefulKnowledgeSession ksession = getSessionFromString(drl);
TraitFactory.setMode( TraitFactory.VirtualPropertyMode.MAP, ksession.getKnowledgeBase() );
ksession.addEventListener( new DebugAgendaEventListener( ) );

ksession.insert( new TraitableFoo( "xx", 0, null ) );
ksession.fireAllRules();

for ( Object o : ksession.getObjects() ) {
System.out.println( o );
}

assertEquals( 3, ksession.getObjects().size() );
}



}
Expand Up @@ -347,8 +347,8 @@ public void update(final FactHandle handle,
InternalFactHandle h = (InternalFactHandle) handle;
((InternalWorkingMemoryEntryPoint) h.getEntryPoint()).update( h,
newObject,
Long.MAX_VALUE,
Object.class,
Long.MIN_VALUE,
newObject.getClass(),
this.activation );
if ( getIdentityMap() != null ) {
this.getIdentityMap().put( newObject,
Expand Down
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -84,8 +85,24 @@ public byte[] buildClass( ClassDefinition core ) throws IOException,
mv = cw.visitMethod( ACC_PUBLIC, "<init>", "()V", null, null );
mv.visitCode();

mv.visitVarInsn( ALOAD, 0 );
mv.visitMethodInsn( INVOKESPECIAL, BuildUtils.getInternalType( coreName ), "<init>", "()V" );
try {
coreKlazz.getConstructor();
mv.visitVarInsn( ALOAD, 0 );
mv.visitMethodInsn( INVOKESPECIAL, BuildUtils.getInternalType( coreName ), "<init>", "()V" );
} catch ( NoSuchMethodException nsme ) {
Constructor con = coreKlazz.getConstructors()[ 0 ];
Class[] params = con.getParameterTypes();

mv.visitVarInsn( ALOAD, 0 );
for ( Class param : params ) {
mv.visitInsn( BuildUtils.zero( param.getName() ) );
}
mv.visitMethodInsn( INVOKESPECIAL,
BuildUtils.getInternalType( coreName ),
"<init>",
Type.getConstructorDescriptor( con ) );
}


// mv.visitVarInsn( ALOAD, 0 );
// mv.visitTypeInsn( NEW, Type.getInternalName( HashMap.class ) );
Expand Down Expand Up @@ -455,6 +472,20 @@ public byte[] buildClass( ClassDefinition core ) throws IOException,
mv.visitCode();
mv.visitVarInsn( ALOAD, 0 );
mv.visitFieldInsn( GETFIELD, BuildUtils.getInternalType( wrapperName ), "core", BuildUtils.getTypeDescriptor( coreName ) );

Label l0 = new Label();
mv.visitJumpInsn( IFNONNULL, l0 );
if ( method.getReturnType() == void.class ) {
mv.visitInsn( RETURN );
} else {
mv.visitInsn( BuildUtils.zero( method.getReturnType().getName() ) );
mv.visitInsn( BuildUtils.returnType( method.getReturnType().getName() ) );
}
mv.visitLabel( l0 );

mv.visitVarInsn( ALOAD, 0 );
mv.visitFieldInsn( GETFIELD, BuildUtils.getInternalType( wrapperName ), "core", BuildUtils.getTypeDescriptor( coreName ) );

int j = 1;
for ( Class arg : method.getParameterTypes() ) {
mv.visitVarInsn( BuildUtils.varType( arg.getName() ), j++ );
Expand Down