< prev index next >

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

Print this page

   1 /*
   2  * Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  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

 129     /** An output buffer for the constant pool.
 130      */
 131     ByteBuffer poolbuf = new ByteBuffer(CLASS_BUF_SIZE);
 132 
 133     /** The constant pool writer.
 134      */
 135     final PoolWriter poolWriter;
 136 
 137     /** The log to use for verbose output.
 138      */
 139     private final Log log;
 140 
 141     /** The name table. */
 142     private final Names names;
 143 
 144     /** Access to files. */
 145     private final JavaFileManager fileManager;
 146 
 147     /** The tags and constants used in compressed stackmap. */
 148     static final int SAME_FRAME_SIZE = 64;

 149     static final int SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247;
 150     static final int SAME_FRAME_EXTENDED = 251;
 151     static final int FULL_FRAME = 255;
 152     static final int MAX_LOCAL_LENGTH_DIFF = 4;
 153 
 154     /** Get the ClassWriter instance for this context. */
 155     public static ClassWriter instance(Context context) {
 156         ClassWriter instance = context.get(classWriterKey);
 157         if (instance == null)
 158             instance = new ClassWriter(context);
 159         return instance;
 160     }
 161 
 162     /** Construct a class writer, given an options table.
 163      */
 164     @SuppressWarnings("this-escape")
 165     protected ClassWriter(Context context) {
 166         context.put(classWriterKey, this);
 167 
 168         log = Log.instance(context);

 205      *    'c' -- classes
 206      *    'f' -- fields
 207      *    'i' -- innerclass attributes
 208      *    'm' -- methods
 209      *  For example, to dump everything:
 210      *    javac -XDdumpmodifiers=cifm MyProg.java
 211      */
 212     private boolean dumpClassModifiers; // -XDdumpmodifiers=c
 213     private boolean dumpFieldModifiers; // -XDdumpmodifiers=f
 214     private boolean dumpInnerClassModifiers; // -XDdumpmodifiers=i
 215     private boolean dumpMethodModifiers; // -XDdumpmodifiers=m
 216 
 217 
 218     /** Return flags as a string, separated by " ".
 219      */
 220     public static String flagNames(long flags) {
 221         StringBuilder sbuf = new StringBuilder();
 222         int i = 0;
 223         long f = flags & StandardFlags;
 224         while (f != 0) {
 225             if ((f & 1) != 0) {
 226                 sbuf.append(" ");
 227                 sbuf.append(flagName[i]);
 228             }
 229             f = f >> 1;
 230             i++;
 231         }
 232         return sbuf.toString();
 233     }
 234     //where
 235         private static final String[] flagName = {
 236             "PUBLIC", "PRIVATE", "PROTECTED", "STATIC", "FINAL",
 237             "SUPER", "VOLATILE", "TRANSIENT", "NATIVE", "INTERFACE",

 238             "ABSTRACT", "STRICTFP"};
 239 
 240 /* ****************************************************************
 241  * Output routines
 242  ******************************************************************/
 243 
 244     /** Write a character into given byte buffer;
 245      *  byte buffer will not be grown.
 246      */
 247     void putChar(ByteBuffer buf, int op, int x) {
 248         buf.elems[op  ] = (byte)((x >>  8) & 0xFF);
 249         buf.elems[op+1] = (byte)((x      ) & 0xFF);
 250     }
 251 
 252     /** Write an integer into given byte buffer;
 253      *  byte buffer will not be grown.
 254      */
 255     void putInt(ByteBuffer buf, int adr, int x) {
 256         buf.elems[adr  ] = (byte)((x >> 24) & 0xFF);
 257         buf.elems[adr+1] = (byte)((x >> 16) & 0xFF);

 825             databuf.appendChar(poolWriter.putClass(srvc));
 826             databuf.appendChar(impls.size());
 827             impls.forEach(impl -> databuf.appendChar(poolWriter.putClass(impl)));
 828         });
 829 
 830         endAttr(alenIdx);
 831         return 1;
 832     }
 833 
 834 /* ********************************************************************
 835  * Writing Objects
 836  **********************************************************************/
 837 
 838     /** Write "inner classes" attribute.
 839      */
 840     void writeInnerClasses() {
 841         int alenIdx = writeAttr(names.InnerClasses);
 842         databuf.appendChar(poolWriter.innerClasses.size());
 843         for (ClassSymbol inner : poolWriter.innerClasses) {
 844             inner.markAbstractIfNeeded(types);
 845             int flags = adjustFlags(inner.flags_field);
 846             if ((flags & INTERFACE) != 0) flags |= ABSTRACT; // Interfaces are always ABSTRACT
 847             flags &= ~STRICTFP; //inner classes should not have the strictfp flag set.
 848             if (dumpInnerClassModifiers) {
 849                 PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
 850                 pw.println("INNERCLASS  " + inner.name);
 851                 pw.println("---" + flagNames(flags));
 852             }
 853             databuf.appendChar(poolWriter.putClass(inner));
 854             databuf.appendChar(
 855                 inner.owner.kind == TYP && !inner.name.isEmpty() ? poolWriter.putClass((ClassSymbol)inner.owner) : 0);
 856             databuf.appendChar(
 857                 !inner.name.isEmpty() ? poolWriter.putName(inner.name) : 0);
 858             databuf.appendChar(flags);
 859         }
 860         endAttr(alenIdx);
 861     }
 862 











 863     int writeRecordAttribute(ClassSymbol csym) {
 864         int alenIdx = writeAttr(names.Record);
 865         Scope s = csym.members();
 866         databuf.appendChar(csym.getRecordComponents().size());
 867         for (VarSymbol v: csym.getRecordComponents()) {
 868             //databuf.appendChar(poolWriter.putMember(v.accessor.head.snd));
 869             databuf.appendChar(poolWriter.putName(v.name));
 870             databuf.appendChar(poolWriter.putDescriptor(v));
 871             int acountIdx = beginAttrs();
 872             int acount = 0;
 873             acount += writeMemberAttrs(v, true);
 874             endAttrs(acountIdx, acount);
 875         }
 876         endAttr(alenIdx);
 877         return 1;
 878     }
 879 
 880     /**
 881      * Write NestMembers attribute (if needed)
 882      */

 956             }
 957         } while (lastBootstrapMethods < poolWriter.bootstrapMethods.size());
 958         databuf.appendChar(poolWriter.bootstrapMethods.size());
 959         for (BsmKey bsmKey : poolWriter.bootstrapMethods.keySet()) {
 960             //write BSM handle
 961             databuf.appendChar(poolWriter.putConstant(bsmKey.bsm));
 962             LoadableConstant[] uniqueArgs = bsmKey.staticArgs;
 963             //write static args length
 964             databuf.appendChar(uniqueArgs.length);
 965             //write static args array
 966             for (LoadableConstant arg : uniqueArgs) {
 967                 databuf.appendChar(poolWriter.putConstant(arg));
 968             }
 969         }
 970         endAttr(alenIdx);
 971     }
 972 
 973     /** Write field symbol, entering all references into constant pool.
 974      */
 975     void writeField(VarSymbol v) {
 976         int flags = adjustFlags(v.flags());
 977         databuf.appendChar(flags);
 978         if (dumpFieldModifiers) {
 979             PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
 980             pw.println("FIELD  " + v.name);
 981             pw.println("---" + flagNames(v.flags()));
 982         }
 983         databuf.appendChar(poolWriter.putName(v.name));
 984         databuf.appendChar(poolWriter.putDescriptor(v));







 985         int acountIdx = beginAttrs();
 986         int acount = 0;
 987         if (v.getConstValue() != null) {
 988             int alenIdx = writeAttr(names.ConstantValue);
 989             databuf.appendChar(poolWriter.putConstant(v.getConstValue()));
 990             endAttr(alenIdx);
 991             acount++;
 992         }
 993         acount += writeMemberAttrs(v, false);
 994         acount += writeExtraAttributes(v);
 995         endAttrs(acountIdx, acount);
 996     }
 997 
 998     /** Write method symbol, entering all references into constant pool.
 999      */
1000     void writeMethod(MethodSymbol m) {
1001         int flags = adjustFlags(m.flags());
1002         databuf.appendChar(flags);
1003         if (dumpMethodModifiers) {
1004             PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
1005             pw.println("METHOD  " + m.name);
1006             pw.println("---" + flagNames(m.flags()));
1007         }
1008         databuf.appendChar(poolWriter.putName(m.name));
1009         databuf.appendChar(poolWriter.putDescriptor(m));
















1010         int acountIdx = beginAttrs();
1011         int acount = 0;
1012         if (m.code != null) {
1013             int alenIdx = writeAttr(names.Code);
1014             writeCode(m.code);
1015             m.code = null; // to conserve space
1016             endAttr(alenIdx);
1017             acount++;
1018         }
1019         List<Type> thrown = m.erasure(types).getThrownTypes();
1020         if (thrown.nonEmpty()) {
1021             int alenIdx = writeAttr(names.Exceptions);
1022             databuf.appendChar(thrown.length());
1023             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
1024                 databuf.appendChar(poolWriter.putClass(l.head));
1025             endAttr(alenIdx);
1026             acount++;
1027         }
1028         if (m.defaultValue != null) {
1029             int alenIdx = writeAttr(names.AnnotationDefault);

1212                 int stackCount = 0;
1213                 for (int j=0; j<frame.stack.length;
1214                      j += Code.width(frame.stack[j])) {
1215                     stackCount++;
1216                 }
1217                 if (debugstackmap) System.out.print(" nstack=" +
1218                                                     stackCount);
1219                 databuf.appendChar(stackCount);
1220                 for (int j=0; j<frame.stack.length;
1221                      j += Code.width(frame.stack[j])) {
1222                     if (debugstackmap) System.out.print(" stack[" + j + "]=");
1223                     writeStackMapType(frame.stack[j]);
1224                 }
1225                 if (debugstackmap) System.out.println();
1226             }
1227             break;
1228         case JSR202: {
1229             Assert.checkNull(code.stackMapBuffer);
1230             for (int i=0; i<nframes; i++) {
1231                 if (debugstackmap) System.out.print("  " + i + ":");
1232                 StackMapTableFrame frame = code.stackMapTableBuffer[i];
1233                 frame.write(this);
1234                 if (debugstackmap) System.out.println();
1235             }
1236             break;
1237         }
1238         default:
1239             throw new AssertionError("Unexpected stackmap format value");
1240         }
1241     }
1242 
1243         //where
1244         void writeStackMapType(Type t) {
1245             if (t == null) {
1246                 if (debugstackmap) System.out.print("empty");
1247                 databuf.appendByte(0);
1248             }
1249             else switch(t.getTag()) {
1250             case BYTE:
1251             case CHAR:
1252             case SHORT:

1278                 databuf.appendByte(7);
1279                 databuf.appendChar(poolWriter.putClass(types.erasure(t)));
1280                 break;
1281             case UNINITIALIZED_THIS:
1282                 if (debugstackmap) System.out.print("uninit_this");
1283                 databuf.appendByte(6);
1284                 break;
1285             case UNINITIALIZED_OBJECT:
1286                 { UninitializedType uninitType = (UninitializedType)t;
1287                 databuf.appendByte(8);
1288                 if (debugstackmap) System.out.print("uninit_object@" + uninitType.offset);
1289                 databuf.appendChar(uninitType.offset);
1290                 }
1291                 break;
1292             default:
1293                 throw new AssertionError();
1294             }
1295         }
1296 
1297     /** An entry in the JSR202 StackMapTable */
1298     abstract static class StackMapTableFrame {
1299         abstract int getFrameType();





1300 
1301         void write(ClassWriter writer) {
1302             int frameType = getFrameType();
1303             writer.databuf.appendByte(frameType);
1304             if (writer.debugstackmap) System.out.print(" frame_type=" + frameType);
1305         }
1306 
1307         static class SameFrame extends StackMapTableFrame {
1308             final int offsetDelta;
1309             SameFrame(int offsetDelta) {

1310                 this.offsetDelta = offsetDelta;
1311             }
1312             int getFrameType() {
1313                 return (offsetDelta < SAME_FRAME_SIZE) ? offsetDelta : SAME_FRAME_EXTENDED;
1314             }
1315             @Override
1316             void write(ClassWriter writer) {
1317                 super.write(writer);
1318                 if (getFrameType() == SAME_FRAME_EXTENDED) {
1319                     writer.databuf.appendChar(offsetDelta);
1320                     if (writer.debugstackmap){
1321                         System.out.print(" offset_delta=" + offsetDelta);
1322                     }
1323                 }
1324             }
1325         }
1326 
1327         static class SameLocals1StackItemFrame extends StackMapTableFrame {
1328             final int offsetDelta;
1329             final Type stack;
1330             SameLocals1StackItemFrame(int offsetDelta, Type stack) {

1331                 this.offsetDelta = offsetDelta;
1332                 this.stack = stack;
1333             }
1334             int getFrameType() {
1335                 return (offsetDelta < SAME_FRAME_SIZE) ?
1336                        (SAME_FRAME_SIZE + offsetDelta) :
1337                        SAME_LOCALS_1_STACK_ITEM_EXTENDED;
1338             }
1339             @Override
1340             void write(ClassWriter writer) {
1341                 super.write(writer);
1342                 if (getFrameType() == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
1343                     writer.databuf.appendChar(offsetDelta);
1344                     if (writer.debugstackmap) {
1345                         System.out.print(" offset_delta=" + offsetDelta);
1346                     }
1347                 }
1348                 if (writer.debugstackmap) {
1349                     System.out.print(" stack[" + 0 + "]=");
1350                 }
1351                 writer.writeStackMapType(stack);
1352             }
1353         }
1354 
1355         static class ChopFrame extends StackMapTableFrame {
1356             final int frameType;
1357             final int offsetDelta;
1358             ChopFrame(int frameType, int offsetDelta) {

1359                 this.frameType = frameType;
1360                 this.offsetDelta = offsetDelta;
1361             }
1362             int getFrameType() { return frameType; }
1363             @Override
1364             void write(ClassWriter writer) {
1365                 super.write(writer);
1366                 writer.databuf.appendChar(offsetDelta);
1367                 if (writer.debugstackmap) {
1368                     System.out.print(" offset_delta=" + offsetDelta);
1369                 }
1370             }
1371         }
1372 
1373         static class AppendFrame extends StackMapTableFrame {
1374             final int frameType;
1375             final int offsetDelta;
1376             final Type[] locals;
1377             AppendFrame(int frameType, int offsetDelta, Type[] locals) {

1378                 this.frameType = frameType;
1379                 this.offsetDelta = offsetDelta;
1380                 this.locals = locals;
1381             }
1382             int getFrameType() { return frameType; }
1383             @Override
1384             void write(ClassWriter writer) {
1385                 super.write(writer);
1386                 writer.databuf.appendChar(offsetDelta);
1387                 if (writer.debugstackmap) {
1388                     System.out.print(" offset_delta=" + offsetDelta);
1389                 }
1390                 for (int i=0; i<locals.length; i++) {
1391                      if (writer.debugstackmap) System.out.print(" locals[" + i + "]=");
1392                      writer.writeStackMapType(locals[i]);
1393                 }
1394             }
1395         }
1396 
1397         static class FullFrame extends StackMapTableFrame {
1398             final int offsetDelta;
1399             final Type[] locals;
1400             final Type[] stack;
1401             FullFrame(int offsetDelta, Type[] locals, Type[] stack) {

1402                 this.offsetDelta = offsetDelta;
1403                 this.locals = locals;
1404                 this.stack = stack;
1405             }
1406             int getFrameType() { return FULL_FRAME; }
1407             @Override
1408             void write(ClassWriter writer) {
1409                 super.write(writer);
1410                 writer.databuf.appendChar(offsetDelta);
1411                 writer.databuf.appendChar(locals.length);
1412                 if (writer.debugstackmap) {
1413                     System.out.print(" offset_delta=" + offsetDelta);
1414                     System.out.print(" nlocals=" + locals.length);
1415                 }
1416                 for (int i=0; i<locals.length; i++) {
1417                     if (writer.debugstackmap) System.out.print(" locals[" + i + "]=");
1418                     writer.writeStackMapType(locals[i]);
1419                 }
1420 
1421                 writer.databuf.appendChar(stack.length);
1422                 if (writer.debugstackmap) { System.out.print(" nstack=" + stack.length); }
1423                 for (int i=0; i<stack.length; i++) {
1424                     if (writer.debugstackmap) System.out.print(" stack[" + i + "]=");
1425                     writer.writeStackMapType(stack[i]);
1426                 }
1427             }
1428         }
1429 



























1430        /** Compare this frame with the previous frame and produce
1431         *  an entry of compressed stack map frame. */
1432         static StackMapTableFrame getInstance(Code.StackMapFrame this_frame,
1433                                               int prev_pc,
1434                                               Type[] prev_locals,
1435                                               Types types) {
1436             Type[] locals = this_frame.locals;
1437             Type[] stack = this_frame.stack;
1438             int offset_delta = this_frame.pc - prev_pc - 1;
1439             if (stack.length == 1) {
1440                 if (locals.length == prev_locals.length
1441                     && compare(prev_locals, locals, types) == 0) {
1442                     return new SameLocals1StackItemFrame(offset_delta, stack[0]);
1443                 }
1444             } else if (stack.length == 0) {
1445                 int diff_length = compare(prev_locals, locals, types);
1446                 if (diff_length == 0) {
1447                     return new SameFrame(offset_delta);
1448                 } else if (-MAX_LOCAL_LENGTH_DIFF < diff_length && diff_length < 0) {
1449                     // APPEND
1450                     Type[] local_diff = new Type[-diff_length];
1451                     for (int i=prev_locals.length, j=0; i<locals.length; i++,j++) {
1452                         local_diff[j] = locals[i];
1453                     }
1454                     return new AppendFrame(SAME_FRAME_EXTENDED - diff_length,
1455                                            offset_delta,
1456                                            local_diff);
1457                 } else if (0 < diff_length && diff_length < MAX_LOCAL_LENGTH_DIFF) {
1458                     // CHOP
1459                     return new ChopFrame(SAME_FRAME_EXTENDED - diff_length,
1460                                          offset_delta);
1461                 }
1462             }
1463             // FULL_FRAME
1464             return new FullFrame(offset_delta, locals, stack);
1465         }
1466 
1467         static boolean isInt(Type t) {
1468             return (t.getTag().isStrictSubRangeOf(INT)  || t.hasTag(BOOLEAN));
1469         }
1470 
1471         static boolean isSameType(Type t1, Type t2, Types types) {
1472             if (t1 == null) { return t2 == null; }
1473             if (t2 == null) { return false; }
1474 
1475             if (isInt(t1) && isInt(t2)) { return true; }
1476 
1477             if (t1.hasTag(UNINITIALIZED_THIS)) {
1478                 return t2.hasTag(UNINITIALIZED_THIS);
1479             } else if (t1.hasTag(UNINITIALIZED_OBJECT)) {
1480                 if (t2.hasTag(UNINITIALIZED_OBJECT)) {
1481                     return ((UninitializedType)t1).offset == ((UninitializedType)t2).offset;
1482                 } else {
1483                     return false;
1484                 }

1567         }
1568         return outFile; // may be null if write failed
1569     }
1570 
1571     /** Write class `c' to outstream `out'.
1572      */
1573     public void writeClassFile(OutputStream out, ClassSymbol c)
1574         throws IOException, PoolOverflow, StringOverflow {
1575         Assert.check((c.flags() & COMPOUND) == 0);
1576         databuf.reset();
1577         poolbuf.reset();
1578 
1579         Type supertype = types.supertype(c.type);
1580         List<Type> interfaces = types.interfaces(c.type);
1581         List<Type> typarams = c.type.getTypeArguments();
1582 
1583         int flags;
1584         if (c.owner.kind == MDL) {
1585             flags = ACC_MODULE;
1586         } else {
1587             flags = adjustFlags(c.flags() & ~DEFAULT);

1588             if ((flags & PROTECTED) != 0) flags |= PUBLIC;
1589             flags = flags & ClassFlags & ~STRICTFP;
1590             if ((flags & INTERFACE) == 0) flags |= ACC_SUPER;
1591         }
1592 
1593         if (dumpClassModifiers) {
1594             PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
1595             pw.println();
1596             pw.println("CLASSFILE  " + c.getQualifiedName());
1597             pw.println("---" + flagNames(flags));
1598         }
1599         databuf.appendChar(flags);
1600 
1601         if (c.owner.kind == MDL) {
1602             PackageSymbol unnamed = ((ModuleSymbol) c.owner).unnamedPackage;
1603             databuf.appendChar(poolWriter.putClass(new ClassSymbol(0, names.module_info, unnamed)));
1604         } else {
1605             databuf.appendChar(poolWriter.putClass(c));
1606         }
1607         databuf.appendChar(supertype.hasTag(CLASS) ? poolWriter.putClass((ClassSymbol)supertype.tsym) : 0);
1608         databuf.appendChar(interfaces.length());
1609         for (List<Type> l = interfaces; l.nonEmpty(); l = l.tail)
1610             databuf.appendChar(poolWriter.putClass((ClassSymbol)l.head.tsym));

1696         }
1697 
1698         if (c.isRecord()) {
1699             acount += writeRecordAttribute(c);
1700         }
1701 
1702         if (target.hasSealedClasses()) {
1703             acount += writePermittedSubclassesIfNeeded(c);
1704         }
1705 
1706         if (!poolWriter.bootstrapMethods.isEmpty()) {
1707             writeBootstrapMethods();
1708             acount++;
1709         }
1710 
1711         if (!poolWriter.innerClasses.isEmpty()) {
1712             writeInnerClasses();
1713             acount++;
1714         }
1715 





1716         endAttrs(acountIdx, acount);
1717 
1718         out.write(poolbuf.elems, 0, poolbuf.length);
1719 
1720         poolWriter.writePool(out);
1721         poolWriter.reset(); // to save space
1722 
1723         out.write(databuf.elems, 0, databuf.length);
1724     }
1725 
1726      /**Allows subclasses to write additional class attributes
1727       *
1728       * @return the number of attributes written
1729       */
1730     protected int writeExtraClassAttributes(ClassSymbol c) {
1731         return 0;
1732     }
1733 
1734     /**Allows friends to write additional attributes
1735      *
1736      * @return the number of attributes written
1737      */
1738     protected int writeExtraAttributes(Symbol sym) {
1739         int i = 0;
1740         for (ToIntFunction<Symbol> hook : extraAttributeHooks) {
1741             i += hook.applyAsInt(sym);
1742         }
1743         return i;
1744     }
1745 
1746     int adjustFlags(final long flags) {
1747         int result = (int)flags;
1748 
1749         // Elide strictfp bit in class files
1750         if (target.obsoleteAccStrict())
1751             result &= ~STRICTFP;
1752 
1753         if ((flags & BRIDGE) != 0)
1754             result |= ACC_BRIDGE;
1755         if ((flags & VARARGS) != 0)
1756             result |= ACC_VARARGS;
1757         if ((flags & DEFAULT) != 0)
1758             result &= ~ABSTRACT;








1759         return result;
1760     }
1761 
1762     long getLastModified(FileObject filename) {
1763         return filename.getLastModified();
1764     }
1765 }

   1 /*
   2  * Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  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

 129     /** An output buffer for the constant pool.
 130      */
 131     ByteBuffer poolbuf = new ByteBuffer(CLASS_BUF_SIZE);
 132 
 133     /** The constant pool writer.
 134      */
 135     final PoolWriter poolWriter;
 136 
 137     /** The log to use for verbose output.
 138      */
 139     private final Log log;
 140 
 141     /** The name table. */
 142     private final Names names;
 143 
 144     /** Access to files. */
 145     private final JavaFileManager fileManager;
 146 
 147     /** The tags and constants used in compressed stackmap. */
 148     static final int SAME_FRAME_SIZE = 64;
 149     static final int ASSERT_UNSET_FIELDS = 246;
 150     static final int SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247;
 151     static final int SAME_FRAME_EXTENDED = 251;
 152     static final int FULL_FRAME = 255;
 153     static final int MAX_LOCAL_LENGTH_DIFF = 4;
 154 
 155     /** Get the ClassWriter instance for this context. */
 156     public static ClassWriter instance(Context context) {
 157         ClassWriter instance = context.get(classWriterKey);
 158         if (instance == null)
 159             instance = new ClassWriter(context);
 160         return instance;
 161     }
 162 
 163     /** Construct a class writer, given an options table.
 164      */
 165     @SuppressWarnings("this-escape")
 166     protected ClassWriter(Context context) {
 167         context.put(classWriterKey, this);
 168 
 169         log = Log.instance(context);

 206      *    'c' -- classes
 207      *    'f' -- fields
 208      *    'i' -- innerclass attributes
 209      *    'm' -- methods
 210      *  For example, to dump everything:
 211      *    javac -XDdumpmodifiers=cifm MyProg.java
 212      */
 213     private boolean dumpClassModifiers; // -XDdumpmodifiers=c
 214     private boolean dumpFieldModifiers; // -XDdumpmodifiers=f
 215     private boolean dumpInnerClassModifiers; // -XDdumpmodifiers=i
 216     private boolean dumpMethodModifiers; // -XDdumpmodifiers=m
 217 
 218 
 219     /** Return flags as a string, separated by " ".
 220      */
 221     public static String flagNames(long flags) {
 222         StringBuilder sbuf = new StringBuilder();
 223         int i = 0;
 224         long f = flags & StandardFlags;
 225         while (f != 0) {
 226             if ((f & 1) != 0 && flagName[i] != "") {
 227                 sbuf.append(" ");
 228                 sbuf.append(flagName[i]);
 229             }
 230             f = f >> 1;
 231             i++;
 232         }
 233         return sbuf.toString();
 234     }
 235     //where
 236         private static final String[] flagName = {
 237             "PUBLIC", "PRIVATE", "PROTECTED", "STATIC", "FINAL",
 238             // the empty position should be for synchronized but right now we don't have any test checking it
 239             "", "VOLATILE", "TRANSIENT", "NATIVE", "INTERFACE",
 240             "ABSTRACT", "STRICTFP"};
 241 
 242 /* ****************************************************************
 243  * Output routines
 244  ******************************************************************/
 245 
 246     /** Write a character into given byte buffer;
 247      *  byte buffer will not be grown.
 248      */
 249     void putChar(ByteBuffer buf, int op, int x) {
 250         buf.elems[op  ] = (byte)((x >>  8) & 0xFF);
 251         buf.elems[op+1] = (byte)((x      ) & 0xFF);
 252     }
 253 
 254     /** Write an integer into given byte buffer;
 255      *  byte buffer will not be grown.
 256      */
 257     void putInt(ByteBuffer buf, int adr, int x) {
 258         buf.elems[adr  ] = (byte)((x >> 24) & 0xFF);
 259         buf.elems[adr+1] = (byte)((x >> 16) & 0xFF);

 827             databuf.appendChar(poolWriter.putClass(srvc));
 828             databuf.appendChar(impls.size());
 829             impls.forEach(impl -> databuf.appendChar(poolWriter.putClass(impl)));
 830         });
 831 
 832         endAttr(alenIdx);
 833         return 1;
 834     }
 835 
 836 /* ********************************************************************
 837  * Writing Objects
 838  **********************************************************************/
 839 
 840     /** Write "inner classes" attribute.
 841      */
 842     void writeInnerClasses() {
 843         int alenIdx = writeAttr(names.InnerClasses);
 844         databuf.appendChar(poolWriter.innerClasses.size());
 845         for (ClassSymbol inner : poolWriter.innerClasses) {
 846             inner.markAbstractIfNeeded(types);
 847             int flags = adjustFlags(inner, inner.flags_field);
 848             if ((flags & INTERFACE) != 0) flags |= ABSTRACT; // Interfaces are always ABSTRACT

 849             if (dumpInnerClassModifiers) {
 850                 PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
 851                 pw.println("INNERCLASS  " + inner.name);
 852                 pw.println("---" + flagNames(flags));
 853             }
 854             databuf.appendChar(poolWriter.putClass(inner));
 855             databuf.appendChar(
 856                 inner.owner.kind == TYP && !inner.name.isEmpty() ? poolWriter.putClass((ClassSymbol)inner.owner) : 0);
 857             databuf.appendChar(
 858                 !inner.name.isEmpty() ? poolWriter.putName(inner.name) : 0);
 859             databuf.appendChar(flags);
 860         }
 861         endAttr(alenIdx);
 862     }
 863 
 864      /** Write out "LoadableDescriptors" attribute by enumerating the value classes encountered in field/method descriptors during this compilation.
 865       */
 866      void writeLoadableDescriptorsAttribute() {
 867         int alenIdx = writeAttr(names.LoadableDescriptors);
 868         databuf.appendChar(poolWriter.loadableDescriptors.size());
 869         for (Symbol c : poolWriter.loadableDescriptors) {
 870             databuf.appendChar(poolWriter.putDescriptor(c));
 871         }
 872         endAttr(alenIdx);
 873      }
 874 
 875     int writeRecordAttribute(ClassSymbol csym) {
 876         int alenIdx = writeAttr(names.Record);
 877         Scope s = csym.members();
 878         databuf.appendChar(csym.getRecordComponents().size());
 879         for (VarSymbol v: csym.getRecordComponents()) {
 880             //databuf.appendChar(poolWriter.putMember(v.accessor.head.snd));
 881             databuf.appendChar(poolWriter.putName(v.name));
 882             databuf.appendChar(poolWriter.putDescriptor(v));
 883             int acountIdx = beginAttrs();
 884             int acount = 0;
 885             acount += writeMemberAttrs(v, true);
 886             endAttrs(acountIdx, acount);
 887         }
 888         endAttr(alenIdx);
 889         return 1;
 890     }
 891 
 892     /**
 893      * Write NestMembers attribute (if needed)
 894      */

 968             }
 969         } while (lastBootstrapMethods < poolWriter.bootstrapMethods.size());
 970         databuf.appendChar(poolWriter.bootstrapMethods.size());
 971         for (BsmKey bsmKey : poolWriter.bootstrapMethods.keySet()) {
 972             //write BSM handle
 973             databuf.appendChar(poolWriter.putConstant(bsmKey.bsm));
 974             LoadableConstant[] uniqueArgs = bsmKey.staticArgs;
 975             //write static args length
 976             databuf.appendChar(uniqueArgs.length);
 977             //write static args array
 978             for (LoadableConstant arg : uniqueArgs) {
 979                 databuf.appendChar(poolWriter.putConstant(arg));
 980             }
 981         }
 982         endAttr(alenIdx);
 983     }
 984 
 985     /** Write field symbol, entering all references into constant pool.
 986      */
 987     void writeField(VarSymbol v) {
 988         int flags = adjustFlags(v, v.flags());
 989         databuf.appendChar(flags);
 990         if (dumpFieldModifiers) {
 991             PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
 992             pw.println("FIELD  " + v.name);
 993             pw.println("---" + flagNames(v.flags()));
 994         }
 995         databuf.appendChar(poolWriter.putName(v.name));
 996         databuf.appendChar(poolWriter.putDescriptor(v));
 997         Type fldType = v.erasure(types);
 998         if (fldType.requiresLoadableDescriptors(v.owner)) {
 999             poolWriter.enterLoadableDescriptorsClass(fldType.tsym);
1000             if (preview.isPreview(Source.Feature.VALUE_CLASSES)) {
1001                 preview.markUsesPreview(null);
1002             }
1003         }
1004         int acountIdx = beginAttrs();
1005         int acount = 0;
1006         if (v.getConstValue() != null) {
1007             int alenIdx = writeAttr(names.ConstantValue);
1008             databuf.appendChar(poolWriter.putConstant(v.getConstValue()));
1009             endAttr(alenIdx);
1010             acount++;
1011         }
1012         acount += writeMemberAttrs(v, false);
1013         acount += writeExtraAttributes(v);
1014         endAttrs(acountIdx, acount);
1015     }
1016 
1017     /** Write method symbol, entering all references into constant pool.
1018      */
1019     void writeMethod(MethodSymbol m) {
1020         int flags = adjustFlags(m, m.flags());
1021         databuf.appendChar(flags);
1022         if (dumpMethodModifiers) {
1023             PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
1024             pw.println("METHOD  " + m.name);
1025             pw.println("---" + flagNames(m.flags()));
1026         }
1027         databuf.appendChar(poolWriter.putName(m.name));
1028         databuf.appendChar(poolWriter.putDescriptor(m));
1029         MethodType mtype = (MethodType) m.externalType(types);
1030         for (Type t : mtype.getParameterTypes()) {
1031             if (t.requiresLoadableDescriptors(m.owner)) {
1032                 poolWriter.enterLoadableDescriptorsClass(t.tsym);
1033                 if (preview.isPreview(Source.Feature.VALUE_CLASSES)) {
1034                     preview.markUsesPreview(null);
1035                 }
1036             }
1037         }
1038         Type returnType = mtype.getReturnType();
1039         if (returnType.requiresLoadableDescriptors(m.owner)) {
1040             poolWriter.enterLoadableDescriptorsClass(returnType.tsym);
1041             if (preview.isPreview(Source.Feature.VALUE_CLASSES)) {
1042                 preview.markUsesPreview(null);
1043             }
1044         }
1045         int acountIdx = beginAttrs();
1046         int acount = 0;
1047         if (m.code != null) {
1048             int alenIdx = writeAttr(names.Code);
1049             writeCode(m.code);
1050             m.code = null; // to conserve space
1051             endAttr(alenIdx);
1052             acount++;
1053         }
1054         List<Type> thrown = m.erasure(types).getThrownTypes();
1055         if (thrown.nonEmpty()) {
1056             int alenIdx = writeAttr(names.Exceptions);
1057             databuf.appendChar(thrown.length());
1058             for (List<Type> l = thrown; l.nonEmpty(); l = l.tail)
1059                 databuf.appendChar(poolWriter.putClass(l.head));
1060             endAttr(alenIdx);
1061             acount++;
1062         }
1063         if (m.defaultValue != null) {
1064             int alenIdx = writeAttr(names.AnnotationDefault);

1247                 int stackCount = 0;
1248                 for (int j=0; j<frame.stack.length;
1249                      j += Code.width(frame.stack[j])) {
1250                     stackCount++;
1251                 }
1252                 if (debugstackmap) System.out.print(" nstack=" +
1253                                                     stackCount);
1254                 databuf.appendChar(stackCount);
1255                 for (int j=0; j<frame.stack.length;
1256                      j += Code.width(frame.stack[j])) {
1257                     if (debugstackmap) System.out.print(" stack[" + j + "]=");
1258                     writeStackMapType(frame.stack[j]);
1259                 }
1260                 if (debugstackmap) System.out.println();
1261             }
1262             break;
1263         case JSR202: {
1264             Assert.checkNull(code.stackMapBuffer);
1265             for (int i=0; i<nframes; i++) {
1266                 if (debugstackmap) System.out.print("  " + i + ":");
1267                 StackMapTableEntry frame = code.stackMapTableBuffer[i];
1268                 frame.write(this);
1269                 if (debugstackmap) System.out.println();
1270             }
1271             break;
1272         }
1273         default:
1274             throw new AssertionError("Unexpected stackmap format value");
1275         }
1276     }
1277 
1278         //where
1279         void writeStackMapType(Type t) {
1280             if (t == null) {
1281                 if (debugstackmap) System.out.print("empty");
1282                 databuf.appendByte(0);
1283             }
1284             else switch(t.getTag()) {
1285             case BYTE:
1286             case CHAR:
1287             case SHORT:

1313                 databuf.appendByte(7);
1314                 databuf.appendChar(poolWriter.putClass(types.erasure(t)));
1315                 break;
1316             case UNINITIALIZED_THIS:
1317                 if (debugstackmap) System.out.print("uninit_this");
1318                 databuf.appendByte(6);
1319                 break;
1320             case UNINITIALIZED_OBJECT:
1321                 { UninitializedType uninitType = (UninitializedType)t;
1322                 databuf.appendByte(8);
1323                 if (debugstackmap) System.out.print("uninit_object@" + uninitType.offset);
1324                 databuf.appendChar(uninitType.offset);
1325                 }
1326                 break;
1327             default:
1328                 throw new AssertionError();
1329             }
1330         }
1331 
1332     /** An entry in the JSR202 StackMapTable */
1333     abstract static class StackMapTableEntry {
1334         abstract int getEntryType();
1335         int pc;
1336 
1337         StackMapTableEntry(int pc) {
1338             this.pc = pc;
1339         }
1340 
1341         void write(ClassWriter writer) {
1342             int entryType = getEntryType();
1343             writer.databuf.appendByte(entryType);
1344             if (writer.debugstackmap) System.out.println(" frame_type=" + entryType + " bytecode offset " + pc);
1345         }
1346 
1347         static class SameFrame extends StackMapTableEntry {
1348             final int offsetDelta;
1349             SameFrame(int pc, int offsetDelta) {
1350                 super(pc);
1351                 this.offsetDelta = offsetDelta;
1352             }
1353             int getEntryType() {
1354                 return (offsetDelta < SAME_FRAME_SIZE) ? offsetDelta : SAME_FRAME_EXTENDED;
1355             }
1356             @Override
1357             void write(ClassWriter writer) {
1358                 super.write(writer);
1359                 if (getEntryType() == SAME_FRAME_EXTENDED) {
1360                     writer.databuf.appendChar(offsetDelta);
1361                     if (writer.debugstackmap){
1362                         System.out.print(" offset_delta=" + offsetDelta);
1363                     }
1364                 }
1365             }
1366         }
1367 
1368         static class SameLocals1StackItemFrame extends StackMapTableEntry {
1369             final int offsetDelta;
1370             final Type stack;
1371             SameLocals1StackItemFrame(int pc, int offsetDelta, Type stack) {
1372                 super(pc);
1373                 this.offsetDelta = offsetDelta;
1374                 this.stack = stack;
1375             }
1376             int getEntryType() {
1377                 return (offsetDelta < SAME_FRAME_SIZE) ?
1378                        (SAME_FRAME_SIZE + offsetDelta) :
1379                        SAME_LOCALS_1_STACK_ITEM_EXTENDED;
1380             }
1381             @Override
1382             void write(ClassWriter writer) {
1383                 super.write(writer);
1384                 if (getEntryType() == SAME_LOCALS_1_STACK_ITEM_EXTENDED) {
1385                     writer.databuf.appendChar(offsetDelta);
1386                     if (writer.debugstackmap) {
1387                         System.out.print(" offset_delta=" + offsetDelta);
1388                     }
1389                 }
1390                 if (writer.debugstackmap) {
1391                     System.out.print(" stack[" + 0 + "]=");
1392                 }
1393                 writer.writeStackMapType(stack);
1394             }
1395         }
1396 
1397         static class ChopFrame extends StackMapTableEntry {
1398             final int frameType;
1399             final int offsetDelta;
1400             ChopFrame(int pc, int frameType, int offsetDelta) {
1401                 super(pc);
1402                 this.frameType = frameType;
1403                 this.offsetDelta = offsetDelta;
1404             }
1405             int getEntryType() { return frameType; }
1406             @Override
1407             void write(ClassWriter writer) {
1408                 super.write(writer);
1409                 writer.databuf.appendChar(offsetDelta);
1410                 if (writer.debugstackmap) {
1411                     System.out.print(" offset_delta=" + offsetDelta);
1412                 }
1413             }
1414         }
1415 
1416         static class AppendFrame extends StackMapTableEntry {
1417             final int frameType;
1418             final int offsetDelta;
1419             final Type[] locals;
1420             AppendFrame(int pc, int frameType, int offsetDelta, Type[] locals) {
1421                 super(pc);
1422                 this.frameType = frameType;
1423                 this.offsetDelta = offsetDelta;
1424                 this.locals = locals;
1425             }
1426             int getEntryType() { return frameType; }
1427             @Override
1428             void write(ClassWriter writer) {
1429                 super.write(writer);
1430                 writer.databuf.appendChar(offsetDelta);
1431                 if (writer.debugstackmap) {
1432                     System.out.print(" offset_delta=" + offsetDelta);
1433                 }
1434                 for (int i=0; i<locals.length; i++) {
1435                      if (writer.debugstackmap) System.out.print(" locals[" + i + "]=");
1436                      writer.writeStackMapType(locals[i]);
1437                 }
1438             }
1439         }
1440 
1441         static class FullFrame extends StackMapTableEntry {
1442             final int offsetDelta;
1443             final Type[] locals;
1444             final Type[] stack;
1445             FullFrame(int pc, int offsetDelta, Type[] locals, Type[] stack) {
1446                 super(pc);
1447                 this.offsetDelta = offsetDelta;
1448                 this.locals = locals;
1449                 this.stack = stack;
1450             }
1451             int getEntryType() { return FULL_FRAME; }
1452             @Override
1453             void write(ClassWriter writer) {
1454                 super.write(writer);
1455                 writer.databuf.appendChar(offsetDelta);
1456                 writer.databuf.appendChar(locals.length);
1457                 if (writer.debugstackmap) {
1458                     System.out.print(" offset_delta=" + offsetDelta);
1459                     System.out.print(" nlocals=" + locals.length);
1460                 }
1461                 for (int i=0; i<locals.length; i++) {
1462                     if (writer.debugstackmap) System.out.print(" locals[" + i + "]=");
1463                     writer.writeStackMapType(locals[i]);
1464                 }
1465 
1466                 writer.databuf.appendChar(stack.length);
1467                 if (writer.debugstackmap) { System.out.print(" nstack=" + stack.length); }
1468                 for (int i=0; i<stack.length; i++) {
1469                     if (writer.debugstackmap) System.out.print(" stack[" + i + "]=");
1470                     writer.writeStackMapType(stack[i]);
1471                 }
1472             }
1473         }
1474 
1475         static class AssertUnsetFields extends StackMapTableEntry {
1476             Set<VarSymbol> unsetFields;
1477 
1478             AssertUnsetFields(int pc, Set<VarSymbol> unsetFields) {
1479                 super(pc);
1480                 this.unsetFields = unsetFields;
1481             }
1482 
1483             int getEntryType() { return ASSERT_UNSET_FIELDS; }
1484 
1485             @Override
1486             void write(ClassWriter writer) {
1487                 super.write(writer);
1488                 writer.databuf.appendChar(unsetFields.size());
1489                 if (writer.debugstackmap) {
1490                     System.out.println("    # writing: AssertUnsetFields stackmap entry with " + unsetFields.size() + " fields");
1491                 }
1492                 for (VarSymbol vsym : unsetFields) {
1493                     int index = writer.poolWriter.putNameAndType(vsym);
1494                     writer.databuf.appendChar(index);
1495                     if (writer.debugstackmap) {
1496                         System.out.println("    #writing unset field: " + index + ", with name: " + vsym.name.toString());
1497                     }
1498                 }
1499             }
1500         }
1501 
1502        /** Compare this frame with the previous frame and produce
1503         *  an entry of compressed stack map frame. */
1504         static StackMapTableEntry getInstance(Code.StackMapFrame this_frame,
1505                                               Code.StackMapFrame prevFrame,
1506                                               Types types,
1507                                               int pc) {
1508             Type[] locals = this_frame.locals;
1509             Type[] stack = this_frame.stack;
1510             int offset_delta = this_frame.pc - prevFrame.pc - 1;
1511             if (stack.length == 1) {
1512                 if (locals.length == prevFrame.locals.length
1513                     && compare(prevFrame.locals, locals, types) == 0) {
1514                     return new SameLocals1StackItemFrame(pc, offset_delta, stack[0]);
1515                 }
1516             } else if (stack.length == 0) {
1517                 int diff_length = compare(prevFrame.locals, locals, types);
1518                 if (diff_length == 0) {
1519                     return new SameFrame(pc, offset_delta);
1520                 } else if (-MAX_LOCAL_LENGTH_DIFF < diff_length && diff_length < 0) {
1521                     // APPEND
1522                     Type[] local_diff = new Type[-diff_length];
1523                     for (int i=prevFrame.locals.length, j=0; i<locals.length; i++,j++) {
1524                         local_diff[j] = locals[i];
1525                     }
1526                     return new AppendFrame(pc, SAME_FRAME_EXTENDED - diff_length,
1527                                            offset_delta,
1528                                            local_diff);
1529                 } else if (0 < diff_length && diff_length < MAX_LOCAL_LENGTH_DIFF) {
1530                     // CHOP
1531                     return new ChopFrame(pc, SAME_FRAME_EXTENDED - diff_length,
1532                                          offset_delta);
1533                 }
1534             }
1535             // FULL_FRAME
1536             return new FullFrame(pc, offset_delta, locals, stack);
1537         }
1538 
1539         static boolean isInt(Type t) {
1540             return (t.getTag().isStrictSubRangeOf(INT)  || t.hasTag(BOOLEAN));
1541         }
1542 
1543         static boolean isSameType(Type t1, Type t2, Types types) {
1544             if (t1 == null) { return t2 == null; }
1545             if (t2 == null) { return false; }
1546 
1547             if (isInt(t1) && isInt(t2)) { return true; }
1548 
1549             if (t1.hasTag(UNINITIALIZED_THIS)) {
1550                 return t2.hasTag(UNINITIALIZED_THIS);
1551             } else if (t1.hasTag(UNINITIALIZED_OBJECT)) {
1552                 if (t2.hasTag(UNINITIALIZED_OBJECT)) {
1553                     return ((UninitializedType)t1).offset == ((UninitializedType)t2).offset;
1554                 } else {
1555                     return false;
1556                 }

1639         }
1640         return outFile; // may be null if write failed
1641     }
1642 
1643     /** Write class `c' to outstream `out'.
1644      */
1645     public void writeClassFile(OutputStream out, ClassSymbol c)
1646         throws IOException, PoolOverflow, StringOverflow {
1647         Assert.check((c.flags() & COMPOUND) == 0);
1648         databuf.reset();
1649         poolbuf.reset();
1650 
1651         Type supertype = types.supertype(c.type);
1652         List<Type> interfaces = types.interfaces(c.type);
1653         List<Type> typarams = c.type.getTypeArguments();
1654 
1655         int flags;
1656         if (c.owner.kind == MDL) {
1657             flags = ACC_MODULE;
1658         } else {
1659             long originalFlags = c.flags();
1660             flags = adjustFlags(c, c.flags() & ~(DEFAULT | STRICTFP));
1661             if ((flags & PROTECTED) != 0) flags |= PUBLIC;
1662             flags = flags & ClassFlags;
1663             flags |= (originalFlags & IDENTITY_TYPE) != 0 ? ACC_IDENTITY : flags;
1664         }
1665 
1666         if (dumpClassModifiers) {
1667             PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
1668             pw.println();
1669             pw.println("CLASSFILE  " + c.getQualifiedName());
1670             pw.println("---" + flagNames(flags));
1671         }
1672         databuf.appendChar(flags);
1673 
1674         if (c.owner.kind == MDL) {
1675             PackageSymbol unnamed = ((ModuleSymbol) c.owner).unnamedPackage;
1676             databuf.appendChar(poolWriter.putClass(new ClassSymbol(0, names.module_info, unnamed)));
1677         } else {
1678             databuf.appendChar(poolWriter.putClass(c));
1679         }
1680         databuf.appendChar(supertype.hasTag(CLASS) ? poolWriter.putClass((ClassSymbol)supertype.tsym) : 0);
1681         databuf.appendChar(interfaces.length());
1682         for (List<Type> l = interfaces; l.nonEmpty(); l = l.tail)
1683             databuf.appendChar(poolWriter.putClass((ClassSymbol)l.head.tsym));

1769         }
1770 
1771         if (c.isRecord()) {
1772             acount += writeRecordAttribute(c);
1773         }
1774 
1775         if (target.hasSealedClasses()) {
1776             acount += writePermittedSubclassesIfNeeded(c);
1777         }
1778 
1779         if (!poolWriter.bootstrapMethods.isEmpty()) {
1780             writeBootstrapMethods();
1781             acount++;
1782         }
1783 
1784         if (!poolWriter.innerClasses.isEmpty()) {
1785             writeInnerClasses();
1786             acount++;
1787         }
1788 
1789         if (!poolWriter.loadableDescriptors.isEmpty()) {
1790             writeLoadableDescriptorsAttribute();
1791             acount++;
1792         }
1793 
1794         endAttrs(acountIdx, acount);
1795 
1796         out.write(poolbuf.elems, 0, poolbuf.length);
1797 
1798         poolWriter.writePool(out);
1799         poolWriter.reset(); // to save space
1800 
1801         out.write(databuf.elems, 0, databuf.length);
1802     }
1803 
1804      /**Allows subclasses to write additional class attributes
1805       *
1806       * @return the number of attributes written
1807       */
1808     protected int writeExtraClassAttributes(ClassSymbol c) {
1809         return 0;
1810     }
1811 
1812     /**Allows friends to write additional attributes
1813      *
1814      * @return the number of attributes written
1815      */
1816     protected int writeExtraAttributes(Symbol sym) {
1817         int i = 0;
1818         for (ToIntFunction<Symbol> hook : extraAttributeHooks) {
1819             i += hook.applyAsInt(sym);
1820         }
1821         return i;
1822     }
1823 
1824     int adjustFlags(Symbol sym, final long flags) {
1825         int result = (int)flags;
1826 
1827         // Elide strictfp bit in class files
1828         if (target.obsoleteAccStrict())
1829             result &= ~STRICTFP;
1830 
1831         if ((flags & BRIDGE) != 0)
1832             result |= ACC_BRIDGE;
1833         if ((flags & VARARGS) != 0)
1834             result |= ACC_VARARGS;
1835         if ((flags & DEFAULT) != 0)
1836             result &= ~ABSTRACT;
1837         if ((flags & IDENTITY_TYPE) != 0) {
1838             result |= ACC_IDENTITY;
1839         }
1840         if (sym.kind == VAR) {
1841             if ((flags & STRICT) != 0) {
1842                 result |= ACC_STRICT;
1843             }
1844         }
1845         return result;
1846     }
1847 
1848     long getLastModified(FileObject filename) {
1849         return filename.getLastModified();
1850     }
1851 }
< prev index next >