I set a bean’s property to a String object, then when I try to get the class name of the property ,below error is thrown out:
Expected hash. plist[0].javaType evaluated instead to freemarker.template.SimpleScalar on line 7, column 26 in ibatis/macro.ftl.
template code is as below:
<#assign clsName=plist[0].javaType.class.name>
When property javaType is set to a java bean, class name can be properly got. Why is it? I need the property could be given any type, java bean ,non java bean.
The root of the issue here is that FreeMarker doesn’t work with Java values/objects directly. The template language has its own simple type-system, and stuff coming from outside is mapped to that through a technique called object-wrapping. (Values that doesn’t come from outside doesn’t even have a wrapped object inside.) That you was still able to get the class of some object is purely accidental… What happens is that the object-wrapping machinery decides that the object should be mapped to the “hash” FreeMarker type, and the hash items will correspond to the JavaBean properties of the objects. The object has a
getClass()method, which is (mistakenly) seen as the getter of the “class” property.So there’s no universal way of getting the class… among others because sometimes there’s no class to get. You could write a
TemplateMethodModelExthat does a good enough effort to do so.