/** * Compile the class from Java source * * @return the bytes that comprise the class file */ public byte[] compile() { if (null != javaByteCode) return javaByteCode; if (null == javaSource) throw new IllegalStateException("Cannot find java source when compiling " + getKey()); compiling = true; long start = System.currentTimeMillis(); try { engine().classes.compiler.compile(new String[] {name()}); if (logger.isTraceEnabled()) { logger.trace("%sms to compile template: %s", System.currentTimeMillis() - start, getKey()); } } catch (CompileException.CompilerException e) { String cn = e.className; TemplateClass tc = S.isEqual(cn, name()) ? this : engine().classes.getByClassName(cn); if (null == tc) tc = this; CompileException ce = new CompileException( tc, e.javaLineNumber, e.message); // init ce before reset java source to get template line info if (engine().isProdMode()) { TextBuilder tb = new TextBuilder(); String[] lines = javaSource.split("(\\n\\r|\\r\\n|\\r|\\n)"); for (int line = 0; line < lines.length; ++line) { tb.p(line + 1).p(":").p(lines[line]).p("\n"); } logger.error("error compiling java source:\n%s", tb.toString()); } javaSource = null; // force parser to regenerate source. This helps to reload after fixing the tag file // compilation failure throw ce; } catch (NullPointerException e) { String clazzName = name(); TemplateClass tc = engine().classes.getByClassName(clazzName); if (this != tc) { logger.error("tc is not this"); } if (!this.equals(tc)) { logger.error("tc not match this"); } logger.error("NPE encountered when compiling template class:" + name()); throw e; } finally { compiling = false; } if (logger.isTraceEnabled()) { logger.trace( "%sms to compile template class %s", System.currentTimeMillis() - start, getKey()); } return javaByteCode; }