use of play.exceptions.CompilationException in project Japid by branaway.
the class JapidPlayRenderer method refreshClasses.
static synchronized void refreshClasses() {
if (classesInited) {
if (!timeToRefresh())
return;
} else {
JapidFlags.info("Japid scripts not loaded yet. Initializing them...");
}
try {
// PlayDirUtil.mkdir(templateRoot);
// find out all removed classes
List<String> allTemps = isDevMode() ? DirUtil.getAllTemplateFiles(new File(defaultTemplateRoot)) : DirUtil.getAllTemplateFilesJavaFiles(new File(defaultTemplateRoot));
Set<String> currentClassesOnDir = createNameSet(allTemps);
Set<String> allNames = new HashSet<String>(currentClassesOnDir);
Set<String> keySet = japidClasses.keySet();
// got new templates
allNames.removeAll(keySet);
removeRemoved(currentClassesOnDir, keySet);
for (String c : allNames) {
RendererClass rc = newRendererClass(c);
japidClasses.put(c, rc);
}
// now all the class set size is up to date
// now update any Java source code
List<File> gen = gen(defaultTemplateRoot);
// this would include both new and updated java
Set<String> updatedClasses = new HashSet<String>();
if (gen.size() > 0) {
for (File f : gen) {
String className = getClassName(f);
updatedClasses.add(className);
RendererClass rendererClass = japidClasses.get(className);
if (rendererClass == null) {
// this should not happen, since
throw new RuntimeException("any new class names should have been in the classes container: " + className);
// rendererClass = newRendererClass(className);
// classes.put(className, rendererClass);
}
JapidRenderer.setSources(rendererClass, f);
removeInnerClasses(className);
cleanClassHolder(rendererClass);
}
}
// find all render class without bytecode
for (Iterator<String> i = japidClasses.keySet().iterator(); i.hasNext(); ) {
String k = i.next();
RendererClass rc = japidClasses.get(k);
if (rc.getSourceCode() == null) {
if (!rc.getClassName().contains("$")) {
String pathname = defaultTemplateRoot + sep + k.replace(".", sep);
File f = new File(pathname + ".java");
JapidRenderer.setSources(rc, f);
cleanClassHolder(rc);
updatedClasses.add(k);
} else {
rc.setLastUpdated(0);
}
} else {
if (rc.getBytecode() == null) {
cleanClassHolder(rc);
updatedClasses.add(k);
}
}
}
// compile all
if (updatedClasses.size() > 0) {
String[] names = new String[updatedClasses.size()];
int i = 0;
for (String s : updatedClasses) {
names[i++] = s;
}
long t = System.currentTimeMillis();
// newly compiled class bytecode bodies are set in the global
// classes set ready for defining
compiler.compile(names);
initJapidClassLoader();
for (RendererClass rc : japidClasses.values()) {
try {
if (isDevMode())
// to enable JIT loading in DEV mode
rc.setClz(null);
else
rc.setClz((Class<JapidTemplateBaseWithoutPlay>) japidClassLoader.loadClass(rc.getClassName()));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
howlong("compile/load time for " + names.length + " classe(s)", t);
classesInited = true;
}
} catch (JapidCompilationException e) {
if (presentErrorInHtml)
throw e;
String tempName = e.getTemplateName();
if (tempName.startsWith(defaultTemplateRoot)) {
} else {
tempName = defaultTemplateRoot + File.separator + tempName;
}
VirtualFile vf = VirtualFile.fromRelativePath(tempName);
CompilationException ce = new CompilationException(vf, "\"" + e.getMessage() + "\"", e.getLineNumber(), 0, 0);
throw ce;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of play.exceptions.CompilationException in project Japid by branaway.
the class PlayExceptionUtils method mapJapidJavaCodeError.
public static Exception mapJapidJavaCodeError(Exception ex) {
if (ex instanceof CompilationException) {
CompilationException e = (CompilationException) ex;
if (!e.isSourceAvailable())
return e;
// now map java error to japidview source code
String srcFilePath = e.getSourceFile();
if (!srcFilePath.startsWith("/app/japidviews/")) {
return e;
} else if (!srcFilePath.endsWith("java")) {
return e;
}
String viewSourceFilePath = DirUtil.mapJavaToSrc(srcFilePath);
// File file = new File(viewSourceFilePath);
VirtualFile vf = VirtualFile.fromRelativePath(viewSourceFilePath);
int oriLineNumber = mapJavaErrorLineToSrcLine(e.getSourceVirtualFile().contentAsString(), e.getLineNumber());
e = new CompilationException(vf, "\"" + e.getMessage() + "\"", oriLineNumber, 0, 0);
return e;
}
return ex;
}
use of play.exceptions.CompilationException in project Japid by branaway.
the class JapidPlugin method beforeDetectingChanges.
@Override
public void beforeDetectingChanges() {
// have a delay in change detection.
if (System.currentTimeMillis() - lastTimeChecked.get() < 1000)
return;
try {
List<File> changed = JapidCommands.reloadChanged();
if (changed.size() > 0) {
for (File f : changed) {
// System.out.println("pre-detect changed: " + f.getName());
}
}
} catch (JapidCompilationException e) {
// turn japid compilation error to Play's template error to get
// better error reporting
JapidPlayTemplate jpt = new JapidPlayTemplate();
jpt.name = e.getTemplateName();
jpt.source = e.getTemplateSrc();
// throw new TemplateExecutionException(jpt, e.getLineNumber(),
// e.getMessage(), e);
VirtualFile vf = VirtualFile.fromRelativePath("/app/" + e.getTemplateName());
throw new CompilationException(vf, "\"" + e.getMessage() + "\"", e.getLineNumber(), 0, 0);
} catch (RuntimeException e) {
// e.printStackTrace();
throw e;
}
boolean hasRealOrphan = JapidCommands.rmOrphanJava();
lastTimeChecked.set(System.currentTimeMillis());
if (hasRealOrphan) {
// a little messy here. clean the cache in case bad files are delete
// remove all the existing ApplicationClass will reload everything.
// ideally we just need to remove the orphan. But the internal cache
// is not visible. Need API change to do that.
Play.classes.clear();
throw new RuntimeException("found orphan template Java artifacts. reload to be safe.");
}
}
Aggregations