aoqi@0: /* aoqi@0: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.javac.jvm; aoqi@0: aoqi@0: import com.sun.tools.javac.code.Kinds; aoqi@0: import com.sun.tools.javac.code.Symbol; aoqi@0: import com.sun.tools.javac.code.Symbol.*; aoqi@0: import com.sun.tools.javac.code.Type; aoqi@0: import com.sun.tools.javac.code.Types; aoqi@0: import com.sun.tools.javac.code.Types.UniqueType; aoqi@0: aoqi@0: import com.sun.tools.javac.util.ArrayUtils; aoqi@0: import com.sun.tools.javac.util.Assert; aoqi@0: import com.sun.tools.javac.util.Filter; aoqi@0: import com.sun.tools.javac.util.Name; aoqi@0: aoqi@0: import java.util.*; aoqi@0: aoqi@0: /** An internal structure that corresponds to the constant pool of a classfile. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own risk. aoqi@0: * This code and its internal interfaces are subject to change or aoqi@0: * deletion without notice. aoqi@0: */ aoqi@0: public class Pool { aoqi@0: aoqi@0: public static final int MAX_ENTRIES = 0xFFFF; aoqi@0: public static final int MAX_STRING_LENGTH = 0xFFFF; aoqi@0: aoqi@0: /** Index of next constant to be entered. aoqi@0: */ aoqi@0: int pp; aoqi@0: aoqi@0: /** The initial pool buffer. aoqi@0: */ aoqi@0: Object[] pool; aoqi@0: aoqi@0: /** A hashtable containing all constants in the pool. aoqi@0: */ aoqi@0: Map indices; aoqi@0: aoqi@0: Types types; aoqi@0: aoqi@0: /** Construct a pool with given number of elements and element array. aoqi@0: */ aoqi@0: public Pool(int pp, Object[] pool, Types types) { aoqi@0: this.pp = pp; aoqi@0: this.pool = pool; aoqi@0: this.types = types; aoqi@0: this.indices = new HashMap(pool.length); aoqi@0: for (int i = 1; i < pp; i++) { aoqi@0: if (pool[i] != null) indices.put(pool[i], i); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** Construct an empty pool. aoqi@0: */ aoqi@0: public Pool(Types types) { aoqi@0: this(1, new Object[64], types); aoqi@0: } aoqi@0: aoqi@0: /** Return the number of entries in the constant pool. aoqi@0: */ aoqi@0: public int numEntries() { aoqi@0: return pp; aoqi@0: } aoqi@0: aoqi@0: /** Remove everything from this pool. aoqi@0: */ aoqi@0: public void reset() { aoqi@0: pp = 1; aoqi@0: indices.clear(); aoqi@0: } aoqi@0: aoqi@0: /** Place an object in the pool, unless it is already there. aoqi@0: * If object is a symbol also enter its owner unless the owner is a aoqi@0: * package. Return the object's index in the pool. aoqi@0: */ aoqi@0: public int put(Object value) { aoqi@0: value = makePoolValue(value); aoqi@0: // assert !(value instanceof Type.TypeVar); aoqi@0: Integer index = indices.get(value); aoqi@0: if (index == null) { aoqi@0: // System.err.println("put " + value + " " + value.getClass());//DEBUG aoqi@0: index = pp; aoqi@0: indices.put(value, index); aoqi@0: pool = ArrayUtils.ensureCapacity(pool, pp); aoqi@0: pool[pp++] = value; aoqi@0: if (value instanceof Long || value instanceof Double) { aoqi@0: pool = ArrayUtils.ensureCapacity(pool, pp); aoqi@0: pool[pp++] = null; aoqi@0: } aoqi@0: } aoqi@0: return index.intValue(); aoqi@0: } aoqi@0: aoqi@0: Object makePoolValue(Object o) { aoqi@0: if (o instanceof DynamicMethodSymbol) { aoqi@0: return new DynamicMethod((DynamicMethodSymbol)o, types); aoqi@0: } else if (o instanceof MethodSymbol) { aoqi@0: return new Method((MethodSymbol)o, types); aoqi@0: } else if (o instanceof VarSymbol) { aoqi@0: return new Variable((VarSymbol)o, types); aoqi@0: } else if (o instanceof Type) { aoqi@0: return new UniqueType((Type)o, types); aoqi@0: } else { aoqi@0: return o; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** Return the given object's index in the pool, aoqi@0: * or -1 if object is not in there. aoqi@0: */ aoqi@0: public int get(Object o) { aoqi@0: Integer n = indices.get(o); aoqi@0: return n == null ? -1 : n.intValue(); aoqi@0: } aoqi@0: aoqi@0: static class Method extends DelegatedSymbol { aoqi@0: UniqueType uniqueType; aoqi@0: Method(MethodSymbol m, Types types) { aoqi@0: super(m); aoqi@0: this.uniqueType = new UniqueType(m.type, types); aoqi@0: } aoqi@0: public boolean equals(Object any) { aoqi@0: if (!(any instanceof Method)) return false; aoqi@0: MethodSymbol o = ((Method)any).other; aoqi@0: MethodSymbol m = this.other; aoqi@0: return aoqi@0: o.name == m.name && aoqi@0: o.owner == m.owner && aoqi@0: ((Method)any).uniqueType.equals(uniqueType); aoqi@0: } aoqi@0: public int hashCode() { aoqi@0: MethodSymbol m = this.other; aoqi@0: return aoqi@0: m.name.hashCode() * 33 + aoqi@0: m.owner.hashCode() * 9 + aoqi@0: uniqueType.hashCode(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static class DynamicMethod extends Method { aoqi@0: public Object[] uniqueStaticArgs; aoqi@0: aoqi@0: DynamicMethod(DynamicMethodSymbol m, Types types) { aoqi@0: super(m, types); aoqi@0: uniqueStaticArgs = getUniqueTypeArray(m.staticArgs, types); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public boolean equals(Object any) { aoqi@0: if (!super.equals(any)) return false; aoqi@0: if (!(any instanceof DynamicMethod)) return false; aoqi@0: DynamicMethodSymbol dm1 = (DynamicMethodSymbol)other; aoqi@0: DynamicMethodSymbol dm2 = (DynamicMethodSymbol)((DynamicMethod)any).other; aoqi@0: return dm1.bsm == dm2.bsm && aoqi@0: dm1.bsmKind == dm2.bsmKind && aoqi@0: Arrays.equals(uniqueStaticArgs, aoqi@0: ((DynamicMethod)any).uniqueStaticArgs); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public int hashCode() { aoqi@0: int hash = super.hashCode(); aoqi@0: DynamicMethodSymbol dm = (DynamicMethodSymbol)other; aoqi@0: hash += dm.bsmKind * 7 + aoqi@0: dm.bsm.hashCode() * 11; aoqi@0: for (int i = 0; i < dm.staticArgs.length; i++) { aoqi@0: hash += (uniqueStaticArgs[i].hashCode() * 23); aoqi@0: } aoqi@0: return hash; aoqi@0: } aoqi@0: aoqi@0: private Object[] getUniqueTypeArray(Object[] objects, Types types) { aoqi@0: Object[] result = new Object[objects.length]; aoqi@0: for (int i = 0; i < objects.length; i++) { aoqi@0: if (objects[i] instanceof Type) { aoqi@0: result[i] = new UniqueType((Type)objects[i], types); aoqi@0: } else { aoqi@0: result[i] = objects[i]; aoqi@0: } aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static class Variable extends DelegatedSymbol { aoqi@0: UniqueType uniqueType; aoqi@0: Variable(VarSymbol v, Types types) { aoqi@0: super(v); aoqi@0: this.uniqueType = new UniqueType(v.type, types); aoqi@0: } aoqi@0: public boolean equals(Object any) { aoqi@0: if (!(any instanceof Variable)) return false; aoqi@0: VarSymbol o = ((Variable)any).other; aoqi@0: VarSymbol v = other; aoqi@0: return aoqi@0: o.name == v.name && aoqi@0: o.owner == v.owner && aoqi@0: ((Variable)any).uniqueType.equals(uniqueType); aoqi@0: } aoqi@0: public int hashCode() { aoqi@0: VarSymbol v = other; aoqi@0: return aoqi@0: v.name.hashCode() * 33 + aoqi@0: v.owner.hashCode() * 9 + aoqi@0: uniqueType.hashCode(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static class MethodHandle { aoqi@0: aoqi@0: /** Reference kind - see ClassFile */ aoqi@0: int refKind; aoqi@0: aoqi@0: /** Reference symbol */ aoqi@0: Symbol refSym; aoqi@0: aoqi@0: UniqueType uniqueType; aoqi@0: aoqi@0: public MethodHandle(int refKind, Symbol refSym, Types types) { aoqi@0: this.refKind = refKind; aoqi@0: this.refSym = refSym; aoqi@0: this.uniqueType = new UniqueType(this.refSym.type, types); aoqi@0: checkConsistent(); aoqi@0: } aoqi@0: public boolean equals(Object other) { aoqi@0: if (!(other instanceof MethodHandle)) return false; aoqi@0: MethodHandle mr = (MethodHandle) other; aoqi@0: if (mr.refKind != refKind) return false; aoqi@0: Symbol o = mr.refSym; aoqi@0: return aoqi@0: o.name == refSym.name && aoqi@0: o.owner == refSym.owner && aoqi@0: ((MethodHandle)other).uniqueType.equals(uniqueType); aoqi@0: } aoqi@0: public int hashCode() { aoqi@0: return aoqi@0: refKind * 65 + aoqi@0: refSym.name.hashCode() * 33 + aoqi@0: refSym.owner.hashCode() * 9 + aoqi@0: uniqueType.hashCode(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Check consistency of reference kind and symbol (see JVMS 4.4.8) aoqi@0: */ aoqi@0: @SuppressWarnings("fallthrough") aoqi@0: private void checkConsistent() { aoqi@0: boolean staticOk = false; aoqi@0: int expectedKind = -1; aoqi@0: Filter nameFilter = nonInitFilter; aoqi@0: boolean interfaceOwner = false; aoqi@0: switch (refKind) { aoqi@0: case ClassFile.REF_getStatic: aoqi@0: case ClassFile.REF_putStatic: aoqi@0: staticOk = true; aoqi@0: case ClassFile.REF_getField: aoqi@0: case ClassFile.REF_putField: aoqi@0: expectedKind = Kinds.VAR; aoqi@0: break; aoqi@0: case ClassFile.REF_newInvokeSpecial: aoqi@0: nameFilter = initFilter; aoqi@0: expectedKind = Kinds.MTH; aoqi@0: break; aoqi@0: case ClassFile.REF_invokeInterface: aoqi@0: interfaceOwner = true; aoqi@0: expectedKind = Kinds.MTH; aoqi@0: break; aoqi@0: case ClassFile.REF_invokeStatic: aoqi@0: interfaceOwner = true; aoqi@0: staticOk = true; aoqi@0: case ClassFile.REF_invokeVirtual: aoqi@0: expectedKind = Kinds.MTH; aoqi@0: break; aoqi@0: case ClassFile.REF_invokeSpecial: aoqi@0: interfaceOwner = true; aoqi@0: expectedKind = Kinds.MTH; aoqi@0: break; aoqi@0: } aoqi@0: Assert.check(!refSym.isStatic() || staticOk); aoqi@0: Assert.check(refSym.kind == expectedKind); aoqi@0: Assert.check(nameFilter.accepts(refSym.name)); aoqi@0: Assert.check(!refSym.owner.isInterface() || interfaceOwner); aoqi@0: } aoqi@0: //where aoqi@0: Filter nonInitFilter = new Filter() { aoqi@0: public boolean accepts(Name n) { aoqi@0: return n != n.table.names.init && n != n.table.names.clinit; aoqi@0: } aoqi@0: }; aoqi@0: aoqi@0: Filter initFilter = new Filter() { aoqi@0: public boolean accepts(Name n) { aoqi@0: return n == n.table.names.init; aoqi@0: } aoqi@0: }; aoqi@0: } aoqi@0: }