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);
}
}
}
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);
}
}
}
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();
}
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);
}
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);
}
Aggregations