Library contents

Groovy String.describeConstable

Expose a String as a nominal constant descriptor for tools that work with JVM constants.

A simple example

String.describeConstable

  1. Start with "A"
  2. Do this Ask for its constant description
  3. Result Present, holding A
A string can describe itself as a constant. Unwrapping the optional description yields the same text.

Syntax

Groovy 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.groovy

Describe 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.

ResultBIO
Lesson.groovy
println "BIO".describeConstable().orElseThrow()
Output
BIO

The string can serve directly as a JVM constant descriptor.

Advertisements

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.

Resulttrue
Lesson.groovy
println "".describeConstable().isPresent()
Output
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?

GivenNC-17
Worked resultNC-17

What to watch for

For String the Optional contains the string itself. This API describes constants and is separate from application serialization.

Documentation for String.describeConstable