Search in sources :

Example 6 with TemplateSequenceModel

use of freemarker.template.TemplateSequenceModel in project freemarker by apache.

the class Java8BeansWrapperTest method testDefaultMethodRecognized.

@Test
public void testDefaultMethodRecognized() throws TemplateModelException {
    BeansWrapperBuilder owb = new BeansWrapperBuilder(Configuration.VERSION_2_3_0);
    owb.setTreatDefaultMethodsAsBeanMembers(true);
    BeansWrapper ow = owb.build();
    TemplateHashModel wrappedBean = (TemplateHashModel) ow.wrap(new Java8DefaultMethodsBean());
    {
        TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBean.NORMAL_PROP);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.NORMAL_PROP_VALUE, prop.getAsString());
    }
    {
        // This is overridden in the subclass, so it's visible even without default method support:
        TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_PROP_2);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.PROP_2_OVERRIDE_VALUE, prop.getAsString());
    }
    {
        TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_PROP);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_PROP_VALUE, prop.getAsString());
    }
    {
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_INDEXED_PROP);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_INDEXED_PROP_VALUE, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        // We see default method indexed read method, but it's invalidated by normal getter in the subclass
        TemplateNumberModel prop = (TemplateNumberModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_VALUE, prop.getAsNumber());
    }
    {
        // The default method read method invalidates the indexed read method in the subclass
        TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_2);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_2_VALUE, prop.getAsString());
    }
    {
        // The default method read method invalidates the indexed read method in the subclass
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_3);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_3_VALUE_0, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        // We see the default method indexed reader, which overrides the plain array reader in the subclass.
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_2);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_2_VALUE_0, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        // We do see the default method non-indexed reader, but the subclass has a matching indexed reader, so that
        // takes over.
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_3);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_3_VALUE, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        // Only present in the subclass.
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.INDEXED_PROP_4);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_4_VALUE, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get(Java8DefaultMethodsBean.NORMAL_ACTION);
        assertNotNull(action);
        assertEquals(Java8DefaultMethodsBean.NORMAL_ACTION_RETURN_VALUE, ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString());
    }
    {
        TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get(Java8DefaultMethodsBean.NORMAL_ACTION);
        assertNotNull(action);
        assertEquals(Java8DefaultMethodsBean.NORMAL_ACTION_RETURN_VALUE, ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString());
    }
    {
        TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_ACTION);
        assertNotNull(action);
        assertEquals(Java8DefaultMethodsBean.DEFAULT_METHOD_ACTION_RETURN_VALUE, ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString());
    }
    {
        TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get(Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION);
        assertNotNull(action);
        assertEquals(Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION_RETURN_VALUE, ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString());
    }
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateHashModel(freemarker.template.TemplateHashModel) TemplateMethodModelEx(freemarker.template.TemplateMethodModelEx) TemplateNumberModel(freemarker.template.TemplateNumberModel) TemplateScalarModel(freemarker.template.TemplateScalarModel) Test(org.junit.Test)

Example 7 with TemplateSequenceModel

use of freemarker.template.TemplateSequenceModel in project freemarker by apache.

the class Java8BeansWrapperTest method testDefaultMethodNotRecognized.

@Test
public void testDefaultMethodNotRecognized() throws TemplateModelException {
    BeansWrapperBuilder owb = new BeansWrapperBuilder(Configuration.VERSION_2_3_0);
    // owb.setTreatDefaultMethodsAsBeanMembers(false);
    BeansWrapper ow = owb.build();
    TemplateHashModel wrappedBean = (TemplateHashModel) ow.wrap(new Java8DefaultMethodsBean());
    {
        TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBean.NORMAL_PROP);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.NORMAL_PROP_VALUE, prop.getAsString());
    }
    {
        // This is overridden in the subclass, so it's visible even without default method support:
        TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_PROP_2);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.PROP_2_OVERRIDE_VALUE, prop.getAsString());
    }
    assertNull(wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_PROP));
    assertNull(wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_INDEXED_PROP));
    {
        // We don't see the default method indexed reader, but see the plain reader method in the subclass.
        TemplateNumberModel prop = (TemplateNumberModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_VALUE, prop.getAsNumber());
    }
    {
        // We don't see the default method non-indexed reader that would spoil the indexed reader in the subclass.
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_2);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_2_VALUE, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        // We don't see the default method non-indexed reader that would spoil the indexed reader in the subclass.
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_3);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_3_VALUE, ((TemplateNumberModel) prop.get(0)).getAsNumber());
    }
    {
        // We don't see the default method indexed reader, but see the plain array reader in the subclass.
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_2);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.ARRAY_PROP_2_VALUE_0, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        // Only present in the subclass.
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.INDEXED_PROP_4);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_4_VALUE, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        // We don't see the default method non-indexed reader
        TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get(Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_3);
        assertNotNull(prop);
        assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_3_VALUE, ((TemplateScalarModel) prop.get(0)).getAsString());
    }
    {
        TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get(Java8DefaultMethodsBean.NORMAL_ACTION);
        assertNotNull(action);
        assertEquals(Java8DefaultMethodsBean.NORMAL_ACTION_RETURN_VALUE, ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString());
    }
    assertNull(wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_ACTION));
    {
        TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get(Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION);
        assertNotNull(action);
        assertEquals(Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION_RETURN_VALUE, ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString());
    }
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateHashModel(freemarker.template.TemplateHashModel) TemplateMethodModelEx(freemarker.template.TemplateMethodModelEx) TemplateNumberModel(freemarker.template.TemplateNumberModel) TemplateScalarModel(freemarker.template.TemplateScalarModel) Test(org.junit.Test)

Example 8 with TemplateSequenceModel

use of freemarker.template.TemplateSequenceModel in project freemarker by apache.

the class BeansWrapperMiscTest method java8InaccessibleIndexedAccessibleNonIndexedReadMethodTest.

@Test
public void java8InaccessibleIndexedAccessibleNonIndexedReadMethodTest() throws TemplateModelException {
    assertTrue("This test case must be ran on Java 8 or later", _JavaVersions.JAVA_8 != null);
    assertFalse(Modifier.isPublic(BeanWithInaccessibleIndexedProperty.class.getModifiers()));
    for (Version ici : new Version[] { Configuration.VERSION_2_3_26, Configuration.VERSION_2_3_27 }) {
        BeansWrapper bw = new BeansWrapper(ici);
        TemplateHashModel beanTM = (TemplateHashModel) bw.wrap(new BeanWithInaccessibleIndexedProperty());
        TemplateModel fooTM = beanTM.get("foo");
        assertThat(fooTM, instanceOf(TemplateSequenceModel.class));
        assertEquals("b", ((TemplateScalarModel) ((TemplateSequenceModel) fooTM).get(1)).getAsString());
        // Even with 2.3.26, where the indexed reader was preferred, as it's inaccessible, we use the normal reader:
        assertEquals(2, ((TemplateSequenceModel) fooTM).size());
        TemplateModel barTM = beanTM.get("bar");
        // all read methods inaccessible
        assertNull(barTM);
    }
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) TemplateHashModel(freemarker.template.TemplateHashModel) Version(freemarker.template.Version) TemplateModel(freemarker.template.TemplateModel) Test(org.junit.Test)

Example 9 with TemplateSequenceModel

use of freemarker.template.TemplateSequenceModel in project freemarker by apache.

the class BeansWrapper method listToArray.

Object listToArray(List<?> list, Class<?> arrayClass, Map<Object, Object> recursionStops) throws TemplateModelException {
    if (list instanceof SequenceAdapter) {
        return unwrapSequenceToArray(((SequenceAdapter) list).getTemplateSequenceModel(), arrayClass, false, recursionStops);
    }
    if (recursionStops != null) {
        Object retval = recursionStops.get(list);
        if (retval != null) {
            return retval;
        }
    } else {
        recursionStops = new IdentityHashMap<Object, Object>();
    }
    Class<?> componentType = arrayClass.getComponentType();
    Object array = Array.newInstance(componentType, list.size());
    recursionStops.put(list, array);
    try {
        boolean isComponentTypeExamined = false;
        // will be filled on demand
        boolean isComponentTypeNumerical = false;
        // will be filled on demand
        boolean isComponentTypeList = false;
        int i = 0;
        for (Iterator<?> it = list.iterator(); it.hasNext(); ) {
            Object listItem = it.next();
            if (listItem != null && !componentType.isInstance(listItem)) {
                // Type conversion is needed. If we can't do it, we just let it fail at Array.set later.
                if (!isComponentTypeExamined) {
                    isComponentTypeNumerical = ClassUtil.isNumerical(componentType);
                    isComponentTypeList = List.class.isAssignableFrom(componentType);
                    isComponentTypeExamined = true;
                }
                if (isComponentTypeNumerical && listItem instanceof Number) {
                    listItem = forceUnwrappedNumberToType((Number) listItem, componentType, true);
                } else if (componentType == String.class && listItem instanceof Character) {
                    listItem = String.valueOf(((Character) listItem).charValue());
                } else if ((componentType == Character.class || componentType == char.class) && listItem instanceof String) {
                    String listItemStr = (String) listItem;
                    if (listItemStr.length() == 1) {
                        listItem = Character.valueOf(listItemStr.charAt(0));
                    }
                } else if (componentType.isArray()) {
                    if (listItem instanceof List) {
                        listItem = listToArray((List<?>) listItem, componentType, recursionStops);
                    } else if (listItem instanceof TemplateSequenceModel) {
                        listItem = unwrapSequenceToArray((TemplateSequenceModel) listItem, componentType, false, recursionStops);
                    }
                } else if (isComponentTypeList && listItem.getClass().isArray()) {
                    listItem = arrayToList(listItem);
                }
            }
            try {
                Array.set(array, i, listItem);
            } catch (IllegalArgumentException e) {
                throw new TemplateModelException("Failed to convert " + ClassUtil.getShortClassNameOfObject(list) + " object to " + ClassUtil.getShortClassNameOfObject(array) + ": Problematic List item at index " + i + " with value type: " + ClassUtil.getShortClassNameOfObject(listItem), e);
            }
            i++;
        }
    } finally {
        recursionStops.remove(list);
    }
    return array;
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) freemarker.core._TemplateModelException(freemarker.core._TemplateModelException) TemplateSequenceModel(freemarker.template.TemplateSequenceModel) AccessibleObject(java.lang.reflect.AccessibleObject) List(java.util.List)

Example 10 with TemplateSequenceModel

use of freemarker.template.TemplateSequenceModel in project freemarker by apache.

the class RmiDebugModelImpl method get.

public DebugModel[] get(int fromIndex, int toIndex) throws TemplateModelException, RemoteException {
    DebugModel[] dm = new DebugModel[toIndex - fromIndex];
    TemplateSequenceModel s = (TemplateSequenceModel) model;
    for (int i = fromIndex; i < toIndex; i++) {
        dm[i - fromIndex] = getDebugModel(s.get(i));
    }
    return dm;
}
Also used : TemplateSequenceModel(freemarker.template.TemplateSequenceModel) DebugModel(freemarker.debug.DebugModel)

Aggregations

TemplateSequenceModel (freemarker.template.TemplateSequenceModel)21 TemplateScalarModel (freemarker.template.TemplateScalarModel)9 TemplateModel (freemarker.template.TemplateModel)8 TemplateHashModel (freemarker.template.TemplateHashModel)7 SimpleSequence (freemarker.template.SimpleSequence)6 TemplateNodeModel (freemarker.template.TemplateNodeModel)6 SimpleScalar (freemarker.template.SimpleScalar)5 TemplateNumberModel (freemarker.template.TemplateNumberModel)4 TemplateModelException (freemarker.template.TemplateModelException)3 Test (org.junit.Test)3 TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)2 TemplateTransformModel (freemarker.template.TemplateTransformModel)2 IOException (java.io.IOException)2 AccessibleObject (java.lang.reflect.AccessibleObject)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 freemarker.core._TemplateModelException (freemarker.core._TemplateModelException)1 DebugModel (freemarker.debug.DebugModel)1 WrapperTemplateModel (freemarker.ext.util.WrapperTemplateModel)1 AdapterTemplateModel (freemarker.template.AdapterTemplateModel)1