src/share/classes/com/sun/tools/javac/jvm/Pool.java

Fri, 30 Nov 2012 15:14:36 +0000

author
mcimadamore
date
Fri, 30 Nov 2012 15:14:36 +0000
changeset 1435
9b26c96f5138
parent 1415
01c9d4161882
child 1452
de1ec6fc93fe
permissions
-rw-r--r--

8004101: Add checks for method reference well-formedness
Summary: Bring method reference type-checking in sync with latest EDR
Reviewed-by: jjg

duke@1 1 /*
jjg@1339 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.jvm;
duke@1 27
mcimadamore@1336 28 import com.sun.tools.javac.code.Kinds;
mcimadamore@1336 29 import com.sun.tools.javac.code.Symbol;
duke@1 30 import com.sun.tools.javac.code.Symbol.*;
mcimadamore@1342 31
mcimadamore@1343 32 import com.sun.tools.javac.util.ArrayUtils;
mcimadamore@1336 33 import com.sun.tools.javac.util.Assert;
mcimadamore@1336 34 import com.sun.tools.javac.util.Filter;
mcimadamore@1336 35 import com.sun.tools.javac.util.Name;
duke@1 36
mcimadamore@1342 37 import java.util.*;
mcimadamore@1342 38
duke@1 39 /** An internal structure that corresponds to the constant pool of a classfile.
duke@1 40 *
jjg@581 41 * <p><b>This is NOT part of any supported API.
jjg@581 42 * If you write code that depends on this, you do so at your own risk.
duke@1 43 * This code and its internal interfaces are subject to change or
duke@1 44 * deletion without notice.</b>
duke@1 45 */
duke@1 46 public class Pool {
duke@1 47
duke@1 48 public static final int MAX_ENTRIES = 0xFFFF;
duke@1 49 public static final int MAX_STRING_LENGTH = 0xFFFF;
duke@1 50
duke@1 51 /** Index of next constant to be entered.
duke@1 52 */
duke@1 53 int pp;
duke@1 54
duke@1 55 /** The initial pool buffer.
duke@1 56 */
duke@1 57 Object[] pool;
duke@1 58
duke@1 59 /** A hashtable containing all constants in the pool.
duke@1 60 */
duke@1 61 Map<Object,Integer> indices;
duke@1 62
duke@1 63 /** Construct a pool with given number of elements and element array.
duke@1 64 */
duke@1 65 public Pool(int pp, Object[] pool) {
duke@1 66 this.pp = pp;
duke@1 67 this.pool = pool;
duke@1 68 this.indices = new HashMap<Object,Integer>(pool.length);
duke@1 69 for (int i = 1; i < pp; i++) {
duke@1 70 if (pool[i] != null) indices.put(pool[i], i);
duke@1 71 }
duke@1 72 }
duke@1 73
duke@1 74 /** Construct an empty pool.
duke@1 75 */
duke@1 76 public Pool() {
duke@1 77 this(1, new Object[64]);
duke@1 78 }
duke@1 79
duke@1 80 /** Return the number of entries in the constant pool.
duke@1 81 */
duke@1 82 public int numEntries() {
duke@1 83 return pp;
duke@1 84 }
duke@1 85
duke@1 86 /** Remove everything from this pool.
duke@1 87 */
duke@1 88 public void reset() {
duke@1 89 pp = 1;
duke@1 90 indices.clear();
duke@1 91 }
duke@1 92
duke@1 93 /** Place an object in the pool, unless it is already there.
duke@1 94 * If object is a symbol also enter its owner unless the owner is a
duke@1 95 * package. Return the object's index in the pool.
duke@1 96 */
duke@1 97 public int put(Object value) {
mcimadamore@1415 98 value = makePoolValue(value);
duke@1 99 // assert !(value instanceof Type.TypeVar);
duke@1 100 Integer index = indices.get(value);
duke@1 101 if (index == null) {
duke@1 102 // System.err.println("put " + value + " " + value.getClass());//DEBUG
duke@1 103 index = pp;
duke@1 104 indices.put(value, index);
jjg@1339 105 pool = ArrayUtils.ensureCapacity(pool, pp);
duke@1 106 pool[pp++] = value;
duke@1 107 if (value instanceof Long || value instanceof Double) {
jjg@1339 108 pool = ArrayUtils.ensureCapacity(pool, pp);
duke@1 109 pool[pp++] = null;
duke@1 110 }
duke@1 111 }
duke@1 112 return index.intValue();
duke@1 113 }
duke@1 114
mcimadamore@1415 115 Object makePoolValue(Object o) {
mcimadamore@1415 116 if (o instanceof DynamicMethodSymbol) {
mcimadamore@1415 117 return new DynamicMethod((DynamicMethodSymbol)o);
mcimadamore@1415 118 } else if (o instanceof MethodSymbol) {
mcimadamore@1415 119 return new Method((MethodSymbol)o);
mcimadamore@1415 120 } else if (o instanceof VarSymbol) {
mcimadamore@1415 121 return new Variable((VarSymbol)o);
mcimadamore@1415 122 } else {
mcimadamore@1415 123 return o;
mcimadamore@1415 124 }
mcimadamore@1415 125 }
mcimadamore@1415 126
duke@1 127 /** Return the given object's index in the pool,
duke@1 128 * or -1 if object is not in there.
duke@1 129 */
duke@1 130 public int get(Object o) {
duke@1 131 Integer n = indices.get(o);
duke@1 132 return n == null ? -1 : n.intValue();
duke@1 133 }
duke@1 134
duke@1 135 static class Method extends DelegatedSymbol {
duke@1 136 MethodSymbol m;
duke@1 137 Method(MethodSymbol m) {
duke@1 138 super(m);
duke@1 139 this.m = m;
duke@1 140 }
duke@1 141 public boolean equals(Object other) {
duke@1 142 if (!(other instanceof Method)) return false;
duke@1 143 MethodSymbol o = ((Method)other).m;
duke@1 144 return
duke@1 145 o.name == m.name &&
duke@1 146 o.owner == m.owner &&
duke@1 147 o.type.equals(m.type);
duke@1 148 }
duke@1 149 public int hashCode() {
duke@1 150 return
duke@1 151 m.name.hashCode() * 33 +
duke@1 152 m.owner.hashCode() * 9 +
duke@1 153 m.type.hashCode();
duke@1 154 }
duke@1 155 }
duke@1 156
mcimadamore@1415 157 static class DynamicMethod extends Method {
mcimadamore@1415 158
mcimadamore@1415 159 DynamicMethod(DynamicMethodSymbol m) {
mcimadamore@1415 160 super(m);
mcimadamore@1415 161 }
mcimadamore@1415 162
mcimadamore@1415 163 @Override
mcimadamore@1415 164 public boolean equals(Object other) {
mcimadamore@1415 165 if (!super.equals(other)) return false;
mcimadamore@1415 166 if (!(other instanceof DynamicMethod)) return false;
mcimadamore@1415 167 DynamicMethodSymbol dm1 = (DynamicMethodSymbol)m;
mcimadamore@1415 168 DynamicMethodSymbol dm2 = (DynamicMethodSymbol)((DynamicMethod)other).m;
mcimadamore@1415 169 return dm1.bsm == dm2.bsm &&
mcimadamore@1415 170 dm1.bsmKind == dm2.bsmKind &&
mcimadamore@1415 171 Arrays.equals(dm1.staticArgs, dm2.staticArgs);
mcimadamore@1415 172 }
mcimadamore@1415 173
mcimadamore@1415 174 @Override
mcimadamore@1415 175 public int hashCode() {
mcimadamore@1415 176 int hash = super.hashCode();
mcimadamore@1415 177 DynamicMethodSymbol dm = (DynamicMethodSymbol)m;
mcimadamore@1415 178 hash += dm.bsmKind * 7 +
mcimadamore@1415 179 dm.bsm.hashCode() * 11;
mcimadamore@1415 180 for (int i = 0; i < dm.staticArgs.length; i++) {
mcimadamore@1415 181 hash += (dm.staticArgs[i].hashCode() * 23);
mcimadamore@1415 182 }
mcimadamore@1415 183 return hash;
mcimadamore@1415 184 }
mcimadamore@1415 185 }
mcimadamore@1415 186
duke@1 187 static class Variable extends DelegatedSymbol {
duke@1 188 VarSymbol v;
duke@1 189 Variable(VarSymbol v) {
duke@1 190 super(v);
duke@1 191 this.v = v;
duke@1 192 }
duke@1 193 public boolean equals(Object other) {
duke@1 194 if (!(other instanceof Variable)) return false;
duke@1 195 VarSymbol o = ((Variable)other).v;
duke@1 196 return
duke@1 197 o.name == v.name &&
duke@1 198 o.owner == v.owner &&
duke@1 199 o.type.equals(v.type);
duke@1 200 }
duke@1 201 public int hashCode() {
duke@1 202 return
duke@1 203 v.name.hashCode() * 33 +
duke@1 204 v.owner.hashCode() * 9 +
duke@1 205 v.type.hashCode();
duke@1 206 }
duke@1 207 }
mcimadamore@1336 208
mcimadamore@1336 209 public static class MethodHandle {
mcimadamore@1336 210
mcimadamore@1336 211 /** Reference kind - see ClassFile */
mcimadamore@1336 212 int refKind;
mcimadamore@1336 213
mcimadamore@1336 214 /** Reference symbol */
mcimadamore@1336 215 Symbol refSym;
mcimadamore@1336 216
mcimadamore@1342 217 public MethodHandle(int refKind, Symbol refSym) {
mcimadamore@1336 218 this.refKind = refKind;
mcimadamore@1336 219 this.refSym = refSym;
mcimadamore@1336 220 checkConsistent();
mcimadamore@1336 221 }
mcimadamore@1336 222 public boolean equals(Object other) {
mcimadamore@1336 223 if (!(other instanceof MethodHandle)) return false;
mcimadamore@1336 224 MethodHandle mr = (MethodHandle) other;
mcimadamore@1336 225 if (mr.refKind != refKind) return false;
mcimadamore@1336 226 Symbol o = mr.refSym;
mcimadamore@1336 227 return
mcimadamore@1336 228 o.name == refSym.name &&
mcimadamore@1336 229 o.owner == refSym.owner &&
mcimadamore@1336 230 o.type.equals(refSym.type);
mcimadamore@1336 231 }
mcimadamore@1336 232 public int hashCode() {
mcimadamore@1336 233 return
mcimadamore@1336 234 refKind * 65 +
mcimadamore@1336 235 refSym.name.hashCode() * 33 +
mcimadamore@1336 236 refSym.owner.hashCode() * 9 +
mcimadamore@1336 237 refSym.type.hashCode();
mcimadamore@1336 238 }
mcimadamore@1336 239
mcimadamore@1336 240 /**
mcimadamore@1336 241 * Check consistency of reference kind and symbol (see JVMS 4.4.8)
mcimadamore@1336 242 */
mcimadamore@1336 243 @SuppressWarnings("fallthrough")
mcimadamore@1336 244 private void checkConsistent() {
mcimadamore@1336 245 boolean staticOk = false;
mcimadamore@1336 246 int expectedKind = -1;
mcimadamore@1336 247 Filter<Name> nameFilter = nonInitFilter;
mcimadamore@1336 248 boolean interfaceOwner = false;
mcimadamore@1336 249 switch (refKind) {
mcimadamore@1336 250 case ClassFile.REF_getStatic:
mcimadamore@1336 251 case ClassFile.REF_putStatic:
mcimadamore@1336 252 staticOk = true;
mcimadamore@1336 253 case ClassFile.REF_getField:
mcimadamore@1336 254 case ClassFile.REF_putField:
mcimadamore@1336 255 expectedKind = Kinds.VAR;
mcimadamore@1336 256 break;
mcimadamore@1336 257 case ClassFile.REF_newInvokeSpecial:
mcimadamore@1336 258 nameFilter = initFilter;
mcimadamore@1336 259 expectedKind = Kinds.MTH;
mcimadamore@1336 260 break;
mcimadamore@1336 261 case ClassFile.REF_invokeInterface:
mcimadamore@1336 262 interfaceOwner = true;
mcimadamore@1336 263 expectedKind = Kinds.MTH;
mcimadamore@1336 264 break;
mcimadamore@1336 265 case ClassFile.REF_invokeStatic:
mcimadamore@1336 266 staticOk = true;
mcimadamore@1336 267 case ClassFile.REF_invokeVirtual:
mcimadamore@1336 268 case ClassFile.REF_invokeSpecial:
mcimadamore@1336 269 expectedKind = Kinds.MTH;
mcimadamore@1336 270 break;
mcimadamore@1336 271 }
mcimadamore@1336 272 Assert.check(!refSym.isStatic() || staticOk);
mcimadamore@1336 273 Assert.check(refSym.kind == expectedKind);
mcimadamore@1336 274 Assert.check(nameFilter.accepts(refSym.name));
mcimadamore@1336 275 Assert.check(!refSym.owner.isInterface() || interfaceOwner);
mcimadamore@1336 276 }
mcimadamore@1336 277 //where
mcimadamore@1336 278 Filter<Name> nonInitFilter = new Filter<Name>() {
mcimadamore@1336 279 public boolean accepts(Name n) {
mcimadamore@1342 280 return n != n.table.names.init && n != n.table.names.clinit;
mcimadamore@1336 281 }
mcimadamore@1336 282 };
mcimadamore@1336 283
mcimadamore@1336 284 Filter<Name> initFilter = new Filter<Name>() {
mcimadamore@1336 285 public boolean accepts(Name n) {
mcimadamore@1342 286 return n == n.table.names.init;
mcimadamore@1336 287 }
mcimadamore@1336 288 };
mcimadamore@1336 289 }
duke@1 290 }

mercurial