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