Search in sources :

Example 1 with JavaCompiler

use of org.kie.memorycompiler.JavaCompiler in project drools by kiegroup.

the class KnowledgeBuilderTest method testCompilerConfiguration.

@Test
public void testCompilerConfiguration() throws Exception {
    // test default is eclipse jdt core
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    PackageDescr pkgDescr = new PackageDescr("org.drools.mvel.compiler.test");
    builder.addPackage(pkgDescr);
    DialectCompiletimeRegistry reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
    final Field dialectField = builder.getClass().getDeclaredField("defaultDialect");
    dialectField.setAccessible(true);
    String dialectName = (String) dialectField.get(builder);
    reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
    Dialect dialect = reg.getDialect(dialectName);
    final Field compilerField = dialect.getClass().getDeclaredField("compiler");
    compilerField.setAccessible(true);
    JavaCompiler compiler = (JavaCompiler) compilerField.get(dialect);
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    JavaForMvelDialectConfiguration javaConf = (JavaForMvelDialectConfiguration) conf.getDialectConfiguration("java");
    switch(javaConf.getCompiler()) {
        case NATIVE:
            assertSame(NativeJavaCompiler.class, compiler.getClass());
            break;
        case ECLIPSE:
            assertSame(EclipseJavaCompiler.class, compiler.getClass());
            break;
        default:
            fail("Unrecognized java compiler");
    }
    // test eclipse jdt core with property settings and default source level
    conf = new KnowledgeBuilderConfigurationImpl();
    javaConf = (JavaForMvelDialectConfiguration) conf.getDialectConfiguration("java");
    javaConf.setCompiler(JavaForMvelDialectConfiguration.CompilerType.ECLIPSE);
    builder = new KnowledgeBuilderImpl(conf);
    builder.addPackage(pkgDescr);
    dialectName = (String) dialectField.get(builder);
    reg = builder.getPackageRegistry(pkgDescr.getName()).getDialectCompiletimeRegistry();
    dialect = reg.getDialect(dialectName);
    compiler = (JavaCompiler) compilerField.get(dialect);
    assertSame(EclipseJavaCompiler.class, compiler.getClass());
}
Also used : FactField(org.kie.api.definition.type.FactField) Field(java.lang.reflect.Field) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Dialect(org.drools.compiler.compiler.Dialect) NativeJavaCompiler(org.kie.memorycompiler.jdknative.NativeJavaCompiler) JavaCompiler(org.kie.memorycompiler.JavaCompiler) EclipseJavaCompiler(org.drools.ecj.EclipseJavaCompiler) JavaForMvelDialectConfiguration(org.drools.mvel.java.JavaForMvelDialectConfiguration) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 2 with JavaCompiler

use of org.kie.memorycompiler.JavaCompiler in project drools by kiegroup.

the class KieBuilderImpl method compileJavaClasses.

private void compileJavaClasses(JavaConfiguration javaConf, ClassLoader classLoader, List<String> javaFiles, String rootFolder) {
    if (!javaFiles.isEmpty()) {
        String[] sourceFiles = javaFiles.toArray(new String[javaFiles.size()]);
        JavaCompiler javaCompiler = createCompiler(javaConf, rootFolder);
        CompilationResult res = javaCompiler.compile(sourceFiles, srcMfs, trgMfs, classLoader);
        for (CompilationProblem problem : res.getErrors()) {
            results.addMessage(new CompilationProblemAdapter(problem));
        }
        for (CompilationProblem problem : res.getWarnings()) {
            results.addMessage(new CompilationProblemAdapter(problem));
        }
    }
}
Also used : JavaCompiler(org.kie.memorycompiler.JavaCompiler) CompilationResult(org.kie.memorycompiler.CompilationResult) CompilationProblem(org.kie.memorycompiler.CompilationProblem)

Example 3 with JavaCompiler

use of org.kie.memorycompiler.JavaCompiler in project drools by kiegroup.

the class CompilerBytecodeLoader method compileUnit.

public <T> T compileUnit(String cuPackage, String cuClass, CompilationUnit cu) {
    try {
        MemoryResourceReader pReader = new MemoryResourceReader();
        pReader.add(cuPackage.replaceAll("\\.", "/") + "/" + cuClass + ".java", cu.toString().getBytes());
        JavaCompiler compiler = createNativeCompiler();
        MemoryFileSystem pStore = new MemoryFileSystem();
        CompilationResult compilationResult = compiler.compile(new String[] { cuPackage.replaceAll("\\.", "/") + "/" + cuClass + ".java" }, pReader, pStore, this.getClass().getClassLoader());
        LOG.debug("{}", Arrays.asList(compilationResult.getErrors()));
        LOG.debug("{}", Arrays.asList(compilationResult.getWarnings()));
        String fqnClassName = cuPackage + "." + cuClass;
        Class<T> loaded = (Class<T>) new TemplateLoader(this.getClass().getClassLoader()).load(pStore, fqnClassName);
        return loaded.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) MemoryResourceReader(org.kie.memorycompiler.resources.MemoryResourceReader) JavaCompiler(org.kie.memorycompiler.JavaCompiler) CompilationResult(org.kie.memorycompiler.CompilationResult)

Example 4 with JavaCompiler

use of org.kie.memorycompiler.JavaCompiler in project drools by kiegroup.

the class AbstractKnowledgeTest method compile.

public List<String> compile(KieModuleModel kproj, MemoryFileSystem srcMfs, MemoryFileSystem trgMfs, List<String> classes) {
    for (KieBaseModel kbase : kproj.getKieBaseModels().values()) {
        Folder srcFolder = srcMfs.getFolder("src/main/resources/" + kbase.getName());
        Folder trgFolder = trgMfs.getFolder(kbase.getName());
        copyFolder(srcMfs, srcFolder, trgMfs, trgFolder, kproj);
    }
    Folder srcFolder = srcMfs.getFolder("META-INF");
    Folder trgFolder = trgMfs.getFolder("META-INF");
    trgFolder.create();
    copyFolder(srcMfs, srcFolder, trgMfs, trgFolder, kproj);
    JavaCompiler compiler = new NativeJavaCompiler();
    CompilationResult res = compiler.compile(classes.toArray(new String[classes.size()]), trgMfs, trgMfs);
    if (res.getErrors().length > 0) {
        fail(res.getErrors()[0].getMessage());
    }
    List<String> classes2 = new ArrayList<String>(classes.size());
    for (String str : classes) {
        classes2.add(filenameToClassname(str));
    }
    return classes2;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) NativeJavaCompiler(org.kie.memorycompiler.jdknative.NativeJavaCompiler) ArrayList(java.util.ArrayList) NativeJavaCompiler(org.kie.memorycompiler.jdknative.NativeJavaCompiler) JavaCompiler(org.kie.memorycompiler.JavaCompiler) CompilationResult(org.kie.memorycompiler.CompilationResult) Folder(org.drools.compiler.compiler.io.Folder)

Example 5 with JavaCompiler

use of org.kie.memorycompiler.JavaCompiler in project drools by kiegroup.

the class KieBuilderImpl method createCompiler.

private JavaCompiler createCompiler(JavaConfiguration javaConf, String sourceFolder) {
    JavaCompiler javaCompiler = JavaCompilerFactory.loadCompiler(javaConf);
    javaCompiler.setSourceFolder(sourceFolder);
    return javaCompiler;
}
Also used : JavaCompiler(org.kie.memorycompiler.JavaCompiler)

Aggregations

JavaCompiler (org.kie.memorycompiler.JavaCompiler)5 CompilationResult (org.kie.memorycompiler.CompilationResult)3 NativeJavaCompiler (org.kie.memorycompiler.jdknative.NativeJavaCompiler)2 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 Dialect (org.drools.compiler.compiler.Dialect)1 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)1 Folder (org.drools.compiler.compiler.io.Folder)1 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)1 PackageDescr (org.drools.drl.ast.descr.PackageDescr)1 EclipseJavaCompiler (org.drools.ecj.EclipseJavaCompiler)1 JavaForMvelDialectConfiguration (org.drools.mvel.java.JavaForMvelDialectConfiguration)1 Test (org.junit.Test)1 KieBaseModel (org.kie.api.builder.model.KieBaseModel)1 FactField (org.kie.api.definition.type.FactField)1 CompilationProblem (org.kie.memorycompiler.CompilationProblem)1 MemoryResourceReader (org.kie.memorycompiler.resources.MemoryResourceReader)1