Search in sources :

Example 6 with CompilationResult

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

the class JavaParserCompiler method compileAll.

public static Map<String, Class<?>> compileAll(KnowledgeBuilderImpl kbuilder, ClassLoader classLoader, List<GeneratedClassWithPackage> classes) {
    if (classes == null || classes.isEmpty()) {
        return Collections.emptyMap();
    }
    MemoryFileSystem srcMfs = new MemoryFileSystem();
    MemoryFileSystem trgMfs = new MemoryFileSystem();
    String[] resources = writeModel(classes, srcMfs);
    CompilationResult resultCompilation = getCompiler().compile(resources, srcMfs, trgMfs, classLoader);
    CompilationProblem[] errors = resultCompilation.getErrors();
    if (errors.length != 0) {
        classes.forEach(c -> logger.error(c.toString()));
        for (CompilationProblem error : errors) {
            kbuilder.addBuilderResult(new CompilationProblemErrorResult(error));
        }
        return Collections.emptyMap();
    }
    InternalClassLoader internalClassLoader = new InternalClassLoader(classLoader, trgMfs);
    Map<String, Class<?>> result = new HashMap<>();
    for (GeneratedClassWithPackage cls : classes) {
        final String fullClassName = cls.getPackageName() + "." + cls.getGeneratedClass().getNameAsString();
        try {
            result.put(fullClassName, Class.forName(fullClassName, true, internalClassLoader));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    return result;
}
Also used : CompilationProblemErrorResult(org.drools.modelcompiler.builder.errors.CompilationProblemErrorResult) HashMap(java.util.HashMap) CompilationProblem(org.drools.compiler.commons.jci.problems.CompilationProblem) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) CompilationResult(org.drools.compiler.commons.jci.compilers.CompilationResult)

Example 7 with CompilationResult

use of org.drools.compiler.commons.jci.compilers.CompilationResult 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);
    EclipseJavaCompilerSettings settings = new EclipseJavaCompilerSettings();
    settings.setSourceVersion("1.5");
    settings.setTargetVersion("1.5");
    EclipseJavaCompiler compiler = new EclipseJavaCompiler(settings, "");
    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) EclipseJavaCompiler(org.drools.compiler.commons.jci.compilers.EclipseJavaCompiler) ArrayList(java.util.ArrayList) CompilationResult(org.drools.compiler.commons.jci.compilers.CompilationResult) Folder(org.drools.compiler.compiler.io.Folder) EclipseJavaCompilerSettings(org.drools.compiler.commons.jci.compilers.EclipseJavaCompilerSettings)

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