changeset 3288:b345d7339ab3

Enhancement: Refine ParameterizedType_info to include enclosing class info and value vs. class flag
author mcimadamore
date Fri, 22 Jan 2016 16:32:32 +0000
parents 659f44b99b6c
children 7943e5b37578
files src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Pool.java src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/PoolReader.java src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/PoolWriter.java src/jdk.jdeps/share/classes/com/sun/tools/classfile/ClassTranslator.java src/jdk.jdeps/share/classes/com/sun/tools/classfile/ConstantPool.java src/jdk.jdeps/share/classes/com/sun/tools/javap/ConstantWriter.java
diffstat 6 files changed, 53 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Pool.java	Fri Jan 22 16:25:26 2016 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Pool.java	Fri Jan 22 16:32:32 2016 +0000
@@ -435,28 +435,40 @@
      */
     static class ParameterizedType extends Entry {
 
+        /** Enclosing type. */
+        ParameterizedType enclosing;
+
+        /** Basic type. */
+        char basicType;
+
         /** Base class. */
-        ClassRef clazz;
+        Constant<Name> templateClassName;
 
         /** Type-arguments list. */
         Entry[] typeargs;
 
-        ParameterizedType(ClassRef clazz, Entry[] typeargs, ClassType ct) {
+        ParameterizedType(ParameterizedType enclosing, char basicType, Constant<Name> templateClassName,
+                          Entry[] typeargs, ClassType ct) {
             super(ClassFile.CONSTANT_ParameterizedType, ct);
-            this.clazz = clazz;
+            this.enclosing = enclosing;
+            this.basicType = basicType;
+            this.templateClassName = templateClassName;
             this.typeargs = typeargs;
         }
 
         @Override
         public int hashCode() {
-            return Objects.hash(clazz, Arrays.hashCode(typeargs));
+            return Objects.hash(enclosing, basicType, templateClassName, Arrays.hashCode(typeargs));
         }
 
         @Override
         public boolean equals(Object obj) {
             if (obj instanceof ParameterizedType) {
                 ParameterizedType that = (ParameterizedType)obj;
-                return that.clazz.equals(clazz) && Objects.deepEquals(that.typeargs, typeargs);
+                return Objects.equals(that.enclosing, enclosing) &&
+                        that.basicType == basicType &&
+                        that.templateClassName.equals(templateClassName) &&
+                        Objects.deepEquals(that.typeargs, typeargs);
             } else {
                 return false;
             }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/PoolReader.java	Fri Jan 22 16:25:26 2016 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/PoolReader.java	Fri Jan 22 16:32:32 2016 +0000
@@ -265,7 +265,7 @@
         @Override
         Type doResolve() {
             //return erased classs type
-            return PoolReader.this.getClass(reader.buf.getChar(offset)).type;
+            return PoolReader.this.resolveClassInternal(reader.buf.getChar(offset + 3)).type;
         }
     }
 
@@ -509,8 +509,6 @@
                         internalize(name.getByteArray(),
                                     name.getByteOffset(),
                                     name.getByteLength())));
-            case CONSTANT_ParameterizedType:
-                return (ClassSymbol)((Type)e.data.get()).tsym;
             default:
                 throw new IllegalStateException("Illegal type entry: " + e.tag);
         }
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/PoolWriter.java	Fri Jan 22 16:25:26 2016 +0530
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/PoolWriter.java	Fri Jan 22 16:32:32 2016 +0000
@@ -38,6 +38,7 @@
 import com.sun.tools.javac.code.Type.ClassType;
 import com.sun.tools.javac.code.Type.TypeVar;
 import com.sun.tools.javac.code.Type.WildcardType;
+import com.sun.tools.javac.code.TypeTag;
 import com.sun.tools.javac.code.Types;
 import com.sun.tools.javac.code.Types.DefaultSymbolVisitor;
 import com.sun.tools.javac.code.Types.DefaultTypeVisitor;
@@ -435,16 +436,26 @@
         }
 
         @Override
+        @SuppressWarnings("unchecked")
         public Entry visitClassType(ClassType t, Function<Type, Name> typeNameFunc) {
             if (t.allparams().isEmpty()) {
                 return super.visitClassType(t, typeNameFunc);
             } else {
                 useNewCPForms = true;
-                ClassRef clazz = (ClassRef)makeSymbol(t.tsym);
+                Type encl = t.getEnclosingType();
+                Entry encl_entry = null;
+                while (!encl.hasTag(TypeTag.NONE)) {
+                    encl_entry = visit(t.getEnclosingType(), typeNameFunc);
+                    if (encl_entry.tag == ClassFile.CONSTANT_ParameterizedType) {
+                        break;
+                    }
+                }
+                Constant<Name> clazz = (Constant<Name>)makeConstant(t.tsym.flatName());
                 Entry[] typeargs = t.getTypeArguments().stream()
                         .map(ta -> visit(ta, PoolWriter.this::typeToSig))
                         .toArray(Entry[]::new);
-                Entry e = new ParameterizedType(clazz, typeargs, t);
+                char basicType = types.isValue(t) ? 'Q' : 'L';
+                Entry e = new ParameterizedType((ParameterizedType)encl_entry, basicType, clazz, typeargs, t);
                 return pool.put(e);
             }
         }
@@ -560,7 +571,10 @@
                     Stream.of(((TypeVariable)e).bounds).mapToInt(b -> b.index).forEach(poolbuf::appendChar);
                     break;
                 case ClassFile.CONSTANT_ParameterizedType:
-                    poolbuf.appendChar(((ParameterizedType)e).clazz.index);
+                    ParameterizedType encl = ((ParameterizedType)e).enclosing;
+                    poolbuf.appendChar(encl == null ? 0 : encl.index);
+                    poolbuf.appendByte(((ParameterizedType)e).basicType);
+                    poolbuf.appendChar(((ParameterizedType)e).templateClassName.index);
                     poolbuf.appendByte(((ParameterizedType)e).typeargs.length);
                     Stream.of(((ParameterizedType)e).typeargs).mapToInt(ta -> ta.index).forEach(poolbuf::appendChar);
                     break;
--- a/src/jdk.jdeps/share/classes/com/sun/tools/classfile/ClassTranslator.java	Fri Jan 22 16:25:26 2016 +0530
+++ b/src/jdk.jdeps/share/classes/com/sun/tools/classfile/ClassTranslator.java	Fri Jan 22 16:32:32 2016 +0000
@@ -425,7 +425,7 @@
     public CPInfo visitParameterizedType(CONSTANT_ParameterizedType_info info, Map<Object, Object> translations) {
         CONSTANT_ParameterizedType_info info2 = (CONSTANT_ParameterizedType_info) translations.get(info);
         if (info2 == null) {
-            info2 = new CONSTANT_ParameterizedType_info(info.clazz_idx, info.params_idxs);
+            info2 = new CONSTANT_ParameterizedType_info(info.encl_idx, info.basicType, info.clazz_idx, info.params_idxs);
             translations.put(info, info2);
         }
         return info;
--- a/src/jdk.jdeps/share/classes/com/sun/tools/classfile/ConstantPool.java	Fri Jan 22 16:25:26 2016 +0530
+++ b/src/jdk.jdeps/share/classes/com/sun/tools/classfile/ConstantPool.java	Fri Jan 22 16:32:32 2016 +0000
@@ -1082,6 +1082,8 @@
     //Todo: add enclosing type
     public static class CONSTANT_ParameterizedType_info extends CPInfo {
         CONSTANT_ParameterizedType_info(ClassReader cr) throws IOException {
+            encl_idx = cr.readUnsignedShort();
+            basicType = cr.readUnsignedByte();
             clazz_idx = cr.readUnsignedShort();
             int nparams = cr.readUnsignedByte();
             params_idxs = new int[nparams];
@@ -1090,7 +1092,9 @@
             }
         }
 
-        public CONSTANT_ParameterizedType_info(int clazz_idx, int[] params_idxs) {
+        public CONSTANT_ParameterizedType_info(int encl_idx, int basicType, int clazz_idx, int[] params_idxs) {
+            this.encl_idx = encl_idx;
+            this.basicType = basicType;
             this.clazz_idx = clazz_idx;
             this.params_idxs = params_idxs;
         }
@@ -1101,6 +1105,8 @@
 
         public int byteLength() {
             return 1 + //tag
+                    2 + //encl_idx
+                    1 + //basicType
                     2 + //clazz_idx
                     1 + //params length
                     (params_idxs.length) * 2; //params_idxs
@@ -1108,13 +1114,16 @@
 
         @Override
         public String toString() {
-            return "CONSTANT_ParameterizedType_info[clazz_index: " + clazz_idx + ", params = " + Arrays.toString(params_idxs) + "]";
+            return "CONSTANT_ParameterizedType_info[encl_index: " + encl_idx + ", basicType: " + basicType
+                    + ", clazz_index: " + clazz_idx + ", params = " + Arrays.toString(params_idxs) + "]";
         }
 
         public <R, D> R accept(Visitor<R, D> visitor, D data) {
             return visitor.visitParameterizedType(this, data);
         }
 
+        public final int encl_idx;
+        public final int basicType;
         public final int clazz_idx;
         public final int[] params_idxs;
     }
@@ -1229,7 +1238,7 @@
         @Override
         public String visitParameterizedType(CONSTANT_ParameterizedType_info info, ConstantPool pool) {
             try {
-                String base = pool.getClassInfo(info.clazz_idx).getName();
+                String base = pool.getUTF8Value(info.clazz_idx);
                 String args = IntStream.of(info.params_idxs)
                         .mapToObj(i -> {
                                     try {
@@ -1277,7 +1286,7 @@
     private static final Visitor<String, ConstantPool> typeVisitor = new NameVisitor() {
         @Override
         public String visitParameterizedType(CONSTANT_ParameterizedType_info info, ConstantPool pool) {
-            return "L" + super.visitParameterizedType(info, pool) + ";";
+            return info.basicType + super.visitParameterizedType(info, pool) + ";";
         }
 
         @Override
--- a/src/jdk.jdeps/share/classes/com/sun/tools/javap/ConstantWriter.java	Fri Jan 22 16:25:26 2016 +0530
+++ b/src/jdk.jdeps/share/classes/com/sun/tools/javap/ConstantWriter.java	Fri Jan 22 16:32:32 2016 +0000
@@ -469,11 +469,14 @@
 
         @Override
         public String visitParameterizedType(CONSTANT_ParameterizedType_info info, Void aVoid) {
+            String encl = info.encl_idx != 0 ?
+                    stringValue(info.encl_idx) :
+                    "";
             String clazz = stringValue(info.clazz_idx);
             String targs = IntStream.of(info.params_idxs)
                     .mapToObj(i -> stringValue(i))
                     .collect(Collectors.joining(",", "<", ">"));
-            return String.format("%s%s", clazz, targs);
+            return String.format("[%s]%c%s%s", encl, info.basicType, clazz, targs);
         }
 
         @Override