Search in sources :

Example 11 with ClassGenerator

use of org.drools.core.rule.builder.dialect.asm.ClassGenerator in project drools by kiegroup.

the class ASMEvalStubBuilder method createEvalBytecode.

protected byte[] createEvalBytecode(final RuleBuildContext ruleContext, final Map vars) {
    final InvokerDataProvider data = new InvokerContext(vars);
    final ClassGenerator generator = InvokerGenerator.createInvokerStubGenerator(data, ruleContext);
    createStubEval(generator, data, vars);
    return generator.generateBytecode();
}
Also used : ClassGenerator(org.drools.core.rule.builder.dialect.asm.ClassGenerator) InvokerDataProvider(org.drools.core.rule.builder.dialect.asm.InvokerDataProvider)

Example 12 with ClassGenerator

use of org.drools.core.rule.builder.dialect.asm.ClassGenerator in project drools by kiegroup.

the class ASMReturnValueStubBuilder method createReturnValueBytecode.

protected byte[] createReturnValueBytecode(RuleBuildContext ruleContext, Map vars, boolean readLocalsFromTuple) {
    final InvokerDataProvider data = new InvokerContext(vars);
    final ClassGenerator generator = InvokerGenerator.createInvokerStubGenerator(data, ruleContext);
    createStubReturnValue(generator, data, vars);
    return generator.generateBytecode();
}
Also used : ClassGenerator(org.drools.core.rule.builder.dialect.asm.ClassGenerator) InvokerDataProvider(org.drools.core.rule.builder.dialect.asm.InvokerDataProvider)

Example 13 with ClassGenerator

use of org.drools.core.rule.builder.dialect.asm.ClassGenerator in project drools by kiegroup.

the class InvokerGenerator method createStubGenerator.

public static ClassGenerator createStubGenerator(final InvokerDataProvider data, final ClassLoader classLoader, final TypeResolver typeResolver, final Set<String> imports) {
    final ClassGenerator generator = new ClassGenerator(data.getPackageName() + "." + data.getInvokerClassName(), classLoader, typeResolver);
    generator.addStaticField(ACC_PRIVATE + ACC_FINAL, "serialVersionUID", Long.TYPE, GeneratorHelper.INVOKER_SERIAL_UID).addDefaultConstructor();
    generator.addMethod(ACC_PUBLIC, "hashCode", generator.methodDescr(Integer.TYPE), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            push(data.hashCode());
            mv.visitInsn(IRETURN);
        }
    }).addMethod(ACC_PUBLIC, "getMethodBytecode", generator.methodDescr(List.class), new GeneratorHelper.GetMethodBytecodeMethod(data)).addMethod(ACC_PUBLIC, "equals", generator.methodDescr(Boolean.TYPE, Object.class), new GeneratorHelper.EqualsMethod()).addMethod(ACC_PUBLIC, "getPackageName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            push(data.getPackageName());
            mv.visitInsn(ARETURN);
        }
    }).addMethod(ACC_PUBLIC, "getGeneratedInvokerClassName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            push(data.getInvokerClassName() + "Generated");
            mv.visitInsn(ARETURN);
        }
    }).addMethod(ACC_PUBLIC, "getExpectedDeclarationTypes", generator.methodDescr(String[].class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            Declaration[] declarations = ((InvokerContext) data).getDeclarations();
            List<String> declarationTypes = new ArrayList<String>(declarations.length);
            for (Declaration decl : declarations) {
                declarationTypes.add(decl.getTypeName());
            }
            returnAsArray(declarationTypes, String.class);
        }
    }).addMethod(ACC_PUBLIC, "getRuleClassName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            push(data.getRuleClassName());
            mv.visitInsn(ARETURN);
        }
    }).addMethod(ACC_PUBLIC, "getInternalRuleClassName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            push(data.getInternalRuleClassName());
            mv.visitInsn(ARETURN);
        }
    }).addMethod(ACC_PUBLIC, "getMethodName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            push(data.getMethodName());
            mv.visitInsn(ARETURN);
        }
    }).addMethod(ACC_PUBLIC, "getInvokerClassName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            push(data.getInvokerClassName());
            mv.visitInsn(ARETURN);
        }
    }).addMethod(ACC_PUBLIC, "getGlobals", generator.methodDescr(String[].class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            returnAsArray(data.getGlobals());
        }
    }).addMethod(ACC_PUBLIC, "getGlobalTypes", generator.methodDescr(String[].class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            returnAsArray(data.getGlobalTypes());
        }
    }).addMethod(ACC_PUBLIC, "getPackageImports", generator.methodDescr(String[].class), new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            returnAsArray(imports, String.class);
        }
    });
    return generator;
}
Also used : ArrayList(java.util.ArrayList) MethodVisitor(org.mvel2.asm.MethodVisitor) ClassGenerator(org.drools.core.rule.builder.dialect.asm.ClassGenerator) GeneratorHelper(org.drools.core.rule.builder.dialect.asm.GeneratorHelper) Declaration(org.drools.core.rule.Declaration)

Example 14 with ClassGenerator

use of org.drools.core.rule.builder.dialect.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.Bean", 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.core.rule.builder.dialect.asm.ClassGenerator)

Aggregations

ClassGenerator (org.drools.core.rule.builder.dialect.asm.ClassGenerator)14 MethodVisitor (org.mvel2.asm.MethodVisitor)7 Declaration (org.drools.core.rule.Declaration)6 InvokerDataProvider (org.drools.core.rule.builder.dialect.asm.InvokerDataProvider)5 CompiledInvoker (org.drools.core.spi.CompiledInvoker)4 Label (org.mvel2.asm.Label)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)2 GeneratorHelper (org.drools.core.rule.builder.dialect.asm.GeneratorHelper)2 Tuple (org.drools.core.spi.Tuple)2 ArrayList (java.util.ArrayList)1 WorkingMemory (org.drools.core.WorkingMemory)1 EvaluatorWrapper (org.drools.core.base.EvaluatorWrapper)1 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)1 Sink (org.drools.core.reteoo.Sink)1 ConsequenceStub (org.drools.core.rule.builder.dialect.asm.ConsequenceStub)1 EvalStub (org.drools.core.rule.builder.dialect.asm.EvalStub)1 GeneratorHelper.matchDeclarationsToTuple (org.drools.core.rule.builder.dialect.asm.GeneratorHelper.matchDeclarationsToTuple)1 InvokerStub (org.drools.core.rule.builder.dialect.asm.InvokerStub)1 PredicateStub (org.drools.core.rule.builder.dialect.asm.PredicateStub)1