< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/PoolWriter.java

Print this page




  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.jvm;
  27 
  28 import com.sun.tools.javac.code.Kinds.Kind;
  29 import com.sun.tools.javac.code.Symbol;
  30 import com.sun.tools.javac.code.Symbol.ClassSymbol;
  31 import com.sun.tools.javac.code.Symbol.DynamicMethodSymbol;
  32 import com.sun.tools.javac.code.Symbol.MethodHandleSymbol;
  33 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
  34 import com.sun.tools.javac.code.Symbol.PackageSymbol;
  35 import com.sun.tools.javac.code.Type;

  36 import com.sun.tools.javac.code.Types;
  37 import com.sun.tools.javac.jvm.ClassWriter.PoolOverflow;
  38 import com.sun.tools.javac.jvm.ClassWriter.StringOverflow;
  39 import com.sun.tools.javac.jvm.PoolConstant.LoadableConstant;
  40 import com.sun.tools.javac.jvm.PoolConstant.LoadableConstant.BasicConstant;
  41 import com.sun.tools.javac.jvm.PoolConstant.Dynamic;
  42 import com.sun.tools.javac.jvm.PoolConstant.Dynamic.BsmKey;
  43 import com.sun.tools.javac.jvm.PoolConstant.NameAndType;
  44 import com.sun.tools.javac.util.ByteBuffer;
  45 import com.sun.tools.javac.util.List;
  46 import com.sun.tools.javac.util.Name;
  47 import com.sun.tools.javac.util.Names;
  48 
  49 import java.io.IOException;
  50 import java.io.OutputStream;
  51 import java.util.ArrayDeque;
  52 import java.util.HashMap;
  53 import java.util.LinkedHashMap;
  54 import java.util.LinkedHashSet;
  55 import java.util.Map;


 102         this.signatureGen = new SharedSignatureGenerator(types);
 103         this.pool = new WriteablePoolHelper();
 104     }
 105 
 106     /**
 107      * Puts a class symbol into the pool and return its index.
 108      */
 109     int putClass(ClassSymbol csym) {
 110         return putClass(csym.type);
 111     }
 112 
 113     /**
 114      * Puts a type into the pool and return its index. The type could be either a class, a type variable
 115      * or an array type.
 116      */
 117     int putClass(Type t) {
 118         return pool.writeIfNeeded(types.erasure(t));
 119     }
 120 
 121     /**








 122      * Puts a member reference into the constant pool. Valid members are either field or method symbols.
 123      */
 124     int putMember(Symbol s) {
 125         return pool.writeIfNeeded(s);
 126     }
 127 
 128     /**
 129      * Puts a dynamic reference into the constant pool and return its index.
 130      */
 131     int putDynamic(Dynamic d) {
 132         return pool.writeIfNeeded(d);
 133     }
 134 
 135     /**
 136      * Puts a field or method descriptor into the constant pool and return its index.
 137      */
 138     int putDescriptor(Type t) {
 139         return putName(typeSig(types.erasure(t)));
 140     }
 141 


 345             Object key = p.poolKey(types);
 346             Integer index = keysToPos.get(key);
 347             if (index == null) {
 348                 keysToPos.put(key, index = currentIndex++);
 349                 boolean first = todo.isEmpty();
 350                 todo.addLast(p);
 351                 if (first) {
 352                     while (!todo.isEmpty()) {
 353                         writeConstant(todo.peekFirst());
 354                         todo.removeFirst();
 355                     }
 356                 }
 357             }
 358             return index;
 359         }
 360 
 361         void writeConstant(PoolConstant c) {
 362             int tag = c.poolTag();
 363             switch (tag) {
 364                 case ClassFile.CONSTANT_Class: {
 365                     Type ct = (Type)c;
 366                     Name name = ct.hasTag(ARRAY) ?
 367                             typeSig(ct) :
 368                             names.fromUtf(externalize(ct.tsym.flatName()));
 369                     poolbuf.appendByte(tag);
 370                     poolbuf.appendChar(putName(name));
 371                     if (ct.hasTag(CLASS)) {
 372                         enterInner((ClassSymbol)ct.tsym);
 373                     }
 374                     break;
 375                 }
 376                 case ClassFile.CONSTANT_Utf8: {
 377                     Name name = (Name)c;
 378                     poolbuf.appendByte(tag);
 379                     byte[] bs = name.toUtf();
 380                     poolbuf.appendChar(bs.length);
 381                     poolbuf.appendBytes(bs, 0, bs.length);
 382                     if (overflowString == null && bs.length > MAX_STRING_LENGTH) {
 383                         //report error only once
 384                         overflowString = new String(bs);
 385                     }
 386                     break;
 387                 }
 388                 case ClassFile.CONSTANT_InterfaceMethodref:




  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.javac.jvm;
  27 
  28 import com.sun.tools.javac.code.Kinds.Kind;
  29 import com.sun.tools.javac.code.Symbol;
  30 import com.sun.tools.javac.code.Symbol.ClassSymbol;
  31 import com.sun.tools.javac.code.Symbol.DynamicMethodSymbol;
  32 import com.sun.tools.javac.code.Symbol.MethodHandleSymbol;
  33 import com.sun.tools.javac.code.Symbol.ModuleSymbol;
  34 import com.sun.tools.javac.code.Symbol.PackageSymbol;
  35 import com.sun.tools.javac.code.Type;
  36 import com.sun.tools.javac.code.Type.ConstantPoolQType;
  37 import com.sun.tools.javac.code.Types;
  38 import com.sun.tools.javac.jvm.ClassWriter.PoolOverflow;
  39 import com.sun.tools.javac.jvm.ClassWriter.StringOverflow;
  40 import com.sun.tools.javac.jvm.PoolConstant.LoadableConstant;
  41 import com.sun.tools.javac.jvm.PoolConstant.LoadableConstant.BasicConstant;
  42 import com.sun.tools.javac.jvm.PoolConstant.Dynamic;
  43 import com.sun.tools.javac.jvm.PoolConstant.Dynamic.BsmKey;
  44 import com.sun.tools.javac.jvm.PoolConstant.NameAndType;
  45 import com.sun.tools.javac.util.ByteBuffer;
  46 import com.sun.tools.javac.util.List;
  47 import com.sun.tools.javac.util.Name;
  48 import com.sun.tools.javac.util.Names;
  49 
  50 import java.io.IOException;
  51 import java.io.OutputStream;
  52 import java.util.ArrayDeque;
  53 import java.util.HashMap;
  54 import java.util.LinkedHashMap;
  55 import java.util.LinkedHashSet;
  56 import java.util.Map;


 103         this.signatureGen = new SharedSignatureGenerator(types);
 104         this.pool = new WriteablePoolHelper();
 105     }
 106 
 107     /**
 108      * Puts a class symbol into the pool and return its index.
 109      */
 110     int putClass(ClassSymbol csym) {
 111         return putClass(csym.type);
 112     }
 113 
 114     /**
 115      * Puts a type into the pool and return its index. The type could be either a class, a type variable
 116      * or an array type.
 117      */
 118     int putClass(Type t) {
 119         return pool.writeIfNeeded(types.erasure(t));
 120     }
 121 
 122     /**
 123      * Puts a type into the pool and return its index. The type could be either a class, a type variable
 124      * or an array type.
 125      */
 126     int putClass(ConstantPoolQType t) {
 127         return pool.writeIfNeeded(t);
 128     }
 129 
 130     /**
 131      * Puts a member reference into the constant pool. Valid members are either field or method symbols.
 132      */
 133     int putMember(Symbol s) {
 134         return pool.writeIfNeeded(s);
 135     }
 136 
 137     /**
 138      * Puts a dynamic reference into the constant pool and return its index.
 139      */
 140     int putDynamic(Dynamic d) {
 141         return pool.writeIfNeeded(d);
 142     }
 143 
 144     /**
 145      * Puts a field or method descriptor into the constant pool and return its index.
 146      */
 147     int putDescriptor(Type t) {
 148         return putName(typeSig(types.erasure(t)));
 149     }
 150 


 354             Object key = p.poolKey(types);
 355             Integer index = keysToPos.get(key);
 356             if (index == null) {
 357                 keysToPos.put(key, index = currentIndex++);
 358                 boolean first = todo.isEmpty();
 359                 todo.addLast(p);
 360                 if (first) {
 361                     while (!todo.isEmpty()) {
 362                         writeConstant(todo.peekFirst());
 363                         todo.removeFirst();
 364                     }
 365                 }
 366             }
 367             return index;
 368         }
 369 
 370         void writeConstant(PoolConstant c) {
 371             int tag = c.poolTag();
 372             switch (tag) {
 373                 case ClassFile.CONSTANT_Class: {
 374                     Type ct = c instanceof ConstantPoolQType ? ((ConstantPoolQType)c).type : (Type)c;
 375                     Name name = ct.hasTag(ARRAY) ?
 376                             typeSig(ct) :
 377                             c instanceof ConstantPoolQType ? names.fromString("Q" + new String(externalize(ct.tsym.flatName())) + ";") : names.fromUtf(externalize(ct.tsym.flatName()));
 378                     poolbuf.appendByte(tag);
 379                     poolbuf.appendChar(putName(name));
 380                     if (ct.hasTag(CLASS)) {
 381                         enterInner((ClassSymbol)ct.tsym);
 382                     }
 383                     break;
 384                 }
 385                 case ClassFile.CONSTANT_Utf8: {
 386                     Name name = (Name)c;
 387                     poolbuf.appendByte(tag);
 388                     byte[] bs = name.toUtf();
 389                     poolbuf.appendChar(bs.length);
 390                     poolbuf.appendBytes(bs, 0, bs.length);
 391                     if (overflowString == null && bs.length > MAX_STRING_LENGTH) {
 392                         //report error only once
 393                         overflowString = new String(bs);
 394                     }
 395                     break;
 396                 }
 397                 case ClassFile.CONSTANT_InterfaceMethodref:


< prev index next >