Search in sources :

Example 1 with freemarker.core._ErrorDescriptionBuilder

use of freemarker.core._ErrorDescriptionBuilder in project freemarker by apache.

the class OverloadedMethods method getMemberAndArguments.

MemberAndArguments getMemberAndArguments(List tmArgs, BeansWrapper unwrapper) throws TemplateModelException {
    // Try to find a fixed args match:
    MaybeEmptyMemberAndArguments fixArgsRes = fixArgMethods.getMemberAndArguments(tmArgs, unwrapper);
    if (fixArgsRes instanceof MemberAndArguments) {
        return (MemberAndArguments) fixArgsRes;
    }
    // Try to find a varargs match:
    MaybeEmptyMemberAndArguments varargsRes;
    if (varargMethods != null) {
        varargsRes = varargMethods.getMemberAndArguments(tmArgs, unwrapper);
        if (varargsRes instanceof MemberAndArguments) {
            return (MemberAndArguments) varargsRes;
        }
    } else {
        varargsRes = null;
    }
    _ErrorDescriptionBuilder edb = new _ErrorDescriptionBuilder(toCompositeErrorMessage((EmptyMemberAndArguments) fixArgsRes, (EmptyMemberAndArguments) varargsRes, tmArgs), "\nThe matching overload was searched among these members:\n", memberListToString());
    if (!bugfixed) {
        edb.tip("You seem to use BeansWrapper with incompatibleImprovements set below 2.3.21. If you think this " + "error is unfounded, enabling 2.3.21 fixes may helps. See version history for more.");
    }
    addMarkupBITipAfterNoNoMarchIfApplicable(edb, tmArgs);
    throw new _TemplateModelException(edb);
}
Also used : freemarker.core._TemplateModelException(freemarker.core._TemplateModelException) freemarker.core._ErrorDescriptionBuilder(freemarker.core._ErrorDescriptionBuilder)

Example 2 with freemarker.core._ErrorDescriptionBuilder

use of freemarker.core._ErrorDescriptionBuilder in project freemarker by apache.

the class OverloadedMethods method addMarkupBITipAfterNoNoMarchIfApplicable.

/**
 * Adds tip to the error message if converting a {@link TemplateMarkupOutputModel} argument to {@link String} might
 * allows finding a matching overload.
 */
private void addMarkupBITipAfterNoNoMarchIfApplicable(_ErrorDescriptionBuilder edb, List tmArgs) {
    for (int argIdx = 0; argIdx < tmArgs.size(); argIdx++) {
        Object tmArg = tmArgs.get(argIdx);
        if (tmArg instanceof TemplateMarkupOutputModel) {
            for (Iterator membDescs = fixArgMethods.getMemberDescriptors(); membDescs.hasNext(); ) {
                CallableMemberDescriptor membDesc = (CallableMemberDescriptor) membDescs.next();
                Class[] paramTypes = membDesc.getParamTypes();
                Class paramType = null;
                if (membDesc.isVarargs() && argIdx >= paramTypes.length - 1) {
                    paramType = paramTypes[paramTypes.length - 1];
                    if (paramType.isArray()) {
                        paramType = paramType.getComponentType();
                    }
                }
                if (paramType == null && argIdx < paramTypes.length) {
                    paramType = paramTypes[argIdx];
                }
                if (paramType != null) {
                    if (paramType.isAssignableFrom(String.class) && !paramType.isAssignableFrom(tmArg.getClass())) {
                        edb.tip(SimpleMethodModel.MARKUP_OUTPUT_TO_STRING_TIP);
                        return;
                    }
                }
            }
        }
    }
}
Also used : Iterator(java.util.Iterator) freemarker.core._DelayedConversionToString(freemarker.core._DelayedConversionToString) TemplateMarkupOutputModel(freemarker.core.TemplateMarkupOutputModel)

Example 3 with freemarker.core._ErrorDescriptionBuilder

use of freemarker.core._ErrorDescriptionBuilder in project freemarker by apache.

the class JspTagModelBase method setupTag.

void setupTag(Object tag, Map args, ObjectWrapper wrapper) throws TemplateModelException, InvocationTargetException, IllegalAccessException {
    if (args != null && !args.isEmpty()) {
        ObjectWrapperAndUnwrapper unwrapper = wrapper instanceof ObjectWrapperAndUnwrapper ? (ObjectWrapperAndUnwrapper) wrapper : // [2.4] Throw exception in this case
        BeansWrapper.getDefaultInstance();
        final Object[] argArray = new Object[1];
        for (Iterator iter = args.entrySet().iterator(); iter.hasNext(); ) {
            final Map.Entry entry = (Map.Entry) iter.next();
            final Object arg = unwrapper.unwrap((TemplateModel) entry.getValue());
            argArray[0] = arg;
            final Object paramName = entry.getKey();
            Method setterMethod = (Method) propertySetters.get(paramName);
            if (setterMethod == null) {
                if (dynaSetter == null) {
                    throw new TemplateModelException("Unknown property " + StringUtil.jQuote(paramName.toString()) + " on instance of " + tagClass.getName());
                } else {
                    dynaSetter.invoke(tag, null, paramName, argArray[0]);
                }
            } else {
                if (arg instanceof BigDecimal) {
                    argArray[0] = BeansWrapper.coerceBigDecimal((BigDecimal) arg, setterMethod.getParameterTypes()[0]);
                }
                try {
                    setterMethod.invoke(tag, argArray);
                } catch (Exception e) {
                    final Class setterType = setterMethod.getParameterTypes()[0];
                    final _ErrorDescriptionBuilder desc = new _ErrorDescriptionBuilder("Failed to set JSP tag parameter ", new _DelayedJQuote(paramName), " (declared type: ", new _DelayedShortClassName(setterType) + ", actual value's type: ", (argArray[0] != null ? (Object) new _DelayedShortClassName(argArray[0].getClass()) : "Null"), "). See cause exception for the more specific cause...");
                    if (e instanceof IllegalArgumentException && !(setterType.isAssignableFrom(String.class)) && argArray[0] != null && argArray[0] instanceof String) {
                        desc.tip("This problem is often caused by unnecessary parameter quotation. Paramters " + "aren't quoted in FTL, similarly as they aren't quoted in most languages. " + "For example, these parameter assignments are wrong: ", "<@my.tag p1=\"true\" p2=\"10\" p3=\"${someVariable}\" p4=\"${x+1}\" />", ". The correct form is: ", "<@my.tag p1=true p2=10 p3=someVariable p4=x+1 />", ". Only string literals are quoted (regardless of where they occur): ", "<@my.box style=\"info\" message=\"Hello ${name}!\" width=200 />", ".");
                    }
                    throw new _TemplateModelException(e, null, desc);
                }
            }
        }
    }
}
Also used : freemarker.core._TemplateModelException(freemarker.core._TemplateModelException) TemplateModelException(freemarker.template.TemplateModelException) ObjectWrapperAndUnwrapper(freemarker.template.ObjectWrapperAndUnwrapper) freemarker.core._DelayedJQuote(freemarker.core._DelayedJQuote) Method(java.lang.reflect.Method) BigDecimal(java.math.BigDecimal) TemplateExceptionWrapperJspException(freemarker.ext.jsp.SimpleTagDirectiveModel.TemplateExceptionWrapperJspException) freemarker.core._TemplateModelException(freemarker.core._TemplateModelException) TemplateModelException(freemarker.template.TemplateModelException) TemplateException(freemarker.template.TemplateException) IntrospectionException(java.beans.IntrospectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) freemarker.core._TemplateModelException(freemarker.core._TemplateModelException) Iterator(java.util.Iterator) freemarker.core._DelayedShortClassName(freemarker.core._DelayedShortClassName) freemarker.core._ErrorDescriptionBuilder(freemarker.core._ErrorDescriptionBuilder) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

freemarker.core._ErrorDescriptionBuilder (freemarker.core._ErrorDescriptionBuilder)2 freemarker.core._TemplateModelException (freemarker.core._TemplateModelException)2 Iterator (java.util.Iterator)2 TemplateMarkupOutputModel (freemarker.core.TemplateMarkupOutputModel)1 freemarker.core._DelayedConversionToString (freemarker.core._DelayedConversionToString)1 freemarker.core._DelayedJQuote (freemarker.core._DelayedJQuote)1 freemarker.core._DelayedShortClassName (freemarker.core._DelayedShortClassName)1 TemplateExceptionWrapperJspException (freemarker.ext.jsp.SimpleTagDirectiveModel.TemplateExceptionWrapperJspException)1 ObjectWrapperAndUnwrapper (freemarker.template.ObjectWrapperAndUnwrapper)1 TemplateException (freemarker.template.TemplateException)1 TemplateModelException (freemarker.template.TemplateModelException)1 IntrospectionException (java.beans.IntrospectionException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 BigDecimal (java.math.BigDecimal)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1