Skip to content

Commit

Permalink
[DROOLS-209] NoSuchMethodException when traiting a legacy class witho…
Browse files Browse the repository at this point in the history
…ut a default constructor
  • Loading branch information
sotty committed Jul 31, 2013
1 parent c950014 commit 0e2f33f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
Expand Up @@ -4407,4 +4407,55 @@ public void traitLogicalRemovalSimple( ) {
}



public static class TraitableFoo {

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

}

}

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" +
"\n" +
"declare trait Bar\n" +
"end\n" +
"\n" +
"rule \"Don\"\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.insert( new TraitableFoo( "xx", 0, null ) );
ksession.fireAllRules();

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

}



}
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

0 comments on commit 0e2f33f

Please sign in to comment.