Example usage for java.io StringWriter getClass

List of usage examples for java.io StringWriter getClass

Introduction

In this page you can find the example usage for java.io StringWriter getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:lu.softec.xwiki.macro.internal.ClassRunnerMacro.java

protected String execute(ClassLoader loader, String className, ClassRunnerMacroParameters parameters,
        Object xcontext) throws MacroExecutionException {
    StringWriter stringWriter = new StringWriter();
    boolean hasArgs = (parameters.getRawProperties() != null);
    Map<String, Object> args = (hasArgs) ? parameters.getRawProperties() : new LinkedHashMap<String, Object>();

    Class<?> klass;/*from   w ww .  ja v a  2s . co m*/
    try {
        klass = loader.loadClass(className);
    } catch (ClassNotFoundException e) {
        throw new MacroExecutionException(e.getMessage(), e);
    }

    Method[] methods = klass.getMethods();
    Method run1 = null;
    Method run2 = null;
    Method run3 = null;
    Method setContext1 = null;
    Method setContext2 = null;
    Method getParser = null;
    for (Method m : methods) {
        if (m.getName().equals("run")) {
            Class<?>[] types = m.getParameterTypes();
            if (types.length == 1 && types[0].isAssignableFrom(stringWriter.getClass())) {
                run1 = m;
            } else if (types.length == 2 && types[0].isAssignableFrom(stringWriter.getClass())
                    && types[1].isAssignableFrom(xcontext.getClass())) {
                run2 = m;
            } else if (types.length == 3 && types[0].isAssignableFrom(stringWriter.getClass())
                    && types[1].isAssignableFrom(args.getClass())
                    && types[2].isAssignableFrom(xcontext.getClass())) {
                run3 = m;
            }
        } else if (m.getName().equals("setContext")) {
            Class<?>[] types = m.getParameterTypes();
            if (types.length == 1 && types[0].isAssignableFrom(xcontext.getClass())) {
                setContext1 = m;
            } else if (types.length == 2 && types[0].isAssignableFrom(args.getClass())
                    && types[1].isAssignableFrom(xcontext.getClass())) {
                setContext2 = m;
            }
        } else if (m.getName().equals("getParser")) {
            Class<?>[] types = m.getParameterTypes();
            if (types.length == 0) {
                getParser = m;
            }
        }
        if (run2 != null && run3 != null && setContext1 != null && setContext2 != null && getParser != null)
            break;
    }

    if (run2 == null && run3 == null) {
        throw new MacroExecutionException(
                "Unable to find the appropriate run(Writer,XWikiContext) or run(Writer,Map,XWikiContext) method in the class.");
    }

    try {
        Object obj = klass.newInstance();
        if (setContext2 != null && (hasArgs || setContext1 == null)) {
            setContext2.invoke(obj, args, xcontext);
        } else if (setContext1 != null) {
            setContext1.invoke(obj, xcontext);
        }
        if (setContext2 != null || setContext1 != null) {
            if (getParser != null) {
                String parser = null;
                parser = (String) getParser.invoke(obj);
                if (parser != null) {
                    parameters.setParser(parser);
                }
            }
            run1.invoke(obj, stringWriter);
        } else {
            if (run3 != null && (hasArgs || run2 == null)) {
                run3.invoke(obj, stringWriter, args, xcontext);
            } else {
                run2.invoke(obj, stringWriter, xcontext);
            }
        }
    } catch (Exception e) {
        throw new MacroExecutionException(e.getMessage(), e);
    }

    return (stringWriter.toString());
}