< prev index next >

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

Print this page




 457                 poolbuf.appendDouble(((Double)value).doubleValue());
 458                 i++;
 459             } else if (value instanceof String) {
 460                 poolbuf.appendByte(CONSTANT_String);
 461                 poolbuf.appendChar(pool.put(names.fromString((String)value)));
 462             } else if (value instanceof UniqueType) {
 463                 Type type = ((UniqueType)value).type;
 464                 if (type.hasTag(METHOD)) {
 465                     poolbuf.appendByte(CONSTANT_MethodType);
 466                     poolbuf.appendChar(pool.put(typeSig((MethodType)type)));
 467                 } else {
 468                     Assert.check(type.hasTag(ARRAY));
 469                     poolbuf.appendByte(CONSTANT_Class);
 470                     poolbuf.appendChar(pool.put(xClassName(type)));
 471                 }
 472             } else if (value instanceof MethodHandle) {
 473                 MethodHandle ref = (MethodHandle)value;
 474                 poolbuf.appendByte(CONSTANT_MethodHandle);
 475                 poolbuf.appendByte(ref.refKind);
 476                 poolbuf.appendChar(pool.put(ref.refSym));








 477             } else {
 478                 Assert.error("writePool " + value);
 479             }
 480             i++;
 481         }
 482         if (pool.pp > Pool.MAX_ENTRIES)
 483             throw new PoolOverflow();
 484         putChar(poolbuf, poolCountIdx, pool.pp);
 485     }
 486 
 487     /** Given a symbol, return its name-and-type.
 488      */
 489     NameAndType nameType(Symbol sym) {
 490         return new NameAndType(sym.name, sym.externalType(types), types);
 491         // the NameAndType is generated from a symbol reference, and the
 492         // adjustment of adding an additional this$n parameter needs to be made.
 493     }
 494 
 495 /******************************************************************
 496  * Writing Attributes


 937         { // Append location data for generics/arrays.
 938             databuf.appendByte(p.location.size());
 939             java.util.List<Integer> loc = TypeAnnotationPosition.getBinaryFromTypePath(p.location);
 940             for (int i : loc)
 941                 databuf.appendByte((byte)i);
 942         }
 943     }
 944 
 945 /**********************************************************************
 946  * Writing module attributes
 947  **********************************************************************/
 948 
 949     /** Write the Module attribute if needed.
 950      *  Returns the number of attributes written (0 or 1).
 951      */
 952     int writeModuleAttribute(ClassSymbol c) {
 953         ModuleSymbol m = (ModuleSymbol) c.owner;
 954 
 955         int alenIdx = writeAttr(names.Module);
 956 
 957         databuf.appendChar(pool.put(names.fromUtf(externalize(m.name))));
 958         databuf.appendChar(ModuleFlags.value(m.flags)); // module_flags

 959 
 960         ListBuffer<RequiresDirective> requires = new ListBuffer<>();
 961         for (RequiresDirective r: m.requires) {
 962             if (!r.flags.contains(RequiresFlag.EXTRA))
 963                 requires.add(r);
 964         }
 965         databuf.appendChar(requires.size());
 966         for (RequiresDirective r: requires) {
 967             databuf.appendChar(pool.put(names.fromUtf(externalize(r.module.name))));
 968             databuf.appendChar(RequiresFlag.value(r.flags));

 969         }
 970 
 971         List<ExportsDirective> exports = m.exports;
 972         databuf.appendChar(exports.size());
 973         for (ExportsDirective e: exports) {
 974             databuf.appendChar(pool.put(names.fromUtf(externalize(e.packge.flatName()))));
 975             databuf.appendChar(ExportsFlag.value(e.flags));
 976             if (e.modules == null) {
 977                 databuf.appendChar(0);
 978             } else {
 979                 databuf.appendChar(e.modules.size());
 980                 for (ModuleSymbol msym: e.modules) {
 981                     databuf.appendChar(pool.put(names.fromUtf(externalize(msym.name))));
 982                 }
 983             }
 984         }
 985 
 986         List<OpensDirective> opens = m.opens;
 987         databuf.appendChar(opens.size());
 988         for (OpensDirective o: opens) {
 989             databuf.appendChar(pool.put(names.fromUtf(externalize(o.packge.flatName()))));
 990             databuf.appendChar(OpensFlag.value(o.flags));
 991             if (o.modules == null) {
 992                 databuf.appendChar(0);
 993             } else {
 994                 databuf.appendChar(o.modules.size());
 995                 for (ModuleSymbol msym: o.modules) {
 996                     databuf.appendChar(pool.put(names.fromUtf(externalize(msym.name))));
 997                 }
 998             }
 999         }
1000 
1001         List<UsesDirective> uses = m.uses;
1002         databuf.appendChar(uses.size());
1003         for (UsesDirective s: uses) {
1004             databuf.appendChar(pool.put(s.service));
1005         }
1006 
1007         // temporary fix to merge repeated provides clause for same service;
1008         // eventually this should be disallowed when analyzing the module,
1009         // so that each service type only appears once.
1010         Map<ClassSymbol, Set<ClassSymbol>> mergedProvides = new LinkedHashMap<>();
1011         for (ProvidesDirective p : m.provides) {
1012             mergedProvides.computeIfAbsent(p.service, s -> new LinkedHashSet<>()).addAll(p.impls);
1013         }
1014         databuf.appendChar(mergedProvides.size());
1015         mergedProvides.forEach((srvc, impls) -> {
1016             databuf.appendChar(pool.put(srvc));


1701 
1702         int flags;
1703         if (c.owner.kind == MDL) {
1704             flags = ACC_MODULE;
1705         } else {
1706             flags = adjustFlags(c.flags() & ~DEFAULT);
1707             if ((flags & PROTECTED) != 0) flags |= PUBLIC;
1708             flags = flags & ClassFlags & ~STRICTFP;
1709             if ((flags & INTERFACE) == 0) flags |= ACC_SUPER;
1710         }
1711 
1712         if (dumpClassModifiers) {
1713             PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
1714             pw.println();
1715             pw.println("CLASSFILE  " + c.getQualifiedName());
1716             pw.println("---" + flagNames(flags));
1717         }
1718         databuf.appendChar(flags);
1719 
1720         if (c.owner.kind == MDL) {
1721             databuf.appendChar(0);

1722         } else {
1723             databuf.appendChar(pool.put(c));
1724         }
1725         databuf.appendChar(supertype.hasTag(CLASS) ? pool.put(supertype.tsym) : 0);
1726         databuf.appendChar(interfaces.length());
1727         for (List<Type> l = interfaces; l.nonEmpty(); l = l.tail)
1728             databuf.appendChar(pool.put(l.head.tsym));
1729         int fieldsCount = 0;
1730         int methodsCount = 0;
1731         for (Symbol sym : c.members().getSymbols(NON_RECURSIVE)) {
1732             switch (sym.kind) {
1733             case VAR: fieldsCount++; break;
1734             case MTH: if ((sym.flags() & HYPOTHETICAL) == 0) methodsCount++;
1735                       break;
1736             case TYP: enterInner((ClassSymbol)sym); break;
1737             default : Assert.error();
1738             }
1739         }
1740 
1741         if (c.trans_local != null) {




 457                 poolbuf.appendDouble(((Double)value).doubleValue());
 458                 i++;
 459             } else if (value instanceof String) {
 460                 poolbuf.appendByte(CONSTANT_String);
 461                 poolbuf.appendChar(pool.put(names.fromString((String)value)));
 462             } else if (value instanceof UniqueType) {
 463                 Type type = ((UniqueType)value).type;
 464                 if (type.hasTag(METHOD)) {
 465                     poolbuf.appendByte(CONSTANT_MethodType);
 466                     poolbuf.appendChar(pool.put(typeSig((MethodType)type)));
 467                 } else {
 468                     Assert.check(type.hasTag(ARRAY));
 469                     poolbuf.appendByte(CONSTANT_Class);
 470                     poolbuf.appendChar(pool.put(xClassName(type)));
 471                 }
 472             } else if (value instanceof MethodHandle) {
 473                 MethodHandle ref = (MethodHandle)value;
 474                 poolbuf.appendByte(CONSTANT_MethodHandle);
 475                 poolbuf.appendByte(ref.refKind);
 476                 poolbuf.appendChar(pool.put(ref.refSym));
 477             } else if (value instanceof ModuleSymbol) {
 478                 ModuleSymbol m = (ModuleSymbol)value;
 479                 poolbuf.appendByte(CONSTANT_Module);
 480                 poolbuf.appendChar(pool.put(m.name));
 481             } else if (value instanceof PackageSymbol) {
 482                 PackageSymbol m = (PackageSymbol)value;
 483                 poolbuf.appendByte(CONSTANT_Package);
 484                 poolbuf.appendChar(pool.put(names.fromUtf(externalize(m.fullname))));
 485             } else {
 486                 Assert.error("writePool " + value);
 487             }
 488             i++;
 489         }
 490         if (pool.pp > Pool.MAX_ENTRIES)
 491             throw new PoolOverflow();
 492         putChar(poolbuf, poolCountIdx, pool.pp);
 493     }
 494 
 495     /** Given a symbol, return its name-and-type.
 496      */
 497     NameAndType nameType(Symbol sym) {
 498         return new NameAndType(sym.name, sym.externalType(types), types);
 499         // the NameAndType is generated from a symbol reference, and the
 500         // adjustment of adding an additional this$n parameter needs to be made.
 501     }
 502 
 503 /******************************************************************
 504  * Writing Attributes


 945         { // Append location data for generics/arrays.
 946             databuf.appendByte(p.location.size());
 947             java.util.List<Integer> loc = TypeAnnotationPosition.getBinaryFromTypePath(p.location);
 948             for (int i : loc)
 949                 databuf.appendByte((byte)i);
 950         }
 951     }
 952 
 953 /**********************************************************************
 954  * Writing module attributes
 955  **********************************************************************/
 956 
 957     /** Write the Module attribute if needed.
 958      *  Returns the number of attributes written (0 or 1).
 959      */
 960     int writeModuleAttribute(ClassSymbol c) {
 961         ModuleSymbol m = (ModuleSymbol) c.owner;
 962 
 963         int alenIdx = writeAttr(names.Module);
 964 
 965         databuf.appendChar(pool.put(m));
 966         databuf.appendChar(ModuleFlags.value(m.flags)); // module_flags
 967         databuf.appendChar(m.version != null ? pool.put(m.version) : 0);
 968 
 969         ListBuffer<RequiresDirective> requires = new ListBuffer<>();
 970         for (RequiresDirective r: m.requires) {
 971             if (!r.flags.contains(RequiresFlag.EXTRA))
 972                 requires.add(r);
 973         }
 974         databuf.appendChar(requires.size());
 975         for (RequiresDirective r: requires) {
 976             databuf.appendChar(pool.put(r.module));
 977             databuf.appendChar(RequiresFlag.value(r.flags));
 978             databuf.appendChar(r.module.version != null ? pool.put(r.module.version) : 0);
 979         }
 980 
 981         List<ExportsDirective> exports = m.exports;
 982         databuf.appendChar(exports.size());
 983         for (ExportsDirective e: exports) {
 984             databuf.appendChar(pool.put(e.packge));
 985             databuf.appendChar(ExportsFlag.value(e.flags));
 986             if (e.modules == null) {
 987                 databuf.appendChar(0);
 988             } else {
 989                 databuf.appendChar(e.modules.size());
 990                 for (ModuleSymbol msym: e.modules) {
 991                     databuf.appendChar(pool.put(msym));
 992                 }
 993             }
 994         }
 995 
 996         List<OpensDirective> opens = m.opens;
 997         databuf.appendChar(opens.size());
 998         for (OpensDirective o: opens) {
 999             databuf.appendChar(pool.put(o.packge));
1000             databuf.appendChar(OpensFlag.value(o.flags));
1001             if (o.modules == null) {
1002                 databuf.appendChar(0);
1003             } else {
1004                 databuf.appendChar(o.modules.size());
1005                 for (ModuleSymbol msym: o.modules) {
1006                     databuf.appendChar(pool.put(msym));
1007                 }
1008             }
1009         }
1010 
1011         List<UsesDirective> uses = m.uses;
1012         databuf.appendChar(uses.size());
1013         for (UsesDirective s: uses) {
1014             databuf.appendChar(pool.put(s.service));
1015         }
1016 
1017         // temporary fix to merge repeated provides clause for same service;
1018         // eventually this should be disallowed when analyzing the module,
1019         // so that each service type only appears once.
1020         Map<ClassSymbol, Set<ClassSymbol>> mergedProvides = new LinkedHashMap<>();
1021         for (ProvidesDirective p : m.provides) {
1022             mergedProvides.computeIfAbsent(p.service, s -> new LinkedHashSet<>()).addAll(p.impls);
1023         }
1024         databuf.appendChar(mergedProvides.size());
1025         mergedProvides.forEach((srvc, impls) -> {
1026             databuf.appendChar(pool.put(srvc));


1711 
1712         int flags;
1713         if (c.owner.kind == MDL) {
1714             flags = ACC_MODULE;
1715         } else {
1716             flags = adjustFlags(c.flags() & ~DEFAULT);
1717             if ((flags & PROTECTED) != 0) flags |= PUBLIC;
1718             flags = flags & ClassFlags & ~STRICTFP;
1719             if ((flags & INTERFACE) == 0) flags |= ACC_SUPER;
1720         }
1721 
1722         if (dumpClassModifiers) {
1723             PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
1724             pw.println();
1725             pw.println("CLASSFILE  " + c.getQualifiedName());
1726             pw.println("---" + flagNames(flags));
1727         }
1728         databuf.appendChar(flags);
1729 
1730         if (c.owner.kind == MDL) {
1731             PackageSymbol unnamed = ((ModuleSymbol) c.owner).unnamedPackage;
1732             databuf.appendChar(pool.put(new ClassSymbol(0, names.module_info, unnamed)));
1733         } else {
1734             databuf.appendChar(pool.put(c));
1735         }
1736         databuf.appendChar(supertype.hasTag(CLASS) ? pool.put(supertype.tsym) : 0);
1737         databuf.appendChar(interfaces.length());
1738         for (List<Type> l = interfaces; l.nonEmpty(); l = l.tail)
1739             databuf.appendChar(pool.put(l.head.tsym));
1740         int fieldsCount = 0;
1741         int methodsCount = 0;
1742         for (Symbol sym : c.members().getSymbols(NON_RECURSIVE)) {
1743             switch (sym.kind) {
1744             case VAR: fieldsCount++; break;
1745             case MTH: if ((sym.flags() & HYPOTHETICAL) == 0) methodsCount++;
1746                       break;
1747             case TYP: enterInner((ClassSymbol)sym); break;
1748             default : Assert.error();
1749             }
1750         }
1751 
1752         if (c.trans_local != null) {


< prev index next >