2

I'm writing an AEM component and I have an object being returned that is a type from an SDK. This type has public properties and no getters. For simplicity, it might be defined like this:

class MyItem {
    public String prop1;
    public String prop2;
}

Now normally, I would need a getter, like so:

class MyItem {
    public String prop1;
    public String prop2;

    public String getProp1() {
        return prop1;
    }
}

But I do not have this luxury. Right now, I've got a Java implementation that uses another type to resolve this, but I think it's sort of crazy that HTL doesn't allow me to just access prop1 directly (it calls the getter). I've reviewed the documentation and can't see any indication of how this could be done. I'd like to be able to write:

${item.prop1}

And have it access the public property instead of calling getProp1().

Is this possible?

Jamie Counsell
  • 7,730
  • 6
  • 46
  • 81

2 Answers2

3

You don't need getters for public fields if those fields were declared by your Java Use-class. There's actually a test in Apache Sling that covers this scenario:

https://github.com/apache/sling/blob/trunk/bundles/scripting/sightly/testing-content/src/main/resources/SLING-INF/apps/sightly/scripts/use/repopojo.html

This also applies to Use-classes exported from bundles.

For Sling Models using the adapter pattern [0] I've created https://issues.apache.org/jira/browse/SLING-7075.

[0] - https://sling.apache.org/documentation/bundles/models.html#specifying-an-alternate-adapter-class-since-110

Radu Cotescu
  • 534
  • 3
  • 10
-1

From the official documentation

Once the use-class has initialized, the HTL file is run. During this stage HTL will typically pull in the state of various member variables of the use-class and render them for presentation.

To provide access to these values from within the HTL file you must define custom getter methods in the use-class according to the following naming convention:

A method of the form getXyz will expose within the HTL file an object property called xyz. For example, in the following example, the methods getTitle and getDescription result in the object properties title and description becoming accessible within the context of the HTL file:

The HTL parser does enumerate all the public properties just like any java enumeration of public fuields which include getters and public memebers.

Although it is questionable on whether you should have public variable but thats not part of this discussion. In essence ot should work as pointed by others.

Community
  • 1
  • 1
Imran Saeed
  • 3,414
  • 1
  • 16
  • 27
  • With the current implementation you only need getters for fields that were not declared directly by the Java Use-class. Nothing prevents you from accessing public fields directly. – Radu Cotescu Aug 23 '17 at 08:13
  • Just tried it in 6.2 SP1 and it doesn't work as you say. Is there a patch relevant to this or are we looking at a bug? – Imran Saeed Aug 23 '17 at 08:27
  • I'm not sure how you tested this, but replicating the linked Sling test from https://stackoverflow.com/a/45834897/380566 on AEM 6.2 SP1 works as expected. I noticed that we're colleagues, so you can contact me directly to check who did what wrong. :) – Radu Cotescu Aug 23 '17 at 09:04