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