use of freemarker.core._DelayedShortClassName in project freemarker by apache.
the class BeansWrapper method unwrapSequenceToArray.
/**
* @param tryOnly
* If {@code true}, if the conversion of an item to the component type isn't possible, the method returns
* {@link ObjectWrapperAndUnwrapper#CANT_UNWRAP_TO_TARGET_CLASS} instead of throwing a
* {@link TemplateModelException}.
*/
Object unwrapSequenceToArray(TemplateSequenceModel seq, Class<?> arrayClass, boolean tryOnly, Map<Object, Object> recursionStops) throws TemplateModelException {
if (recursionStops != null) {
Object retval = recursionStops.get(seq);
if (retval != null) {
return retval;
}
} else {
recursionStops = new IdentityHashMap<Object, Object>();
}
Class<?> componentType = arrayClass.getComponentType();
final int size = seq.size();
Object array = Array.newInstance(componentType, size);
recursionStops.put(seq, array);
try {
for (int i = 0; i < size; i++) {
final TemplateModel seqItem = seq.get(i);
Object val = tryUnwrapTo(seqItem, componentType, 0, recursionStops);
if (val == ObjectWrapperAndUnwrapper.CANT_UNWRAP_TO_TARGET_CLASS) {
if (tryOnly) {
return ObjectWrapperAndUnwrapper.CANT_UNWRAP_TO_TARGET_CLASS;
} else {
throw new _TemplateModelException("Failed to convert ", new _DelayedFTLTypeDescription(seq), " object to ", new _DelayedShortClassName(array.getClass()), ": Problematic sequence item at index ", Integer.valueOf(i), " with value type: ", new _DelayedFTLTypeDescription(seqItem));
}
}
Array.set(array, i, val);
}
} finally {
recursionStops.remove(seq);
}
return array;
}
use of freemarker.core._DelayedShortClassName 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);
}
}
}
}
}
Aggregations