Search in sources :

Example 1 with ScanException

use of org.codehaus.janino.Scanner.ScanException in project joons-renderer by joonhyublee.

the class Plugins method registerPlugin.

/**
 * Define a new plugin type from java source code. The code string contains
 * import declarations and a class body only. The implemented type is
 * implicitly the one of the plugin list being registered against.If the
 * plugin type name was previously associated with a different class, it
 * will be overriden. This allows the behavior core classes to be modified
 * at runtime.
 *
 * @param name plugin type name
 * @param sourceCode Java source code definition for the plugin
 * @return <code>true</code> if the code compiled and registered
 * successfully, <code>false</code> otherwise
 */
@SuppressWarnings("unchecked")
public boolean registerPlugin(String name, String sourceCode) {
    try {
        ClassBodyEvaluator cbe = new ClassBodyEvaluator();
        cbe.setClassName(name);
        if (baseClass.isInterface()) {
            cbe.setImplementedTypes(new Class[] { baseClass });
        } else {
            cbe.setExtendedType(baseClass);
        }
        cbe.cook(sourceCode);
        return registerPlugin(name, cbe.getClazz());
    } catch (CompileException e) {
        UI.printError(Module.API, "Plugin \"%s\" could not be declared - %s", name, e.getLocalizedMessage());
        return false;
    } catch (ParseException e) {
        UI.printError(Module.API, "Plugin \"%s\" could not be declared - %s", name, e.getLocalizedMessage());
        return false;
    } catch (ScanException e) {
        UI.printError(Module.API, "Plugin \"%s\" could not be declared - %s", name, e.getLocalizedMessage());
        return false;
    }
}
Also used : ScanException(org.codehaus.janino.Scanner.ScanException) ClassBodyEvaluator(org.codehaus.janino.ClassBodyEvaluator) CompileException(org.codehaus.janino.CompileException) ParseException(org.codehaus.janino.Parser.ParseException)

Example 2 with ScanException

use of org.codehaus.janino.Scanner.ScanException in project jaffa-framework by jaffa-projects.

the class MDCFilter method getExpressionEvaluator.

/**
 * Create "ExpressionEvaluator" object.
 *
 * @param types
 *          <code>List</code>
 * @param values
 *          <code>String</code>
 * @return <code>ExpressionEvaluator</code>
 */
private ExpressionEvaluator getExpressionEvaluator(final List<Class<String>> types, final String[] values) {
    ExpressionEvaluator evaluator = ExpressionCache.getInstance().get(expression);
    if (evaluator == null) {
        evaluator = new ExpressionEvaluator();
        evaluator.setParameters(values, types.toArray(new Class[0]));
        try {
            evaluator.cook(expression);
            ExpressionCache.getInstance().put(expression, evaluator);
        } catch (CompileException e) {
            LOG.error(e);
        } catch (ParseException e) {
            LOG.error(e);
        } catch (ScanException e) {
            LOG.error(e);
        }
    }
    return evaluator;
}
Also used : ScanException(org.codehaus.janino.Scanner.ScanException) CompileException(org.codehaus.janino.CompileException) ParseException(org.codehaus.janino.Parser.ParseException) ExpressionEvaluator(org.codehaus.janino.ExpressionEvaluator)

Example 3 with ScanException

use of org.codehaus.janino.Scanner.ScanException 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 4 with ScanException

use of org.codehaus.janino.Scanner.ScanException in project joons-renderer by joonhyublee.

the class SunflowAPI method compile.

/**
 * Compile the specified code string via Janino. The code must implement a
 * build method as described above. The build method is not called on the
 * output, it is up the caller to do so.
 *
 * @param code java code string
 * @return a valid SunflowAPI object upon succes, <code>null</code>
 * otherwise.
 */
public static SunflowAPI compile(String code) {
    try {
        Timer t = new Timer();
        t.start();
        SunflowAPI api = (SunflowAPI) ClassBodyEvaluator.createFastClassBodyEvaluator(new Scanner(null, new StringReader(code)), SunflowAPI.class, (ClassLoader) null);
        t.end();
        UI.printInfo(Module.API, "Compile time: %s", t.toString());
        return api;
    } catch (CompileException e) {
        UI.printError(Module.API, "%s", e.getMessage());
        return null;
    } catch (ParseException e) {
        UI.printError(Module.API, "%s", e.getMessage());
        return null;
    } catch (ScanException e) {
        UI.printError(Module.API, "%s", e.getMessage());
        return null;
    } catch (IOException e) {
        UI.printError(Module.API, "%s", e.getMessage());
        return null;
    }
}
Also used : Scanner(org.codehaus.janino.Scanner) Timer(org.sunflow.system.Timer) ScanException(org.codehaus.janino.Scanner.ScanException) StringReader(java.io.StringReader) CompileException(org.codehaus.janino.CompileException) ParseException(org.codehaus.janino.Parser.ParseException) IOException(java.io.IOException)

Example 5 with ScanException

use of org.codehaus.janino.Scanner.ScanException in project joons-renderer by joonhyublee.

the class SunflowAPI method create.

/**
 * Create an API object from the specified file. Java files are read by
 * Janino and are expected to implement a build method (they implement a
 * derived class of SunflowAPI. The build method is called if the code
 * compiles succesfully. Other files types are handled by the parse method.
 *
 * @param filename filename to load
 * @return a valid SunflowAPI object or <code>null</code> on failure
 */
public static SunflowAPI create(String filename, int frameNumber) {
    if (filename == null) {
        return new SunflowAPI();
    }
    SunflowAPI api = null;
    if (filename.endsWith(".java")) {
        Timer t = new Timer();
        UI.printInfo(Module.API, "Compiling \"" + filename + "\" ...");
        t.start();
        try {
            FileInputStream stream = new FileInputStream(filename);
            api = (SunflowAPI) ClassBodyEvaluator.createFastClassBodyEvaluator(new Scanner(filename, stream), SunflowAPI.class, ClassLoader.getSystemClassLoader());
            stream.close();
        } catch (CompileException e) {
            UI.printError(Module.API, COULDNT_MESSAGE_FORMAT, filename);
            UI.printError(Module.API, "%s", e.getMessage());
            return null;
        } catch (ParseException e) {
            UI.printError(Module.API, COULDNT_MESSAGE_FORMAT, filename);
            UI.printError(Module.API, "%s", e.getMessage());
            return null;
        } catch (ScanException e) {
            UI.printError(Module.API, COULDNT_MESSAGE_FORMAT, filename);
            UI.printError(Module.API, "%s", e.getMessage());
            return null;
        } catch (IOException e) {
            UI.printError(Module.API, COULDNT_MESSAGE_FORMAT, filename);
            UI.printError(Module.API, "%s", e.getMessage());
            return null;
        }
        t.end();
        UI.printInfo(Module.API, "Compile time: " + t.toString());
        // allow relative paths
        String currentFolder = new File(filename).getAbsoluteFile().getParentFile().getAbsolutePath();
        api.includeSearchPath.addSearchPath(currentFolder);
        api.textureSearchPath.addSearchPath(currentFolder);
        UI.printInfo(Module.API, "Build script running ...");
        t.start();
        api.currentFrame(frameNumber);
        api.build();
        t.end();
        UI.printInfo(Module.API, "Build script time: %s", t.toString());
    } else {
        api = new SunflowAPI();
        api = api.include(filename) ? api : null;
    }
    return api;
}
Also used : Scanner(org.codehaus.janino.Scanner) Timer(org.sunflow.system.Timer) ScanException(org.codehaus.janino.Scanner.ScanException) CompileException(org.codehaus.janino.CompileException) ParseException(org.codehaus.janino.Parser.ParseException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

CompileException (org.codehaus.janino.CompileException)5 ParseException (org.codehaus.janino.Parser.ParseException)5 ScanException (org.codehaus.janino.Scanner.ScanException)5 IOException (java.io.IOException)3 Scanner (org.codehaus.janino.Scanner)2 Timer (org.sunflow.system.Timer)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 OutputStream (java.io.OutputStream)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 ClassBodyEvaluator (org.codehaus.janino.ClassBodyEvaluator)1 ClassLoaderIClassLoader (org.codehaus.janino.ClassLoaderIClassLoader)1 Compiler (org.codehaus.janino.Compiler)1 ExpressionEvaluator (org.codehaus.janino.ExpressionEvaluator)1 FilterWarningHandler (org.codehaus.janino.FilterWarningHandler)1 Location (org.codehaus.janino.Location)1 ErrorHandler (org.codehaus.janino.UnitCompiler.ErrorHandler)1