Library contents

Groovy String.resolveConstantDesc

Resolve a String constant descriptor using the standard constant-description API.

A simple example

String.resolveConstantDesc

  1. Start with The string constant "A"
  2. Do this Resolve its description
  3. Result "A"
A Java string already represents its constant value. Resolution returns that same string.

Syntax

Groovy syntax
public String resolveConstantDesc(MethodHandles.Lookup lookup)
Run these examples in Groovy

Save one example at a time as Lesson.groovy. Use Groovy 4 on JDK 21.

groovy Lesson.groovy

Resolve a label for a JVM tool

A JVM inspection tool needs a runtime label from a symbolic constant. Supply the lookup context and examine the resolved text before presenting it to the user.

ResultGate B
Lesson.groovy
println "Gate B".resolveConstantDesc(java.lang.invoke.MethodHandles.lookup())
Output
Gate B

String resolves to its own value.

Advertisements

Verify descriptor identity

A JVM tool receives a descriptor for a constant string and has to resolve it in a lookup context. Follow the resolved value through the example to see what the tool can use after that step.

From the programString name = "Lab"
Resulttrue
Lesson.groovy
String name = "Lab"
println name.is(name.resolveConstantDesc(java.lang.invoke.MethodHandles.lookup()))
Output
true

Resolution preserves the receiver identity.

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

String is its own resolved constant. Other descriptor types may perform lookups and have different failure modes.

Documentation for String.resolveConstantDesc