use of freemarker.template.TemplateHashModel in project freemarker by apache.
the class EnumModelsTest method modelCaching.
@Test
public void modelCaching() throws Exception {
BeansWrapper bw = new BeansWrapper(Configuration.VERSION_2_3_21);
TemplateHashModel enums = bw.getEnumModels();
TemplateHashModel e = (TemplateHashModel) enums.get(E.class.getName());
assertNotNull(e);
assertNotNull(e.get("A"));
assertNotNull(e.get("B"));
assertNull(e.get("C"));
try {
enums.get("no.such.ClassExists");
fail();
} catch (TemplateModelException ex) {
assertTrue(ex.getCause() instanceof ClassNotFoundException);
}
TemplateModel a = e.get("A");
assertTrue(a instanceof TemplateScalarModel);
assertTrue(a instanceof TemplateHashModel);
assertEquals(((TemplateScalarModel) a).getAsString(), "ts:A");
TemplateMethodModelEx nameMethod = (TemplateMethodModelEx) ((TemplateHashModel) a).get("name");
assertEquals(((TemplateScalarModel) nameMethod.exec(new ArrayList())).getAsString(), "A");
assertSame(e, enums.get(E.class.getName()));
bw.clearClassIntrospecitonCache();
TemplateHashModel eAfterClean = (TemplateHashModel) enums.get(E.class.getName());
assertNotSame(e, eAfterClean);
assertSame(eAfterClean, enums.get(E.class.getName()));
assertNotNull(eAfterClean.get("A"));
assertNotNull(eAfterClean.get("B"));
assertNull(eAfterClean.get("C"));
}
use of freemarker.template.TemplateHashModel in project freemarker by apache.
the class FineTuneMethodAppearanceTest method assertSSubvariableValue.
private void assertSSubvariableValue(Boolean replaceExistingProperty, boolean preferGetS, String expectedValue) throws TemplateModelException {
DefaultObjectWrapper ow = new DefaultObjectWrapper(Configuration.VERSION_2_3_27);
ow.setMethodAppearanceFineTuner(new PropertyReplacementMethodAppearanceFineTuner(replaceExistingProperty, preferGetS));
assertEquals(expectedValue, ((TemplateScalarModel) ((TemplateHashModel) ow.wrap(new PropertyReplacementTestBean())).get("s")).getAsString());
}
use of freemarker.template.TemplateHashModel in project freemarker by apache.
the class FineTuneMethodAppearanceTest method checkIfProperlyWrapped.
private void checkIfProperlyWrapped(TemplateModel tm) throws TemplateModelException {
TemplateHashModel thm = (TemplateHashModel) tm;
assertEquals("v1", ((TemplateScalarModel) thm.get("v1")).getAsString());
assertEquals("v2()", ((TemplateScalarModel) thm.get("v2")).getAsString());
assertEquals("getV3()", ((TemplateScalarModel) thm.get("v3")).getAsString());
assertTrue(thm.get("getV3") instanceof TemplateMethodModelEx);
}
use of freemarker.template.TemplateHashModel in project freemarker by apache.
the class RmiDebugModelImpl method get.
public DebugModel[] get(String[] keys) throws TemplateModelException, RemoteException {
DebugModel[] dm = new DebugModel[keys.length];
TemplateHashModel h = (TemplateHashModel) model;
for (int i = 0; i < keys.length; i++) {
dm[i] = getDebugModel(h.get(keys[i]));
}
return dm;
}
use of freemarker.template.TemplateHashModel in project freemarker by apache.
the class _ObjectBuilderSettingEvaluator method setJavaBeanProperties.
private void setJavaBeanProperties(Object bean, List namedParamNames, List namedParamValues) throws _ObjectBuilderSettingEvaluationException {
if (namedParamNames.isEmpty()) {
return;
}
final Class cl = bean.getClass();
Map /*<String,Method>*/
beanPropSetters;
try {
PropertyDescriptor[] propDescs = Introspector.getBeanInfo(cl).getPropertyDescriptors();
beanPropSetters = new HashMap(propDescs.length * 4 / 3, 1.0f);
for (int i = 0; i < propDescs.length; i++) {
PropertyDescriptor propDesc = propDescs[i];
final Method writeMethod = propDesc.getWriteMethod();
if (writeMethod != null) {
beanPropSetters.put(propDesc.getName(), writeMethod);
}
}
} catch (Exception e) {
throw new _ObjectBuilderSettingEvaluationException("Failed to inspect " + cl.getName() + " class", e);
}
TemplateHashModel beanTM = null;
for (int i = 0; i < namedParamNames.size(); i++) {
String name = (String) namedParamNames.get(i);
if (!beanPropSetters.containsKey(name)) {
throw new _ObjectBuilderSettingEvaluationException("The " + cl.getName() + " class has no writeable JavaBeans property called " + StringUtil.jQuote(name) + ".");
}
Method beanPropSetter = (Method) beanPropSetters.put(name, null);
if (beanPropSetter == null) {
throw new _ObjectBuilderSettingEvaluationException("JavaBeans property " + StringUtil.jQuote(name) + " is set twice.");
}
try {
if (beanTM == null) {
TemplateModel wrappedObj = env.getObjectWrapper().wrap(bean);
if (!(wrappedObj instanceof TemplateHashModel)) {
throw new _ObjectBuilderSettingEvaluationException("The " + cl.getName() + " class is not a wrapped as TemplateHashModel.");
}
beanTM = (TemplateHashModel) wrappedObj;
}
TemplateModel m = beanTM.get(beanPropSetter.getName());
if (m == null) {
throw new _ObjectBuilderSettingEvaluationException("Can't find " + beanPropSetter + " as FreeMarker method.");
}
if (!(m instanceof TemplateMethodModelEx)) {
throw new _ObjectBuilderSettingEvaluationException(StringUtil.jQuote(beanPropSetter.getName()) + " wasn't a TemplateMethodModelEx.");
}
List /*TemplateModel*/
args = new ArrayList();
args.add(env.getObjectWrapper().wrap(namedParamValues.get(i)));
((TemplateMethodModelEx) m).exec(args);
} catch (Exception e) {
throw new _ObjectBuilderSettingEvaluationException("Failed to set " + StringUtil.jQuote(name), e);
}
}
}
Aggregations