Search in sources :

Example 1 with CompilationProblem

use of org.drools.compiler.commons.jci.problems.CompilationProblem 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 CompilationProblem

use of org.drools.compiler.commons.jci.problems.CompilationProblem 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 CompilationProblem

use of org.drools.compiler.commons.jci.problems.CompilationProblem in project drools by kiegroup.

the class JavaDialect method compileAll.

/**
 * This actually triggers the compiling of all the resources.
 * Errors are mapped back to the element that originally generated the semantic
 * code.
 */
public void compileAll() {
    if (this.generatedClassList.isEmpty()) {
        this.errorHandlers.clear();
        return;
    }
    final String[] classes = new String[this.generatedClassList.size()];
    this.generatedClassList.toArray(classes);
    File dumpDir = this.configuration.getPackageBuilderConfiguration().getDumpDir();
    if (dumpDir != null) {
        dumpResources(classes, dumpDir);
    }
    final CompilationResult result = this.compiler.compile(classes, this.src, this.packageStoreWrapper, rootClassLoader);
    // this will sort out the errors based on what class/file they happened in
    if (result.getErrors().length > 0) {
        for (int i = 0; i < result.getErrors().length; i++) {
            final CompilationProblem err = result.getErrors()[i];
            final ErrorHandler handler = this.errorHandlers.get(err.getFileName());
            handler.addError(err);
        }
        final Collection errors = this.errorHandlers.values();
        for (Object error : errors) {
            final ErrorHandler handler = (ErrorHandler) error;
            if (handler.isInError()) {
                this.results.add(handler.getError());
            }
        }
    }
    // We've compiled everthing, so clear it for the next set of additions
    this.generatedClassList.clear();
    this.errorHandlers.clear();
}
Also used : RuleErrorHandler(org.drools.compiler.builder.impl.errors.RuleErrorHandler) SrcErrorHandler(org.drools.compiler.builder.impl.errors.SrcErrorHandler) FunctionErrorHandler(org.drools.compiler.builder.impl.errors.FunctionErrorHandler) ErrorHandler(org.drools.compiler.builder.impl.errors.ErrorHandler) RuleInvokerErrorHandler(org.drools.compiler.builder.impl.errors.RuleInvokerErrorHandler) Collection(java.util.Collection) CompilationResult(org.drools.compiler.commons.jci.compilers.CompilationResult) CompilationProblem(org.drools.compiler.commons.jci.problems.CompilationProblem) File(java.io.File)

Example 4 with CompilationProblem

use of org.drools.compiler.commons.jci.problems.CompilationProblem in project drools by kiegroup.

the class RuleErrorTest method testNewLineInMessage.

@Test
public void testNewLineInMessage() {
    CompilationProblem[] probs = new CompilationProblem[3];
    probs[0] = new MockCompilationProblem();
    probs[1] = new MockCompilationProblem();
    probs[2] = new MockCompilationProblem();
    DescrBuildError err = new DescrBuildError(new RuleDescr("ruleName"), new AndDescr(), probs, "IM IN YR EROR");
    assertNotNull(err.toString());
    String msg = err.getMessage();
    assertTrue(msg.indexOf("IM IN YR EROR") != -1);
    System.err.println(msg);
    assertEquals("IM IN YR EROR problem\nproblem\nproblem", msg);
}
Also used : AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) CompilationProblem(org.drools.compiler.commons.jci.problems.CompilationProblem) Test(org.junit.Test)

Example 5 with CompilationProblem

use of org.drools.compiler.commons.jci.problems.CompilationProblem in project drools by kiegroup.

the class CanonicalModelKieProject method writeProjectOutput.

@Override
public void writeProjectOutput(MemoryFileSystem trgMfs, ResultsImpl messages) {
    MemoryFileSystem srcMfs = new MemoryFileSystem();
    ModelWriter modelWriter = new ModelWriter();
    List<String> modelFiles = new ArrayList<>();
    for (ModelBuilderImpl modelBuilder : modelBuilders) {
        final ModelWriter.Result result = modelWriter.writeModel(srcMfs, modelBuilder.getPackageModels());
        modelFiles.addAll(result.getModelFiles());
        final String[] sources = result.getSources();
        if (sources.length != 0) {
            CompilationResult res = getCompiler().compile(sources, srcMfs, trgMfs, getClassLoader());
            Stream.of(res.getErrors()).collect(groupingBy(CompilationProblem::getFileName)).forEach((name, errors) -> {
                errors.forEach(messages::addMessage);
                File srcFile = srcMfs.getFile(name);
                if (srcFile instanceof MemoryFile) {
                    String src = new String(srcMfs.getFileContents((MemoryFile) srcFile));
                    messages.addMessage(Message.Level.ERROR, name, "Java source of " + name + " in error:\n" + src);
                }
            });
            for (CompilationProblem problem : res.getWarnings()) {
                messages.addMessage(problem);
            }
        }
    }
    modelWriter.writeModelFile(modelFiles, trgMfs);
}
Also used : MemoryFile(org.drools.compiler.compiler.io.memory.MemoryFile) ArrayList(java.util.ArrayList) CompilationProblem(org.drools.compiler.commons.jci.problems.CompilationProblem) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) CompilationResult(org.drools.compiler.commons.jci.compilers.CompilationResult) File(org.drools.compiler.compiler.io.File) MemoryFile(org.drools.compiler.compiler.io.memory.MemoryFile)

Aggregations

CompilationProblem (org.drools.compiler.commons.jci.problems.CompilationProblem)7 CompilationResult (org.drools.compiler.commons.jci.compilers.CompilationResult)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 EclipseJavaCompiler (org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler)2 JavaCompiler (org.drools.compiler.commons.jci.compilers.JavaCompiler)2 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Map (java.util.Map)1 ZipFile (java.util.zip.ZipFile)1 ClassLoaderIClassLoader (org.codehaus.janino.ClassLoaderIClassLoader)1 CompileException (org.codehaus.janino.CompileException)1 Compiler (org.codehaus.janino.Compiler)1 FilterWarningHandler (org.codehaus.janino.FilterWarningHandler)1 Location (org.codehaus.janino.Location)1 ParseException (org.codehaus.janino.Parser.ParseException)1