use of org.mvel2.ast.And in project mvel by mvel.
the class TemplateTests method testDRLTemplate.
public void testDRLTemplate() {
String template = "@declare{\"drl\"}@includeNamed{\"ced\"; node=root }@end{}" + "" + "@declare{\"ced\"}" + "@if{ node.base==1 } @includeNamed{ \"cedX\"; connect=\"AND\"; args=node.list }" + "@elseif{ node.base ==2 }@includeNamed{ \"cedX\"; connect=\"OR\"; args=node.list }" + "@end{}" + "@end{}" + "" + "@declare{\"cedX\"}@{connect}@foreach{child : args}" + "@includeNamed{\"ced\"; node=child; }@end{} @{connect}@end{}";
TemplateRegistry reportRegistry = new SimpleTemplateRegistry();
reportRegistry.addNamedTemplate("drl", TemplateCompiler.compileTemplate(template));
TemplateRuntime.execute(reportRegistry.getNamedTemplate("drl"), null, reportRegistry);
Map<String, Object> context = new HashMap<String, Object>();
context.put("root", new Node(2, Arrays.asList(new Node(1, Collections.EMPTY_LIST))));
String result = (String) TemplateRuntime.execute(reportRegistry.getNamedTemplate("drl"), null, new MapVariableResolverFactory(context), reportRegistry);
assertEquals("OR AND AND OR", result);
}
use of org.mvel2.ast.And in project drools by kiegroup.
the class DefaultEnumClassBuilder method buildClass.
/**
* Dynamically builds, defines and loads a class based on the given class definition
*
* @param classDef the class definition object structure
*
* @return the Class instance for the given class definition
*
* @throws java.io.IOException
* @throws java.lang.reflect.InvocationTargetException
* @throws IllegalAccessException
* @throws NoSuchMethodException
* @throws ClassNotFoundException
* @throws IllegalArgumentException
* @throws SecurityException
* @throws NoSuchFieldException
* @throws InstantiationException
*/
public byte[] buildClass(ClassDefinition classDef, ClassLoader classLoader) throws IOException, SecurityException, IllegalArgumentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
if (!(classDef instanceof EnumClassDefinition)) {
throw new RuntimeException("FATAL : Trying to create an enum out of a bean class definition " + classDef);
}
EnumClassDefinition edef = (EnumClassDefinition) classDef;
ClassWriter cw = this.buildClassHeader(classLoader, edef);
this.buildLiterals(cw, edef);
this.buildFields(cw, edef);
this.buildConstructors(cw, edef);
this.buildGettersAndSetters(cw, edef);
this.buildEqualityMethods(cw, edef);
this.buildToString(cw, edef);
cw.visitEnd();
byte[] serializedClass = cw.toByteArray();
return serializedClass;
}
use of org.mvel2.ast.And in project drools by kiegroup.
the class DefaultBeanClassBuilder method fieldConstructorStart.
protected void fieldConstructorStart(MethodVisitor mv, ClassDefinition classDef, Collection<FieldDefinition> fieldDefs) {
mv.visitVarInsn(Opcodes.ALOAD, 0);
String sup = "";
try {
sup = Type.getInternalName(Class.forName(classDef.getSuperClass()));
} catch (ClassNotFoundException e) {
sup = BuildUtils.getInternalType(classDef.getSuperClass());
}
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, sup, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
// local vars start at 1, as 0 is "this"
int index = 1;
for (FieldDefinition field : fieldDefs) {
if (this.debug) {
Label l11 = new Label();
mv.visitLabel(l11);
}
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitVarInsn(Type.getType(BuildUtils.getTypeDescriptor(field.getTypeName())).getOpcode(Opcodes.ILOAD), index++);
if (field.getTypeName().equals("long") || field.getTypeName().equals("double")) {
// long and double variables use 2 words on the variables table
index++;
}
if (!field.isInherited()) {
mv.visitFieldInsn(Opcodes.PUTFIELD, BuildUtils.getInternalType(classDef.getClassName()), field.getName(), BuildUtils.getTypeDescriptor(field.getTypeName()));
} else {
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getClassName()), field.getWriteMethod(), Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(BuildUtils.getTypeDescriptor(field.getTypeName())) }));
}
}
for (FieldDefinition field : classDef.getFieldsDefinitions()) {
if (!fieldDefs.contains(field) && field.getInitExpr() != null && !"".equals(field.getInitExpr().trim())) {
initFieldWithDefaultValue(mv, classDef, field);
}
}
if (classDef.isTraitable()) {
initializeDynamicTypeStructures(mv, classDef);
}
}
use of org.mvel2.ast.And in project drools by kiegroup.
the class DefaultBeanClassBuilder method buildFieldTMS.
/**
* A traitable class is a special class with support for dynamic properties and types.
*
* A traitable class in logical mode provides additional control over the values
* and type(s) of its fields.
*
* @param cw
* @param def
*/
protected void buildFieldTMS(ClassWriter cw, ClassDefinition def) {
FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class), null, null);
fv.visitEnd();
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "_getFieldTMS", Type.getMethodDescriptor(Type.getType(TraitFieldTMS.class), new Type[] {}), null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(def.getName()), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
mv = cw.visitMethod(ACC_PUBLIC, "_setFieldTMS", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(TraitFieldTMS.class) }), null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(def.getName()), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.mvel2.ast.And in project drools by kiegroup.
the class DefaultBeanClassBuilder method buildConstructorWithFields.
/**
* Creates a constructor that takes and assigns values to all
* fields in the order they are declared.
*
* @param cw
* @param classDef
*/
protected void buildConstructorWithFields(ClassVisitor cw, ClassDefinition classDef, Collection<FieldDefinition> fieldDefs) {
Type[] params = new Type[fieldDefs.size()];
int index = 0;
for (FieldDefinition field : fieldDefs) {
params[index++] = Type.getType(BuildUtils.getTypeDescriptor(field.getTypeName()));
}
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, params), null, null);
mv.visitCode();
Label l0 = null;
if (this.debug) {
l0 = new Label();
mv.visitLabel(l0);
}
fieldConstructorStart(mv, classDef, fieldDefs);
mv.visitInsn(Opcodes.RETURN);
Label l1 = null;
if (this.debug) {
l1 = new Label();
mv.visitLabel(l1);
mv.visitLocalVariable("this", BuildUtils.getTypeDescriptor(classDef.getClassName()), null, l0, l1, 0);
for (FieldDefinition field : classDef.getFieldsDefinitions()) {
Label l11 = new Label();
mv.visitLabel(l11);
mv.visitLocalVariable(field.getName(), BuildUtils.getTypeDescriptor(field.getTypeName()), null, l0, l1, 0);
}
}
mv.visitMaxs(0, 0);
mv.visitEnd();
}
Aggregations