duke@1: /* jjg@1521: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.jvm; duke@1: duke@1: import java.io.*; mcimadamore@1336: import java.util.LinkedHashMap; mcimadamore@1336: import java.util.Map; duke@1: import java.util.Set; duke@1: import java.util.HashSet; duke@1: jjg@1521: import javax.lang.model.type.TypeKind; duke@1: import javax.tools.JavaFileManager; duke@1: import javax.tools.FileObject; duke@1: import javax.tools.JavaFileObject; duke@1: duke@1: import com.sun.tools.javac.code.*; jjg@657: import com.sun.tools.javac.code.Attribute.RetentionPolicy; jjg@1521: import com.sun.tools.javac.code.Attribute.TypeCompound; rfield@1587: import static com.sun.tools.javac.code.BoundKind.EXTENDS; rfield@1587: import static com.sun.tools.javac.code.BoundKind.SUPER; rfield@1587: import static com.sun.tools.javac.code.BoundKind.UNBOUND; duke@1: import com.sun.tools.javac.code.Symbol.*; duke@1: import com.sun.tools.javac.code.Type.*; vromero@1452: import com.sun.tools.javac.code.Types.UniqueType; jjg@415: import com.sun.tools.javac.file.BaseFileObject; vromero@1452: import com.sun.tools.javac.jvm.Pool.DynamicMethod; vromero@1452: import com.sun.tools.javac.jvm.Pool.Method; vromero@1452: import com.sun.tools.javac.jvm.Pool.MethodHandle; vromero@1452: import com.sun.tools.javac.jvm.Pool.Variable; duke@1: import com.sun.tools.javac.util.*; duke@1: duke@1: import static com.sun.tools.javac.code.Flags.*; duke@1: import static com.sun.tools.javac.code.Kinds.*; jjg@1374: import static com.sun.tools.javac.code.TypeTag.*; duke@1: import static com.sun.tools.javac.jvm.UninitializedType.*; jjg@1157: import static com.sun.tools.javac.main.Option.*; duke@1: import static javax.tools.StandardLocation.CLASS_OUTPUT; duke@1: jjg@700: duke@1: /** This class provides operations to map an internal symbol table graph duke@1: * rooted in a ClassSymbol into a classfile. duke@1: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public class ClassWriter extends ClassFile { duke@1: protected static final Context.Key classWriterKey = duke@1: new Context.Key(); duke@1: duke@1: private final Options options; duke@1: duke@1: /** Switch: verbose output. duke@1: */ duke@1: private boolean verbose; duke@1: jjg@1521: /** Switch: scramble private field names. duke@1: */ duke@1: private boolean scramble; duke@1: jjg@1521: /** Switch: scramble all field names. duke@1: */ duke@1: private boolean scrambleAll; duke@1: duke@1: /** Switch: retrofit mode. duke@1: */ duke@1: private boolean retrofit; duke@1: duke@1: /** Switch: emit source file attribute. duke@1: */ duke@1: private boolean emitSourceFile; duke@1: duke@1: /** Switch: generate CharacterRangeTable attribute. duke@1: */ duke@1: private boolean genCrt; duke@1: jjg@1521: /** Switch: describe the generated stackmap. duke@1: */ duke@1: boolean debugstackmap; duke@1: duke@1: /** duke@1: * Target class version. duke@1: */ duke@1: private Target target; duke@1: duke@1: /** duke@1: * Source language version. duke@1: */ duke@1: private Source source; duke@1: duke@1: /** Type utilities. */ duke@1: private Types types; duke@1: duke@1: /** The initial sizes of the data and constant pool buffers. jjg@1521: * Sizes are increased when buffers get full. duke@1: */ duke@1: static final int DATA_BUF_SIZE = 0x0fff0; duke@1: static final int POOL_BUF_SIZE = 0x1fff0; duke@1: duke@1: /** An output buffer for member info. duke@1: */ duke@1: ByteBuffer databuf = new ByteBuffer(DATA_BUF_SIZE); duke@1: duke@1: /** An output buffer for the constant pool. duke@1: */ duke@1: ByteBuffer poolbuf = new ByteBuffer(POOL_BUF_SIZE); duke@1: duke@1: /** The constant pool. duke@1: */ duke@1: Pool pool; duke@1: duke@1: /** The inner classes to be written, as a set. duke@1: */ duke@1: Set innerClasses; duke@1: duke@1: /** The inner classes to be written, as a queue where duke@1: * enclosing classes come first. duke@1: */ duke@1: ListBuffer innerClassesQueue; duke@1: mcimadamore@1336: /** The bootstrap methods to be written in the corresponding class attribute mcimadamore@1336: * (one for each invokedynamic) mcimadamore@1336: */ vromero@1452: Map bootstrapMethods; mcimadamore@1336: duke@1: /** The log to use for verbose output. duke@1: */ duke@1: private final Log log; duke@1: duke@1: /** The name table. */ jjg@113: private final Names names; duke@1: duke@1: /** Access to files. */ duke@1: private final JavaFileManager fileManager; duke@1: rfield@1587: /** Sole signature generator */ rfield@1587: private final CWSignatureGenerator signatureGen; rfield@1587: duke@1: /** The tags and constants used in compressed stackmap. */ duke@1: static final int SAME_FRAME_SIZE = 64; duke@1: static final int SAME_LOCALS_1_STACK_ITEM_EXTENDED = 247; duke@1: static final int SAME_FRAME_EXTENDED = 251; duke@1: static final int FULL_FRAME = 255; duke@1: static final int MAX_LOCAL_LENGTH_DIFF = 4; duke@1: duke@1: /** Get the ClassWriter instance for this context. */ duke@1: public static ClassWriter instance(Context context) { duke@1: ClassWriter instance = context.get(classWriterKey); duke@1: if (instance == null) duke@1: instance = new ClassWriter(context); duke@1: return instance; duke@1: } duke@1: duke@1: /** Construct a class writer, given an options table. duke@1: */ ksrini@1309: protected ClassWriter(Context context) { duke@1: context.put(classWriterKey, this); duke@1: duke@1: log = Log.instance(context); jjg@113: names = Names.instance(context); duke@1: options = Options.instance(context); duke@1: target = Target.instance(context); duke@1: source = Source.instance(context); duke@1: types = Types.instance(context); duke@1: fileManager = context.get(JavaFileManager.class); rfield@1587: signatureGen = new CWSignatureGenerator(types); duke@1: jjg@700: verbose = options.isSet(VERBOSE); jjg@700: scramble = options.isSet("-scramble"); jjg@700: scrambleAll = options.isSet("-scrambleAll"); jjg@700: retrofit = options.isSet("-retrofit"); jjg@700: genCrt = options.isSet(XJCOV); jjg@700: debugstackmap = options.isSet("debugstackmap"); duke@1: jjg@700: emitSourceFile = options.isUnset(G_CUSTOM) || jjg@700: options.isSet(G_CUSTOM, "source"); duke@1: duke@1: String dumpModFlags = options.get("dumpmodifiers"); duke@1: dumpClassModifiers = duke@1: (dumpModFlags != null && dumpModFlags.indexOf('c') != -1); duke@1: dumpFieldModifiers = duke@1: (dumpModFlags != null && dumpModFlags.indexOf('f') != -1); duke@1: dumpInnerClassModifiers = duke@1: (dumpModFlags != null && dumpModFlags.indexOf('i') != -1); duke@1: dumpMethodModifiers = duke@1: (dumpModFlags != null && dumpModFlags.indexOf('m') != -1); duke@1: } duke@1: duke@1: /****************************************************************** duke@1: * Diagnostics: dump generated class names and modifiers duke@1: ******************************************************************/ duke@1: duke@1: /** Value of option 'dumpmodifiers' is a string duke@1: * indicating which modifiers should be dumped for debugging: duke@1: * 'c' -- classes duke@1: * 'f' -- fields duke@1: * 'i' -- innerclass attributes duke@1: * 'm' -- methods duke@1: * For example, to dump everything: duke@1: * javac -XDdumpmodifiers=cifm MyProg.java duke@1: */ duke@1: private final boolean dumpClassModifiers; // -XDdumpmodifiers=c duke@1: private final boolean dumpFieldModifiers; // -XDdumpmodifiers=f duke@1: private final boolean dumpInnerClassModifiers; // -XDdumpmodifiers=i duke@1: private final boolean dumpMethodModifiers; // -XDdumpmodifiers=m duke@1: duke@1: duke@1: /** Return flags as a string, separated by " ". duke@1: */ duke@1: public static String flagNames(long flags) { jjg@816: StringBuilder sbuf = new StringBuilder(); duke@1: int i = 0; duke@1: long f = flags & StandardFlags; duke@1: while (f != 0) { jjg@816: if ((f & 1) != 0) { jjg@816: sbuf.append(" "); jjg@816: sbuf.append(flagName[i]); jjg@816: } duke@1: f = f >> 1; duke@1: i++; duke@1: } duke@1: return sbuf.toString(); duke@1: } duke@1: //where duke@1: private final static String[] flagName = { duke@1: "PUBLIC", "PRIVATE", "PROTECTED", "STATIC", "FINAL", duke@1: "SUPER", "VOLATILE", "TRANSIENT", "NATIVE", "INTERFACE", duke@1: "ABSTRACT", "STRICTFP"}; duke@1: duke@1: /****************************************************************** duke@1: * Output routines duke@1: ******************************************************************/ duke@1: duke@1: /** Write a character into given byte buffer; duke@1: * byte buffer will not be grown. duke@1: */ duke@1: void putChar(ByteBuffer buf, int op, int x) { duke@1: buf.elems[op ] = (byte)((x >> 8) & 0xFF); duke@1: buf.elems[op+1] = (byte)((x ) & 0xFF); duke@1: } duke@1: duke@1: /** Write an integer into given byte buffer; duke@1: * byte buffer will not be grown. duke@1: */ duke@1: void putInt(ByteBuffer buf, int adr, int x) { duke@1: buf.elems[adr ] = (byte)((x >> 24) & 0xFF); duke@1: buf.elems[adr+1] = (byte)((x >> 16) & 0xFF); duke@1: buf.elems[adr+2] = (byte)((x >> 8) & 0xFF); duke@1: buf.elems[adr+3] = (byte)((x ) & 0xFF); duke@1: } duke@1: rfield@1587: /** rfield@1587: * Signature Generation rfield@1587: */ rfield@1587: private class CWSignatureGenerator extends Types.SignatureGenerator { duke@1: rfield@1587: /** rfield@1587: * An output buffer for type signatures. rfield@1587: */ rfield@1587: ByteBuffer sigbuf = new ByteBuffer(); rfield@1587: rfield@1587: CWSignatureGenerator(Types types) { rfield@1587: super(types); rfield@1587: } rfield@1587: rfield@1587: /** rfield@1587: * Assemble signature of given type in string buffer. rfield@1587: * Check for uninitialized types before calling the general case. rfield@1587: */ rfield@1587: @Override rfield@1587: public void assembleSig(Type type) { rfield@1587: type = type.unannotatedType(); rfield@1587: switch (type.getTag()) { rfield@1587: case UNINITIALIZED_THIS: rfield@1587: case UNINITIALIZED_OBJECT: rfield@1587: // we don't yet have a spec for uninitialized types in the rfield@1587: // local variable table rfield@1587: assembleSig(types.erasure(((UninitializedType)type).qtype)); rfield@1587: break; rfield@1587: default: rfield@1587: super.assembleSig(type); duke@1: } duke@1: } rfield@1587: rfield@1587: @Override rfield@1587: protected void append(char ch) { rfield@1587: sigbuf.appendByte(ch); rfield@1587: } rfield@1587: rfield@1587: @Override rfield@1587: protected void append(byte[] ba) { rfield@1587: sigbuf.appendBytes(ba); rfield@1587: } rfield@1587: rfield@1587: @Override rfield@1587: protected void append(Name name) { rfield@1587: sigbuf.appendName(name); rfield@1587: } rfield@1587: rfield@1587: @Override rfield@1587: protected void classReference(ClassSymbol c) { rfield@1587: enterInner(c); rfield@1587: } rfield@1587: rfield@1587: private void reset() { rfield@1587: sigbuf.reset(); rfield@1587: } rfield@1587: rfield@1587: private Name toName() { rfield@1587: return sigbuf.toName(names); rfield@1587: } rfield@1587: rfield@1587: private boolean isEmpty() { rfield@1587: return sigbuf.length == 0; duke@1: } duke@1: } duke@1: rfield@1587: /** rfield@1587: * Return signature of given type duke@1: */ duke@1: Name typeSig(Type type) { rfield@1587: Assert.check(signatureGen.isEmpty()); duke@1: //- System.out.println(" ? " + type); rfield@1587: signatureGen.assembleSig(type); rfield@1587: Name n = signatureGen.toName(); rfield@1587: signatureGen.reset(); duke@1: //- System.out.println(" " + n); duke@1: return n; duke@1: } duke@1: duke@1: /** Given a type t, return the extended class name of its erasure in duke@1: * external representation. duke@1: */ duke@1: public Name xClassName(Type t) { jjg@1374: if (t.hasTag(CLASS)) { duke@1: return names.fromUtf(externalize(t.tsym.flatName())); jjg@1374: } else if (t.hasTag(ARRAY)) { duke@1: return typeSig(types.erasure(t)); duke@1: } else { duke@1: throw new AssertionError("xClassName"); duke@1: } duke@1: } duke@1: duke@1: /****************************************************************** duke@1: * Writing the Constant Pool duke@1: ******************************************************************/ duke@1: duke@1: /** Thrown when the constant pool is over full. duke@1: */ duke@1: public static class PoolOverflow extends Exception { duke@1: private static final long serialVersionUID = 0; duke@1: public PoolOverflow() {} duke@1: } duke@1: public static class StringOverflow extends Exception { duke@1: private static final long serialVersionUID = 0; duke@1: public final String value; duke@1: public StringOverflow(String s) { duke@1: value = s; duke@1: } duke@1: } duke@1: duke@1: /** Write constant pool to pool buffer. duke@1: * Note: during writing, constant pool duke@1: * might grow since some parts of constants still need to be entered. duke@1: */ duke@1: void writePool(Pool pool) throws PoolOverflow, StringOverflow { duke@1: int poolCountIdx = poolbuf.length; duke@1: poolbuf.appendChar(0); duke@1: int i = 1; duke@1: while (i < pool.pp) { duke@1: Object value = pool.pool[i]; jjg@816: Assert.checkNonNull(value); vromero@1541: if (value instanceof Method || value instanceof Variable) vromero@1541: value = ((DelegatedSymbol)value).getUnderlyingSymbol(); duke@1: duke@1: if (value instanceof MethodSymbol) { duke@1: MethodSymbol m = (MethodSymbol)value; mcimadamore@1336: if (!m.isDynamic()) { mcimadamore@1336: poolbuf.appendByte((m.owner.flags() & INTERFACE) != 0 mcimadamore@1336: ? CONSTANT_InterfaceMethodref mcimadamore@1336: : CONSTANT_Methodref); mcimadamore@1336: poolbuf.appendChar(pool.put(m.owner)); mcimadamore@1336: poolbuf.appendChar(pool.put(nameType(m))); mcimadamore@1336: } else { mcimadamore@1336: //invokedynamic mcimadamore@1336: DynamicMethodSymbol dynSym = (DynamicMethodSymbol)m; vromero@1452: MethodHandle handle = new MethodHandle(dynSym.bsmKind, dynSym.bsm, types); vromero@1452: DynamicMethod dynMeth = new DynamicMethod(dynSym, types); vromero@1452: bootstrapMethods.put(dynMeth, handle); mcimadamore@1336: //init cp entries mcimadamore@1336: pool.put(names.BootstrapMethods); mcimadamore@1336: pool.put(handle); mcimadamore@1336: for (Object staticArg : dynSym.staticArgs) { mcimadamore@1336: pool.put(staticArg); mcimadamore@1336: } mcimadamore@1336: poolbuf.appendByte(CONSTANT_InvokeDynamic); mcimadamore@1336: poolbuf.appendChar(bootstrapMethods.size() - 1); mcimadamore@1336: poolbuf.appendChar(pool.put(nameType(dynSym))); mcimadamore@1336: } duke@1: } else if (value instanceof VarSymbol) { duke@1: VarSymbol v = (VarSymbol)value; duke@1: poolbuf.appendByte(CONSTANT_Fieldref); duke@1: poolbuf.appendChar(pool.put(v.owner)); duke@1: poolbuf.appendChar(pool.put(nameType(v))); duke@1: } else if (value instanceof Name) { duke@1: poolbuf.appendByte(CONSTANT_Utf8); duke@1: byte[] bs = ((Name)value).toUtf(); duke@1: poolbuf.appendChar(bs.length); duke@1: poolbuf.appendBytes(bs, 0, bs.length); duke@1: if (bs.length > Pool.MAX_STRING_LENGTH) duke@1: throw new StringOverflow(value.toString()); duke@1: } else if (value instanceof ClassSymbol) { duke@1: ClassSymbol c = (ClassSymbol)value; duke@1: if (c.owner.kind == TYP) pool.put(c.owner); duke@1: poolbuf.appendByte(CONSTANT_Class); jjg@1374: if (c.type.hasTag(ARRAY)) { duke@1: poolbuf.appendChar(pool.put(typeSig(c.type))); duke@1: } else { duke@1: poolbuf.appendChar(pool.put(names.fromUtf(externalize(c.flatname)))); duke@1: enterInner(c); duke@1: } duke@1: } else if (value instanceof NameAndType) { duke@1: NameAndType nt = (NameAndType)value; duke@1: poolbuf.appendByte(CONSTANT_NameandType); duke@1: poolbuf.appendChar(pool.put(nt.name)); vromero@1452: poolbuf.appendChar(pool.put(typeSig(nt.uniqueType.type))); duke@1: } else if (value instanceof Integer) { duke@1: poolbuf.appendByte(CONSTANT_Integer); duke@1: poolbuf.appendInt(((Integer)value).intValue()); duke@1: } else if (value instanceof Long) { duke@1: poolbuf.appendByte(CONSTANT_Long); duke@1: poolbuf.appendLong(((Long)value).longValue()); duke@1: i++; duke@1: } else if (value instanceof Float) { duke@1: poolbuf.appendByte(CONSTANT_Float); duke@1: poolbuf.appendFloat(((Float)value).floatValue()); duke@1: } else if (value instanceof Double) { duke@1: poolbuf.appendByte(CONSTANT_Double); duke@1: poolbuf.appendDouble(((Double)value).doubleValue()); duke@1: i++; duke@1: } else if (value instanceof String) { duke@1: poolbuf.appendByte(CONSTANT_String); duke@1: poolbuf.appendChar(pool.put(names.fromString((String)value))); vromero@1452: } else if (value instanceof UniqueType) { vromero@1452: Type type = ((UniqueType)value).type; vromero@1452: if (type instanceof MethodType) { vromero@1452: poolbuf.appendByte(CONSTANT_MethodType); vromero@1452: poolbuf.appendChar(pool.put(typeSig((MethodType)type))); vromero@1452: } else { vromero@1452: if (type.hasTag(CLASS)) enterInner((ClassSymbol)type.tsym); vromero@1452: poolbuf.appendByte(CONSTANT_Class); vromero@1452: poolbuf.appendChar(pool.put(xClassName(type))); vromero@1452: } vromero@1452: } else if (value instanceof MethodHandle) { vromero@1452: MethodHandle ref = (MethodHandle)value; mcimadamore@1336: poolbuf.appendByte(CONSTANT_MethodHandle); mcimadamore@1336: poolbuf.appendByte(ref.refKind); mcimadamore@1336: poolbuf.appendChar(pool.put(ref.refSym)); duke@1: } else { jjg@816: Assert.error("writePool " + value); duke@1: } duke@1: i++; duke@1: } duke@1: if (pool.pp > Pool.MAX_ENTRIES) duke@1: throw new PoolOverflow(); duke@1: putChar(poolbuf, poolCountIdx, pool.pp); duke@1: } duke@1: duke@1: /** Given a field, return its name. duke@1: */ duke@1: Name fieldName(Symbol sym) { duke@1: if (scramble && (sym.flags() & PRIVATE) != 0 || duke@1: scrambleAll && (sym.flags() & (PROTECTED | PUBLIC)) == 0) jjg@113: return names.fromString("_$" + sym.name.getIndex()); duke@1: else duke@1: return sym.name; duke@1: } duke@1: duke@1: /** Given a symbol, return its name-and-type. duke@1: */ duke@1: NameAndType nameType(Symbol sym) { duke@1: return new NameAndType(fieldName(sym), duke@1: retrofit duke@1: ? sym.erasure(types) vromero@1452: : sym.externalType(types), types); duke@1: // if we retrofit, then the NameAndType has been read in as is duke@1: // and no change is necessary. If we compile normally, the duke@1: // NameAndType is generated from a symbol reference, and the duke@1: // adjustment of adding an additional this$n parameter needs to be made. duke@1: } duke@1: duke@1: /****************************************************************** duke@1: * Writing Attributes duke@1: ******************************************************************/ duke@1: duke@1: /** Write header for an attribute to data buffer and return duke@1: * position past attribute length index. duke@1: */ duke@1: int writeAttr(Name attrName) { duke@1: databuf.appendChar(pool.put(attrName)); duke@1: databuf.appendInt(0); duke@1: return databuf.length; duke@1: } duke@1: duke@1: /** Fill in attribute length. duke@1: */ duke@1: void endAttr(int index) { duke@1: putInt(databuf, index - 4, databuf.length - index); duke@1: } duke@1: duke@1: /** Leave space for attribute count and return index for duke@1: * number of attributes field. duke@1: */ duke@1: int beginAttrs() { duke@1: databuf.appendChar(0); duke@1: return databuf.length; duke@1: } duke@1: duke@1: /** Fill in number of attributes. duke@1: */ duke@1: void endAttrs(int index, int count) { duke@1: putChar(databuf, index - 2, count); duke@1: } duke@1: duke@1: /** Write the EnclosingMethod attribute if needed. duke@1: * Returns the number of attributes written (0 or 1). duke@1: */ duke@1: int writeEnclosingMethodAttribute(ClassSymbol c) { ksrini@1309: if (!target.hasEnclosingMethodAttribute()) ksrini@1309: return 0; ksrini@1309: return writeEnclosingMethodAttribute(names.EnclosingMethod, c); ksrini@1309: } ksrini@1309: ksrini@1309: /** Write the EnclosingMethod attribute with a specified name. ksrini@1309: * Returns the number of attributes written (0 or 1). ksrini@1309: */ ksrini@1309: protected int writeEnclosingMethodAttribute(Name attributeName, ClassSymbol c) { ksrini@1309: if (c.owner.kind != MTH && // neither a local class duke@1: c.name != names.empty) // nor anonymous duke@1: return 0; duke@1: ksrini@1309: int alenIdx = writeAttr(attributeName); duke@1: ClassSymbol enclClass = c.owner.enclClass(); duke@1: MethodSymbol enclMethod = duke@1: (c.owner.type == null // local to init block duke@1: || c.owner.kind != MTH) // or member init duke@1: ? null duke@1: : (MethodSymbol)c.owner; duke@1: databuf.appendChar(pool.put(enclClass)); duke@1: databuf.appendChar(enclMethod == null ? 0 : pool.put(nameType(c.owner))); duke@1: endAttr(alenIdx); duke@1: return 1; duke@1: } duke@1: duke@1: /** Write flag attributes; return number of attributes written. duke@1: */ duke@1: int writeFlagAttrs(long flags) { duke@1: int acount = 0; duke@1: if ((flags & DEPRECATED) != 0) { duke@1: int alenIdx = writeAttr(names.Deprecated); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: if ((flags & ENUM) != 0 && !target.useEnumFlag()) { duke@1: int alenIdx = writeAttr(names.Enum); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: if ((flags & SYNTHETIC) != 0 && !target.useSyntheticFlag()) { duke@1: int alenIdx = writeAttr(names.Synthetic); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: if ((flags & BRIDGE) != 0 && !target.useBridgeFlag()) { duke@1: int alenIdx = writeAttr(names.Bridge); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: if ((flags & VARARGS) != 0 && !target.useVarargsFlag()) { duke@1: int alenIdx = writeAttr(names.Varargs); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: if ((flags & ANNOTATION) != 0 && !target.useAnnotationFlag()) { duke@1: int alenIdx = writeAttr(names.Annotation); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: return acount; duke@1: } duke@1: duke@1: /** Write member (field or method) attributes; duke@1: * return number of attributes written. duke@1: */ duke@1: int writeMemberAttrs(Symbol sym) { duke@1: int acount = writeFlagAttrs(sym.flags()); duke@1: long flags = sym.flags(); duke@1: if (source.allowGenerics() && duke@1: (flags & (SYNTHETIC|BRIDGE)) != SYNTHETIC && duke@1: (flags & ANONCONSTR) == 0 && duke@1: (!types.isSameType(sym.type, sym.erasure(types)) || rfield@1587: signatureGen.hasTypeVar(sym.type.getThrownTypes()))) { duke@1: // note that a local class with captured variables duke@1: // will get a signature attribute duke@1: int alenIdx = writeAttr(names.Signature); duke@1: databuf.appendChar(pool.put(typeSig(sym.type))); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } jfranck@1464: acount += writeJavaAnnotations(sym.getRawAttributes()); jjg@1521: acount += writeTypeAnnotations(sym.getRawTypeAttributes()); duke@1: return acount; duke@1: } duke@1: jjg@1473: /** jjg@1473: * Write method parameter names attribute. jjg@1473: */ jjg@1473: int writeMethodParametersAttr(MethodSymbol m) { mcimadamore@1565: MethodType ty = m.externalType(types).asMethodType(); mcimadamore@1565: final int allparams = ty.argtypes.size(); mcimadamore@1565: if (m.params != null && allparams != 0) { mcimadamore@1565: final int attrIndex = writeAttr(names.MethodParameters); mcimadamore@1565: databuf.appendByte(allparams); mcimadamore@1565: // Write extra parameters first mcimadamore@1565: for (VarSymbol s : m.extraParams) { mcimadamore@1565: final int flags = mcimadamore@1565: ((int) s.flags() & (FINAL | SYNTHETIC | MANDATED)) | mcimadamore@1565: ((int) m.flags() & SYNTHETIC); mcimadamore@1565: databuf.appendChar(pool.put(s.name)); mcimadamore@1565: databuf.appendInt(flags); mcimadamore@1565: } mcimadamore@1565: // Now write the real parameters jjg@1473: for (VarSymbol s : m.params) { mcimadamore@1565: final int flags = mcimadamore@1565: ((int) s.flags() & (FINAL | SYNTHETIC | MANDATED)) | mcimadamore@1565: ((int) m.flags() & SYNTHETIC); jjg@1473: databuf.appendChar(pool.put(s.name)); ksrini@1592: databuf.appendChar(flags); jjg@1473: } jjg@1473: endAttr(attrIndex); jjg@1473: return 1; jjg@1473: } else jjg@1473: return 0; jjg@1473: } jjg@1473: jjg@1473: duke@1: /** Write method parameter annotations; duke@1: * return number of attributes written. duke@1: */ duke@1: int writeParameterAttrs(MethodSymbol m) { duke@1: boolean hasVisible = false; duke@1: boolean hasInvisible = false; duke@1: if (m.params != null) for (VarSymbol s : m.params) { jfranck@1464: for (Attribute.Compound a : s.getRawAttributes()) { jjg@657: switch (types.getRetention(a)) { duke@1: case SOURCE: break; duke@1: case CLASS: hasInvisible = true; break; duke@1: case RUNTIME: hasVisible = true; break; duke@1: default: ;// /* fail soft */ throw new AssertionError(vis); duke@1: } duke@1: } duke@1: } duke@1: duke@1: int attrCount = 0; duke@1: if (hasVisible) { duke@1: int attrIndex = writeAttr(names.RuntimeVisibleParameterAnnotations); duke@1: databuf.appendByte(m.params.length()); duke@1: for (VarSymbol s : m.params) { duke@1: ListBuffer buf = new ListBuffer(); jfranck@1464: for (Attribute.Compound a : s.getRawAttributes()) jjg@657: if (types.getRetention(a) == RetentionPolicy.RUNTIME) duke@1: buf.append(a); duke@1: databuf.appendChar(buf.length()); duke@1: for (Attribute.Compound a : buf) duke@1: writeCompoundAttribute(a); duke@1: } duke@1: endAttr(attrIndex); duke@1: attrCount++; duke@1: } duke@1: if (hasInvisible) { duke@1: int attrIndex = writeAttr(names.RuntimeInvisibleParameterAnnotations); duke@1: databuf.appendByte(m.params.length()); duke@1: for (VarSymbol s : m.params) { duke@1: ListBuffer buf = new ListBuffer(); jfranck@1464: for (Attribute.Compound a : s.getRawAttributes()) jjg@657: if (types.getRetention(a) == RetentionPolicy.CLASS) duke@1: buf.append(a); duke@1: databuf.appendChar(buf.length()); duke@1: for (Attribute.Compound a : buf) duke@1: writeCompoundAttribute(a); duke@1: } duke@1: endAttr(attrIndex); duke@1: attrCount++; duke@1: } duke@1: return attrCount; duke@1: } duke@1: duke@1: /********************************************************************** duke@1: * Writing Java-language annotations (aka metadata, attributes) duke@1: **********************************************************************/ duke@1: duke@1: /** Write Java-language annotations; return number of JVM duke@1: * attributes written (zero or one). duke@1: */ duke@1: int writeJavaAnnotations(List attrs) { duke@1: if (attrs.isEmpty()) return 0; duke@1: ListBuffer visibles = new ListBuffer(); duke@1: ListBuffer invisibles = new ListBuffer(); duke@1: for (Attribute.Compound a : attrs) { jjg@657: switch (types.getRetention(a)) { duke@1: case SOURCE: break; duke@1: case CLASS: invisibles.append(a); break; duke@1: case RUNTIME: visibles.append(a); break; duke@1: default: ;// /* fail soft */ throw new AssertionError(vis); duke@1: } duke@1: } duke@1: duke@1: int attrCount = 0; duke@1: if (visibles.length() != 0) { duke@1: int attrIndex = writeAttr(names.RuntimeVisibleAnnotations); duke@1: databuf.appendChar(visibles.length()); duke@1: for (Attribute.Compound a : visibles) duke@1: writeCompoundAttribute(a); duke@1: endAttr(attrIndex); duke@1: attrCount++; duke@1: } duke@1: if (invisibles.length() != 0) { duke@1: int attrIndex = writeAttr(names.RuntimeInvisibleAnnotations); duke@1: databuf.appendChar(invisibles.length()); duke@1: for (Attribute.Compound a : invisibles) duke@1: writeCompoundAttribute(a); duke@1: endAttr(attrIndex); duke@1: attrCount++; duke@1: } duke@1: return attrCount; duke@1: } duke@1: jjg@1521: int writeTypeAnnotations(List typeAnnos) { jjg@1521: if (typeAnnos.isEmpty()) return 0; jjg@1521: jjg@1521: ListBuffer visibles = ListBuffer.lb(); jjg@1521: ListBuffer invisibles = ListBuffer.lb(); jjg@1521: jjg@1521: for (Attribute.TypeCompound tc : typeAnnos) { jjg@1521: if (tc.position == null || tc.position.type == TargetType.UNKNOWN) { jjg@1521: boolean found = false; jjg@1521: // TODO: the position for the container annotation of a jjg@1521: // repeating type annotation has to be set. jjg@1521: // This cannot be done when the container is created, because jjg@1521: // then the position is not determined yet. jjg@1521: // How can we link these pieces better together? jjg@1521: if (tc.values.size() == 1) { jjg@1521: Pair val = tc.values.get(0); jjg@1521: if (val.fst.getSimpleName().contentEquals("value") && jjg@1521: val.snd instanceof Attribute.Array) { jjg@1521: Attribute.Array arr = (Attribute.Array) val.snd; jjg@1521: if (arr.values.length != 0 && jjg@1521: arr.values[0] instanceof Attribute.TypeCompound) { jjg@1521: TypeCompound atycomp = (Attribute.TypeCompound) arr.values[0]; jjg@1521: if (atycomp.position.type != TargetType.UNKNOWN) { jjg@1521: tc.position = atycomp.position; jjg@1521: found = true; jjg@1521: } jjg@1521: } jjg@1521: } jjg@1521: } jjg@1521: if (!found) { jjg@1521: // This happens for nested types like @A Outer. @B Inner. jjg@1521: // For method parameters we get the annotation twice! Once with jjg@1521: // a valid position, once unknown. jjg@1521: // TODO: find a cleaner solution. jjg@1521: // System.err.println("ClassWriter: Position UNKNOWN in type annotation: " + tc); jjg@1521: continue; jjg@1521: } jjg@1521: } jjg@1521: if (!tc.position.emitToClassfile()) jjg@1521: continue; jjg@1521: switch (types.getRetention(tc)) { jjg@1521: case SOURCE: break; jjg@1521: case CLASS: invisibles.append(tc); break; jjg@1521: case RUNTIME: visibles.append(tc); break; jjg@1521: default: ;// /* fail soft */ throw new AssertionError(vis); jjg@1521: } jjg@1521: } jjg@1521: jjg@1521: int attrCount = 0; jjg@1521: if (visibles.length() != 0) { jjg@1521: int attrIndex = writeAttr(names.RuntimeVisibleTypeAnnotations); jjg@1521: databuf.appendChar(visibles.length()); jjg@1521: for (Attribute.TypeCompound p : visibles) jjg@1521: writeTypeAnnotation(p); jjg@1521: endAttr(attrIndex); jjg@1521: attrCount++; jjg@1521: } jjg@1521: jjg@1521: if (invisibles.length() != 0) { jjg@1521: int attrIndex = writeAttr(names.RuntimeInvisibleTypeAnnotations); jjg@1521: databuf.appendChar(invisibles.length()); jjg@1521: for (Attribute.TypeCompound p : invisibles) jjg@1521: writeTypeAnnotation(p); jjg@1521: endAttr(attrIndex); jjg@1521: attrCount++; jjg@1521: } jjg@1521: jjg@1521: return attrCount; jjg@1521: } jjg@1521: duke@1: /** A visitor to write an attribute including its leading duke@1: * single-character marker. duke@1: */ duke@1: class AttributeWriter implements Attribute.Visitor { duke@1: public void visitConstant(Attribute.Constant _value) { duke@1: Object value = _value.value; jjg@1374: switch (_value.type.getTag()) { duke@1: case BYTE: duke@1: databuf.appendByte('B'); duke@1: break; duke@1: case CHAR: duke@1: databuf.appendByte('C'); duke@1: break; duke@1: case SHORT: duke@1: databuf.appendByte('S'); duke@1: break; duke@1: case INT: duke@1: databuf.appendByte('I'); duke@1: break; duke@1: case LONG: duke@1: databuf.appendByte('J'); duke@1: break; duke@1: case FLOAT: duke@1: databuf.appendByte('F'); duke@1: break; duke@1: case DOUBLE: duke@1: databuf.appendByte('D'); duke@1: break; duke@1: case BOOLEAN: duke@1: databuf.appendByte('Z'); duke@1: break; duke@1: case CLASS: jjg@816: Assert.check(value instanceof String); duke@1: databuf.appendByte('s'); duke@1: value = names.fromString(value.toString()); // CONSTANT_Utf8 duke@1: break; duke@1: default: duke@1: throw new AssertionError(_value.type); duke@1: } duke@1: databuf.appendChar(pool.put(value)); duke@1: } duke@1: public void visitEnum(Attribute.Enum e) { duke@1: databuf.appendByte('e'); duke@1: databuf.appendChar(pool.put(typeSig(e.value.type))); duke@1: databuf.appendChar(pool.put(e.value.name)); duke@1: } duke@1: public void visitClass(Attribute.Class clazz) { duke@1: databuf.appendByte('c'); jfranck@1313: databuf.appendChar(pool.put(typeSig(clazz.classType))); duke@1: } duke@1: public void visitCompound(Attribute.Compound compound) { duke@1: databuf.appendByte('@'); duke@1: writeCompoundAttribute(compound); duke@1: } duke@1: public void visitError(Attribute.Error x) { duke@1: throw new AssertionError(x); duke@1: } duke@1: public void visitArray(Attribute.Array array) { duke@1: databuf.appendByte('['); duke@1: databuf.appendChar(array.values.length); duke@1: for (Attribute a : array.values) { duke@1: a.accept(this); duke@1: } duke@1: } duke@1: } duke@1: AttributeWriter awriter = new AttributeWriter(); duke@1: duke@1: /** Write a compound attribute excluding the '@' marker. */ duke@1: void writeCompoundAttribute(Attribute.Compound c) { duke@1: databuf.appendChar(pool.put(typeSig(c.type))); duke@1: databuf.appendChar(c.values.length()); duke@1: for (Pair p : c.values) { duke@1: databuf.appendChar(pool.put(p.fst.name)); duke@1: p.snd.accept(awriter); duke@1: } duke@1: } jjg@1521: jjg@1521: void writeTypeAnnotation(Attribute.TypeCompound c) { jjg@1521: writePosition(c.position); jjg@1521: writeCompoundAttribute(c); jjg@1521: } jjg@1521: jjg@1521: void writePosition(TypeAnnotationPosition p) { jjg@1521: databuf.appendByte(p.type.targetTypeValue()); // TargetType tag is a byte jjg@1521: switch (p.type) { jjg@1521: // instanceof jjg@1521: case INSTANCEOF: jjg@1521: // new expression jjg@1521: case NEW: jjg@1563: // constructor/method reference receiver jjg@1563: case CONSTRUCTOR_REFERENCE: jjg@1563: case METHOD_REFERENCE: jjg@1521: databuf.appendChar(p.offset); jjg@1521: break; jjg@1521: // local variable jjg@1521: case LOCAL_VARIABLE: jjg@1521: // resource variable jjg@1521: case RESOURCE_VARIABLE: jjg@1521: databuf.appendChar(p.lvarOffset.length); // for table length jjg@1521: for (int i = 0; i < p.lvarOffset.length; ++i) { jjg@1521: databuf.appendChar(p.lvarOffset[i]); jjg@1521: databuf.appendChar(p.lvarLength[i]); jjg@1521: databuf.appendChar(p.lvarIndex[i]); jjg@1521: } jjg@1521: break; jjg@1521: // exception parameter jjg@1521: case EXCEPTION_PARAMETER: jjg@1521: databuf.appendByte(p.exception_index); jjg@1521: break; jjg@1521: // method receiver jjg@1521: case METHOD_RECEIVER: jjg@1521: // Do nothing jjg@1521: break; jjg@1521: // type parameter jjg@1521: case CLASS_TYPE_PARAMETER: jjg@1521: case METHOD_TYPE_PARAMETER: jjg@1521: databuf.appendByte(p.parameter_index); jjg@1521: break; jjg@1521: // type parameter bound jjg@1521: case CLASS_TYPE_PARAMETER_BOUND: jjg@1521: case METHOD_TYPE_PARAMETER_BOUND: jjg@1521: databuf.appendByte(p.parameter_index); jjg@1521: databuf.appendByte(p.bound_index); jjg@1521: break; jjg@1521: // class extends or implements clause jjg@1521: case CLASS_EXTENDS: jjg@1521: databuf.appendChar(p.type_index); jjg@1521: break; jjg@1521: // throws jjg@1521: case THROWS: jjg@1521: databuf.appendChar(p.type_index); jjg@1521: break; jjg@1521: // method parameter jjg@1521: case METHOD_FORMAL_PARAMETER: jjg@1521: databuf.appendByte(p.parameter_index); jjg@1521: break; jjg@1563: // type cast jjg@1563: case CAST: jjg@1521: // method/constructor/reference type argument jjg@1521: case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT: jjg@1521: case METHOD_INVOCATION_TYPE_ARGUMENT: jjg@1563: case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT: jjg@1521: case METHOD_REFERENCE_TYPE_ARGUMENT: jjg@1521: databuf.appendChar(p.offset); jjg@1521: databuf.appendByte(p.type_index); jjg@1521: break; jjg@1521: // We don't need to worry about these jjg@1521: case METHOD_RETURN: jjg@1521: case FIELD: jjg@1521: break; jjg@1521: case UNKNOWN: jjg@1521: throw new AssertionError("jvm.ClassWriter: UNKNOWN target type should never occur!"); jjg@1521: default: jjg@1521: throw new AssertionError("jvm.ClassWriter: Unknown target type for position: " + p); jjg@1521: } jjg@1521: jjg@1521: { // Append location data for generics/arrays. jjg@1521: databuf.appendByte(p.location.size()); jjg@1521: java.util.List loc = TypeAnnotationPosition.getBinaryFromTypePath(p.location); jjg@1521: for (int i : loc) jjg@1521: databuf.appendByte((byte)i); jjg@1521: } jjg@1521: } jjg@1521: duke@1: /********************************************************************** duke@1: * Writing Objects duke@1: **********************************************************************/ duke@1: duke@1: /** Enter an inner class into the `innerClasses' set/queue. duke@1: */ duke@1: void enterInner(ClassSymbol c) { mcimadamore@747: if (c.type.isCompound()) { mcimadamore@747: throw new AssertionError("Unexpected intersection type: " + c.type); mcimadamore@747: } duke@1: try { duke@1: c.complete(); duke@1: } catch (CompletionFailure ex) { duke@1: System.err.println("error: " + c + ": " + ex.getMessage()); duke@1: throw ex; duke@1: } jjg@1374: if (!c.type.hasTag(CLASS)) return; // arrays duke@1: if (pool != null && // pool might be null if called from xClassName mcimadamore@1086: c.owner.enclClass() != null && duke@1: (innerClasses == null || !innerClasses.contains(c))) { duke@1: // log.errWriter.println("enter inner " + c);//DEBUG mcimadamore@1086: enterInner(c.owner.enclClass()); duke@1: pool.put(c); duke@1: pool.put(c.name); duke@1: if (innerClasses == null) { duke@1: innerClasses = new HashSet(); duke@1: innerClassesQueue = new ListBuffer(); duke@1: pool.put(names.InnerClasses); duke@1: } duke@1: innerClasses.add(c); duke@1: innerClassesQueue.append(c); duke@1: } duke@1: } duke@1: duke@1: /** Write "inner classes" attribute. duke@1: */ duke@1: void writeInnerClasses() { duke@1: int alenIdx = writeAttr(names.InnerClasses); duke@1: databuf.appendChar(innerClassesQueue.length()); duke@1: for (List l = innerClassesQueue.toList(); duke@1: l.nonEmpty(); duke@1: l = l.tail) { duke@1: ClassSymbol inner = l.head; duke@1: char flags = (char) adjustFlags(inner.flags_field); duke@1: if ((flags & INTERFACE) != 0) flags |= ABSTRACT; // Interfaces are always ABSTRACT duke@1: if (inner.name.isEmpty()) flags &= ~FINAL; // Anonymous class: unset FINAL flag duke@1: if (dumpInnerClassModifiers) { jjg@1135: PrintWriter pw = log.getWriter(Log.WriterKind.ERROR); jjg@1135: pw.println("INNERCLASS " + inner.name); jjg@1135: pw.println("---" + flagNames(flags)); duke@1: } duke@1: databuf.appendChar(pool.get(inner)); duke@1: databuf.appendChar( duke@1: inner.owner.kind == TYP ? pool.get(inner.owner) : 0); duke@1: databuf.appendChar( jjg@113: !inner.name.isEmpty() ? pool.get(inner.name) : 0); duke@1: databuf.appendChar(flags); duke@1: } duke@1: endAttr(alenIdx); duke@1: } duke@1: mcimadamore@1336: /** Write "bootstrapMethods" attribute. mcimadamore@1336: */ mcimadamore@1336: void writeBootstrapMethods() { mcimadamore@1336: int alenIdx = writeAttr(names.BootstrapMethods); mcimadamore@1336: databuf.appendChar(bootstrapMethods.size()); vromero@1452: for (Map.Entry entry : bootstrapMethods.entrySet()) { vromero@1452: DynamicMethod dmeth = entry.getKey(); vromero@1452: DynamicMethodSymbol dsym = (DynamicMethodSymbol)dmeth.baseSymbol(); mcimadamore@1336: //write BSM handle mcimadamore@1336: databuf.appendChar(pool.get(entry.getValue())); mcimadamore@1336: //write static args length mcimadamore@1336: databuf.appendChar(dsym.staticArgs.length); mcimadamore@1336: //write static args array vromero@1452: Object[] uniqueArgs = dmeth.uniqueStaticArgs; vromero@1452: for (Object o : uniqueArgs) { mcimadamore@1336: databuf.appendChar(pool.get(o)); mcimadamore@1336: } mcimadamore@1336: } mcimadamore@1336: endAttr(alenIdx); mcimadamore@1336: } mcimadamore@1336: duke@1: /** Write field symbol, entering all references into constant pool. duke@1: */ duke@1: void writeField(VarSymbol v) { duke@1: int flags = adjustFlags(v.flags()); duke@1: databuf.appendChar(flags); duke@1: if (dumpFieldModifiers) { jjg@1135: PrintWriter pw = log.getWriter(Log.WriterKind.ERROR); jjg@1135: pw.println("FIELD " + fieldName(v)); jjg@1135: pw.println("---" + flagNames(v.flags())); duke@1: } duke@1: databuf.appendChar(pool.put(fieldName(v))); duke@1: databuf.appendChar(pool.put(typeSig(v.erasure(types)))); duke@1: int acountIdx = beginAttrs(); duke@1: int acount = 0; duke@1: if (v.getConstValue() != null) { duke@1: int alenIdx = writeAttr(names.ConstantValue); duke@1: databuf.appendChar(pool.put(v.getConstValue())); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: acount += writeMemberAttrs(v); duke@1: endAttrs(acountIdx, acount); duke@1: } duke@1: duke@1: /** Write method symbol, entering all references into constant pool. duke@1: */ duke@1: void writeMethod(MethodSymbol m) { duke@1: int flags = adjustFlags(m.flags()); duke@1: databuf.appendChar(flags); duke@1: if (dumpMethodModifiers) { jjg@1135: PrintWriter pw = log.getWriter(Log.WriterKind.ERROR); jjg@1135: pw.println("METHOD " + fieldName(m)); jjg@1135: pw.println("---" + flagNames(m.flags())); duke@1: } duke@1: databuf.appendChar(pool.put(fieldName(m))); duke@1: databuf.appendChar(pool.put(typeSig(m.externalType(types)))); duke@1: int acountIdx = beginAttrs(); duke@1: int acount = 0; duke@1: if (m.code != null) { duke@1: int alenIdx = writeAttr(names.Code); duke@1: writeCode(m.code); duke@1: m.code = null; // to conserve space duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: List thrown = m.erasure(types).getThrownTypes(); duke@1: if (thrown.nonEmpty()) { duke@1: int alenIdx = writeAttr(names.Exceptions); duke@1: databuf.appendChar(thrown.length()); duke@1: for (List l = thrown; l.nonEmpty(); l = l.tail) duke@1: databuf.appendChar(pool.put(l.head.tsym)); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: if (m.defaultValue != null) { duke@1: int alenIdx = writeAttr(names.AnnotationDefault); duke@1: m.defaultValue.accept(awriter); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } jjg@1473: if (options.isSet(PARAMETERS)) jjg@1473: acount += writeMethodParametersAttr(m); duke@1: acount += writeMemberAttrs(m); duke@1: acount += writeParameterAttrs(m); duke@1: endAttrs(acountIdx, acount); duke@1: } duke@1: duke@1: /** Write code attribute of method. duke@1: */ duke@1: void writeCode(Code code) { duke@1: databuf.appendChar(code.max_stack); duke@1: databuf.appendChar(code.max_locals); duke@1: databuf.appendInt(code.cp); duke@1: databuf.appendBytes(code.code, 0, code.cp); duke@1: databuf.appendChar(code.catchInfo.length()); duke@1: for (List l = code.catchInfo.toList(); duke@1: l.nonEmpty(); duke@1: l = l.tail) { duke@1: for (int i = 0; i < l.head.length; i++) duke@1: databuf.appendChar(l.head[i]); duke@1: } duke@1: int acountIdx = beginAttrs(); duke@1: int acount = 0; duke@1: duke@1: if (code.lineInfo.nonEmpty()) { duke@1: int alenIdx = writeAttr(names.LineNumberTable); duke@1: databuf.appendChar(code.lineInfo.length()); duke@1: for (List l = code.lineInfo.reverse(); duke@1: l.nonEmpty(); duke@1: l = l.tail) duke@1: for (int i = 0; i < l.head.length; i++) duke@1: databuf.appendChar(l.head[i]); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: duke@1: if (genCrt && (code.crt != null)) { duke@1: CRTable crt = code.crt; duke@1: int alenIdx = writeAttr(names.CharacterRangeTable); duke@1: int crtIdx = beginAttrs(); duke@1: int crtEntries = crt.writeCRT(databuf, code.lineMap, log); duke@1: endAttrs(crtIdx, crtEntries); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: duke@1: // counter for number of generic local variables duke@1: int nGenericVars = 0; duke@1: duke@1: if (code.varBufferSize > 0) { duke@1: int alenIdx = writeAttr(names.LocalVariableTable); duke@1: databuf.appendChar(code.varBufferSize); duke@1: duke@1: for (int i=0; i= 0 jjg@816: && var.start_pc <= code.cp); duke@1: databuf.appendChar(var.start_pc); jjg@816: Assert.check(var.length >= 0 jjg@816: && (var.start_pc + var.length) <= code.cp); duke@1: databuf.appendChar(var.length); duke@1: VarSymbol sym = var.sym; duke@1: databuf.appendChar(pool.put(sym.name)); duke@1: Type vartype = sym.erasure(types); mcimadamore@781: if (needsLocalVariableTypeEntry(sym.type)) duke@1: nGenericVars++; duke@1: databuf.appendChar(pool.put(typeSig(vartype))); duke@1: databuf.appendChar(var.reg); duke@1: } duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: duke@1: if (nGenericVars > 0) { duke@1: int alenIdx = writeAttr(names.LocalVariableTypeTable); duke@1: databuf.appendChar(nGenericVars); duke@1: int count = 0; duke@1: duke@1: for (int i=0; i 0) { duke@1: if (debugstackmap) System.out.println("Stack map for " + code.meth); duke@1: int alenIdx = writeAttr(code.stackMap.getAttributeName(names)); duke@1: writeStackMap(code); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: endAttrs(acountIdx, acount); duke@1: } mcimadamore@781: //where mcimadamore@781: private boolean needsLocalVariableTypeEntry(Type t) { mcimadamore@781: //a local variable needs a type-entry if its type T is generic mcimadamore@781: //(i.e. |T| != T) and if it's not an intersection type (not supported mcimadamore@781: //in signature attribute grammar) mcimadamore@781: return (!types.isSameType(t, types.erasure(t)) && mcimadamore@781: !t.isCompound()); mcimadamore@781: } duke@1: duke@1: void writeStackMap(Code code) { duke@1: int nframes = code.stackMapBufferSize; duke@1: if (debugstackmap) System.out.println(" nframes = " + nframes); duke@1: databuf.appendChar(nframes); duke@1: duke@1: switch (code.stackMap) { duke@1: case CLDC: duke@1: for (int i=0; i(); duke@1: duke@1: Type supertype = types.supertype(c.type); duke@1: List interfaces = types.interfaces(c.type); duke@1: List typarams = c.type.getTypeArguments(); duke@1: mcimadamore@1393: int flags = adjustFlags(c.flags() & ~DEFAULT); duke@1: if ((flags & PROTECTED) != 0) flags |= PUBLIC; duke@1: flags = flags & ClassFlags & ~STRICTFP; duke@1: if ((flags & INTERFACE) == 0) flags |= ACC_SUPER; duke@1: if (c.isInner() && c.name.isEmpty()) flags &= ~FINAL; duke@1: if (dumpClassModifiers) { jjg@1135: PrintWriter pw = log.getWriter(Log.WriterKind.ERROR); jjg@1135: pw.println(); jjg@1135: pw.println("CLASSFILE " + c.getQualifiedName()); jjg@1135: pw.println("---" + flagNames(flags)); duke@1: } duke@1: databuf.appendChar(flags); duke@1: duke@1: databuf.appendChar(pool.put(c)); jjg@1374: databuf.appendChar(supertype.hasTag(CLASS) ? pool.put(supertype.tsym) : 0); duke@1: databuf.appendChar(interfaces.length()); duke@1: for (List l = interfaces; l.nonEmpty(); l = l.tail) duke@1: databuf.appendChar(pool.put(l.head.tsym)); duke@1: int fieldsCount = 0; duke@1: int methodsCount = 0; duke@1: for (Scope.Entry e = c.members().elems; e != null; e = e.sibling) { duke@1: switch (e.sym.kind) { duke@1: case VAR: fieldsCount++; break; duke@1: case MTH: if ((e.sym.flags() & HYPOTHETICAL) == 0) methodsCount++; duke@1: break; duke@1: case TYP: enterInner((ClassSymbol)e.sym); break; jjg@816: default : Assert.error(); duke@1: } duke@1: } mcimadamore@1086: mcimadamore@1086: if (c.trans_local != null) { mcimadamore@1086: for (ClassSymbol local : c.trans_local) { mcimadamore@1086: enterInner(local); mcimadamore@1086: } mcimadamore@1086: } mcimadamore@1086: duke@1: databuf.appendChar(fieldsCount); duke@1: writeFields(c.members().elems); duke@1: databuf.appendChar(methodsCount); duke@1: writeMethods(c.members().elems); duke@1: duke@1: int acountIdx = beginAttrs(); duke@1: int acount = 0; duke@1: duke@1: boolean sigReq = mcimadamore@297: typarams.length() != 0 || supertype.allparams().length() != 0; duke@1: for (List l = interfaces; !sigReq && l.nonEmpty(); l = l.tail) mcimadamore@297: sigReq = l.head.allparams().length() != 0; duke@1: if (sigReq) { jjg@816: Assert.check(source.allowGenerics()); duke@1: int alenIdx = writeAttr(names.Signature); rfield@1587: if (typarams.length() != 0) signatureGen.assembleParamsSig(typarams); rfield@1587: signatureGen.assembleSig(supertype); duke@1: for (List l = interfaces; l.nonEmpty(); l = l.tail) rfield@1587: signatureGen.assembleSig(l.head); rfield@1587: databuf.appendChar(pool.put(signatureGen.toName())); rfield@1587: signatureGen.reset(); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: duke@1: if (c.sourcefile != null && emitSourceFile) { duke@1: int alenIdx = writeAttr(names.SourceFile); duke@1: // WHM 6/29/1999: Strip file path prefix. We do it here at duke@1: // the last possible moment because the sourcefile may be used duke@1: // elsewhere in error diagnostics. Fixes 4241573. duke@1: //databuf.appendChar(c.pool.put(c.sourcefile)); jjg@415: String simpleName = BaseFileObject.getSimpleName(c.sourcefile); jjg@415: databuf.appendChar(c.pool.put(names.fromString(simpleName))); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: duke@1: if (genCrt) { duke@1: // Append SourceID attribute duke@1: int alenIdx = writeAttr(names.SourceID); duke@1: databuf.appendChar(c.pool.put(names.fromString(Long.toString(getLastModified(c.sourcefile))))); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: // Append CompilationID attribute duke@1: alenIdx = writeAttr(names.CompilationID); duke@1: databuf.appendChar(c.pool.put(names.fromString(Long.toString(System.currentTimeMillis())))); duke@1: endAttr(alenIdx); duke@1: acount++; duke@1: } duke@1: duke@1: acount += writeFlagAttrs(c.flags()); jfranck@1464: acount += writeJavaAnnotations(c.getRawAttributes()); jjg@1521: acount += writeTypeAnnotations(c.getRawTypeAttributes()); duke@1: acount += writeEnclosingMethodAttribute(c); ksrini@1309: acount += writeExtraClassAttributes(c); duke@1: duke@1: poolbuf.appendInt(JAVA_MAGIC); duke@1: poolbuf.appendChar(target.minorVersion); duke@1: poolbuf.appendChar(target.majorVersion); duke@1: duke@1: writePool(c.pool); duke@1: duke@1: if (innerClasses != null) { duke@1: writeInnerClasses(); duke@1: acount++; duke@1: } mcimadamore@1336: mcimadamore@1336: if (!bootstrapMethods.isEmpty()) { mcimadamore@1336: writeBootstrapMethods(); mcimadamore@1336: acount++; mcimadamore@1336: } mcimadamore@1336: duke@1: endAttrs(acountIdx, acount); duke@1: duke@1: poolbuf.appendBytes(databuf.elems, 0, databuf.length); duke@1: out.write(poolbuf.elems, 0, poolbuf.length); duke@1: duke@1: pool = c.pool = null; // to conserve space duke@1: } duke@1: ksrini@1309: /**Allows subclasses to write additional class attributes ksrini@1309: * ksrini@1309: * @return the number of attributes written ksrini@1309: */ ksrini@1309: protected int writeExtraClassAttributes(ClassSymbol c) { ksrini@1309: return 0; ksrini@1309: } ksrini@1309: duke@1: int adjustFlags(final long flags) { duke@1: int result = (int)flags; duke@1: if ((flags & SYNTHETIC) != 0 && !target.useSyntheticFlag()) duke@1: result &= ~SYNTHETIC; duke@1: if ((flags & ENUM) != 0 && !target.useEnumFlag()) duke@1: result &= ~ENUM; duke@1: if ((flags & ANNOTATION) != 0 && !target.useAnnotationFlag()) duke@1: result &= ~ANNOTATION; duke@1: duke@1: if ((flags & BRIDGE) != 0 && target.useBridgeFlag()) duke@1: result |= ACC_BRIDGE; duke@1: if ((flags & VARARGS) != 0 && target.useVarargsFlag()) duke@1: result |= ACC_VARARGS; mcimadamore@1393: if ((flags & DEFAULT) != 0) mcimadamore@1393: result &= ~ABSTRACT; duke@1: return result; duke@1: } duke@1: duke@1: long getLastModified(FileObject filename) { duke@1: long mod = 0; duke@1: try { duke@1: mod = filename.getLastModified(); duke@1: } catch (SecurityException e) { duke@1: throw new AssertionError("CRT: couldn't get source file modification date: " + e.getMessage()); duke@1: } duke@1: return mod; duke@1: } duke@1: }