Search in sources :

Example 1 with ClassBuilder

use of org.drools.core.factmodel.ClassBuilder in project drools by kiegroup.

the class Jenerator method createJar.

public byte[] createJar(Fact[] facts, String packageName) throws SecurityException, IllegalArgumentException, IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    JarOutputStream jout = new JarOutputStream(result);
    JarEntry metaModel = new JarEntry("factmodel.xml");
    jout.putNextEntry(metaModel);
    jout.write(toXML(facts));
    jout.closeEntry();
    String packagePath = packageName.replace('.', '/');
    for (int i = 0; i < facts.length; i++) {
        ClassBuilder cb = new TraitClassBuilderFactory().getBeanClassBuilder();
        ClassDefinition classDef = new ClassDefinition(packageName, null, new String[] { "java.io.Serializable" });
        for (int j = 0; j < facts[i].fields.size(); j++) {
            Field fd = (Field) facts[i].fields.get(j);
            classDef.addField(new FieldDefinition(fd.name, fd.type));
        }
        JarEntry je = new JarEntry(packagePath + "/" + facts[i].name + ".class");
        jout.putNextEntry(je);
        jout.write(cb.buildClass(classDef, classLoader));
        jout.closeEntry();
    }
    jout.flush();
    jout.close();
    return result.toByteArray();
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JarEntry(java.util.jar.JarEntry) ClassBuilder(org.drools.core.factmodel.ClassBuilder) ClassDefinition(org.drools.core.factmodel.ClassDefinition)

Example 2 with ClassBuilder

use of org.drools.core.factmodel.ClassBuilder in project drools by kiegroup.

the class ClassBuilderTest method testBuildClass.

/*
     * Test method for 'org.drools.core.common.asm.ClassBuilder.buildClass(ClassDefinition)'
     */
@Test
public void testBuildClass() {
    try {
        ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder();
        ClassDefinition classDef = new ClassDefinition("org.drools.TestClass1", null, new String[] { "java.io.Serializable" });
        FieldDefinition intDef = new FieldDefinition("intAttr", "int");
        FieldDefinition stringDef = new FieldDefinition("stringAttr", // "java.lang.String" );
        "java.lang.String");
        classDef.addField(intDef);
        classDef.addField(stringDef);
        Class clazz = build(builder, classDef);
        intDef.setReadWriteAccessor(store.getAccessor(clazz, intDef.getName()));
        stringDef.setReadWriteAccessor(store.getAccessor(clazz, stringDef.getName()));
        byte[] d = builder.buildClass(classDef, classLoader);
        assertSame("Returned class should be the same", clazz, classDef.getDefinedClass());
        assertEquals("Class name should be equal", classDef.getClassName(), clazz.getName());
        Serializable instance = (Serializable) clazz.newInstance();
        String stringValue = "Atributo String ok";
        stringDef.setValue(instance, stringValue);
        assertEquals("Attribute should have been correctly set", stringValue, stringDef.getValue(instance));
        int intValue = 50;
        intDef.setValue(instance, new Integer(intValue));
        assertEquals("Attribute should have been correctly set", intValue, ((Integer) intDef.getValue(instance)).intValue());
        // testing class rebuilding
        clazz = build(builder, classDef);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Error creating class");
    }
}
Also used : Serializable(java.io.Serializable) FieldDefinition(org.drools.core.factmodel.FieldDefinition) ClassBuilder(org.drools.core.factmodel.ClassBuilder) ClassDefinition(org.drools.core.factmodel.ClassDefinition) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 3 with ClassBuilder

use of org.drools.core.factmodel.ClassBuilder in project drools by kiegroup.

the class ClassBuilderTest method testToString.

@Test
public void testToString() {
    try {
        ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder();
        ClassDefinition classDef = new ClassDefinition("org.drools.TestClass4", null, new String[] {});
        FieldDefinition long1Def = new FieldDefinition("longAttr1", "long", true);
        FieldDefinition long2Def = new FieldDefinition("longAttr2", "long", true);
        FieldDefinition doubleDef = new FieldDefinition("doubleAttr", "double", true);
        FieldDefinition intDef = new FieldDefinition("intAttr", "int", true);
        FieldDefinition strDef = new FieldDefinition("stringAttr", "java.lang.String", true);
        FieldDefinition dateDef = new FieldDefinition("dateAttr", "java.util.Date", true);
        FieldDefinition str2Def = new FieldDefinition("stringAttr2", "java.lang.String");
        classDef.addField(long1Def);
        classDef.addField(long2Def);
        classDef.addField(doubleDef);
        classDef.addField(intDef);
        classDef.addField(strDef);
        classDef.addField(dateDef);
        classDef.addField(str2Def);
        Class clazz = build(builder, classDef);
        long1Def.setReadWriteAccessor(store.getAccessor(clazz, long1Def.getName()));
        long2Def.setReadWriteAccessor(store.getAccessor(clazz, long2Def.getName()));
        doubleDef.setReadWriteAccessor(store.getAccessor(clazz, doubleDef.getName()));
        intDef.setReadWriteAccessor(store.getAccessor(clazz, intDef.getName()));
        strDef.setReadWriteAccessor(store.getAccessor(clazz, strDef.getName()));
        dateDef.setReadWriteAccessor(store.getAccessor(clazz, dateDef.getName()));
        str2Def.setReadWriteAccessor(store.getAccessor(clazz, str2Def.getName()));
        Object x = clazz.newInstance();
        long1Def.setValue(x, new Long(20));
        long2Def.setValue(x, new Long(30));
        doubleDef.setValue(x, new Double(50.0));
        intDef.setValue(x, new Integer(10));
        strDef.setValue(x, "abc");
        dateDef.setValue(x, new Date(1000));
        str2Def.setValue(x, "instance1");
        String result = x.toString();
        assertTrue(result.contains(long1Def.getName()));
        assertTrue(result.contains(long2Def.getName()));
        assertTrue(result.contains(doubleDef.getName()));
        assertTrue(result.contains(intDef.getName()));
        assertTrue(result.contains(strDef.getName()));
        assertTrue(result.contains(dateDef.getName()));
        assertTrue(result.contains(str2Def.getName()));
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception not expected");
    }
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) ClassBuilder(org.drools.core.factmodel.ClassBuilder) ClassDefinition(org.drools.core.factmodel.ClassDefinition) Date(java.util.Date) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 4 with ClassBuilder

use of org.drools.core.factmodel.ClassBuilder in project drools by kiegroup.

the class ClassBuilderTest method testGetResourcesJBRULES3122.

@Test
public void testGetResourcesJBRULES3122() {
    try {
        ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder();
        ClassDefinition classDef = new ClassDefinition("org.drools.TestClass4", null, new String[] {});
        Class clazz = build(builder, classDef);
        ClassLoader cl = clazz.getClassLoader();
        // We expect normal classloader stuff to work
        assertFalse(cl.getResources("not-there.txt").hasMoreElements());
    } catch (Exception e) {
        e.printStackTrace();
        fail("Exception not expected: " + e.getMessage());
    }
}
Also used : PackageClassLoader(org.drools.wiring.dynamic.PackageClassLoader) ProjectClassLoader(org.drools.wiring.api.classloader.ProjectClassLoader) ClassBuilder(org.drools.core.factmodel.ClassBuilder) ClassDefinition(org.drools.core.factmodel.ClassDefinition) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 5 with ClassBuilder

use of org.drools.core.factmodel.ClassBuilder in project drools by kiegroup.

the class TypeDeclarationBuilder method createBean.

protected void createBean(AbstractClassTypeDeclarationDescr typeDescr, PackageRegistry pkgRegistry, ClassHierarchyManager hierarchyManager, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs) {
    // descriptor needs fields inherited from superclass
    if (typeDescr instanceof TypeDeclarationDescr) {
        hierarchyManager.inheritFields(pkgRegistry, typeDescr, unprocesseableDescrs);
    }
    TypeDeclaration type = typeDeclarationFactory.processTypeDeclaration(pkgRegistry, typeDescr);
    boolean success = !kbuilder.hasErrors();
    try {
        // the type declaration is generated in any case (to be used by subclasses, if any)
        // the actual class will be generated only if needed
        ClassDefinition def = null;
        if (success) {
            def = classDefinitionFactory.generateDeclaredBean(typeDescr, type, pkgRegistry, unresolvedTypes, unprocesseableDescrs);
            // this has to be done after the classDef has been generated
            if (!type.isNovel()) {
                typeDeclarationFactory.checkRedeclaration(typeDescr, type, pkgRegistry);
            }
        }
        success = (def != null) && (!kbuilder.hasErrors());
        if (success) {
            this.postGenerateDeclaredBean(typeDescr, type, def, pkgRegistry);
        }
        success = !kbuilder.hasErrors();
        if (success) {
            ClassBuilder classBuilder = RuntimeComponentFactory.get().getClassBuilderFactory().getClassBuilder(type);
            declaredClassBuilder.generateBeanFromDefinition(typeDescr, type, pkgRegistry, def, classBuilder);
        }
        success = !kbuilder.hasErrors();
        if (success) {
            Class<?> clazz = pkgRegistry.getTypeResolver().resolveType(typeDescr.getType().getFullName());
            type.setTypeClass(clazz);
            type.setValid(true);
        } else {
            unprocesseableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
            type.setValid(false);
        }
        typeDeclarationConfigurator.finalizeConfigurator(type, typeDescr, pkgRegistry, kbuilder.getPackageRegistry(), hierarchyManager);
    } catch (final ClassNotFoundException e) {
        unprocesseableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
        kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Class '" + type.getTypeClassName() + "' not found for type declaration of '" + type.getTypeName() + "'"));
    }
    if (!success) {
        unresolvedTypes.add(new TypeDefinition(type, typeDescr));
    } else {
        registerGeneratedType(typeDescr);
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) ClassDefinition(org.drools.core.factmodel.ClassDefinition) ClassBuilder(org.drools.core.factmodel.ClassBuilder) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Aggregations

ClassBuilder (org.drools.core.factmodel.ClassBuilder)9 ClassDefinition (org.drools.core.factmodel.ClassDefinition)9 FieldDefinition (org.drools.core.factmodel.FieldDefinition)7 Test (org.junit.Test)7 FileNotFoundException (java.io.FileNotFoundException)6 IOException (java.io.IOException)6 Date (java.util.Date)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Serializable (java.io.Serializable)1 Constructor (java.lang.reflect.Constructor)1 Calendar (java.util.Calendar)1 JarEntry (java.util.jar.JarEntry)1 JarOutputStream (java.util.jar.JarOutputStream)1 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 AbstractClassTypeDeclarationDescr (org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr)1 TypeDeclarationDescr (org.drools.drl.ast.descr.TypeDeclarationDescr)1 ProjectClassLoader (org.drools.wiring.api.classloader.ProjectClassLoader)1 PackageClassLoader (org.drools.wiring.dynamic.PackageClassLoader)1