final byte[] content = resourceReader.getBytes(resourceNameFromClass);
if (content == null) {
return null;
}
final Reader reader = new BufferedReader(new StringReader(new String(content)));
Scanner scanner = null;
try {
scanner = new Scanner(resourceNameFromClass, reader);
final Java.CompilationUnit unit = new Parser(scanner).parseCompilationUnit();
final UnitCompiler uc = new UnitCompiler(unit, this);
uc.setCompileErrorHandler(new ErrorHandler() {
public void handleError(final String pMessage, final Location pOptionalLocation) throws CompileException {
final CompilationProblem problem = new JaninoCompilationProblem(pOptionalLocation, pMessage, true);
if (problemHandler != null) {
problemHandler.handle(problem);
}
problems.add(problem);
}
});
uc.setWarningHandler(new WarningHandler() {
public void handleWarning(final String pHandle, final String pMessage, final Location pOptionalLocation) {
final CompilationProblem problem = new JaninoCompilationProblem(pOptionalLocation, pMessage, false);
if (problemHandler != null) {
problemHandler.handle(problem);
}
problems.add(problem);
}
});
log.debug("compile " + className);
final ClassFile[] classFiles = uc.compileUnit(DebuggingInformation.ALL);
for (int i = 0; i < classFiles.length; i++) {
log.debug("compiled " + classFiles[i].getThisClassName());
classes.put(classFiles[i].getThisClassName(), classFiles[i].toByteArray());
}
final IClass ic = uc.findClass(className);
if (null != ic) {
types.put(pType, ic);
}
return ic;
} catch (final LocatedException e) {
problems.add(new JaninoCompilationProblem(e));
} catch (final IOException e) {
problems.add(new JaninoCompilationProblem(resourceNameFromClass, "IOException:" + e.getMessage(), true));
} catch (final Exception e) {
problems.add(new JaninoCompilationProblem(resourceNameFromClass, "Exception:" + e.getMessage(), true));
} finally {
if (scanner != null) {
try {
scanner.close();
} catch (IOException e) {
log.error("IOException occured while compiling " + className, e);
}
}
}