changeset 1637:c336be86b44d tip

Fix: add support for nestmate attributes to nasgen
author mcimadamore
date Mon, 03 Apr 2017 17:55:21 +0100
parents 0714a30d7833
children
files buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/Main.java
diffstat 1 files changed, 71 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/Main.java	Thu Mar 10 09:28:20 2016 -0800
+++ b/buildtools/nasgen/src/jdk/nashorn/internal/tools/nasgen/Main.java	Mon Apr 03 17:55:21 2017 +0100
@@ -30,8 +30,13 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.Arrays;
+
+import jdk.internal.org.objectweb.asm.Attribute;
+import jdk.internal.org.objectweb.asm.ByteVector;
 import jdk.internal.org.objectweb.asm.ClassReader;
 import jdk.internal.org.objectweb.asm.ClassWriter;
+import jdk.internal.org.objectweb.asm.Label;
 import jdk.internal.org.objectweb.asm.Opcodes;
 import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
 
@@ -40,6 +45,11 @@
  *
  */
 public class Main {
+
+    static final Attribute[] ATTRIBUTE_PROTOS = new Attribute[] {
+        new NestMembers(),
+        new MemberOfNest()
+    };
     /**
      * ASM version to be used by nasgen tool.
      */
@@ -124,7 +134,7 @@
                 final ClassWriter writer = ClassGenerator.makeClassWriter();
                 final ClassReader reader = new ClassReader(buf);
                 final ScriptClassInstrumentor inst = new ScriptClassInstrumentor(writer, sci);
-                reader.accept(inst, 0);
+                reader.accept(inst, ATTRIBUTE_PROTOS, 0);
                 //noinspection UnusedAssignment
 
                 // write instrumented class
@@ -184,4 +194,64 @@
         System.err.println(msg);
         System.exit(exitCode);
     }
+
+    static class NestMembers extends Attribute {
+        public NestMembers() {
+            super("NestMembers");
+        }
+
+        byte[] bytes;
+        String[] classes;
+
+        @Override
+        protected Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) {
+            int offset = off;
+            NestMembers a = new NestMembers();
+            int size = cr.readShort(off);
+            a.classes = new String[size];
+            off += 2;
+            for (int i = 0; i < size ; i++) {
+                a.classes[i] = cr.readClass(off, buf);
+                off += 2;
+            }
+            a.bytes = Arrays.copyOfRange(cr.b, offset, offset + len);
+            return a;
+        }
+
+        @Override
+        protected ByteVector write(ClassWriter cw, byte[] code, int len, int maxStack, int maxLocals) {
+            ByteVector v = new ByteVector(bytes.length);
+            v.putShort(classes.length);
+            for (String s : classes) {
+                v.putShort(cw.newClass(s));
+            }
+            return v;
+        }
+    }
+
+    static class MemberOfNest extends Attribute {
+
+        byte[] bytes;
+        String clazz;
+
+        public MemberOfNest() {
+            super("MemberOfNest");
+        }
+
+        @Override
+        protected Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) {
+            int offset = off;
+            MemberOfNest a = new MemberOfNest();
+            a.clazz = cr.readClass(off, buf);
+            a.bytes = Arrays.copyOfRange(cr.b, offset, offset + len);
+            return a;
+        }
+
+        @Override
+        protected ByteVector write(ClassWriter cw, byte[] code, int len, int maxStack, int maxLocals) {
+            ByteVector v = new ByteVector(bytes.length);
+            v.putShort(cw.newClass(clazz));
+            return v;
+        }
+    }
 }