Calling static methods, revisited

Christopher Schultz <[email protected]> Wed, 27 Sep 2023 18:03:34 -0400
Newsgroups gmane.comp.jakarta.velocity.user
Message-ID <[email protected]>
All,

(This is all in the context of Velocity 1.7, but I'm not sure anything 
has changed through 2.x.)

Sometimes you want to call a static method. Past workarounds are ugly 
but get the job done. For example:

#set($string = "")
$string.format($locale, "format string", args)

I have a case where I've been working around java.util.TimeZone like this:

#set($tz = $dateIHave.timeZone)
#set($utc = $tx.getTimeZone('UTC'))
... use $utc

In recent versions of Java (I'm using Java 17 at the moment), I'm 
getting this error:

Exception: org.apache.velocity.exception.VelocityException: 
ASTMethod.execute() : exception invoking method 'getTimeZone' in class 
sun.util.calendar.ZoneInfo
Stack Trace:
org.apache.velocity.exception.VelocityException: ASTMethod.execute() : 
exception invoking method "getTimeZone" in class sun.util.calendar.ZoneInfo
org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:208)
...

I'm intending to call java.util.TimeZone.getTimeZone which should be 
fine. But the runtime type of $tz ends up being 
sun.util.calendar.ZoneInfo which, unfortunately, defines its own static 
getTimeZone method whose signature matches that of java.util.TimeZone 
but is not callable due to module.

In this case, is it possible for me to make a call to 
java.util.TimeZone.getTimeZone, or is it time to write a new Tool or 
customize an existing one (e.g. DateTool.getTimeZone(String zoneId) 
would be handy).

Thanks,
-chris