< 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;

 98 
 99     public PoolWriter(Types types, Names names) {
100         this.types = types;
101         this.names = names;
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     }

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:

485     }
486 
487     /**
488      * Return signature of given type
489      */
490     private Name typeSig(Type type) {
491         signatureGen.reset();
492         signatureGen.assembleSig(type);
493         return signatureGen.toName();
494     }
495 
496     private Name classSig(Type t) {
497         signatureGen.reset();
498         List<Type> typarams = t.getTypeArguments();
499         if (typarams.nonEmpty()) {
500             signatureGen.assembleParamsSig(typarams);
501         }
502         signatureGen.assembleSig(types.supertype(t));
503         for (Type i : types.interfaces(t))
504             signatureGen.assembleSig(i);

505         return signatureGen.toName();
506     }
507 
508     void reset() {
509         innerClasses.clear();
510         bootstrapMethods.clear();
511         pool.reset();
512     }
513 }

 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;

 99 
100     public PoolWriter(Types types, Names names) {
101         this.types = types;
102         this.names = names;
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         /* Their is nothing to be gained by having the pair of class types Foo.ref and Foo.val
120            result in two different CONSTANT_Class_info strucures in the pool. These are
121            indistinguishable at the class file level. Hence we coalesce them here.
122         */
123         if (t.tsym.isPrimitiveClass())
124             t = t.asValueType();
125         return pool.writeIfNeeded(types.erasure(t));
126     }
127 
128     /**
129      * Puts a type into the pool and return its index. The type could be either a class, a type variable
130      * or an array type.
131      */
132     int putClass(ConstantPoolQType t) {
133         return pool.writeIfNeeded(t);
134     }
135 
136     /**
137      * Puts a member reference into the constant pool. Valid members are either field or method symbols.
138      */
139     int putMember(Symbol s) {
140         return pool.writeIfNeeded(s);
141     }
142 
143     /**
144      * Puts a dynamic reference into the constant pool and return its index.
145      */
146     int putDynamic(Dynamic d) {
147         return pool.writeIfNeeded(d);
148     }
149 
150     /**
151      * Puts a field or method descriptor into the constant pool and return its index.
152      */
153     int putDescriptor(Type t) {
154         return putName(typeSig(types.erasure(t)));
155     }

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

500     }
501 
502     /**
503      * Return signature of given type
504      */
505     private Name typeSig(Type type) {
506         signatureGen.reset();
507         signatureGen.assembleSig(type);
508         return signatureGen.toName();
509     }
510 
511     private Name classSig(Type t) {
512         signatureGen.reset();
513         List<Type> typarams = t.getTypeArguments();
514         if (typarams.nonEmpty()) {
515             signatureGen.assembleParamsSig(typarams);
516         }
517         signatureGen.assembleSig(types.supertype(t));
518         for (Type i : types.interfaces(t))
519             signatureGen.assembleSig(i);
520 
521         return signatureGen.toName();
522     }
523 
524     void reset() {
525         innerClasses.clear();
526         bootstrapMethods.clear();
527         pool.reset();
528     }
529 }
< prev index next >