You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Java 14 introduced the record keyword, which allows the creation of a lightweight "class" which really only holds data, similar to a struct in C for example. StringTemplate does not support records because the way records define fields/method is different.
Say I have a record Person (with the equivalent class after it) defined as
/* * Lightweight Person record, only one line. */public record Person(Stringname, intage) {
// Nothing inside
}
/* * This class is equivalent to the record above, but much more code needs to be written. */publicclassPerson {
privateStringname;
privateintage;
publicPerson(Stringname, intage) {
this.name = name;
this.age = age;
}
publicStringname() {
returnthis.name;
}
publicintage() {
returnthis.age;
}
}
The record will have two methods, name(), and age(), for getting the name and age fields.
Related to #302, the fields of a record are private, and the methods do not follow StringTemplate's findMember method search pattern of getField, isField, and hasField:
// try getXXX and isXXX properties, look up using reflectionStringmethodSuffix = Character.toUpperCase(memberName.charAt(0)) +
memberName.substring(1, memberName.length());
member = tryGetMethod(clazz, "get" + methodSuffix);
if (member == null) {
member = tryGetMethod(clazz, "is" + methodSuffix);
if (member == null) {
member = tryGetMethod(clazz, "has" + methodSuffix);
}
}
if (member == null) {
// try for a visible fieldmember = tryGetField(clazz, memberName);
}
The addition of a tryGetMethod(clazz, memberName) would solve this because the methods of a record are public. Or, fix the issue of #302, but doing this would help as well.
The text was updated successfully, but these errors were encountered:
Java 14 introduced the
record
keyword, which allows the creation of a lightweight "class" which really only holds data, similar to astruct
in C for example. StringTemplate does not support records because the way records define fields/method is different.Say I have a record
Person
(with the equivalent class after it) defined asThe record will have two methods,
name()
, andage()
, for getting the name and age fields.Related to #302, the fields of a record are private, and the methods do not follow StringTemplate's
findMember
method search pattern ofgetField
,isField
, andhasField
:The addition of a
tryGetMethod(clazz, memberName)
would solve this because the methods of a record are public. Or, fix the issue of #302, but doing this would help as well.The text was updated successfully, but these errors were encountered: