Search in sources :

Example 1 with JapidTemplateException

use of cn.bran.japid.exceptions.JapidTemplateException in project Japid by branaway.

the class JapidPlayRenderer method handleException.

public static RenderResult handleException(Throwable e) throws RuntimeException {
    if (!presentErrorInHtml)
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new RuntimeException(e);
    // if (Play.mode == Mode.PROD)
    // throw new RuntimeException(e);
    // 
    Class<? extends JapidTemplateBaseWithoutPlay> rendererClass = getErrorRendererClass();
    if (e instanceof JapidTemplateException) {
        RenderResult rr = RenderInvokerUtils.invokeRender(rendererClass, (JapidTemplateException) e);
        return (rr);
    }
    if (e instanceof RuntimeException && e.getCause() != null)
        e = e.getCause();
    if (e instanceof JapidCompilationException) {
        JapidCompilationException jce = (JapidCompilationException) e;
        JapidTemplateException te = JapidTemplateException.from(jce);
        RenderResult rr = RenderInvokerUtils.invokeRender(rendererClass, te);
        return (rr);
    }
    e.printStackTrace();
    // find the latest japidviews exception or the controller that caused
    // the exception
    StackTraceElement[] stackTrace = e.getStackTrace();
    for (StackTraceElement ele : stackTrace) {
        String className = ele.getClassName();
        if (className.startsWith("japidviews")) {
            int lineNumber = ele.getLineNumber();
            RendererClass applicationClass = japidClasses.get(className);
            if (applicationClass != null) {
                // let's get the line of problem
                int oriLineNumber = applicationClass.mapJavaLineToJapidScriptLine(lineNumber);
                if (oriLineNumber > 0) {
                    if (rendererClass != null) {
                        String path = applicationClass.getOriSourceCode();
                        JapidTemplateException te = new JapidTemplateException("Japid Error", path + "(" + oriLineNumber + "): " + e.getClass().getName() + ": " + e.getMessage(), oriLineNumber, path, applicationClass.getOriSourceCode());
                        RenderResult rr = RenderInvokerUtils.invokeRender(rendererClass, te);
                        return (rr);
                    }
                }
            }
        } else if (className.startsWith("controllers.")) {
            if (e instanceof RuntimeException)
                throw (RuntimeException) e;
            else
                throw new RuntimeException(e);
        }
    }
    JapidTemplateException te = new JapidTemplateException(e);
    RenderResult rr = RenderInvokerUtils.invokeRender(rendererClass, te);
    return rr;
// if (e instanceof RuntimeException)
// throw (RuntimeException) e;
// else
// throw new RuntimeException(e);
}
Also used : JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) RenderResult(cn.bran.japid.template.RenderResult) RendererClass(cn.bran.japid.rendererloader.RendererClass)

Example 2 with JapidTemplateException

use of cn.bran.japid.exceptions.JapidTemplateException in project japid42 by branaway.

the class JapidRenderer method refreshClassesInMemory.

/**
 * all artifacts in memory
 *
 * @author Bing Ran (bing.ran@gmail.com)
 */
static synchronized void refreshClassesInMemory() {
    if (templateRoots == null)
        return;
    try {
        Set<File> allTemplates = DirUtil.getAllTemplateFiles(templateRoots);
        Set<File> toBeUpdated = new HashSet<File>();
        // find out all the classes that need to be updated
        for (File tf : allTemplates) {
            String cname = getClassName(tf);
            RendererClass rc = japidClasses.get(cname);
            if (rc == null) {
                toBeUpdated.add(tf);
            } else if (rc.getScriptTimestamp() < tf.lastModified()) {
                toBeUpdated.add(tf);
            } else if (rc.getJavaSourceCode() == null || rc.getJavaSourceCode().length() == 0) {
                toBeUpdated.add(tf);
            } else if (rc.getBytecode() == null || rc.getBytecode().length == 0) {
                toBeUpdated.add(tf);
            }
        }
        Set<String> currentClassesOnDir = createClassNameSet(allTemplates);
        Set<String> currentClassNames = japidClasses.keySet();
        if (!currentClassNames.equals(currentClassesOnDir)) {
            Set<String> classNamesRegistered = new HashSet<String>(currentClassNames);
            Set<String> classNamesDir = new HashSet<String>(currentClassesOnDir);
            if (classNamesRegistered.containsAll(classNamesDir)) {
                classNamesRegistered.removeAll(classNamesDir);
                if (!classNamesRegistered.isEmpty()) {
                    for (String n : classNamesRegistered) {
                        if (!n.contains("$")) {
                            if (!specialClasses.contains(n)) {
                                touch();
                                break;
                            }
                        }
                    }
                }
            } else {
                touch();
            }
        } else {
        // no name changes
        }
        // allClassNamesOnDir.removeAll(currentClassNames); // got new
        // templates
        removeRemoved(currentClassesOnDir, currentClassNames);
        for (File tb : toBeUpdated) {
            String scriptSrc = DirUtil.readFileAsString(tb);
            String javaCode = JapidTemplateTransformer.generateInMemory(scriptSrc, cleanPath(tb), usePlay);
            JapidFlags.log("converted: " + tb.getPath());
            String className = getClassName(tb);
            RendererClass rc = newRendererClass(className);
            rc.setScriptFile(tb);
            rc.setJapidSourceCode(scriptSrc);
            rc.setJavaSourceCode(javaCode);
            removeInnerClasses(className);
            cleanByteCode(rc);
            japidClasses.put(className, rc);
        }
        setupImports();
        // compile all
        if (toBeUpdated.size() > 0) {
            // XXX why clear the dynamics?
            dynamicClasses.clear();
            Set<String> names = createClassNameSet(toBeUpdated);
            long t = System.currentTimeMillis();
            compiler.compile(names.toArray(new String[] {}));
            howlong("compile time for " + names.size() + " classes", t);
            TemplateClassLoader loader = getClassLoader();
            for (String cname : names) {
                loader.loadClass(cname);
            }
        }
    } catch (Exception e) {
        if (e instanceof JapidTemplateException)
            throw (JapidTemplateException) e;
        throw new RuntimeException(e);
    }
}
Also used : JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) RendererClass(cn.bran.japid.rendererloader.RendererClass) JarFile(java.util.jar.JarFile) File(java.io.File) TemplateClassLoader(cn.bran.japid.rendererloader.TemplateClassLoader) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 3 with JapidTemplateException

use of cn.bran.japid.exceptions.JapidTemplateException in project japid42 by branaway.

the class CompilerRequestor method acceptResult.

@Override
public void acceptResult(CompilationResult result) {
    // If error
    if (result.hasErrors()) {
        // bran: sort the problems and report the first one
        CategorizedProblem[] errors = result.getErrors();
        Arrays.sort(errors, new Comparator<CategorizedProblem>() {

            @Override
            public int compare(CategorizedProblem o1, CategorizedProblem o2) {
                return o1.getSourceLineNumber() - o2.getSourceLineNumber();
            }
        });
        for (IProblem problem : errors) {
            String className = new String(problem.getOriginatingFileName()).replace("/", ".");
            className = className.substring(0, className.length() - 5);
            String message = problem.getMessage();
            int line = problem.getSourceLineNumber();
            String srcFile = new String(problem.getOriginatingFileName());
            if (problem.getID() == IProblem.CannotImportPackage) {
                // Non sense !
                message = problem.getArguments()[0] + " cannot be resolved";
            }
            RendererClass rc = JapidRenderer.japidClasses.get(className);
            if (rc.getJapidSourceCode() == null)
                throw new RuntimeException("no original source code for compiling " + className);
            String descr = srcFile + "(" + line + "): " + message;
            String javaSourceCode = rc.getJavaSourceCode();
            int oriSrcLineNum = DirUtil.mapJavaLineToSrcLine(javaSourceCode, problem.getSourceLineNumber());
            String scriptPath = rc.getScriptPath();
            if (oriSrcLineNum > 0) {
                // has a original script marker
                descr = scriptPath + "(line " + oriSrcLineNum + "): " + message;
                JapidTemplateException te = new JapidTemplateException("Japid Compilation Error", descr, oriSrcLineNum, scriptPath, rc.getJapidSourceCode());
                throw te;
            } else {
                JapidTemplateException te = new JapidTemplateException("Japid Compilation Error", descr, line, srcFile, javaSourceCode);
                throw te;
            }
        }
    }
    // Something has been compiled
    ClassFile[] clazzFiles = result.getClassFiles();
    for (int i = 0; i < clazzFiles.length; i++) {
        final ClassFile clazzFile = clazzFiles[i];
        final char[][] compoundName = clazzFile.getCompoundName();
        final StringBuffer clazzName = new StringBuffer();
        for (int j = 0; j < compoundName.length; j++) {
            if (j != 0) {
                clazzName.append('.');
            }
            clazzName.append(compoundName[j]);
        }
        byte[] bytes = clazzFile.getBytes();
        JapidFlags.debug("compiled: " + clazzName);
        // XXX address anonymous inner class issue!! ....$1...
        String cname = clazzName.toString();
        RendererClass rc = JapidRenderer.japidClasses.get(cname);
        if (rc == null) {
            if (cname.contains("$")) {
                // inner class
                rc = new RendererClass();
                rc.className = cname;
                JapidRenderer.japidClasses.put(cname, rc);
            } else {
                throw new RuntimeException("name not in the classes container: " + cname);
            }
        }
        rc.setBytecode(bytes);
        rc.setClz(null);
        rc.setLastCompiled(System.currentTimeMillis());
    }
}
Also used : ClassFile(org.eclipse.jdt.internal.compiler.ClassFile) IProblem(org.eclipse.jdt.core.compiler.IProblem) CategorizedProblem(org.eclipse.jdt.core.compiler.CategorizedProblem) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException)

Example 4 with JapidTemplateException

use of cn.bran.japid.exceptions.JapidTemplateException in project japid42 by branaway.

the class JapidRenderer method handleException.

public static RenderResult handleException(Throwable e) throws RuntimeException {
    if (!presentErrorInHtml)
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new RuntimeException(e);
    // if (Play.mode == Mode.PROD)
    // throw new RuntimeException(e);
    // 
    Class<? extends JapidTemplateBaseWithoutPlay> rendererClass = getErrorRendererClass();
    if (e instanceof JapidTemplateException) {
        RenderResult rr = RenderInvokerUtils.invokeRender(rendererClass, (JapidTemplateException) e);
        return (rr);
    }
    if (e instanceof RuntimeException && e.getCause() != null)
        e = e.getCause();
    if (e instanceof JapidCompilationException) {
        JapidCompilationException jce = (JapidCompilationException) e;
        JapidTemplateException te = JapidTemplateException.from(jce);
        RenderResult rr = RenderInvokerUtils.invokeRender(rendererClass, te);
        return (rr);
    }
    e.printStackTrace();
    // find the latest japidviews exception or the controller that caused
    // the exception
    StackTraceElement[] stackTrace = e.getStackTrace();
    for (StackTraceElement ele : stackTrace) {
        String className = ele.getClassName();
        if (className.startsWith("japidviews")) {
            int lineNumber = ele.getLineNumber();
            RendererClass applicationClass = japidClasses.get(className);
            if (applicationClass != null) {
                // let's get the line of problem
                int oriLineNumber = applicationClass.mapJavaLineToJapidScriptLine(lineNumber);
                if (oriLineNumber > 0) {
                    if (rendererClass != null) {
                        String path = applicationClass.getScriptPath();
                        JapidTemplateException te = new JapidTemplateException("Japid Error", path + "(" + oriLineNumber + "): " + e.getClass().getName() + ": " + e.getMessage(), oriLineNumber, path, applicationClass.getJapidSourceCode());
                        RenderResult rr = RenderInvokerUtils.invokeRender(rendererClass, te);
                        return (rr);
                    }
                }
            }
        } else if (className.startsWith("controllers.")) {
            if (e instanceof RuntimeException)
                throw (RuntimeException) e;
            else
                throw new RuntimeException(e);
        }
    }
    JapidTemplateException te = new JapidTemplateException(e);
    RenderResult rr = RenderInvokerUtils.invokeRender(rendererClass, te);
    return rr;
// if (e instanceof RuntimeException)
// throw (RuntimeException) e;
// else
// throw new RuntimeException(e);
}
Also used : JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) RendererClass(cn.bran.japid.rendererloader.RendererClass)

Example 5 with JapidTemplateException

use of cn.bran.japid.exceptions.JapidTemplateException in project japid42 by branaway.

the class JapidRenderer method process.

// compile japid script to Java code from an inputstream
private static RendererClass process(String name, InputStream input) throws IOException {
    InputStreamReader isr = new InputStreamReader(input, "UTF-8");
    BufferedReader reader = new BufferedReader(isr);
    String content = "";
    String line;
    while ((line = reader.readLine()) != null) content += line + "\n";
    reader.close();
    String fqName = DirUtil.deriveClassName(name);
    try {
        String javaCode = JapidTemplateTransformer.generateInMemory(content, (fqName), MimeTypeEnum.inferFromName(name), usePlay);
        // System.out.println(javaCode);
        RendererClass rc = newRendererClass(fqName);
        rc.setJapidSourceCode(content);
        rc.setJavaSourceCode(javaCode);
        removeInnerClasses(fqName);
        cleanByteCode(rc);
        // remember the current impl of class
        japidClasses.put(fqName, rc);
        // class from this container.
        return rc;
    } catch (Exception e) {
        if (e instanceof JapidTemplateException)
            throw (JapidTemplateException) e;
        throw new RuntimeException(e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) RendererClass(cn.bran.japid.rendererloader.RendererClass) JapidTemplateException(cn.bran.japid.exceptions.JapidTemplateException) JapidCompilationException(cn.bran.japid.compiler.JapidCompilationException) IOException(java.io.IOException)

Aggregations

JapidTemplateException (cn.bran.japid.exceptions.JapidTemplateException)8 JapidCompilationException (cn.bran.japid.compiler.JapidCompilationException)7 RendererClass (cn.bran.japid.rendererloader.RendererClass)7 IOException (java.io.IOException)5 TemplateClassLoader (cn.bran.japid.rendererloader.TemplateClassLoader)3 File (java.io.File)2 HashSet (java.util.HashSet)2 JarFile (java.util.jar.JarFile)2 RenderResult (cn.bran.japid.template.RenderResult)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 CategorizedProblem (org.eclipse.jdt.core.compiler.CategorizedProblem)1 IProblem (org.eclipse.jdt.core.compiler.IProblem)1 ClassFile (org.eclipse.jdt.internal.compiler.ClassFile)1