You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by "Eric Norman (Jira)" <ji...@apache.org> on 2021/07/27 23:05:00 UTC

[jira] [Created] (SLING-10677) add support for using an enum class via the Java Use-API

Eric Norman created SLING-10677:
-----------------------------------

             Summary: add support for using an enum class via the Java Use-API
                 Key: SLING-10677
                 URL: https://issues.apache.org/jira/browse/SLING-10677
             Project: Sling
          Issue Type: Improvement
            Reporter: Eric Norman
            Assignee: Eric Norman
             Fix For: Scripting HTL Engine 1.4.10-1.4.0


For a use case where you need access to an enum value in your ui script for some reason, it would be nice to be able to "data-sly-use" your enum class with something like this:

 
{code:java}
<sly data-sly-use.myEnum="org.hello.MyEnum"></sly>
{code}
... and then later on you can reference the enum values with an expression that looks something like this:
{code:java}
<p>
 Enum value by name: ${myEnum.SOMEVALUE}
</p>
<p>
 Enum value by ordinal: ${myEnum[1]}
</p>
<p>
 Constant static field within the enum: ${myEnum.SOME_NON_VALUE_CONSTANT_FIELD}
</p>

<p data-sly-repeat.value="${myEnum.values}">
     Enum value[${value.ordinal}] from static method call: ${value}
</p>
{code}
Since the enum can not have a default constructor, special handling in JavaUseProvider would be required to return the enum class itself instead of trying to instantiate a new instance of the class.

 

NOTE: A companion fix will also be required in org.apache.sling.scripting.sightly.render.ObjectModel#resolveProperty to deal with the target object being an Enum class instead of a POJO object in order for the expressions to resolve as expected.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)