private Method getMainMethod()

in freemarker-docgen-core/src/main/java/org/freemarker/docgen/core/PrintTextWithDocgenSubstitutionsDirective.java [489:530]


        private Method getMainMethod(String cmdKey, Transform.InsertableOutputCommandProperties cmdProps) throws
                TemplateException {
            String mainClassName = cmdProps.getMainClassName();
            Class<?> mainClass;
            try {
                mainClass = Transform.class.getClassLoader().loadClass(mainClassName);
            } catch (Exception e) {
                throw newErrorInDocgenTag(
                        "The main class referred by "
                                + Transform.SETTING_INSERTABLE_OUTPUT_COMMANDS + "[" + StringUtil.jQuote(cmdKey) + "], "
                                + StringUtil.jQuote(mainClassName) + ", couldn't be loaded",
                        e);
            }

            String mainMethodName = cmdProps.getMainMethodName();
            Method mainMethod;
            try {
                mainMethod = mainClass.getMethod(mainMethodName, String[].class);
            } catch (Exception e) {
                throw newErrorInDocgenTag(
                        "Couldn't get " + mainMethodName + "(String[]) method from class "
                                + mainClassName + ".",
                        e);
            }
            if ((mainMethod.getModifiers() & Modifier.STATIC) == 0) {
                throw newErrorInDocgenTag(
                        mainMethodName + "(String[]) method from class "
                                + mainClassName + " must be static.");
            }
            if ((mainMethod.getModifiers() & Modifier.PUBLIC) == 0) {
                throw newErrorInDocgenTag(
                        mainMethodName + "(String[]) method from class "
                                + mainClassName + " must be public.");
            }
            Class<?> returnType = mainMethod.getReturnType();
            if (returnType != void.class && returnType != int.class) {
                throw newErrorInDocgenTag(
                        mainMethodName + "(String[]) method from class "
                                + mainClassName + " must return void or int, but return type was " + returnType);
            }
            return mainMethod;
        }