Search in sources :

Example 1 with ClassLoaderIClassLoader

use of org.codehaus.janino.ClassLoaderIClassLoader in project drools by kiegroup.

the class JaninoJavaCompiler method compile.

public CompilationResult compile(final String[] pSourceNames, final ResourceReader pResourceReader, final ResourceStore pStore, final ClassLoader pClassLoader, final JavaCompilerSettings pSettings) {
    final Collection problems = new ArrayList();
    final StringPattern[] pattern = StringPattern.PATTERNS_NONE;
    final Compiler compiler = new Compiler(new ResourceFinder() {

        public Resource findResource(final String pSourceName) {
            final byte[] bytes = pResourceReader.getBytes(pSourceName);
            if (bytes == null) {
                return null;
            }
            return new JciResource(pSourceName, bytes);
        }
    }, new ClassLoaderIClassLoader(pClassLoader), new ResourceFinder() {

        public Resource findResource(final String pResourceName) {
            final byte[] bytes = pStore.read(pResourceName);
            if (bytes == null) {
                return null;
            }
            return new JciResource(pResourceName, bytes);
        }
    }, new ResourceCreator() {

        public OutputStream createResource(final String pResourceName) throws IOException {
            return new JciOutputStream(pResourceName, pStore);
        }

        public boolean deleteResource(final String pResourceName) {
            pStore.remove(pResourceName);
            return true;
        }
    }, pSettings.getSourceEncoding(), false, pSettings.isDebug() ? DebuggingInformation.ALL : DebuggingInformation.NONE, new FilterWarningHandler(pattern, new WarningHandler() {

        public void handleWarning(final String pHandle, final String pMessage, final Location pLocation) {
            final CompilationProblem problem = new JaninoCompilationProblem(pLocation.getFileName(), pLocation, pMessage, false);
            if (problemHandler != null) {
                problemHandler.handle(problem);
            }
            problems.add(problem);
        }
    }));
    compiler.setCompileErrorHandler(new ErrorHandler() {

        public void handleError(final String pMessage, final Location pLocation) throws CompileException {
            final CompilationProblem problem = new JaninoCompilationProblem(pLocation.getFileName(), pLocation, pMessage, true);
            if (problemHandler != null) {
                problemHandler.handle(problem);
            }
            problems.add(problem);
        }
    });
    final Resource[] resources = new Resource[pSourceNames.length];
    for (int i = 0; i < pSourceNames.length; i++) {
        final byte[] source = pResourceReader.getBytes(pSourceNames[i]);
        resources[i] = new JciResource(pSourceNames[i], source);
    }
    try {
        compiler.compile(resources);
    } catch (ScanException e) {
        problems.add(new JaninoCompilationProblem(e));
    } catch (ParseException e) {
        problems.add(new JaninoCompilationProblem(e));
    } catch (IOException e) {
    // I'm hoping the existing compiler problems handler catches these
    } catch (CompileException e) {
    // I'm hoping the existing compiler problems handler catches these
    }
    final CompilationProblem[] result = new CompilationProblem[problems.size()];
    problems.toArray(result);
    return new CompilationResult(result);
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ArrayList(java.util.ArrayList) CompilationProblem(org.drools.compiler.commons.jci.problems.CompilationProblem) ScanException(org.codehaus.janino.Scanner.ScanException) CompileException(org.codehaus.janino.CompileException) StringPattern(org.codehaus.janino.util.StringPattern) Compiler(org.codehaus.janino.Compiler) ResourceFinder(org.codehaus.janino.util.resource.ResourceFinder) ErrorHandler(org.codehaus.janino.UnitCompiler.ErrorHandler) ResourceCreator(org.codehaus.janino.util.resource.ResourceCreator) WarningHandler(org.codehaus.janino.WarningHandler) FilterWarningHandler(org.codehaus.janino.FilterWarningHandler) Resource(org.codehaus.janino.util.resource.Resource) IOException(java.io.IOException) ClassLoaderIClassLoader(org.codehaus.janino.ClassLoaderIClassLoader) FilterWarningHandler(org.codehaus.janino.FilterWarningHandler) Collection(java.util.Collection) ParseException(org.codehaus.janino.Parser.ParseException) Location(org.codehaus.janino.Location)

Example 2 with ClassLoaderIClassLoader

use of org.codehaus.janino.ClassLoaderIClassLoader in project narchy by automenta.

the class DiffableFunctionMarshaller method compile.

private DiffableFunction compile(DiffableFunctionSource dfs) {
    SourceEnvironment se1 = new SourceEnvironment();
    String rv1 = dfs.valueToSource(se1);
    SourceEnvironment se2 = new SourceEnvironment();
    String rv2 = dfs.partialDeriveToSource(se2);
    String classPackage = getClass().getPackage().getName() + ".compiled";
    String className = "JaninoCompiledFastexpr" + COMPILED_CLASS_INDEX++;
    StringBuilder sb = new StringBuilder();
    sb.append("package ").append(classPackage).append(";\n");
    sb.append("import ").append(DiffableFunction.class.getCanonicalName()).append(";\n");
    sb.append("public class ").append(className).append(" implements DiffableFunction {\n");
    sb.append("public double value(double[] xValues) {\n");
    sb.append(se1);
    sb.append("return ").append(rv1).append(";\n");
    sb.append("}\n");
    sb.append("public double partialDerive(double[] xValues, int parameterIndex) {\n");
    sb.append(se2);
    sb.append("return ").append(rv2).append(";\n");
    sb.append("}\n");
    sb.append("}\n");
    try {
        Scanner scanner = new Scanner(null, new ByteArrayInputStream(sb.toString().getBytes("UTF-8")), "UTF-8");
        JaninoRestrictedClassLoader cl = new JaninoRestrictedClassLoader();
        UnitCompiler unitCompiler = new UnitCompiler(new Parser(scanner).parseCompilationUnit(), new ClassLoaderIClassLoader(cl));
        ClassFile[] classFiles = unitCompiler.compileUnit(debug, debug, debug);
        Class<?> clazz = cl.defineClass(classPackage + "." + className, classFiles[0].toByteArray());
        return (DiffableFunction) clazz.newInstance();
    } catch (CompileException | IOException | InstantiationException | IllegalAccessException ex) {
        Logger.getLogger(DiffableFunctionMarshaller.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}
Also used : Scanner(org.codehaus.janino.Scanner) UnitCompiler(org.codehaus.janino.UnitCompiler) ClassFile(org.codehaus.janino.util.ClassFile) IOException(java.io.IOException) Parser(org.codehaus.janino.Parser) ClassLoaderIClassLoader(org.codehaus.janino.ClassLoaderIClassLoader) ByteArrayInputStream(java.io.ByteArrayInputStream) CompileException(org.codehaus.commons.compiler.CompileException)

Aggregations

IOException (java.io.IOException)2 ClassLoaderIClassLoader (org.codehaus.janino.ClassLoaderIClassLoader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 CompileException (org.codehaus.commons.compiler.CompileException)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 Parser (org.codehaus.janino.Parser)1 ParseException (org.codehaus.janino.Parser.ParseException)1 Scanner (org.codehaus.janino.Scanner)1 ScanException (org.codehaus.janino.Scanner.ScanException)1 UnitCompiler (org.codehaus.janino.UnitCompiler)1 ErrorHandler (org.codehaus.janino.UnitCompiler.ErrorHandler)1 WarningHandler (org.codehaus.janino.WarningHandler)1 ClassFile (org.codehaus.janino.util.ClassFile)1