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"));
}
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());
}
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());
}
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);
}
Aggregations