1.1 --- a/rt/emul/compact/src/main/java/sun/invoke/anon/ConstantPoolParser.java Sun Aug 10 05:56:32 2014 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,368 +0,0 @@
1.4 -/*
1.5 - * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 - *
1.8 - * This code is free software; you can redistribute it and/or modify it
1.9 - * under the terms of the GNU General Public License version 2 only, as
1.10 - * published by the Free Software Foundation. Oracle designates this
1.11 - * particular file as subject to the "Classpath" exception as provided
1.12 - * by Oracle in the LICENSE file that accompanied this code.
1.13 - *
1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 - * version 2 for more details (a copy is included in the LICENSE file that
1.18 - * accompanied this code).
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License version
1.21 - * 2 along with this work; if not, write to the Free Software Foundation,
1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 - *
1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 - * or visit www.oracle.com if you need additional information or have any
1.26 - * questions.
1.27 - */
1.28 -
1.29 -package sun.invoke.anon;
1.30 -
1.31 -import java.io.IOException;
1.32 -import java.io.OutputStream;
1.33 -import java.nio.BufferUnderflowException;
1.34 -import java.nio.ByteBuffer;
1.35 -
1.36 -import static sun.invoke.anon.ConstantPoolVisitor.*;
1.37 -
1.38 -/** A constant pool parser.
1.39 - */
1.40 -public class ConstantPoolParser {
1.41 - final byte[] classFile;
1.42 - final byte[] tags;
1.43 - final char[] firstHeader; // maghi, maglo, minor, major, cplen
1.44 -
1.45 - // these are filled in on first parse:
1.46 - int endOffset;
1.47 - char[] secondHeader; // flags, this_class, super_class, intlen
1.48 -
1.49 - // used to decode UTF8 array
1.50 - private char[] charArray = new char[80];
1.51 -
1.52 - /** Creates a constant pool parser.
1.53 - * @param classFile an array of bytes containing a class.
1.54 - * @throws InvalidConstantPoolFormatException if the header of the class has errors.
1.55 - */
1.56 - public ConstantPoolParser(byte[] classFile) throws InvalidConstantPoolFormatException {
1.57 - this.classFile = classFile;
1.58 - this.firstHeader = parseHeader(classFile);
1.59 - this.tags = new byte[firstHeader[4]];
1.60 - }
1.61 -
1.62 - /** Create a constant pool parser by loading the bytecodes of the
1.63 - * class taken as argument.
1.64 - *
1.65 - * @param templateClass the class to parse.
1.66 - *
1.67 - * @throws IOException raised if an I/O occurs when loading
1.68 - * the bytecode of the template class.
1.69 - * @throws InvalidConstantPoolFormatException if the header of the class has errors.
1.70 - *
1.71 - * @see #ConstantPoolParser(byte[])
1.72 - * @see AnonymousClassLoader#readClassFile(Class)
1.73 - */
1.74 - public ConstantPoolParser(Class<?> templateClass) throws IOException, InvalidConstantPoolFormatException {
1.75 - this(AnonymousClassLoader.readClassFile(templateClass));
1.76 - }
1.77 -
1.78 - /** Creates an empty patch to patch the class file
1.79 - * used by the current parser.
1.80 - * @return a new class patch.
1.81 - */
1.82 - public ConstantPoolPatch createPatch() {
1.83 - return new ConstantPoolPatch(this);
1.84 - }
1.85 -
1.86 - /** Report the tag of the indicated CP entry.
1.87 - * @param index
1.88 - * @return one of {@link ConstantPoolVisitor#CONSTANT_Utf8}, etc.
1.89 - */
1.90 - public byte getTag(int index) {
1.91 - getEndOffset(); // trigger an exception if we haven't parsed yet
1.92 - return tags[index];
1.93 - }
1.94 -
1.95 - /** Report the length of the constant pool. */
1.96 - public int getLength() {
1.97 - return firstHeader[4];
1.98 - }
1.99 -
1.100 - /** Report the offset, within the class file, of the start of the constant pool. */
1.101 - public int getStartOffset() {
1.102 - return firstHeader.length * 2;
1.103 - }
1.104 -
1.105 - /** Report the offset, within the class file, of the end of the constant pool. */
1.106 - public int getEndOffset() {
1.107 - if (endOffset == 0)
1.108 - throw new IllegalStateException("class file has not yet been parsed");
1.109 - return endOffset;
1.110 - }
1.111 -
1.112 - /** Report the CP index of this class's own name. */
1.113 - public int getThisClassIndex() {
1.114 - getEndOffset(); // provoke exception if not yet parsed
1.115 - return secondHeader[1];
1.116 - }
1.117 -
1.118 - /** Report the total size of the class file. */
1.119 - public int getTailLength() {
1.120 - return classFile.length - getEndOffset();
1.121 - }
1.122 -
1.123 - /** Write the head (header plus constant pool)
1.124 - * of the class file to the indicated stream.
1.125 - */
1.126 - public void writeHead(OutputStream out) throws IOException {
1.127 - out.write(classFile, 0, getEndOffset());
1.128 - }
1.129 -
1.130 - /** Write the head (header plus constant pool)
1.131 - * of the class file to the indicated stream,
1.132 - * incorporating the non-null entries of the given array
1.133 - * as patches.
1.134 - */
1.135 - void writePatchedHead(OutputStream out, Object[] patchArray) {
1.136 - // this will be useful to partially emulate the class loader on old JVMs
1.137 - throw new UnsupportedOperationException("Not yet implemented");
1.138 - }
1.139 -
1.140 - /** Write the tail (everything after the constant pool)
1.141 - * of the class file to the indicated stream.
1.142 - */
1.143 - public void writeTail(OutputStream out) throws IOException {
1.144 - out.write(classFile, getEndOffset(), getTailLength());
1.145 - }
1.146 -
1.147 - private static char[] parseHeader(byte[] classFile) throws InvalidConstantPoolFormatException {
1.148 - char[] result = new char[5];
1.149 - ByteBuffer buffer = ByteBuffer.wrap(classFile);
1.150 - for (int i = 0; i < result.length; i++)
1.151 - result[i] = (char) getUnsignedShort(buffer);
1.152 - int magic = result[0] << 16 | result[1] << 0;
1.153 - if (magic != 0xCAFEBABE)
1.154 - throw new InvalidConstantPoolFormatException("invalid magic number "+magic);
1.155 - // skip major, minor version
1.156 - int len = result[4];
1.157 - if (len < 1)
1.158 - throw new InvalidConstantPoolFormatException("constant pool length < 1");
1.159 - return result;
1.160 - }
1.161 -
1.162 - /** Parse the constant pool of the class
1.163 - * calling a method visit* each time a constant pool entry is parsed.
1.164 - *
1.165 - * The order of the calls to visit* is not guaranteed to be the same
1.166 - * than the order of the constant pool entry in the bytecode array.
1.167 - *
1.168 - * @param visitor
1.169 - * @throws InvalidConstantPoolFormatException
1.170 - */
1.171 - public void parse(ConstantPoolVisitor visitor) throws InvalidConstantPoolFormatException {
1.172 - ByteBuffer buffer = ByteBuffer.wrap(classFile);
1.173 - buffer.position(getStartOffset()); //skip header
1.174 -
1.175 - Object[] values = new Object[getLength()];
1.176 - try {
1.177 - parseConstantPool(buffer, values, visitor);
1.178 - } catch(BufferUnderflowException e) {
1.179 - throw new InvalidConstantPoolFormatException(e);
1.180 - }
1.181 - if (endOffset == 0) {
1.182 - endOffset = buffer.position();
1.183 - secondHeader = new char[4];
1.184 - for (int i = 0; i < secondHeader.length; i++) {
1.185 - secondHeader[i] = (char) getUnsignedShort(buffer);
1.186 - }
1.187 - }
1.188 - resolveConstantPool(values, visitor);
1.189 - }
1.190 -
1.191 - private char[] getCharArray(int utfLength) {
1.192 - if (utfLength <= charArray.length)
1.193 - return charArray;
1.194 - return charArray = new char[utfLength];
1.195 - }
1.196 -
1.197 - private void parseConstantPool(ByteBuffer buffer, Object[] values, ConstantPoolVisitor visitor) throws InvalidConstantPoolFormatException {
1.198 - for (int i = 1; i < tags.length; ) {
1.199 - byte tag = (byte) getUnsignedByte(buffer);
1.200 - assert(tags[i] == 0 || tags[i] == tag);
1.201 - tags[i] = tag;
1.202 - switch (tag) {
1.203 - case CONSTANT_Utf8:
1.204 - int utfLen = getUnsignedShort(buffer);
1.205 - String value = getUTF8(buffer, utfLen, getCharArray(utfLen));
1.206 - visitor.visitUTF8(i, CONSTANT_Utf8, value);
1.207 - tags[i] = tag;
1.208 - values[i++] = value;
1.209 - break;
1.210 - case CONSTANT_Integer:
1.211 - visitor.visitConstantValue(i, tag, buffer.getInt());
1.212 - i++;
1.213 - break;
1.214 - case CONSTANT_Float:
1.215 - visitor.visitConstantValue(i, tag, buffer.getFloat());
1.216 - i++;
1.217 - break;
1.218 - case CONSTANT_Long:
1.219 - visitor.visitConstantValue(i, tag, buffer.getLong());
1.220 - i+=2;
1.221 - break;
1.222 - case CONSTANT_Double:
1.223 - visitor.visitConstantValue(i, tag, buffer.getDouble());
1.224 - i+=2;
1.225 - break;
1.226 -
1.227 - case CONSTANT_Class: // fall through:
1.228 - case CONSTANT_String:
1.229 - tags[i] = tag;
1.230 - values[i++] = new int[] { getUnsignedShort(buffer) };
1.231 - break;
1.232 -
1.233 - case CONSTANT_Fieldref: // fall through:
1.234 - case CONSTANT_Methodref: // fall through:
1.235 - case CONSTANT_InterfaceMethodref: // fall through:
1.236 - case CONSTANT_NameAndType:
1.237 - tags[i] = tag;
1.238 - values[i++] = new int[] { getUnsignedShort(buffer), getUnsignedShort(buffer) };
1.239 - break;
1.240 - default:
1.241 - throw new AssertionError("invalid constant "+tag);
1.242 - }
1.243 - }
1.244 - }
1.245 -
1.246 - private void resolveConstantPool(Object[] values, ConstantPoolVisitor visitor) {
1.247 - // clean out the int[] values, which are temporary
1.248 - for (int beg = 1, end = values.length-1, beg2, end2;
1.249 - beg <= end;
1.250 - beg = beg2, end = end2) {
1.251 - beg2 = end; end2 = beg-1;
1.252 - //System.out.println("CP resolve pass: "+beg+".."+end);
1.253 - for (int i = beg; i <= end; i++) {
1.254 - Object value = values[i];
1.255 - if (!(value instanceof int[]))
1.256 - continue;
1.257 - int[] array = (int[]) value;
1.258 - byte tag = tags[i];
1.259 - switch (tag) {
1.260 - case CONSTANT_String:
1.261 - String stringBody = (String) values[array[0]];
1.262 - visitor.visitConstantString(i, tag, stringBody, array[0]);
1.263 - values[i] = null;
1.264 - break;
1.265 - case CONSTANT_Class: {
1.266 - String className = (String) values[array[0]];
1.267 - // use the external form favored by Class.forName:
1.268 - className = className.replace('/', '.');
1.269 - visitor.visitConstantString(i, tag, className, array[0]);
1.270 - values[i] = className;
1.271 - break;
1.272 - }
1.273 - case CONSTANT_NameAndType: {
1.274 - String memberName = (String) values[array[0]];
1.275 - String signature = (String) values[array[1]];
1.276 - visitor.visitDescriptor(i, tag, memberName, signature,
1.277 - array[0], array[1]);
1.278 - values[i] = new String[] {memberName, signature};
1.279 - break;
1.280 - }
1.281 - case CONSTANT_Fieldref: // fall through:
1.282 - case CONSTANT_Methodref: // fall through:
1.283 - case CONSTANT_InterfaceMethodref: {
1.284 - Object className = values[array[0]];
1.285 - Object nameAndType = values[array[1]];
1.286 - if (!(className instanceof String) ||
1.287 - !(nameAndType instanceof String[])) {
1.288 - // one more pass is needed
1.289 - if (beg2 > i) beg2 = i;
1.290 - if (end2 < i) end2 = i;
1.291 - continue;
1.292 - }
1.293 - String[] nameAndTypeArray = (String[]) nameAndType;
1.294 - visitor.visitMemberRef(i, tag,
1.295 - (String)className,
1.296 - nameAndTypeArray[0],
1.297 - nameAndTypeArray[1],
1.298 - array[0], array[1]);
1.299 - values[i] = null;
1.300 - }
1.301 - break;
1.302 - default:
1.303 - continue;
1.304 - }
1.305 - }
1.306 - }
1.307 - }
1.308 -
1.309 - private static int getUnsignedByte(ByteBuffer buffer) {
1.310 - return buffer.get() & 0xFF;
1.311 - }
1.312 -
1.313 - private static int getUnsignedShort(ByteBuffer buffer) {
1.314 - int b1 = getUnsignedByte(buffer);
1.315 - int b2 = getUnsignedByte(buffer);
1.316 - return (b1 << 8) + (b2 << 0);
1.317 - }
1.318 -
1.319 - private static String getUTF8(ByteBuffer buffer, int utfLen, char[] charArray) throws InvalidConstantPoolFormatException {
1.320 - int utfLimit = buffer.position() + utfLen;
1.321 - int index = 0;
1.322 - while (buffer.position() < utfLimit) {
1.323 - int c = buffer.get() & 0xff;
1.324 - if (c > 127) {
1.325 - buffer.position(buffer.position() - 1);
1.326 - return getUTF8Extended(buffer, utfLimit, charArray, index);
1.327 - }
1.328 - charArray[index++] = (char)c;
1.329 - }
1.330 - return new String(charArray, 0, index);
1.331 - }
1.332 -
1.333 - private static String getUTF8Extended(ByteBuffer buffer, int utfLimit, char[] charArray, int index) throws InvalidConstantPoolFormatException {
1.334 - int c, c2, c3;
1.335 - while (buffer.position() < utfLimit) {
1.336 - c = buffer.get() & 0xff;
1.337 - switch (c >> 4) {
1.338 - case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
1.339 - /* 0xxxxxxx*/
1.340 - charArray[index++] = (char)c;
1.341 - break;
1.342 - case 12: case 13:
1.343 - /* 110x xxxx 10xx xxxx*/
1.344 - c2 = buffer.get();
1.345 - if ((c2 & 0xC0) != 0x80)
1.346 - throw new InvalidConstantPoolFormatException(
1.347 - "malformed input around byte " + buffer.position());
1.348 - charArray[index++] = (char)(((c & 0x1F) << 6) |
1.349 - (c2 & 0x3F));
1.350 - break;
1.351 - case 14:
1.352 - /* 1110 xxxx 10xx xxxx 10xx xxxx */
1.353 - c2 = buffer.get();
1.354 - c3 = buffer.get();
1.355 - if (((c2 & 0xC0) != 0x80) || ((c3 & 0xC0) != 0x80))
1.356 - throw new InvalidConstantPoolFormatException(
1.357 - "malformed input around byte " + (buffer.position()));
1.358 - charArray[index++] = (char)(((c & 0x0F) << 12) |
1.359 - ((c2 & 0x3F) << 6) |
1.360 - ((c3 & 0x3F) << 0));
1.361 - break;
1.362 - default:
1.363 - /* 10xx xxxx, 1111 xxxx */
1.364 - throw new InvalidConstantPoolFormatException(
1.365 - "malformed input around byte " + buffer.position());
1.366 - }
1.367 - }
1.368 - // The number of chars produced may be less than utflen
1.369 - return new String(charArray, 0, index);
1.370 - }
1.371 -}