javatapestry

Access public final static field from tml page


I have a grid where I would like to load the data from method. This method is taking String as a parameter and produce necessary List as output. For example, it can look like this:

public List<SomeObject> getContactBasedOnType(final String type)
    {
        final List<SomeObject> returnList = new ArrayList<>();
        ...//based on "type" list will be populated by different data
        return returnList;
    }

and then in my tml page I will use it as follows:

<t:grid t:source="getSomeData('STRING')"...
>...</t:grid>

Now, I would like to replace 'STRING' with a public static String field from a class other than component class, for example:

<t:grid t:source="getSomeData(com.example.Class.STATIC_FINAL_FIELD)"...
>...</t:grid>

Is there any way I can do that directly? So without using any additional methods in a component class or annotated fields?


Solution

  • There is a way to achieve what you asked, but it's an awful hack.

    <t:grid
    t:source="getSomeData(getClass().getClassLoader().loadClass('com.example.Class').getField('STATIC_FINAL_FIELD').get(getClass().getClassLoader().loadClass('com.example.Class').getField('STATIC_FINAL_FIELD').getType().newInstance()))">
    ...
    </t:grid>
    

    Note that, in your question, the method in the component class is named getContactBasedOnType while in your tmls you are referencing getSomeData. The method names must match, of course.

    Again, the above is a terrible hack, but the only solution I got to work under the constraint that the component class may not be touched.

    Making the list a property of the component class and populating it in the setupRender() method would be a much better design.