Search in sources :

Example 1 with JapidTemplateBaseWithoutPlay

use of cn.bran.japid.template.JapidTemplateBaseWithoutPlay in project Japid by branaway.

the class JapidPlayRenderer method getTemplateClass.

/**
	 * Get a newly loaded class for the template renderer
	 * 
	 * @param name
	 * @return
	 */
public static Class<? extends JapidTemplateBaseWithoutPlay> getTemplateClass(String name) {
    refreshClasses();
    RendererClass rc = japidClasses.get(name);
    if (rc == null)
        throw new RuntimeException("Japid template class not found: " + name);
    else {
        if (isDevMode() && playClassloaderChanged()) {
            // always clear the mark to redefine all
            for (String c : japidClasses.keySet()) {
                japidClasses.get(c).setLastUpdated(0);
            }
            initJapidClassLoader();
        } else {
            if (rc.getLastUpdated() > 0 && rc.getClz() != null) {
                return rc.getClz();
            }
        }
    }
    try {
        return (Class<? extends JapidTemplateBaseWithoutPlay>) japidClassLoader.loadClass(name);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}
Also used : JapidTemplateBaseWithoutPlay(cn.bran.japid.template.JapidTemplateBaseWithoutPlay) RendererClass(cn.bran.japid.rendererloader.RendererClass) RendererClass(cn.bran.japid.rendererloader.RendererClass)

Example 2 with JapidTemplateBaseWithoutPlay

use of cn.bran.japid.template.JapidTemplateBaseWithoutPlay in project Japid by branaway.

the class TemplateClassLoader method loadClass.

@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
    // System.out.println("TemplateClassLoader.loadClass(): " + name);
    if (!name.startsWith(JapidRenderer.JAPIDVIEWS)) {
        try {
            Class<?> cls = getParentClassLoader().loadClass(name);
            return cls;
        } catch (ClassNotFoundException e) {
            return super.loadClass(name);
        }
    }
    String oid = "[TemplateClassLoader@" + Integer.toHexString(hashCode()) + "]";
    RendererClass rc = getJapidRendererClassWrapper(name);
    if (rc.getLastUpdated() > 0) {
        Class<?> cla = localClasses.get(name);
        if (cla != null) {
            return cla;
        }
    }
    // System.out.println("[TemplateClassLoader] loading: " + name);
    byte[] bytecode = rc.getBytecode();
    if (bytecode == null) {
        throw new RuntimeException(oid + " could not find the bytecode for: " + name);
    }
    // the defineClass method will load the classes of the dependency
    // classes.
    Class<? extends JapidTemplateBaseWithoutPlay> cl = (Class<? extends JapidTemplateBaseWithoutPlay>) defineClass(name, bytecode, 0, bytecode.length);
    rc.setClz(cl);
    JapidFlags.debug("redefined: " + rc.getClassName());
    // extract the apply(...)
    long count = 0;
    for (Method m : cl.getDeclaredMethods()) {
        if (m.getName().equals("apply") && Modifier.isPublic(m.getModifiers()) && Modifier.isStatic(m.getModifiers())) {
            rc.setApplyMethod(m);
            count++;
        }
    }
    if (count == 0 && !name.contains("$")) {
        if (!Modifier.isAbstract(cl.getModifiers()))
            JapidFlags.warn("could not find the apply() with japid class: " + name);
    }
    localClasses.put(name, cl);
    // System.currentTimeMillis();
    rc.setLastUpdated(1);
    // System.out.println(oid + "class redefined from bytecode: " + name);
    return cl;
}
Also used : JapidTemplateBaseWithoutPlay(cn.bran.japid.template.JapidTemplateBaseWithoutPlay) Method(java.lang.reflect.Method)

Example 3 with JapidTemplateBaseWithoutPlay

use of cn.bran.japid.template.JapidTemplateBaseWithoutPlay in project Japid by branaway.

the class RenderInvokerUtils method invokeNamedArgsRender.

public static <T extends JapidTemplateBaseWithoutPlay> RenderResult invokeNamedArgsRender(Class<T> c, NamedArgRuntime[] args) {
    int modifiers = c.getModifiers();
    if (Modifier.isAbstract(modifiers)) {
        throw new RuntimeException("Cannot init the template class since it's an abstract class: " + c.getName());
    }
    try {
        // String methodName = "render";
        Constructor<T> ctor = c.getConstructor(StringBuilder.class);
        StringBuilder sb = new StringBuilder(8000);
        JapidTemplateBaseWithoutPlay t = ctor.newInstance(sb);
        RenderResult rr = (RenderResult) renderWithNamedArgs(t, args);
        JapidFlags.logTimeLogs(t);
        return rr;
    } catch (NoSuchMethodException e) {
        throw new RuntimeException("Could not match the arguments with the template args.");
    } catch (InstantiationException e) {
        // e.printStackTrace();
        throw new RuntimeException("Could not instantiate the template object. Abstract?");
    } catch (InvocationTargetException e) {
        // e.printStackTrace();
        Throwable e1 = e.getTargetException();
        throw new RuntimeException("Could not invoke the template object:  ", e1);
    } catch (Exception e) {
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new RuntimeException("Could not invoke the template object: ", e);
    // throw new RuntimeException(e);
    }
}
Also used : JapidTemplateBaseWithoutPlay(cn.bran.japid.template.JapidTemplateBaseWithoutPlay) RenderResult(cn.bran.japid.template.RenderResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with JapidTemplateBaseWithoutPlay

use of cn.bran.japid.template.JapidTemplateBaseWithoutPlay in project japid42 by branaway.

the class RenderInvokerUtils method invokeNamedArgsRender.

public static <T extends JapidTemplateBaseWithoutPlay> RenderResult invokeNamedArgsRender(Class<T> c, NamedArgRuntime[] args) {
    int modifiers = c.getModifiers();
    if (Modifier.isAbstract(modifiers)) {
        throw new RuntimeException("Cannot init the template class since it's an abstract class: " + c.getName());
    }
    try {
        // String methodName = "render";
        Constructor<T> ctor = c.getConstructor(StringBuilder.class);
        StringBuilder sb = new StringBuilder(8000);
        JapidTemplateBaseWithoutPlay t = ctor.newInstance(sb);
        RenderResult rr = renderWithNamedArgs(t, args);
        JapidFlags.logTimeLogs(t);
        return rr;
    } catch (NoSuchMethodException e) {
        throw new RuntimeException("Could not match the arguments with the template args.");
    } catch (InstantiationException e) {
        // e.printStackTrace();
        throw new RuntimeException("Could not instantiate the template object. Abstract?");
    } catch (InvocationTargetException e) {
        // e.printStackTrace();
        Throwable e1 = e.getTargetException();
        throw new RuntimeException("Could not invoke the template object:  ", e1);
    } catch (Exception e) {
        if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new RuntimeException("Could not invoke the template object: ", e);
    // throw new RuntimeException(e);
    }
}
Also used : JapidTemplateBaseWithoutPlay(cn.bran.japid.template.JapidTemplateBaseWithoutPlay) RenderResult(cn.bran.japid.template.RenderResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

JapidTemplateBaseWithoutPlay (cn.bran.japid.template.JapidTemplateBaseWithoutPlay)4 RenderResult (cn.bran.japid.template.RenderResult)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 RendererClass (cn.bran.japid.rendererloader.RendererClass)1 Method (java.lang.reflect.Method)1