Search in sources :

Example 1 with JavaCompiler

use of org.drools.compiler.commons.jci.compilers.JavaCompiler in project drools by kiegroup.

the class KieBuilderImpl method compileJavaClasses.

private void compileJavaClasses(JavaDialectConfiguration javaConf, ClassLoader classLoader, List<String> javaFiles, String rootFolder, ResourceReader source) {
    if (!javaFiles.isEmpty()) {
        String[] sourceFiles = javaFiles.toArray(new String[javaFiles.size()]);
        File dumpDir = javaConf.getPackageBuilderConfiguration().getDumpDir();
        if (dumpDir != null) {
            String dumpDirName;
            try {
                dumpDirName = dumpDir.getCanonicalPath().endsWith("/") ? dumpDir.getCanonicalPath() : dumpDir.getCanonicalPath() + "/";
                for (String srcFile : sourceFiles) {
                    String baseName = (srcFile.startsWith(JAVA_ROOT) ? srcFile.substring(JAVA_ROOT.length()) : srcFile).replaceAll("/", ".");
                    String fname = dumpDirName + baseName;
                    byte[] srcData = source.getBytes(srcFile);
                    try (FileOutputStream fos = new FileOutputStream(fname)) {
                        fos.write(srcData);
                    } catch (IOException iox) {
                        results.addMessage(Level.WARNING, fname, "Unable to 'dump' the generated Java class: " + fname);
                    }
                }
            } catch (IOException e) {
                results.addMessage(Level.WARNING, "", "Unable to get the 'dump directory for the generated Java classes");
            }
        }
        JavaCompiler javaCompiler = createCompiler(javaConf, rootFolder);
        CompilationResult res = javaCompiler.compile(sourceFiles, source, trgMfs, classLoader);
        for (CompilationProblem problem : res.getErrors()) {
            results.addMessage(problem);
        }
        for (CompilationProblem problem : res.getWarnings()) {
            results.addMessage(problem);
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) EclipseJavaCompiler(org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler) JavaCompiler(org.drools.compiler.commons.jci.compilers.JavaCompiler) IOException(java.io.IOException) CompilationResult(org.drools.compiler.commons.jci.compilers.CompilationResult) CompilationProblem(org.drools.compiler.commons.jci.problems.CompilationProblem) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with JavaCompiler

use of org.drools.compiler.commons.jci.compilers.JavaCompiler in project drools by kiegroup.

the class KieBuilderImpl method compileJavaClasses.

private void compileJavaClasses(JavaDialectConfiguration 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(problem);
        }
        for (CompilationProblem problem : res.getWarnings()) {
            results.addMessage(problem);
        }
    }
}
Also used : EclipseJavaCompiler(org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler) JavaCompiler(org.drools.compiler.commons.jci.compilers.JavaCompiler) CompilationResult(org.drools.compiler.commons.jci.compilers.CompilationResult) CompilationProblem(org.drools.compiler.commons.jci.problems.CompilationProblem)

Example 3 with JavaCompiler

use of org.drools.compiler.commons.jci.compilers.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.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();
    JavaDialectConfiguration javaConf = (JavaDialectConfiguration) conf.getDialectConfiguration("java");
    switch(javaConf.getCompiler()) {
        case NATIVE:
            assertSame(NativeJavaCompiler.class, compiler.getClass());
            break;
        case ECLIPSE:
            assertSame(EclipseJavaCompiler.class, compiler.getClass());
            break;
        case JANINO:
            assertSame(JaninoJavaCompiler.class, compiler.getClass());
            break;
        default:
            fail("Unrecognized java compiler");
    }
    // test JANINO with property settings
    conf = new KnowledgeBuilderConfigurationImpl();
    javaConf = (JavaDialectConfiguration) conf.getDialectConfiguration("java");
    javaConf.setCompiler(JavaDialectConfiguration.CompilerType.JANINO);
    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(JaninoJavaCompiler.class, compiler.getClass());
    // test eclipse jdt core with property settings and default source level
    conf = new KnowledgeBuilderConfigurationImpl();
    javaConf = (JavaDialectConfiguration) conf.getDialectConfiguration("java");
    javaConf.setCompiler(JavaDialectConfiguration.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) Dialect(org.drools.compiler.compiler.Dialect) NativeJavaCompiler(org.drools.compiler.commons.jci.compilers.NativeJavaCompiler) EclipseJavaCompiler(org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler) JaninoJavaCompiler(org.drools.compiler.commons.jci.compilers.JaninoJavaCompiler) JavaCompiler(org.drools.compiler.commons.jci.compilers.JavaCompiler) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) JavaDialectConfiguration(org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Aggregations

EclipseJavaCompiler (org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler)3 JavaCompiler (org.drools.compiler.commons.jci.compilers.JavaCompiler)3 CompilationResult (org.drools.compiler.commons.jci.compilers.CompilationResult)2 CompilationProblem (org.drools.compiler.commons.jci.problems.CompilationProblem)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 ZipFile (java.util.zip.ZipFile)1 JaninoJavaCompiler (org.drools.compiler.commons.jci.compilers.JaninoJavaCompiler)1 NativeJavaCompiler (org.drools.compiler.commons.jci.compilers.NativeJavaCompiler)1 Dialect (org.drools.compiler.compiler.Dialect)1 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)1 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)1 JavaDialectConfiguration (org.drools.compiler.rule.builder.dialect.java.JavaDialectConfiguration)1 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)1 Test (org.junit.Test)1 FactField (org.kie.api.definition.type.FactField)1