Search in sources :

Example 1 with CompilationResult

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

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

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

the class ProjectJavaCompiler method compileAll.

public List<KnowledgeBuilderResult> compileAll(ProjectClassLoader projectClassLoader, List<String> classList, MemoryResourceReader src) {
    List<KnowledgeBuilderResult> results = new ArrayList<KnowledgeBuilderResult>();
    if (classList.isEmpty()) {
        return results;
    }
    final String[] classes = new String[classList.size()];
    classList.toArray(classes);
    CompilationResult result = compiler.compile(classes, src, new ProjectResourceStore(projectClassLoader), projectClassLoader);
    if (result.getErrors().length > 0) {
        Map<String, ErrorHandler> errorHandlerMap = new HashMap<String, ErrorHandler>();
        for (int i = 0; i < result.getErrors().length; i++) {
            final CompilationProblem err = result.getErrors()[i];
            ErrorHandler handler = errorHandlerMap.get(err.getFileName());
            if (handler == null) {
                handler = new SrcErrorHandler("Src compile error");
                errorHandlerMap.put(err.getFileName(), handler);
            }
            handler.addError(err);
        }
        for (ErrorHandler handler : errorHandlerMap.values()) {
            if (handler.isInError()) {
                results.add(handler.getError());
            }
        }
    }
    return results;
}
Also used : SrcErrorHandler(org.drools.compiler.builder.impl.errors.SrcErrorHandler) ErrorHandler(org.drools.compiler.builder.impl.errors.ErrorHandler) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CompilationProblem(org.drools.compiler.commons.jci.problems.CompilationProblem) SrcErrorHandler(org.drools.compiler.builder.impl.errors.SrcErrorHandler) CompilationResult(org.drools.compiler.commons.jci.compilers.CompilationResult) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 4 with CompilationResult

use of org.drools.compiler.commons.jci.compilers.CompilationResult 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 5 with CompilationResult

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

CompilationResult (org.drools.compiler.commons.jci.compilers.CompilationResult)7 CompilationProblem (org.drools.compiler.commons.jci.problems.CompilationProblem)6 ArrayList (java.util.ArrayList)3 EclipseJavaCompiler (org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler)3 File (java.io.File)2 HashMap (java.util.HashMap)2 ErrorHandler (org.drools.compiler.builder.impl.errors.ErrorHandler)2 SrcErrorHandler (org.drools.compiler.builder.impl.errors.SrcErrorHandler)2 JavaCompiler (org.drools.compiler.commons.jci.compilers.JavaCompiler)2 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 ZipFile (java.util.zip.ZipFile)1 FunctionErrorHandler (org.drools.compiler.builder.impl.errors.FunctionErrorHandler)1 RuleErrorHandler (org.drools.compiler.builder.impl.errors.RuleErrorHandler)1 RuleInvokerErrorHandler (org.drools.compiler.builder.impl.errors.RuleInvokerErrorHandler)1 EclipseJavaCompilerSettings (org.drools.compiler.commons.jci.compilers.EclipseJavaCompilerSettings)1 File (org.drools.compiler.compiler.io.File)1 Folder (org.drools.compiler.compiler.io.Folder)1