use of freemarker.core._TemplateModelException 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);
}
use of freemarker.core._TemplateModelException in project freemarker by apache.
the class SimpleMethod method unwrapArguments.
Object[] unwrapArguments(List arguments, BeansWrapper wrapper) throws TemplateModelException {
if (arguments == null) {
arguments = Collections.EMPTY_LIST;
}
boolean isVarArg = _MethodUtil.isVarargs(member);
int typesLen = argTypes.length;
if (isVarArg) {
if (typesLen - 1 > arguments.size()) {
throw new _TemplateModelException(_MethodUtil.invocationErrorMessageStart(member), " takes at least ", Integer.valueOf(typesLen - 1), typesLen - 1 == 1 ? " argument" : " arguments", ", but ", Integer.valueOf(arguments.size()), " was given.");
}
} else if (typesLen != arguments.size()) {
throw new _TemplateModelException(_MethodUtil.invocationErrorMessageStart(member), " takes ", Integer.valueOf(typesLen), typesLen == 1 ? " argument" : " arguments", ", but ", Integer.valueOf(arguments.size()), " was given.");
}
Object[] args = unwrapArguments(arguments, argTypes, isVarArg, wrapper);
return args;
}
use of freemarker.core._TemplateModelException in project freemarker by apache.
the class SimpleHash method get.
public TemplateModel get(String key) throws TemplateModelException {
Object result;
try {
result = map.get(key);
} catch (ClassCastException e) {
throw new _TemplateModelException(e, "ClassCastException while getting Map entry with String key ", new _DelayedJQuote(key));
} catch (NullPointerException e) {
throw new _TemplateModelException(e, "NullPointerException while getting Map entry with String key ", new _DelayedJQuote(key));
}
// The key to use for putting -- it's the key that already exists in
// the map (either key or charKey below). This way, we'll never put a
// new key in the map, avoiding spurious ConcurrentModificationException
// from another thread iterating over the map, see bug #1939742 in
// SourceForge tracker.
Object putKey = null;
if (result == null) {
// In SortedMap-s, however, we can't do that safely, as it can cause ClassCastException.
if (key.length() == 1 && !(map instanceof SortedMap)) {
Character charKey = Character.valueOf(key.charAt(0));
try {
result = map.get(charKey);
if (result != null || map.containsKey(charKey)) {
putKey = charKey;
}
} catch (ClassCastException e) {
throw new _TemplateModelException(e, "ClassCastException while getting Map entry with Character key ", new _DelayedJQuote(key));
} catch (NullPointerException e) {
throw new _TemplateModelException(e, "NullPointerException while getting Map entry with Character key ", new _DelayedJQuote(key));
}
}
if (putKey == null) {
if (!map.containsKey(key)) {
return null;
} else {
putKey = key;
}
}
} else {
putKey = key;
}
if (result instanceof TemplateModel) {
return (TemplateModel) result;
}
TemplateModel tm = wrap(result);
if (!putFailed) {
try {
map.put(putKey, tm);
} catch (Exception e) {
// If it's immutable or something, we just keep going.
putFailed = true;
}
}
return tm;
}
use of freemarker.core._TemplateModelException in project freemarker by apache.
the class BeanModel method get.
/**
* Uses Beans introspection to locate a property or method with name
* matching the key name. If a method or property is found, it's wrapped
* into {@link freemarker.template.TemplateMethodModelEx} (for a method or
* indexed property), or evaluated on-the-fly and the return value wrapped
* into appropriate model (for a non-indexed property) Models for various
* properties and methods are cached on a per-class basis, so the costly
* introspection is performed only once per property or method of a class.
* (Side-note: this also implies that any class whose method has been called
* will be strongly referred to by the framework and will not become
* unloadable until this class has been unloaded first. Normally this is not
* an issue, but can be in a rare scenario where you create many classes on-
* the-fly. Also, as the cache grows with new classes and methods introduced
* to the framework, it may appear as if it were leaking memory. The
* framework does, however detect class reloads (if you happen to be in an
* environment that does this kind of things--servlet containers do it when
* they reload a web application) and flushes the cache. If no method or
* property matching the key is found, the framework will try to invoke
* methods with signature
* <tt>non-void-return-type get(java.lang.String)</tt>,
* then <tt>non-void-return-type get(java.lang.Object)</tt>, or
* alternatively (if the wrapped object is a resource bundle)
* <tt>Object getObject(java.lang.String)</tt>.
* @throws TemplateModelException if there was no property nor method nor
* a generic <tt>get</tt> method to invoke.
*/
public TemplateModel get(String key) throws TemplateModelException {
Class<?> clazz = object.getClass();
Map<Object, Object> classInfo = wrapper.getClassIntrospector().get(clazz);
TemplateModel retval = null;
try {
if (wrapper.isMethodsShadowItems()) {
Object fd = classInfo.get(key);
if (fd != null) {
retval = invokeThroughDescriptor(fd, classInfo);
} else {
retval = invokeGenericGet(classInfo, clazz, key);
}
} else {
TemplateModel model = invokeGenericGet(classInfo, clazz, key);
final TemplateModel nullModel = wrapper.wrap(null);
if (model != nullModel && model != UNKNOWN) {
return model;
}
Object fd = classInfo.get(key);
if (fd != null) {
retval = invokeThroughDescriptor(fd, classInfo);
if (retval == UNKNOWN && model == nullModel) {
// This is the (somewhat subtle) case where the generic get() returns null
// and we have no bean info, so we respect the fact that
// the generic get() returns null and return null. (JR)
retval = nullModel;
}
}
}
if (retval == UNKNOWN) {
if (wrapper.isStrict()) {
throw new InvalidPropertyException("No such bean property: " + key);
} else if (LOG.isDebugEnabled()) {
logNoSuchKey(key, classInfo);
}
retval = wrapper.wrap(null);
}
return retval;
} catch (TemplateModelException e) {
throw e;
} catch (Exception e) {
throw new _TemplateModelException(e, "An error has occurred when reading existing sub-variable ", new _DelayedJQuote(key), "; see cause exception! The type of the containing value was: ", new _DelayedFTLTypeDescription(this));
}
}
use of freemarker.core._TemplateModelException 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;
}
Aggregations