Groovy String.describeConstable
Expose a String as a nominal constant descriptor for tools that work with JVM constants.
A simple example
String.describeConstable
- Start with "A"
- Do this Ask for its constant description
- Result Present, holding A
Syntax
public Optional<String> describeConstable()
Run these examples in Groovy
Save one example at a time as Lesson.groovy. Use Groovy 4 on JDK 21.
groovy Lesson.groovyDescribe a course constant
A compiler exercise starts with the name of a course as a runtime string. Inspect the constant description it offers before handing the value to the next stage of the tool.
println "BIO".describeConstable().orElseThrow()
BIO
The string can serve directly as a JVM constant descriptor.
Describe an empty default
A JVM tool inspects a string that may become a constant in generated code. Ask for its constant description and examine what is present before treating the descriptor as a usable value.
println "".describeConstable().isPresent()
true
An empty value still has a descriptor.
Read a parcel sorting code
The depot scans a parcel marked NC-17. The program recovers the text of this existing string for a scanner log. What exact sequence of characters should survive?
What to watch for
For String the Optional contains the string itself. This API describes constants and is separate from application serialization.