Java String.resolveConstantDesc
Resolve a String constant descriptor using the standard constant-description API.
A simple example
String.resolveConstantDesc
- Start with The string constant "A"
- Do this Resolve its description
- Result "A"
class Main {
public static void main(String[] args) throws Exception {
System.out.println("A".resolveConstantDesc(java.lang.invoke.MethodHandles.lookup()));
}
}
A
Syntax
public String resolveConstantDesc(MethodHandles.Lookup lookup)
JDK 21 method signatures (1)
public String resolveConstantDesc(MethodHandles.Lookup lookup)Run these examples in Java
Save one example at a time as Main.java. Use JDK 21.
java Main.javaResolve a label from a JVM constants exercise
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.
class Main {
public static void main(String[] args) throws Exception {
String label = "Gate A";
System.out.println(label.resolveConstantDesc(java.lang.invoke.MethodHandles.lookup()));
}
}
Gate A
Resolving this string descriptor gives its original text.
Check the identity of a resolved course code
A tool resolves a course-code constant that originated as a string. Inspect the resolved reference so the tool author can tell whether this particular resolution preserves the object's identity.
class Main {
public static void main(String[] args) throws Exception {
String course = "CS101";
System.out.println(course.resolveConstantDesc(java.lang.invoke.MethodHandles.lookup()) == course);
}
}
true
String resolution returns the receiver itself.
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
String is its own resolved constant. Other descriptor types may perform lookups and have different failure modes.