Search in sources :

Example 1 with CompilationProblem

use of org.kie.memorycompiler.CompilationProblem in project drools by kiegroup.

the class KieBuilderImpl method compileJavaClasses.

private void compileJavaClasses(JavaConfiguration 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(new CompilationProblemAdapter(problem));
        }
        for (CompilationProblem problem : res.getWarnings()) {
            results.addMessage(new CompilationProblemAdapter(problem));
        }
    }
}
Also used : JavaCompiler(org.kie.memorycompiler.JavaCompiler) CompilationResult(org.kie.memorycompiler.CompilationResult) CompilationProblem(org.kie.memorycompiler.CompilationProblem)

Example 2 with CompilationProblem

use of org.kie.memorycompiler.CompilationProblem in project drools by kiegroup.

the class CanonicalModelKieProject method writeProjectOutput.

@Override
public void writeProjectOutput(MemoryFileSystem trgMfs, BuildContext buildContext) {
    MemoryFileSystem srcMfs = new MemoryFileSystem();
    ModelWriter modelWriter = new ModelWriter();
    Collection<String> modelFiles = new HashSet<>();
    Collection<String> sourceFiles = new HashSet<>();
    Map<String, List<String>> modelsByKBase = new HashMap<>();
    for (Map.Entry<String, ModelBuilderImpl> modelBuilder : modelBuilders.entrySet()) {
        ModelWriter.Result result = modelWriter.writeModel(srcMfs, modelBuilder.getValue().getPackageSources());
        modelFiles.addAll(result.getModelFiles());
        sourceFiles.addAll(result.getSourceFiles());
        List<String> modelFilesForKieBase = new ArrayList<>();
        modelFilesForKieBase.addAll(result.getModelFiles());
        modelFilesForKieBase.addAll(((CanonicalModelBuildContext) buildContext).getNotOwnedModelFiles(modelBuilders, modelBuilder.getKey()));
        modelsByKBase.put(modelBuilder.getKey(), modelFilesForKieBase);
    }
    InternalKieModule kieModule = getInternalKieModule();
    ModelSourceClass modelSourceClass = new ModelSourceClass(kieModule.getReleaseId(), kieModule.getKieModuleModel().getKieBaseModels(), modelsByKBase, hasDynamicClassLoader());
    String projectSourcePath = modelWriter.getBasePath().asString() + "/" + modelSourceClass.getName();
    srcMfs.write(projectSourcePath, modelSourceClass.generate().getBytes());
    sourceFiles.add(projectSourcePath);
    Set<KiePath> origFileNames = new HashSet<>(trgMfs.getFilePaths());
    String[] sources = sourceFiles.toArray(new String[sourceFiles.size()]);
    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(m -> buildContext.getMessages().addMessage(new CompilationProblemAdapter(m)));
            File srcFile = srcMfs.getFile(name);
            if (srcFile instanceof MemoryFile) {
                String src = new String(srcMfs.getFileContents((MemoryFile) srcFile));
                buildContext.getMessages().addMessage(Message.Level.ERROR, name, "Java source of " + name + " in error:\n" + src);
            }
        });
        for (CompilationProblem problem : res.getWarnings()) {
            buildContext.getMessages().addMessage(new CompilationProblemAdapter(problem));
        }
    }
    if (ProjectClassLoader.isEnableStoreFirst()) {
        Set<KiePath> generatedClassPaths = new HashSet<>(trgMfs.getFilePaths());
        generatedClassPaths.removeAll(origFileNames);
        Set<String> generatedClassNames = generatedClassPaths.stream().map(KiePath::asString).map(ClassUtils::convertResourceToClassName).collect(Collectors.toSet());
        modelWriter.writeGeneratedClassNamesFile(generatedClassNames, trgMfs, getInternalKieModule().getReleaseId());
    }
    modelWriter.writeModelFile(modelFiles, trgMfs, getInternalKieModule().getReleaseId());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CompilationProblem(org.kie.memorycompiler.CompilationProblem) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) MemoryFile(org.drools.compiler.compiler.io.memory.MemoryFile) KiePath(org.kie.memorycompiler.resources.KiePath) CompilationResult(org.kie.memorycompiler.CompilationResult) HashMap(java.util.HashMap) Map(java.util.Map) MemoryFile(org.drools.compiler.compiler.io.memory.MemoryFile) File(org.drools.compiler.compiler.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) CompilationProblemAdapter(org.drools.compiler.kie.builder.impl.CompilationProblemAdapter)

Example 3 with CompilationProblem

use of org.kie.memorycompiler.CompilationProblem in project drools by kiegroup.

the class NativeJavaCompiler method compile.

@Override
public CompilationResult compile(String[] pResourcePaths, ResourceReader pReader, ResourceStore pStore, ClassLoader pClassLoader, JavaCompilerSettings pSettings) {
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
    JavaCompiler compiler = NativeJavaCompiler.getJavaCompiler();
    try (StandardJavaFileManager jFileManager = compiler.getStandardFileManager(diagnostics, null, null)) {
        try {
            jFileManager.setLocation(StandardLocation.CLASS_PATH, pSettings.getClasspathLocations());
            jFileManager.setLocation(StandardLocation.CLASS_OUTPUT, Collections.singletonList(new File("target/classes")));
        } catch (IOException e) {
        // ignore if cannot set the classpath
        }
        try (MemoryFileManager fileManager = new MemoryFileManager(jFileManager, pClassLoader)) {
            final List<JavaFileObject> units = new ArrayList<JavaFileObject>();
            for (final String sourcePath : pResourcePaths) {
                units.add(new CompilationUnit(KiePath.of(sourcePath), pReader));
            }
            Iterable<String> options = new NativeJavaCompilerSettings(pSettings).toOptionsList();
            if (compiler.getTask(null, fileManager, diagnostics, options, null, units).call()) {
                for (CompilationOutput compilationOutput : fileManager.getOutputs()) {
                    pStore.write(compilationOutput.getBinaryName().replace('.', '/') + ".class", compilationOutput.toByteArray());
                }
                return new CompilationResult(new CompilationProblem[0]);
            }
        }
        List<Diagnostic<? extends JavaFileObject>> problems = diagnostics.getDiagnostics();
        CompilationProblem[] result = new CompilationProblem[problems.size()];
        for (int i = 0; i < problems.size(); i++) {
            result[i] = new NativeCompilationProblem((Diagnostic<JavaFileObject>) problems.get(i));
        }
        return new CompilationResult(result);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) AbstractJavaCompiler(org.kie.memorycompiler.AbstractJavaCompiler) JavaCompiler(javax.tools.JavaCompiler) Diagnostic(javax.tools.Diagnostic) IOException(java.io.IOException) CompilationProblem(org.kie.memorycompiler.CompilationProblem) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) CompilationResult(org.kie.memorycompiler.CompilationResult) File(java.io.File)

Example 4 with CompilationProblem

use of org.kie.memorycompiler.CompilationProblem in project drools by kiegroup.

the class EclipseJavaCompiler method compile.

public CompilationResult compile(final String[] pSourceFiles, final ResourceReader pReader, final ResourceStore pStore, final ClassLoader pClassLoader, final JavaCompilerSettings pSettings) {
    final Collection problems = new ArrayList();
    final ICompilationUnit[] compilationUnits = new ICompilationUnit[pSourceFiles.length];
    for (int i = 0; i < compilationUnits.length; i++) {
        final KiePath sourceFile = KiePath.of(pSourceFiles[i]);
        if (pReader.isAvailable(sourceFile)) {
            compilationUnits[i] = new CompilationUnit(pReader, sourceFile.asString());
        } else {
            // log.error("source not found " + sourceFile);
            final CompilationProblem problem = new CompilationProblem() {

                public int getEndColumn() {
                    return 0;
                }

                public int getEndLine() {
                    return 0;
                }

                public String getFileName() {
                    return sourceFile.asString();
                }

                public String getMessage() {
                    return "Source " + sourceFile.asString() + " could not be found";
                }

                public int getStartColumn() {
                    return 0;
                }

                public int getStartLine() {
                    return 0;
                }

                public boolean isError() {
                    return true;
                }

                public String toString() {
                    return getMessage();
                }
            };
            problems.add(problem);
        }
    }
    if (problems.size() > 0) {
        final CompilationProblem[] result = new CompilationProblem[problems.size()];
        problems.toArray(result);
        return new CompilationResult(result);
    }
    final IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems();
    final IProblemFactory problemFactory = new DefaultProblemFactory(Locale.getDefault());
    final INameEnvironment nameEnvironment = new INameEnvironment() {

        public NameEnvironmentAnswer findType(final char[][] pCompoundTypeName) {
            final StringBuilder result = new StringBuilder();
            for (int i = 0; i < pCompoundTypeName.length; i++) {
                if (i != 0) {
                    result.append('.');
                }
                result.append(pCompoundTypeName[i]);
            }
            return findType(result.toString());
        }

        public NameEnvironmentAnswer findType(final char[] pTypeName, final char[][] pPackageName) {
            final StringBuilder result = new StringBuilder();
            for (char[] chars : pPackageName) {
                result.append(chars);
                result.append('.');
            }
            result.append(pTypeName);
            return findType(result.toString());
        }

        private NameEnvironmentAnswer findType(final String pClazzName) {
            final String resourceName = ClassUtils.convertClassToResourcePath(pClazzName);
            final byte[] clazzBytes = pStore.read(resourceName);
            if (clazzBytes != null) {
                try {
                    return createNameEnvironmentAnswer(pClazzName, clazzBytes);
                } catch (final ClassFormatException e) {
                    throw new RuntimeException("ClassFormatException in loading class '" + pClazzName + "' with JCI.");
                }
            }
            try (InputStream is = pClassLoader.getResourceAsStream(resourceName)) {
                if (is == null) {
                    return null;
                }
                if (ClassUtils.isCaseSenstiveOS()) {
                    // check it really is a class, this issue is due to windows case sensitivity issues for the class org.kie.Process and path org/droosl/process
                    try {
                        pClassLoader.loadClass(pClazzName);
                    } catch (ClassNotFoundException | NoClassDefFoundError e) {
                        return null;
                    }
                }
                final byte[] buffer = new byte[8192];
                try (ByteArrayOutputStream baos = new ByteArrayOutputStream(buffer.length)) {
                    int count;
                    while ((count = is.read(buffer, 0, buffer.length)) > 0) {
                        baos.write(buffer, 0, count);
                    }
                    baos.flush();
                    return createNameEnvironmentAnswer(pClazzName, baos.toByteArray());
                }
            } catch (final IOException e) {
                throw new RuntimeException("could not read class", e);
            } catch (final ClassFormatException e) {
                throw new RuntimeException("wrong class format", e);
            }
        }

        private NameEnvironmentAnswer createNameEnvironmentAnswer(final String pClazzName, final byte[] clazzBytes) throws ClassFormatException {
            final char[] fileName = pClazzName.toCharArray();
            final ClassFileReader classFileReader = new ClassFileReader(clazzBytes, fileName, true);
            return new NameEnvironmentAnswer(classFileReader, null);
        }

        private boolean isSourceAvailable(final String pClazzName, final ResourceReader pReader) {
            // FIXME: this should not be tied to the extension
            final String javaSource = pClazzName.replace('.', '/') + ".java";
            final String classSource = pClazzName.replace('.', '/') + ".class";
            return pReader.isAvailable(sourceFolder + javaSource) || pReader.isAvailable(sourceFolder + classSource);
        }

        private boolean isPackage(final String pClazzName) {
            try (InputStream is = pClassLoader.getResourceAsStream(ClassUtils.convertClassToResourcePath(pClazzName))) {
                if (is != null) {
                    if (ClassUtils.isWindows() || ClassUtils.isOSX()) {
                        try {
                            Class cls = pClassLoader.loadClass(pClazzName);
                            if (cls != null) {
                                return false;
                            }
                        } catch (ClassNotFoundException | NoClassDefFoundError e) {
                            return true;
                        }
                    }
                }
                return is == null && !isSourceAvailable(pClazzName, pReader);
            } catch (IOException e) {
                throw new RuntimeException("Cannot open or close resource stream!", e);
            }
        }

        public boolean isPackage(char[][] parentPackageName, char[] pPackageName) {
            final StringBuilder result = new StringBuilder();
            if (parentPackageName != null) {
                for (int i = 0; i < parentPackageName.length; i++) {
                    if (i != 0) {
                        result.append('.');
                    }
                    result.append(parentPackageName[i]);
                }
            }
            if (parentPackageName != null && parentPackageName.length > 0) {
                result.append('.');
            }
            result.append(pPackageName);
            return isPackage(result.toString());
        }

        public void cleanup() {
        }
    };
    final ICompilerRequestor compilerRequestor = pResult -> {
        if (pResult.hasProblems()) {
            final IProblem[] iproblems = pResult.getProblems();
            for (final IProblem iproblem : iproblems) {
                final CompilationProblem problem = new EclipseCompilationProblem(iproblem);
                problems.add(problem);
            }
        }
        if (!pResult.hasErrors()) {
            final ClassFile[] clazzFiles = pResult.getClassFiles();
            for (final ClassFile clazzFile : clazzFiles) {
                final char[][] compoundName = clazzFile.getCompoundName();
                final StringBuilder clazzName = new StringBuilder();
                for (int j = 0; j < compoundName.length; j++) {
                    if (j != 0) {
                        clazzName.append('.');
                    }
                    clazzName.append(compoundName[j]);
                }
                pStore.write(clazzName.toString().replace('.', '/') + ".class", clazzFile.getBytes());
            }
        }
    };
    final Map settingsMap = new EclipseJavaCompilerSettings(pSettings).toNativeSettings();
    CompilerOptions compilerOptions = new CompilerOptions(settingsMap);
    compilerOptions.parseLiteralExpressionsAsConstants = false;
    final Compiler compiler = new Compiler(nameEnvironment, policy, compilerOptions, compilerRequestor, problemFactory);
    if (ClassBuilderFactory.DUMP_GENERATED_CLASSES) {
        dumpUnits(compilationUnits, pReader);
    }
    compiler.compile(compilationUnits);
    final CompilationProblem[] result = new CompilationProblem[problems.size()];
    problems.toArray(result);
    return new CompilationResult(result);
}
Also used : IErrorHandlingPolicy(org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy) ResourceReader(org.kie.memorycompiler.resources.ResourceReader) INameEnvironment(org.eclipse.jdt.internal.compiler.env.INameEnvironment) KiePath(org.kie.memorycompiler.resources.KiePath) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URLDecoder(java.net.URLDecoder) ResourceStore(org.kie.memorycompiler.resources.ResourceStore) ClassFormatException(org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException) CompilationProblem(org.kie.memorycompiler.CompilationProblem) CompilationResult(org.kie.memorycompiler.CompilationResult) AbstractJavaCompiler(org.kie.memorycompiler.AbstractJavaCompiler) ArrayList(java.util.ArrayList) ClassBuilderFactory(org.drools.core.factmodel.ClassBuilderFactory) NameEnvironmentAnswer(org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer) IoUtils(org.drools.core.util.IoUtils) Locale(java.util.Locale) StringTokenizer(java.util.StringTokenizer) Map(java.util.Map) ResourceReader(org.kie.memorycompiler.resources.ResourceReader) ClassFileReader(org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader) IProblemFactory(org.eclipse.jdt.internal.compiler.IProblemFactory) ClassUtils(org.drools.core.util.ClassUtils) IErrorHandlingPolicy(org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy) Collection(java.util.Collection) ClassFile(org.eclipse.jdt.internal.compiler.ClassFile) IOException(java.io.IOException) ICompilerRequestor(org.eclipse.jdt.internal.compiler.ICompilerRequestor) Compiler(org.eclipse.jdt.internal.compiler.Compiler) DefaultProblemFactory(org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory) DefaultErrorHandlingPolicies(org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies) IProblem(org.eclipse.jdt.core.compiler.IProblem) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions) JavaCompilerSettings(org.kie.memorycompiler.JavaCompilerSettings) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) InputStream(java.io.InputStream) NameEnvironmentAnswer(org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer) ClassFileReader(org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader) ArrayList(java.util.ArrayList) CompilationProblem(org.kie.memorycompiler.CompilationProblem) ClassFormatException(org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException) INameEnvironment(org.eclipse.jdt.internal.compiler.env.INameEnvironment) DefaultProblemFactory(org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory) IProblemFactory(org.eclipse.jdt.internal.compiler.IProblemFactory) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) AbstractJavaCompiler(org.kie.memorycompiler.AbstractJavaCompiler) Compiler(org.eclipse.jdt.internal.compiler.Compiler) ClassFile(org.eclipse.jdt.internal.compiler.ClassFile) ICompilerRequestor(org.eclipse.jdt.internal.compiler.ICompilerRequestor) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IProblem(org.eclipse.jdt.core.compiler.IProblem) CompilerOptions(org.eclipse.jdt.internal.compiler.impl.CompilerOptions) Collection(java.util.Collection) KiePath(org.kie.memorycompiler.resources.KiePath) CompilationResult(org.kie.memorycompiler.CompilationResult) Map(java.util.Map)

Example 5 with CompilationProblem

use of org.kie.memorycompiler.CompilationProblem 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 = createDefaultCompiler().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(new CompilationProblemAdapter(error)));
        }
        return Collections.emptyMap();
    }
    InternalClassLoader internalClassLoader = AccessController.doPrivileged((PrivilegedAction<InternalClassLoader>) () -> new InternalClassLoader(classLoader, trgMfs));
    return loadClasses(getClassNames(classLoader, trgMfs), internalClassLoader);
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) CompilationProblemErrorResult(org.drools.modelcompiler.builder.errors.CompilationProblemErrorResult) CompilationResult(org.kie.memorycompiler.CompilationResult) CompilationProblem(org.kie.memorycompiler.CompilationProblem) CompilationProblemAdapter(org.drools.compiler.kie.builder.impl.CompilationProblemAdapter)

Aggregations

CompilationProblem (org.kie.memorycompiler.CompilationProblem)5 CompilationResult (org.kie.memorycompiler.CompilationResult)5 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 Map (java.util.Map)2 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)2 CompilationProblemAdapter (org.drools.compiler.kie.builder.impl.CompilationProblemAdapter)2 AbstractJavaCompiler (org.kie.memorycompiler.AbstractJavaCompiler)2 KiePath (org.kie.memorycompiler.resources.KiePath)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URLDecoder (java.net.URLDecoder)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Locale (java.util.Locale)1 StringTokenizer (java.util.StringTokenizer)1