Search in sources :

Example 1 with ClassGenerator

use of org.drools.mvel.asm.ClassGenerator in project drools by kiegroup.

the class InvokerGeneratorTest method testGenerate.

@Test
public void testGenerate() {
    Map<String, Object> invokerContext = new HashMap<String, Object>();
    invokerContext.put("package", "pkg");
    invokerContext.put("invokerClassName", "TestInvoker");
    invokerContext.put("ruleClassName", "TestRule");
    invokerContext.put("methodName", "testMethod");
    invokerContext.put("consequenceName", "TestConsequence");
    invokerContext.put("hashCode", 111);
    invokerContext.put("declarations", new Declaration[0]);
    invokerContext.put("globals", new String[] { "globalList" });
    invokerContext.put("globalTypes", new String[] { "java/util/List" });
    Set<String> imports = new HashSet<String>();
    imports.add("p1");
    imports.add("p2");
    final ClassGenerator generator = InvokerGenerator.createStubGenerator(new InvokerContext(invokerContext), getClass().getClassLoader(), null, imports);
    generator.setInterfaces(InvokerStub.class);
    InvokerStub stub = generator.newInstance();
    assertEquals("pkg", stub.getPackageName());
    assertEquals("TestRule", stub.getRuleClassName());
    assertEquals("testMethod", stub.getMethodName());
    assertEquals("TestInvokerGenerated", stub.getGeneratedInvokerClassName());
    assertEquals(111, stub.hashCode());
    assertTrue(Arrays.equals(new String[] { "globalList" }, stub.getGlobals()));
    assertTrue(Arrays.equals(new String[] { "java/util/List" }, stub.getGlobalTypes()));
    List<String> importList = Arrays.asList(stub.getPackageImports());
    assertTrue(importList.contains("p1"));
    assertTrue(importList.contains("p2"));
}
Also used : ClassGenerator(org.drools.mvel.asm.ClassGenerator) HashMap(java.util.HashMap) InvokerContext(org.drools.mvel.asm.InvokerContext) InvokerStub(org.drools.mvel.asm.InvokerStub) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with ClassGenerator

use of org.drools.mvel.asm.ClassGenerator in project drools by kiegroup.

the class ClassGeneratorTest method testGenerateBean.

@Test
public void testGenerateBean() {
    final String MY_NAME = "myName";
    ClassGenerator generator = new ClassGenerator("pkg.Bean1", getClass().getClassLoader()).addField(ACC_PRIVATE | ACC_FINAL, "name", String.class);
    generator.addDefaultConstructor(new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            // read local variable 0 (initialized to this) and push it on the stack
            mv.visitVarInsn(ALOAD, 0);
            // push the String MY_NAME on the stack
            mv.visitLdcInsn(MY_NAME);
            putFieldInThis("name", String.class);
            mv.visitInsn(RETURN);
        }
    }).addMethod(ACC_PUBLIC, "toString", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            getFieldFromThis("name", String.class);
            // return the first object on the stack
            mv.visitInsn(ARETURN);
        }
    });
    Object instance = generator.newInstance();
    assertEquals(MY_NAME, instance.toString());
}
Also used : ClassGenerator(org.drools.mvel.asm.ClassGenerator)

Example 3 with ClassGenerator

use of org.drools.mvel.asm.ClassGenerator in project drools by kiegroup.

the class ClassGeneratorTest method testGenerateWithConstructorArg.

@Test
public void testGenerateWithConstructorArg() {
    final String MY_NAME = "myName";
    ClassGenerator generator = new ClassGenerator("pkg.Bean2", getClass().getClassLoader()).addField(ACC_PRIVATE | ACC_FINAL, "name", String.class);
    generator.addDefaultConstructor(new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            putFieldInThisFromRegistry("name", String.class, 1);
            mv.visitInsn(RETURN);
        }
    }, String.class);
    generator.addMethod(ACC_PUBLIC, "toString", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            getFieldFromThis("name", String.class);
            // return the first object on the stack
            mv.visitInsn(ARETURN);
        }
    });
    Object instance = generator.newInstance(String.class, MY_NAME);
    assertEquals(MY_NAME, instance.toString());
}
Also used : ClassGenerator(org.drools.mvel.asm.ClassGenerator)

Example 4 with ClassGenerator

use of org.drools.mvel.asm.ClassGenerator in project drools by kiegroup.

the class ASMConditionEvaluatorJitter method jitEvaluator.

public static ConditionEvaluator jitEvaluator(String expression, Condition condition, Declaration[] declarations, EvaluatorWrapper[] operators, ClassLoader classLoader, Tuple tuple) {
    ClassGenerator generator = new ClassGenerator(getUniqueClassName(), classLoader).setInterfaces(ConditionEvaluator.class).addStaticField(ACC_PRIVATE | ACC_FINAL, "EXPRESSION", String.class, expression).addField(ACC_PRIVATE | ACC_FINAL, "declarations", Declaration[].class);
    generator.addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(boolean.class, InternalFactHandle.class, ReteEvaluator.class, Tuple.class), new EvaluateMethodGenerator(condition, declarations, operators, tuple));
    if (operators.length == 0) {
        generator.addDefaultConstructor(new ClassGenerator.MethodBody() {

            public void body(MethodVisitor mv) {
                putFieldInThisFromRegistry("declarations", Declaration[].class, 1);
                mv.visitInsn(RETURN);
            }
        }, Declaration[].class);
        return generator.newInstance(Declaration[].class, declarations);
    }
    generator.addField(ACC_PRIVATE | ACC_FINAL, "operators", EvaluatorWrapper[].class);
    generator.addDefaultConstructor(new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            putFieldInThisFromRegistry("declarations", Declaration[].class, 1);
            putFieldInThisFromRegistry("operators", EvaluatorWrapper[].class, 2);
            mv.visitInsn(RETURN);
        }
    }, Declaration[].class, EvaluatorWrapper[].class);
    return generator.newInstance(Declaration[].class, declarations, EvaluatorWrapper[].class, operators);
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) ClassGenerator(org.drools.mvel.asm.ClassGenerator) EvaluatorWrapper(org.drools.compiler.rule.builder.EvaluatorWrapper) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) GeneratorHelper.matchDeclarationsToTuple(org.drools.mvel.asm.GeneratorHelper.matchDeclarationsToTuple) Tuple(org.drools.core.spi.Tuple) MethodVisitor(org.mvel2.asm.MethodVisitor)

Aggregations

ClassGenerator (org.drools.mvel.asm.ClassGenerator)4 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 EvaluatorWrapper (org.drools.compiler.rule.builder.EvaluatorWrapper)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 ReteEvaluator (org.drools.core.common.ReteEvaluator)1 Declaration (org.drools.core.rule.Declaration)1 Tuple (org.drools.core.spi.Tuple)1 GeneratorHelper.matchDeclarationsToTuple (org.drools.mvel.asm.GeneratorHelper.matchDeclarationsToTuple)1 InvokerContext (org.drools.mvel.asm.InvokerContext)1 InvokerStub (org.drools.mvel.asm.InvokerStub)1 Test (org.junit.Test)1 MethodVisitor (org.mvel2.asm.MethodVisitor)1