changeset 4484:b5eae0a5878c

Replace hashes in ModuleHashes with array
author alanb
date Sat, 26 Nov 2016 15:15:50 +0000
parents 7178621e9e1f
children 8156e205fcb4
files src/jdk.jdeps/share/classes/com/sun/tools/classfile/ClassWriter.java src/jdk.jdeps/share/classes/com/sun/tools/classfile/ModuleHashes_attribute.java src/jdk.jdeps/share/classes/com/sun/tools/javap/AttributeWriter.java
diffstat 3 files changed, 38 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.jdeps/share/classes/com/sun/tools/classfile/ClassWriter.java	Thu Nov 24 09:14:01 2016 +0000
+++ b/src/jdk.jdeps/share/classes/com/sun/tools/classfile/ClassWriter.java	Sat Nov 26 15:15:50 2016 +0000
@@ -471,8 +471,11 @@
             out.writeShort(attr.algorithm_index);
             out.writeShort(attr.hashes_table.length);
             for (ModuleHashes_attribute.Entry e: attr.hashes_table) {
-                out.writeShort(e.requires_index);
-                out.writeShort(e.hash_index);
+                out.writeShort(e.module_name_index);
+                out.writeShort(e.hash.length);
+                for (byte b: e.hash) {
+                    out.writeByte(b);
+                }
             }
             return null;
         }
--- a/src/jdk.jdeps/share/classes/com/sun/tools/classfile/ModuleHashes_attribute.java	Thu Nov 24 09:14:01 2016 +0000
+++ b/src/jdk.jdeps/share/classes/com/sun/tools/classfile/ModuleHashes_attribute.java	Sat Nov 26 15:15:50 2016 +0000
@@ -51,7 +51,7 @@
     }
 
     public ModuleHashes_attribute(int name_index, int algorithm_index, Entry[] hashes_table) {
-        super(name_index, 4 + hashes_table.length * Entry.length());
+        super(name_index, 2 + 2 + length(hashes_table));
         this.algorithm_index = algorithm_index;
         this.hashes_table_length = hashes_table.length;
         this.hashes_table = hashes_table;
@@ -62,22 +62,34 @@
         return visitor.visitModuleHashes(this, data);
     }
 
+    private static int length(Entry[] hashes_table) {
+        int len = 0;
+        for (Entry e: hashes_table) {
+            len += e.length();
+        }
+        return len;
+    }
+
     public final int algorithm_index;
     public final int hashes_table_length;
     public final Entry[] hashes_table;
 
     public static class Entry {
         Entry(ClassReader cr) throws IOException {
-            requires_index = cr.readUnsignedShort();
-            hash_index = cr.readUnsignedShort();
+            module_name_index = cr.readUnsignedShort();
+            int hash_length = cr.readUnsignedShort();
+            hash = new byte[hash_length];
+            for (int i=0; i<hash_length; i++) {
+                hash[i] = (byte) cr.readUnsignedByte();
+            }
         }
 
-        public static int length() {
-            return 4;
+        public int length() {
+            return 4 + hash.length;
         }
 
-        public final int requires_index;
-        public final int hash_index;
+        public final int module_name_index;
+        public final byte[] hash;
     }
 
 }
--- a/src/jdk.jdeps/share/classes/com/sun/tools/javap/AttributeWriter.java	Thu Nov 24 09:14:01 2016 +0000
+++ b/src/jdk.jdeps/share/classes/com/sun/tools/javap/AttributeWriter.java	Sat Nov 26 15:15:50 2016 +0000
@@ -330,9 +330,11 @@
         tab();
         println("// " + getAlgorithm(attr));
         for (ModuleHashes_attribute.Entry e : attr.hashes_table) {
-            print("#" + e.requires_index + ", #" + e.hash_index);
+            print("#" + e.module_name_index);
             tab();
-            println("// " + getRequires(e) + ": " + getHash(e));
+            println("// " + getModuleName(e));
+            println("hash_length: " + e.hash.length);
+            println("hash: [" + toHex(e.hash) + "]");
         }
         indent(-1);
         return null;
@@ -346,17 +348,9 @@
         }
     }
 
-    private String getRequires(ModuleHashes_attribute.Entry entry) {
+    private String getModuleName(ModuleHashes_attribute.Entry entry) {
         try {
-            return constant_pool.getUTF8Value(entry.requires_index);
-        } catch (ConstantPoolException e) {
-            return report(e);
-        }
-    }
-
-    private String getHash(ModuleHashes_attribute.Entry entry) {
-        try {
-            return constant_pool.getUTF8Value(entry.hash_index);
+            return constant_pool.getUTF8Value(entry.module_name_index);
         } catch (ConstantPoolException e) {
             return report(e);
         }
@@ -1022,6 +1016,14 @@
         return StringUtils.toUpperCase(s);
     }
 
+    static String toHex(byte[] ba) {
+        StringBuilder sb = new StringBuilder(ba.length);
+        for (byte b: ba) {
+            sb.append(String.format("%02x", b & 0xff));
+        }
+        return sb.toString();
+    }
+
     private final AnnotationWriter annotationWriter;
     private final CodeWriter codeWriter;
     private final ConstantWriter constantWriter;