JDK-8344203 : java.lang.classfile.Attribute attributeName() method should return Utf8Entry
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.lang.classfile
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 24
  • Submitted: 2024-11-14
  • Updated: 2024-11-15
  • Resolved: 2024-11-14
Related Reports
CSR :  
Relates :  
Description
Summary
-------

Change `java.lang.classfile.Attribute::attributeName` method return type from `String` to `java.lang.classfile.constantpool.Utf8Entry`.

Problem
-------

Class-File API usually expose names in a form of  `java.lang.classfile.constantpool.Utf8Entry`. Unfortunately `java.lang.classfile.Attribute::attributeName` method was omitted during the API review and it returns inflated `String` value of the name. 

Solution
--------

Change `java.lang.classfile.Attribute::attributeName` method return type from `String` to `java.lang.classfile.constantpool.Utf8Entry`.

Specification
-------------

```
diff --git a/src/java.base/share/classes/java/lang/classfile/Attribute.java b/src/java.base/share/classes/java/lang/classfile/Attribute.java
index e2f0072d3967f..2d559552684a1 100644
--- a/src/java.base/share/classes/java/lang/classfile/Attribute.java
+++ b/src/java.base/share/classes/java/lang/classfile/Attribute.java
@@ -25,6 +25,7 @@
 package java.lang.classfile;
 
 import java.lang.classfile.attribute.*;
+import java.lang.classfile.constantpool.Utf8Entry;
 
 import jdk.internal.classfile.impl.BoundAttribute;
 import jdk.internal.classfile.impl.UnboundAttribute;
@@ -65,7 +66,7 @@ public sealed interface Attribute<A extends Attribute<A>>
     /**
      * {@return the name of the attribute}
      */
-    String attributeName();
+    Utf8Entry attributeName();
 
     /**
      * {@return the {@link AttributeMapper} associated with this attribute}
diff --git a/src/java.base/share/classes/java/lang/classfile/CustomAttribute.java b/src/java.base/share/classes/java/lang/classfile/CustomAttribute.java
index 9fe492dc22c4c..6c3a0de2b7af3 100644
--- a/src/java.base/share/classes/java/lang/classfile/CustomAttribute.java
+++ b/src/java.base/share/classes/java/lang/classfile/CustomAttribute.java
@@ -24,6 +24,8 @@
  */
 package java.lang.classfile;
 
+import java.lang.classfile.constantpool.Utf8Entry;
+import jdk.internal.classfile.impl.TemporaryConstantPool;
 import jdk.internal.javac.PreviewFeature;
 
 /**
@@ -55,8 +57,8 @@ public final AttributeMapper<T> attributeMapper() {
     }
 
     @Override
-    public final String attributeName() {
-        return mapper.name();
+    public Utf8Entry attributeName() {
+        return TemporaryConstantPool.INSTANCE.utf8Entry(mapper.name());
     }
 
     @Override
```


Comments
Moving to Approved.
14-11-2024

Is there any specific feedback from CSR review so you moved this to proposed instead of finalized? Like how the Class-File API main CSR should be updated if this specification change is applied?
14-11-2024

CSR body looks good. Added trivial compatibility risk and interface kind and scope. Reviewed.
14-11-2024