langtools/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
changeset 8032 e1aa25ccdabb
parent 8031 d5fe2c1cecfc
child 8630 cd9eefe597f6
equal deleted inserted replaced
8031:d5fe2c1cecfc 8032:e1aa25ccdabb
   217 
   217 
   218 
   218 
   219     /** Return flags as a string, separated by " ".
   219     /** Return flags as a string, separated by " ".
   220      */
   220      */
   221     public static String flagNames(long flags) {
   221     public static String flagNames(long flags) {
   222         StringBuffer sbuf = new StringBuffer();
   222         StringBuilder sbuf = new StringBuilder();
   223         int i = 0;
   223         int i = 0;
   224         long f = flags & StandardFlags;
   224         long f = flags & StandardFlags;
   225         while (f != 0) {
   225         while (f != 0) {
   226             if ((f & 1) != 0) sbuf.append(" " + flagName[i]);
   226             if ((f & 1) != 0) {
       
   227                 sbuf.append(" ");
       
   228                 sbuf.append(flagName[i]);
       
   229             }
   227             f = f >> 1;
   230             f = f >> 1;
   228             i++;
   231             i++;
   229         }
   232         }
   230         return sbuf.toString();
   233         return sbuf.toString();
   231     }
   234     }
   374                 c.name == names.empty; // or anonymous
   377                 c.name == names.empty; // or anonymous
   375             assembleClassSig(rawOuter
   378             assembleClassSig(rawOuter
   376                              ? types.erasure(outer)
   379                              ? types.erasure(outer)
   377                              : outer);
   380                              : outer);
   378             sigbuf.appendByte('.');
   381             sigbuf.appendByte('.');
   379             assert c.flatname.startsWith(c.owner.enclClass().flatname);
   382             Assert.check(c.flatname.startsWith(c.owner.enclClass().flatname));
   380             sigbuf.appendName(rawOuter
   383             sigbuf.appendName(rawOuter
   381                               ? c.flatname.subName(c.owner.enclClass().flatname.getByteLength()+1,c.flatname.getByteLength())
   384                               ? c.flatname.subName(c.owner.enclClass().flatname.getByteLength()+1,c.flatname.getByteLength())
   382                               : c.name);
   385                               : c.name);
   383         } else {
   386         } else {
   384             sigbuf.appendBytes(externalize(c.flatname));
   387             sigbuf.appendBytes(externalize(c.flatname));
   414     }
   417     }
   415 
   418 
   416     /** Return signature of given type
   419     /** Return signature of given type
   417      */
   420      */
   418     Name typeSig(Type type) {
   421     Name typeSig(Type type) {
   419         assert sigbuf.length == 0;
   422         Assert.check(sigbuf.length == 0);
   420         //- System.out.println(" ? " + type);
   423         //- System.out.println(" ? " + type);
   421         assembleSig(type);
   424         assembleSig(type);
   422         Name n = sigbuf.toName(names);
   425         Name n = sigbuf.toName(names);
   423         sigbuf.reset();
   426         sigbuf.reset();
   424         //- System.out.println("   " + n);
   427         //- System.out.println("   " + n);
   464         int poolCountIdx = poolbuf.length;
   467         int poolCountIdx = poolbuf.length;
   465         poolbuf.appendChar(0);
   468         poolbuf.appendChar(0);
   466         int i = 1;
   469         int i = 1;
   467         while (i < pool.pp) {
   470         while (i < pool.pp) {
   468             Object value = pool.pool[i];
   471             Object value = pool.pool[i];
   469             assert value != null;
   472             Assert.checkNonNull(value);
   470             if (value instanceof Pool.Method)
   473             if (value instanceof Pool.Method)
   471                 value = ((Pool.Method)value).m;
   474                 value = ((Pool.Method)value).m;
   472             else if (value instanceof Pool.Variable)
   475             else if (value instanceof Pool.Variable)
   473                 value = ((Pool.Variable)value).v;
   476                 value = ((Pool.Variable)value).v;
   474 
   477 
   527                 Type type = (Type)value;
   530                 Type type = (Type)value;
   528                 if (type.tag == CLASS) enterInner((ClassSymbol)type.tsym);
   531                 if (type.tag == CLASS) enterInner((ClassSymbol)type.tsym);
   529                 poolbuf.appendByte(CONSTANT_Class);
   532                 poolbuf.appendByte(CONSTANT_Class);
   530                 poolbuf.appendChar(pool.put(xClassName(type)));
   533                 poolbuf.appendChar(pool.put(xClassName(type)));
   531             } else {
   534             } else {
   532                 assert false : "writePool " + value;
   535                 Assert.error("writePool " + value);
   533             }
   536             }
   534             i++;
   537             i++;
   535         }
   538         }
   536         if (pool.pp > Pool.MAX_ENTRIES)
   539         if (pool.pp > Pool.MAX_ENTRIES)
   537             throw new PoolOverflow();
   540             throw new PoolOverflow();
   796                 break;
   799                 break;
   797             case BOOLEAN:
   800             case BOOLEAN:
   798                 databuf.appendByte('Z');
   801                 databuf.appendByte('Z');
   799                 break;
   802                 break;
   800             case CLASS:
   803             case CLASS:
   801                 assert value instanceof String;
   804                 Assert.check(value instanceof String);
   802                 databuf.appendByte('s');
   805                 databuf.appendByte('s');
   803                 value = names.fromString(value.toString()); // CONSTANT_Utf8
   806                 value = names.fromString(value.toString()); // CONSTANT_Utf8
   804                 break;
   807                 break;
   805             default:
   808             default:
   806                 throw new AssertionError(_value.type);
   809                 throw new AssertionError(_value.type);
  1013 
  1016 
  1014             for (int i=0; i<code.varBufferSize; i++) {
  1017             for (int i=0; i<code.varBufferSize; i++) {
  1015                 Code.LocalVar var = code.varBuffer[i];
  1018                 Code.LocalVar var = code.varBuffer[i];
  1016 
  1019 
  1017                 // write variable info
  1020                 // write variable info
  1018                 assert var.start_pc >= 0;
  1021                 Assert.check(var.start_pc >= 0
  1019                 assert var.start_pc <= code.cp;
  1022                         && var.start_pc <= code.cp);
  1020                 databuf.appendChar(var.start_pc);
  1023                 databuf.appendChar(var.start_pc);
  1021                 assert var.length >= 0;
  1024                 Assert.check(var.length >= 0
  1022                 assert (var.start_pc + var.length) <= code.cp;
  1025                         && (var.start_pc + var.length) <= code.cp);
  1023                 databuf.appendChar(var.length);
  1026                 databuf.appendChar(var.length);
  1024                 VarSymbol sym = var.sym;
  1027                 VarSymbol sym = var.sym;
  1025                 databuf.appendChar(pool.put(sym.name));
  1028                 databuf.appendChar(pool.put(sym.name));
  1026                 Type vartype = sym.erasure(types);
  1029                 Type vartype = sym.erasure(types);
  1027                 if (needsLocalVariableTypeEntry(sym.type))
  1030                 if (needsLocalVariableTypeEntry(sym.type))
  1049                 databuf.appendChar(var.length);
  1052                 databuf.appendChar(var.length);
  1050                 databuf.appendChar(pool.put(sym.name));
  1053                 databuf.appendChar(pool.put(sym.name));
  1051                 databuf.appendChar(pool.put(typeSig(sym.type)));
  1054                 databuf.appendChar(pool.put(typeSig(sym.type)));
  1052                 databuf.appendChar(var.reg);
  1055                 databuf.appendChar(var.reg);
  1053             }
  1056             }
  1054             assert count == nGenericVars;
  1057             Assert.check(count == nGenericVars);
  1055             endAttr(alenIdx);
  1058             endAttr(alenIdx);
  1056             acount++;
  1059             acount++;
  1057         }
  1060         }
  1058 
  1061 
  1059         if (code.stackMapBufferSize > 0) {
  1062         if (code.stackMapBufferSize > 0) {
  1120                 }
  1123                 }
  1121                 if (debugstackmap) System.out.println();
  1124                 if (debugstackmap) System.out.println();
  1122             }
  1125             }
  1123             break;
  1126             break;
  1124         case JSR202: {
  1127         case JSR202: {
  1125             assert code.stackMapBuffer == null;
  1128             Assert.checkNull(code.stackMapBuffer);
  1126             for (int i=0; i<nframes; i++) {
  1129             for (int i=0; i<nframes; i++) {
  1127                 if (debugstackmap) System.out.print("  " + i + ":");
  1130                 if (debugstackmap) System.out.print("  " + i + ":");
  1128                 StackMapTableFrame frame = code.stackMapTableBuffer[i];
  1131                 StackMapTableFrame frame = code.stackMapTableBuffer[i];
  1129                 frame.write(this);
  1132                 frame.write(this);
  1130                 if (debugstackmap) System.out.println();
  1133                 if (debugstackmap) System.out.println();
  1460 
  1463 
  1461     /** Write class `c' to outstream `out'.
  1464     /** Write class `c' to outstream `out'.
  1462      */
  1465      */
  1463     public void writeClassFile(OutputStream out, ClassSymbol c)
  1466     public void writeClassFile(OutputStream out, ClassSymbol c)
  1464         throws IOException, PoolOverflow, StringOverflow {
  1467         throws IOException, PoolOverflow, StringOverflow {
  1465         assert (c.flags() & COMPOUND) == 0;
  1468         Assert.check((c.flags() & COMPOUND) == 0);
  1466         databuf.reset();
  1469         databuf.reset();
  1467         poolbuf.reset();
  1470         poolbuf.reset();
  1468         sigbuf.reset();
  1471         sigbuf.reset();
  1469         pool = c.pool;
  1472         pool = c.pool;
  1470         innerClasses = null;
  1473         innerClasses = null;
  1497             switch (e.sym.kind) {
  1500             switch (e.sym.kind) {
  1498             case VAR: fieldsCount++; break;
  1501             case VAR: fieldsCount++; break;
  1499             case MTH: if ((e.sym.flags() & HYPOTHETICAL) == 0) methodsCount++;
  1502             case MTH: if ((e.sym.flags() & HYPOTHETICAL) == 0) methodsCount++;
  1500                       break;
  1503                       break;
  1501             case TYP: enterInner((ClassSymbol)e.sym); break;
  1504             case TYP: enterInner((ClassSymbol)e.sym); break;
  1502             default : assert false;
  1505             default : Assert.error();
  1503             }
  1506             }
  1504         }
  1507         }
  1505         databuf.appendChar(fieldsCount);
  1508         databuf.appendChar(fieldsCount);
  1506         writeFields(c.members().elems);
  1509         writeFields(c.members().elems);
  1507         databuf.appendChar(methodsCount);
  1510         databuf.appendChar(methodsCount);
  1513         boolean sigReq =
  1516         boolean sigReq =
  1514             typarams.length() != 0 || supertype.allparams().length() != 0;
  1517             typarams.length() != 0 || supertype.allparams().length() != 0;
  1515         for (List<Type> l = interfaces; !sigReq && l.nonEmpty(); l = l.tail)
  1518         for (List<Type> l = interfaces; !sigReq && l.nonEmpty(); l = l.tail)
  1516             sigReq = l.head.allparams().length() != 0;
  1519             sigReq = l.head.allparams().length() != 0;
  1517         if (sigReq) {
  1520         if (sigReq) {
  1518             assert source.allowGenerics();
  1521             Assert.check(source.allowGenerics());
  1519             int alenIdx = writeAttr(names.Signature);
  1522             int alenIdx = writeAttr(names.Signature);
  1520             if (typarams.length() != 0) assembleParamsSig(typarams);
  1523             if (typarams.length() != 0) assembleParamsSig(typarams);
  1521             assembleSig(supertype);
  1524             assembleSig(supertype);
  1522             for (List<Type> l = interfaces; l.nonEmpty(); l = l.tail)
  1525             for (List<Type> l = interfaces; l.nonEmpty(); l = l.tail)
  1523                 assembleSig(l.head);
  1526                 assembleSig(l.head);