changeset 56915:47cdf3544a0a records-and-sealed

updating the Record attribute
author vromero
date Wed, 03 Jul 2019 14:55:00 -0400
parents 0c7ace1bea93
children 2392106362e3
files src/hotspot/share/classfile/classFileParser.cpp src/hotspot/share/oops/instanceKlass.hpp src/hotspot/share/oops/recordParamInfo.hpp src/hotspot/share/oops/recordParamStreams.hpp src/hotspot/share/prims/jvm.cpp src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java src/jdk.jdeps/share/classes/com/sun/tools/classfile/ClassWriter.java src/jdk.jdeps/share/classes/com/sun/tools/classfile/Record_attribute.java src/jdk.jdeps/share/classes/com/sun/tools/javap/AttributeWriter.java
diffstat 10 files changed, 54 insertions(+), 147 deletions(-) [+]
line wrap: on
line diff
--- a/src/hotspot/share/classfile/classFileParser.cpp	Sat Jun 29 13:22:45 2019 -0700
+++ b/src/hotspot/share/classfile/classFileParser.cpp	Wed Jul 03 14:55:00 2019 -0400
@@ -3282,37 +3282,18 @@
   for (int n = 0; n < num_of_params; n++) {
     cfs->guarantee_more(RecordParamInfo::param_slots, CHECK);
 
-    const u2 name_index = cfs->get_u2_fast();
-    check_property(valid_symbol_at(name_index),
-      "Invalid constant pool index %u for record parameter name in class file %s",
-      name_index, CHECK);
-    const Symbol* const name = cp->symbol_at(name_index);
-    verify_legal_field_name(name, CHECK);
+    const u2 method_ref_index = cfs->get_u2_fast();
+    //check_property(valid_symbol_at(method_ref_index),
+    //  "Invalid constant pool index %u for record parameter name in class file %s",
+    //  method_ref_index, CHECK);
+    //const Symbol* const method_ref = cp->symbol_at(method_ref_index);
+    // we will need a way to verify that the symbol is correct
+    // verify_legal_field_name(name, CHECK);
     // DEBUG
     // tty->print_cr("name read %s", name->as_klass_external_name());
 
-    const u2 descriptor_index = cfs->get_u2_fast();
-    check_property(valid_symbol_at(descriptor_index),
-      "Invalid constant pool index %u for record parameter descriptor in class file %s",
-      descriptor_index, CHECK);
-    const Symbol* const descriptor = cp->symbol_at(descriptor_index);
-    verify_legal_field_signature(name, descriptor, CHECK);
-    // DEBUG
-    // tty->print_cr("descriptor read %s", descriptor->as_klass_external_name());
-
-    const u2 signature_index = cfs->get_u2_fast();
-    check_property(valid_symbol_at(signature_index),
-      "Invalid constant pool index %u for record parameter signature in class file %s",
-      signature_index, CHECK);
-    const Symbol* const sig = cp->symbol_at(signature_index);
-    // DEBUG
-    // tty->print_cr("signature read %s", sig->as_klass_external_name());
-
     RecordParamInfo* const record_param_info = RecordParamInfo::from_record_params_array(record_params_array, n);
-    record_param_info->initialize(
-                      name_index,
-                      descriptor_index,
-                      signature_index);
+    record_param_info->initialize(method_ref_index);
   }
 
   assert(NULL == _record_params, "invariant");
--- a/src/hotspot/share/oops/instanceKlass.hpp	Sat Jun 29 13:22:45 2019 -0700
+++ b/src/hotspot/share/oops/instanceKlass.hpp	Wed Jul 03 14:55:00 2019 -0400
@@ -457,9 +457,7 @@
   void set_nest_host_index(u2 i)  { _nest_host_index = i; }
 
   // record parameters
-  Symbol* record_param_name(int index) const { return record_param(index)->name(constants()); }
-  Symbol* record_param_signature(int index) const { return record_param(index)->signature(constants()); }
-  Symbol* record_param_descriptor(int index) const { return record_param(index)->signature(constants()); }
+  int record_param_accessor_index(int index) const { return record_param(index)->accessor_index(); }
 
   int record_params_count() const       { return _record_params_count; }
 
--- a/src/hotspot/share/oops/recordParamInfo.hpp	Sat Jun 29 13:22:45 2019 -0700
+++ b/src/hotspot/share/oops/recordParamInfo.hpp	Wed Jul 03 14:55:00 2019 -0400
@@ -40,22 +40,15 @@
   friend class RecordParameterStreamBase;
   friend class JavaRecordParameterStream;
   enum ParamOffset {
-    name_index_offset        = 0,
-    descriptor_index_offset  = 1,
-    signature_index_offset   = 2,
-    param_slots              = 3
+    accessor_index_offset    = 0,
+    param_slots              = 1
   };
 
 private:
   u2 _shorts[param_slots];
 
-  void set_name_index(u2 val)                              { _shorts[name_index_offset] = val;         }
-  void set_descriptor_index(u2 val)                        { _shorts[descriptor_index_offset] = val;   }
-  void set_signature_index(u2 val)                         { _shorts[signature_index_offset] = val;    }
+  void set_accessor_index(u2 val)                          { _shorts[accessor_index_offset] = val;    }
 
-  u2 name_index() const                                    { return _shorts[name_index_offset];        }
-  u2 descriptor_index() const                              { return _shorts[descriptor_index_offset];  }
-  u2 signature_index() const                               { return _shorts[signature_index_offset];   }
 public:
   static RecordParamInfo* from_record_params_array(Array<u2>* record_params, int index) {
     return ((RecordParamInfo*)record_params->adr_at(index * param_slots));
@@ -64,17 +57,11 @@
     return ((RecordParamInfo*)(record_params + index * param_slots));
   }
 
-  void initialize(u2 name_index,
-                  u2 descriptor_index,
-                  u2 signature_index) {
-    _shorts[name_index_offset] = name_index;
-    _shorts[descriptor_index_offset] = descriptor_index;
-    _shorts[signature_index_offset] = signature_index;
+  void initialize(u2 accessor_index) {
+    _shorts[accessor_index_offset] = accessor_index;
   }
 
-  Symbol* name(const constantPoolHandle& cp) const          { return cp->symbol_at(name_index());       }
-  Symbol* signature(const constantPoolHandle& cp) const     { return cp->symbol_at(signature_index());  }
-  Symbol* descriptor(const constantPoolHandle& cp) const    { return cp->symbol_at(descriptor_index()); }
+  u2 accessor_index() const                                { return _shorts[accessor_index_offset];   }
 };
 
 #endif // SHARE_VM_OOPS_RECORDPARAMINFO_HPP
--- a/src/hotspot/share/oops/recordParamStreams.hpp	Sat Jun 29 13:22:45 2019 -0700
+++ b/src/hotspot/share/oops/recordParamStreams.hpp	Wed Jul 03 14:55:00 2019 -0400
@@ -76,16 +76,8 @@
   }
   bool done() const { return _index >= _limit; }
 
-  Symbol* name() const {
-    return record_param()->name(_constants);
-  }
-
-  Symbol* descriptor() const {
-    return record_param()->descriptor(_constants);
-  }
-
-  Symbol* signature() const {
-    return record_param()->signature(_constants);
+  int accessor_index() const {
+    return record_param()->accessor_index();
   }
 };
 
@@ -94,23 +86,11 @@
  public:
   JavaRecordParameterStream(const InstanceKlass* k): RecordParameterStreamBase(k->record_params(), k->constants(), 0, k->record_params_count()) {}
 
-  int name_index() const {
-    return record_param()->name_index();
+  int accessor_index() const {
+    return record_param()->accessor_index();
   }
-  void set_name_index(int index) {
-    record_param()->set_name_index(index);
-  }
-  int descriptor_index() const {
-    return record_param()->descriptor_index();
-  }
-  void set_descriptor_index(int index) {
-    record_param()->set_descriptor_index(index);
-  }
-  int signature_index() const {
-    return record_param()->signature_index();
-  }
-  void set_generic_signature_index(int index) {
-    record_param()->set_signature_index(index);
+  void set_accessor_index(int index) {
+    record_param()->set_accessor_index(index);
   }
 };
 
--- a/src/hotspot/share/prims/jvm.cpp	Sat Jun 29 13:22:45 2019 -0700
+++ b/src/hotspot/share/prims/jvm.cpp	Wed Jul 03 14:55:00 2019 -0400
@@ -1689,7 +1689,14 @@
 
   int out_idx = 0;
   for (JavaRecordParameterStream recordParamsStream(k); !recordParamsStream.done(); recordParamsStream.next()) {
-    Handle str = java_lang_String::create_from_symbol(recordParamsStream.name(), CHECK_NULL);
+    int accessor_index = recordParamsStream.accessor_index();
+    //tty->print_cr("accessor index == %d", accessor_index);
+    int method_ref_info = (int)k->constants()->int_at(accessor_index);
+    int name_and_type_index = extract_high_short_from_int(method_ref_info);
+    int name_and_type_info = (int)k->constants()->int_at(name_and_type_index);
+    int name_index = extract_low_short_from_int(name_and_type_info);
+    //tty->print_cr("name index == %d", name_index);
+    Handle str = java_lang_String::create_from_symbol(k->constants()->symbol_at(name_index), CHECK_NULL);
     dest->obj_at_put(out_idx, str());
     ++out_idx;
   }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Sat Jun 29 13:22:45 2019 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java	Wed Jul 03 14:55:00 2019 -0400
@@ -2303,6 +2303,23 @@
         return buffer.toList();
     }
 
+    /* this method looks for explicit accessors to add them to the corresponding field
+     */
+    void findUserDefinedAccessors(JCClassDecl tree) {
+        tree.defs.stream()
+                .filter(t -> t.hasTag(VARDEF))
+                .map(t -> (JCVariableDecl)t)
+                .filter(vd -> (vd.sym.accessors.isEmpty() && !vd.sym.isStatic()))
+                .forEach(vd -> {
+                    MethodSymbol msym = lookupMethod(tree.pos(),
+                            vd.name,
+                            tree.sym.type,
+                            List.nil());
+                    Assert.check(msym != null, "there has to be a user defined accessor");
+                    vd.sym.accessors = List.of(new Pair<>(Accessors.Kind.GET, msym));
+                });
+    }
+
     /** Translate an enum class. */
     private void visitEnumDef(JCClassDecl tree) {
         make_at(tree.pos());
@@ -2488,6 +2505,7 @@
                 recordExtractor(tree, getterMethHandlesForExtractor),
                 recordReadResolve(tree)
         ));
+        findUserDefinedAccessors(tree);
     }
 
     JCTree generateRecordMethod(JCClassDecl tree, Name name, List<VarSymbol> vars, MethodHandleSymbol[] getterMethHandles) {
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Sat Jun 29 13:22:45 2019 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Wed Jul 03 14:55:00 2019 -0400
@@ -845,16 +845,8 @@
         }
         databuf.appendChar(numParams);
         for (VarSymbol v: vars) {
-            databuf.appendChar(poolWriter.putName(v.name));
-            // descriptor
-            databuf.appendChar(poolWriter.putDescriptor(v));
-            // signature
-            databuf.appendChar(poolWriter.putSignature(v));
+            databuf.appendChar(poolWriter.putMember(v.accessors.head.snd));
         }
-        int acountIdx = beginAttrs();
-        int acount = 0;
-        acount += writeParameterAttrs(vars);
-        endAttrs(acountIdx, acount);
         endAttr(alenIdx);
         return 1;
     }
--- a/src/jdk.jdeps/share/classes/com/sun/tools/classfile/ClassWriter.java	Sat Jun 29 13:22:45 2019 -0700
+++ b/src/jdk.jdeps/share/classes/com/sun/tools/classfile/ClassWriter.java	Wed Jul 03 14:55:00 2019 -0400
@@ -644,19 +644,11 @@
         @Override
         public Void visitRecord(Record_attribute attr, ClassOutputStream out) {
             out.writeShort(attr.num_params);
-            for (Record_attribute.Param_data e: attr.params)
-                writeParamData(e, out);
-            new AttributeWriter().write(attr.attributes, out);
+            for (int e: attr.accessors)
+                out.writeShort(e);
             return null;
         }
 
-        protected void writeParamData(Record_attribute.Param_data pd, ClassOutputStream out) {
-            out.writeShort(pd.param_name_index);
-            out.writeShort(pd.param_flags);
-            out.writeShort(pd.param_descriptor);
-            out.writeShort(pd.param_signature);
-        }
-
         @Override
         public Void visitRuntimeInvisibleAnnotations(RuntimeInvisibleAnnotations_attribute attr, ClassOutputStream out) {
             annotationWriter.write(attr.annotations, out);
--- a/src/jdk.jdeps/share/classes/com/sun/tools/classfile/Record_attribute.java	Sat Jun 29 13:22:45 2019 -0700
+++ b/src/jdk.jdeps/share/classes/com/sun/tools/classfile/Record_attribute.java	Wed Jul 03 14:55:00 2019 -0400
@@ -38,11 +38,10 @@
     Record_attribute(ClassReader cr, int name_index, int length) throws IOException {
         super(name_index, length);
         num_params = cr.readUnsignedShort();
-        params = new Param_data[num_params];
+        accessors = new int[num_params];
         for (int i = 0; i < num_params; i++) {
-            params[i] = new Param_data(cr);
+            accessors[i] = cr.readUnsignedShort();
         }
-        attributes = new Attributes(cr);
     }
 
     @Override
@@ -51,24 +50,5 @@
     }
 
     public final int num_params;
-    public final Param_data[] params;
-    public final Attributes attributes;
-
-    public static class Param_data {
-        Param_data(ClassReader cr) throws IOException {
-            param_name_index = cr.readUnsignedShort();
-            param_flags = cr.readUnsignedShort();
-            param_descriptor = cr.readUnsignedShort();
-            param_signature = cr.readUnsignedShort();
-        }
-
-        public String getName(ConstantPool constant_pool) throws ConstantPoolException {
-            return constant_pool.getUTF8Value(param_name_index);
-        }
-
-        public final int param_name_index;
-        public final int param_flags;
-        public final int param_descriptor;
-        public final int param_signature;
-    }
+    public final int[] accessors;
 }
--- a/src/jdk.jdeps/share/classes/com/sun/tools/javap/AttributeWriter.java	Sat Jun 29 13:22:45 2019 -0700
+++ b/src/jdk.jdeps/share/classes/com/sun/tools/javap/AttributeWriter.java	Wed Jul 03 14:55:00 2019 -0400
@@ -61,7 +61,6 @@
 import com.sun.tools.classfile.NestHost_attribute;
 import com.sun.tools.classfile.NestMembers_attribute;
 import com.sun.tools.classfile.Record_attribute;
-import com.sun.tools.classfile.Record_attribute.Param_data;
 import com.sun.tools.classfile.RuntimeInvisibleAnnotations_attribute;
 import com.sun.tools.classfile.RuntimeInvisibleParameterAnnotations_attribute;
 import com.sun.tools.classfile.RuntimeInvisibleTypeAnnotations_attribute;
@@ -731,39 +730,12 @@
         println("Record:");
         indent(+1);
         for (int i = 0; i < attr.num_params; i++) {
-            writeParamData(attr.params[i]);
+            println("#" + attr.accessors[i]);
         }
-        write(attr, attr.attributes, constant_pool);
         indent(-1);
         return null;
     }
 
-    void writeParamData(Param_data pd) {
-        AccessFlags flags = new AccessFlags(pd.param_flags);
-        writeModifiers(flags.getFieldModifiers());
-        Descriptor descriptor = new Descriptor(pd.param_descriptor);
-        print(getJavaFieldType(descriptor));
-        print(" ");
-        try {
-            print(pd.getName(constant_pool));
-        } catch (ConstantPoolException cpe) {
-            // ignore
-        }
-        println(";");
-        try {
-            indent(+1);
-            println("descriptor: " + descriptor.getValue(constant_pool));
-            writeList(String.format("flags: (0x%04x) ", flags.flags), flags.getFieldFlags(), "\n");
-            Signature signature = new Signature(pd.param_signature);
-            Type t = signature.getType(constant_pool);
-            println("signature: " + getJavaName(t.toString()));
-            indent(-1);
-        } catch (ConstantPoolException cpe) {
-            // ignore
-        }
-        println();
-    }
-
     void writeList(String prefix, Collection<?> items, String suffix) {
         print(prefix);
         String sep = "";