duke@1: /*
jjg@1230: * Copyright (c) 1999, 2012, 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:
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;
duke@1: import com.sun.tools.javac.code.Symbol.*;
duke@1: import com.sun.tools.javac.code.Type.*;
jjg@415: import com.sun.tools.javac.file.BaseFileObject;
duke@1: import com.sun.tools.javac.util.*;
duke@1:
duke@1: import static com.sun.tools.javac.code.BoundKind.*;
duke@1: import static com.sun.tools.javac.code.Flags.*;
duke@1: import static com.sun.tools.javac.code.Kinds.*;
duke@1: import static com.sun.tools.javac.code.TypeTags.*;
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 Symtab syms;
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@1230: /** Switch: scramble private names.
duke@1: */
duke@1: private boolean scramble;
duke@1:
jjg@1230: /** Switch: scramble private 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:
duke@1: /** 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.
duke@1: * 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: /** An output buffer for type signatures.
duke@1: */
duke@1: ByteBuffer sigbuf = new ByteBuffer();
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: */
mcimadamore@1336: 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:
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: syms = Symtab.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);
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:
duke@1: /******************************************************************
duke@1: * Signature Generation
duke@1: ******************************************************************/
duke@1:
duke@1: /** Assemble signature of given type in string buffer.
duke@1: */
duke@1: void assembleSig(Type type) {
duke@1: switch (type.tag) {
duke@1: case BYTE:
duke@1: sigbuf.appendByte('B');
duke@1: break;
duke@1: case SHORT:
duke@1: sigbuf.appendByte('S');
duke@1: break;
duke@1: case CHAR:
duke@1: sigbuf.appendByte('C');
duke@1: break;
duke@1: case INT:
duke@1: sigbuf.appendByte('I');
duke@1: break;
duke@1: case LONG:
duke@1: sigbuf.appendByte('J');
duke@1: break;
duke@1: case FLOAT:
duke@1: sigbuf.appendByte('F');
duke@1: break;
duke@1: case DOUBLE:
duke@1: sigbuf.appendByte('D');
duke@1: break;
duke@1: case BOOLEAN:
duke@1: sigbuf.appendByte('Z');
duke@1: break;
duke@1: case VOID:
duke@1: sigbuf.appendByte('V');
duke@1: break;
duke@1: case CLASS:
duke@1: sigbuf.appendByte('L');
duke@1: assembleClassSig(type);
duke@1: sigbuf.appendByte(';');
duke@1: break;
duke@1: case ARRAY:
duke@1: ArrayType at = (ArrayType)type;
duke@1: sigbuf.appendByte('[');
duke@1: assembleSig(at.elemtype);
duke@1: break;
duke@1: case METHOD:
duke@1: MethodType mt = (MethodType)type;
duke@1: sigbuf.appendByte('(');
duke@1: assembleSig(mt.argtypes);
duke@1: sigbuf.appendByte(')');
duke@1: assembleSig(mt.restype);
duke@1: if (hasTypeVar(mt.thrown)) {
duke@1: for (List l = mt.thrown; l.nonEmpty(); l = l.tail) {
duke@1: sigbuf.appendByte('^');
duke@1: assembleSig(l.head);
duke@1: }
duke@1: }
duke@1: break;
duke@1: case WILDCARD: {
duke@1: WildcardType ta = (WildcardType) type;
duke@1: switch (ta.kind) {
duke@1: case SUPER:
duke@1: sigbuf.appendByte('-');
duke@1: assembleSig(ta.type);
duke@1: break;
duke@1: case EXTENDS:
duke@1: sigbuf.appendByte('+');
duke@1: assembleSig(ta.type);
duke@1: break;
duke@1: case UNBOUND:
duke@1: sigbuf.appendByte('*');
duke@1: break;
duke@1: default:
duke@1: throw new AssertionError(ta.kind);
duke@1: }
duke@1: break;
duke@1: }
duke@1: case TYPEVAR:
duke@1: sigbuf.appendByte('T');
duke@1: sigbuf.appendName(type.tsym.name);
duke@1: sigbuf.appendByte(';');
duke@1: break;
duke@1: case FORALL:
duke@1: ForAll ft = (ForAll)type;
duke@1: assembleParamsSig(ft.tvars);
duke@1: assembleSig(ft.qtype);
duke@1: break;
duke@1: case UNINITIALIZED_THIS:
duke@1: case UNINITIALIZED_OBJECT:
duke@1: // we don't yet have a spec for uninitialized types in the
duke@1: // local variable table
duke@1: assembleSig(types.erasure(((UninitializedType)type).qtype));
duke@1: break;
duke@1: default:
duke@1: throw new AssertionError("typeSig " + type.tag);
duke@1: }
duke@1: }
duke@1:
duke@1: boolean hasTypeVar(List l) {
duke@1: while (l.nonEmpty()) {
duke@1: if (l.head.tag == TypeTags.TYPEVAR) return true;
duke@1: l = l.tail;
duke@1: }
duke@1: return false;
duke@1: }
duke@1:
duke@1: void assembleClassSig(Type type) {
duke@1: ClassType ct = (ClassType)type;
duke@1: ClassSymbol c = (ClassSymbol)ct.tsym;
duke@1: enterInner(c);
duke@1: Type outer = ct.getEnclosingType();
duke@1: if (outer.allparams().nonEmpty()) {
duke@1: boolean rawOuter =
duke@1: c.owner.kind == MTH || // either a local class
duke@1: c.name == names.empty; // or anonymous
duke@1: assembleClassSig(rawOuter
duke@1: ? types.erasure(outer)
duke@1: : outer);
duke@1: sigbuf.appendByte('.');
jjg@816: Assert.check(c.flatname.startsWith(c.owner.enclClass().flatname));
duke@1: sigbuf.appendName(rawOuter
jjg@113: ? c.flatname.subName(c.owner.enclClass().flatname.getByteLength()+1,c.flatname.getByteLength())
duke@1: : c.name);
duke@1: } else {
duke@1: sigbuf.appendBytes(externalize(c.flatname));
duke@1: }
duke@1: if (ct.getTypeArguments().nonEmpty()) {
duke@1: sigbuf.appendByte('<');
duke@1: assembleSig(ct.getTypeArguments());
duke@1: sigbuf.appendByte('>');
duke@1: }
duke@1: }
duke@1:
duke@1:
duke@1: void assembleSig(List types) {
duke@1: for (List ts = types; ts.nonEmpty(); ts = ts.tail)
duke@1: assembleSig(ts.head);
duke@1: }
duke@1:
duke@1: void assembleParamsSig(List typarams) {
duke@1: sigbuf.appendByte('<');
duke@1: for (List ts = typarams; ts.nonEmpty(); ts = ts.tail) {
duke@1: TypeVar tvar = (TypeVar)ts.head;
duke@1: sigbuf.appendName(tvar.tsym.name);
duke@1: List bounds = types.getBounds(tvar);
duke@1: if ((bounds.head.tsym.flags() & INTERFACE) != 0) {
duke@1: sigbuf.appendByte(':');
duke@1: }
duke@1: for (List l = bounds; l.nonEmpty(); l = l.tail) {
duke@1: sigbuf.appendByte(':');
duke@1: assembleSig(l.head);
duke@1: }
duke@1: }
duke@1: sigbuf.appendByte('>');
duke@1: }
duke@1:
duke@1: /** Return signature of given type
duke@1: */
duke@1: Name typeSig(Type type) {
jjg@816: Assert.check(sigbuf.length == 0);
duke@1: //- System.out.println(" ? " + type);
duke@1: assembleSig(type);
duke@1: Name n = sigbuf.toName(names);
duke@1: sigbuf.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) {
duke@1: if (t.tag == CLASS) {
duke@1: return names.fromUtf(externalize(t.tsym.flatName()));
duke@1: } else if (t.tag == 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);
duke@1: if (value instanceof Pool.Method)
duke@1: value = ((Pool.Method)value).m;
duke@1: else if (value instanceof Pool.Variable)
duke@1: value = ((Pool.Variable)value).v;
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;
mcimadamore@1343: Pool.MethodHandle handle = new Pool.MethodHandle(dynSym.bsmKind, dynSym.bsm);
mcimadamore@1336: bootstrapMethods.put(dynSym, 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);
duke@1: if (c.type.tag == 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));
duke@1: poolbuf.appendChar(pool.put(typeSig(nt.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)));
mcimadamore@1336: } else if (value instanceof MethodType) {
mcimadamore@1336: MethodType mtype = (MethodType)value;
mcimadamore@1336: poolbuf.appendByte(CONSTANT_MethodType);
mcimadamore@1336: poolbuf.appendChar(pool.put(typeSig(mtype)));
duke@1: } else if (value instanceof Type) {
duke@1: Type type = (Type)value;
duke@1: if (type.tag == CLASS) enterInner((ClassSymbol)type.tsym);
duke@1: poolbuf.appendByte(CONSTANT_Class);
duke@1: poolbuf.appendChar(pool.put(xClassName(type)));
mcimadamore@1336: } else if (value instanceof Pool.MethodHandle) {
mcimadamore@1336: Pool.MethodHandle ref = (Pool.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)
duke@1: : sym.externalType(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)) ||
duke@1: 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: }
duke@1: acount += writeJavaAnnotations(sym.getAnnotationMirrors());
duke@1: return acount;
duke@1: }
duke@1:
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) {
duke@1: for (Attribute.Compound a : s.getAnnotationMirrors()) {
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();
duke@1: for (Attribute.Compound a : s.getAnnotationMirrors())
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();
duke@1: for (Attribute.Compound a : s.getAnnotationMirrors())
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:
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;
duke@1: switch (_value.type.tag) {
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: }
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: }
duke@1: if (c.type.tag != 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());
mcimadamore@1336: for (Map.Entry entry : bootstrapMethods.entrySet()) {
mcimadamore@1336: DynamicMethodSymbol dsym = (DynamicMethodSymbol)entry.getKey();
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
mcimadamore@1336: for (Object o : dsym.staticArgs) {
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: }
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 MAX_LOCAL_LENGTH_DIFF || diff_length < -MAX_LOCAL_LENGTH_DIFF) {
duke@1: return Integer.MAX_VALUE;
duke@1: }
duke@1: int len = (diff_length > 0) ? arr2.length : arr1.length;
duke@1: for (int i=0; i vars = List.nil();
duke@1: for (Scope.Entry i = e; i != null; i = i.sibling) {
duke@1: if (i.sym.kind == VAR) vars = vars.prepend((VarSymbol)i.sym);
duke@1: }
duke@1: while (vars.nonEmpty()) {
duke@1: writeField(vars.head);
duke@1: vars = vars.tail;
duke@1: }
duke@1: }
duke@1:
duke@1: void writeMethods(Scope.Entry e) {
duke@1: List methods = List.nil();
duke@1: for (Scope.Entry i = e; i != null; i = i.sibling) {
duke@1: if (i.sym.kind == MTH && (i.sym.flags() & HYPOTHETICAL) == 0)
duke@1: methods = methods.prepend((MethodSymbol)i.sym);
duke@1: }
duke@1: while (methods.nonEmpty()) {
duke@1: writeMethod(methods.head);
duke@1: methods = methods.tail;
duke@1: }
duke@1: }
duke@1:
duke@1: /** Emit a class file for a given class.
duke@1: * @param c The class from which a class file is generated.
duke@1: */
duke@1: public JavaFileObject writeClass(ClassSymbol c)
duke@1: throws IOException, PoolOverflow, StringOverflow
duke@1: {
duke@1: JavaFileObject outFile
duke@1: = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
duke@1: c.flatname.toString(),
duke@1: JavaFileObject.Kind.CLASS,
duke@1: c.sourcefile);
duke@1: OutputStream out = outFile.openOutputStream();
duke@1: try {
duke@1: writeClassFile(out, c);
duke@1: if (verbose)
jjg@909: log.printVerbose("wrote.file", outFile);
duke@1: out.close();
duke@1: out = null;
duke@1: } finally {
duke@1: if (out != null) {
duke@1: // if we are propogating an exception, delete the file
duke@1: out.close();
duke@1: outFile.delete();
duke@1: outFile = null;
duke@1: }
duke@1: }
duke@1: return outFile; // may be null if write failed
duke@1: }
duke@1:
duke@1: /** Write class `c' to outstream `out'.
duke@1: */
duke@1: public void writeClassFile(OutputStream out, ClassSymbol c)
duke@1: throws IOException, PoolOverflow, StringOverflow {
jjg@816: Assert.check((c.flags() & COMPOUND) == 0);
duke@1: databuf.reset();
duke@1: poolbuf.reset();
duke@1: sigbuf.reset();
duke@1: pool = c.pool;
duke@1: innerClasses = null;
duke@1: innerClassesQueue = null;
mcimadamore@1336: bootstrapMethods = new LinkedHashMap();
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:
duke@1: int flags = adjustFlags(c.flags());
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));
duke@1: databuf.appendChar(supertype.tag == 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);
duke@1: if (typarams.length() != 0) assembleParamsSig(typarams);
duke@1: assembleSig(supertype);
duke@1: for (List l = interfaces; l.nonEmpty(); l = l.tail)
duke@1: assembleSig(l.head);
duke@1: databuf.appendChar(pool.put(sigbuf.toName(names)));
duke@1: sigbuf.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());
duke@1: acount += writeJavaAnnotations(c.getAnnotationMirrors());
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;
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: }