1193N/A/*
3793N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
1193N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1193N/A *
1193N/A * This code is free software; you can redistribute it and/or modify it
1193N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1193N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1193N/A *
1193N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1193N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1193N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1193N/A * version 2 for more details (a copy is included in the LICENSE file that
1193N/A * accompanied this code).
1193N/A *
1193N/A * You should have received a copy of the GNU General Public License version
1193N/A * 2 along with this work; if not, write to the Free Software Foundation,
1193N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1193N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1193N/A */
1193N/A
3793N/Apackage sun.invoke.anon;
1193N/A
1193N/Aimport java.io.IOException;
1193N/Aimport java.io.OutputStream;
1193N/Aimport java.nio.BufferUnderflowException;
1193N/Aimport java.nio.ByteBuffer;
1193N/A
3793N/Aimport static sun.invoke.anon.ConstantPoolVisitor.*;
1193N/A
1193N/A/** A constant pool parser.
1193N/A */
1193N/Apublic class ConstantPoolParser {
1193N/A final byte[] classFile;
1193N/A final byte[] tags;
1193N/A final char[] firstHeader; // maghi, maglo, minor, major, cplen
1193N/A
1193N/A // these are filled in on first parse:
1193N/A int endOffset;
1193N/A char[] secondHeader; // flags, this_class, super_class, intlen
1193N/A
1193N/A // used to decode UTF8 array
1193N/A private char[] charArray = new char[80];
1193N/A
1193N/A /** Creates a constant pool parser.
1193N/A * @param classFile an array of bytes containing a class.
1193N/A * @throws InvalidConstantPoolFormatException if the header of the class has errors.
1193N/A */
1193N/A public ConstantPoolParser(byte[] classFile) throws InvalidConstantPoolFormatException {
1193N/A this.classFile = classFile;
1193N/A this.firstHeader = parseHeader(classFile);
1193N/A this.tags = new byte[firstHeader[4]];
1193N/A }
1193N/A
1193N/A /** Create a constant pool parser by loading the bytecodes of the
1193N/A * class taken as argument.
1193N/A *
1193N/A * @param templateClass the class to parse.
1193N/A *
1193N/A * @throws IOException raised if an I/O occurs when loading
1193N/A * the bytecode of the template class.
1193N/A * @throws InvalidConstantPoolFormatException if the header of the class has errors.
1193N/A *
1193N/A * @see #ConstantPoolParser(byte[])
1193N/A * @see AnonymousClassLoader#readClassFile(Class)
1193N/A */
1193N/A public ConstantPoolParser(Class<?> templateClass) throws IOException, InvalidConstantPoolFormatException {
1193N/A this(AnonymousClassLoader.readClassFile(templateClass));
1193N/A }
1193N/A
1193N/A /** Creates an empty patch to patch the class file
1193N/A * used by the current parser.
1193N/A * @return a new class patch.
1193N/A */
1193N/A public ConstantPoolPatch createPatch() {
1193N/A return new ConstantPoolPatch(this);
1193N/A }
1193N/A
1193N/A /** Report the tag of the indicated CP entry.
1193N/A * @param index
1193N/A * @return one of {@link ConstantPoolVisitor#CONSTANT_Utf8}, etc.
1193N/A */
1193N/A public byte getTag(int index) {
1193N/A getEndOffset(); // trigger an exception if we haven't parsed yet
1193N/A return tags[index];
1193N/A }
1193N/A
1193N/A /** Report the length of the constant pool. */
1193N/A public int getLength() {
1193N/A return firstHeader[4];
1193N/A }
1193N/A
1193N/A /** Report the offset, within the class file, of the start of the constant pool. */
1193N/A public int getStartOffset() {
1193N/A return firstHeader.length * 2;
1193N/A }
1193N/A
1193N/A /** Report the offset, within the class file, of the end of the constant pool. */
1193N/A public int getEndOffset() {
1193N/A if (endOffset == 0)
1193N/A throw new IllegalStateException("class file has not yet been parsed");
1193N/A return endOffset;
1193N/A }
1193N/A
1193N/A /** Report the CP index of this class's own name. */
1193N/A public int getThisClassIndex() {
1193N/A getEndOffset(); // provoke exception if not yet parsed
1193N/A return secondHeader[1];
1193N/A }
1193N/A
1193N/A /** Report the total size of the class file. */
1193N/A public int getTailLength() {
1193N/A return classFile.length - getEndOffset();
1193N/A }
1193N/A
1193N/A /** Write the head (header plus constant pool)
1193N/A * of the class file to the indicated stream.
1193N/A */
1193N/A public void writeHead(OutputStream out) throws IOException {
1193N/A out.write(classFile, 0, getEndOffset());
1193N/A }
1193N/A
1193N/A /** Write the head (header plus constant pool)
1193N/A * of the class file to the indicated stream,
1193N/A * incorporating the non-null entries of the given array
1193N/A * as patches.
1193N/A */
1193N/A void writePatchedHead(OutputStream out, Object[] patchArray) {
1193N/A // this will be useful to partially emulate the class loader on old JVMs
1193N/A throw new UnsupportedOperationException("Not yet implemented");
1193N/A }
1193N/A
1193N/A /** Write the tail (everything after the constant pool)
1193N/A * of the class file to the indicated stream.
1193N/A */
1193N/A public void writeTail(OutputStream out) throws IOException {
1193N/A out.write(classFile, getEndOffset(), getTailLength());
1193N/A }
1193N/A
1193N/A private static char[] parseHeader(byte[] classFile) throws InvalidConstantPoolFormatException {
1193N/A char[] result = new char[5];
1193N/A ByteBuffer buffer = ByteBuffer.wrap(classFile);
1193N/A for (int i = 0; i < result.length; i++)
1193N/A result[i] = (char) getUnsignedShort(buffer);
1193N/A int magic = result[0] << 16 | result[1] << 0;
1193N/A if (magic != 0xCAFEBABE)
1193N/A throw new InvalidConstantPoolFormatException("invalid magic number "+magic);
1193N/A // skip major, minor version
1193N/A int len = result[4];
1193N/A if (len < 1)
1193N/A throw new InvalidConstantPoolFormatException("constant pool length < 1");
1193N/A return result;
1193N/A }
1193N/A
1193N/A /** Parse the constant pool of the class
1193N/A * calling a method visit* each time a constant pool entry is parsed.
1193N/A *
1193N/A * The order of the calls to visit* is not guaranteed to be the same
1193N/A * than the order of the constant pool entry in the bytecode array.
1193N/A *
1193N/A * @param visitor
1193N/A * @throws InvalidConstantPoolFormatException
1193N/A */
1193N/A public void parse(ConstantPoolVisitor visitor) throws InvalidConstantPoolFormatException {
1193N/A ByteBuffer buffer = ByteBuffer.wrap(classFile);
1193N/A buffer.position(getStartOffset()); //skip header
1193N/A
1193N/A Object[] values = new Object[getLength()];
1193N/A try {
1193N/A parseConstantPool(buffer, values, visitor);
1193N/A } catch(BufferUnderflowException e) {
1193N/A throw new InvalidConstantPoolFormatException(e);
1193N/A }
1193N/A if (endOffset == 0) {
1193N/A endOffset = buffer.position();
1193N/A secondHeader = new char[4];
1193N/A for (int i = 0; i < secondHeader.length; i++) {
1193N/A secondHeader[i] = (char) getUnsignedShort(buffer);
1193N/A }
1193N/A }
1193N/A resolveConstantPool(values, visitor);
1193N/A }
1193N/A
1193N/A private char[] getCharArray(int utfLength) {
1193N/A if (utfLength <= charArray.length)
1193N/A return charArray;
1193N/A return charArray = new char[utfLength];
1193N/A }
1193N/A
1193N/A private void parseConstantPool(ByteBuffer buffer, Object[] values, ConstantPoolVisitor visitor) throws InvalidConstantPoolFormatException {
1193N/A for (int i = 1; i < tags.length; ) {
1193N/A byte tag = (byte) getUnsignedByte(buffer);
1193N/A assert(tags[i] == 0 || tags[i] == tag);
1193N/A tags[i] = tag;
1193N/A switch (tag) {
1193N/A case CONSTANT_Utf8:
1193N/A int utfLen = getUnsignedShort(buffer);
1193N/A String value = getUTF8(buffer, utfLen, getCharArray(utfLen));
1193N/A visitor.visitUTF8(i, CONSTANT_Utf8, value);
1193N/A tags[i] = tag;
1193N/A values[i++] = value;
1193N/A break;
1193N/A case CONSTANT_Integer:
1193N/A visitor.visitConstantValue(i, tag, buffer.getInt());
1193N/A i++;
1193N/A break;
1193N/A case CONSTANT_Float:
1193N/A visitor.visitConstantValue(i, tag, buffer.getFloat());
1193N/A i++;
1193N/A break;
1193N/A case CONSTANT_Long:
1193N/A visitor.visitConstantValue(i, tag, buffer.getLong());
1193N/A i+=2;
1193N/A break;
1193N/A case CONSTANT_Double:
1193N/A visitor.visitConstantValue(i, tag, buffer.getDouble());
1193N/A i+=2;
1193N/A break;
1193N/A
1193N/A case CONSTANT_Class: // fall through:
1193N/A case CONSTANT_String:
1193N/A tags[i] = tag;
1193N/A values[i++] = new int[] { getUnsignedShort(buffer) };
1193N/A break;
1193N/A
1193N/A case CONSTANT_Fieldref: // fall through:
1193N/A case CONSTANT_Methodref: // fall through:
1193N/A case CONSTANT_InterfaceMethodref: // fall through:
1193N/A case CONSTANT_NameAndType:
1193N/A tags[i] = tag;
1193N/A values[i++] = new int[] { getUnsignedShort(buffer), getUnsignedShort(buffer) };
1193N/A break;
1193N/A default:
1193N/A throw new AssertionError("invalid constant "+tag);
1193N/A }
1193N/A }
1193N/A }
1193N/A
1193N/A private void resolveConstantPool(Object[] values, ConstantPoolVisitor visitor) {
1193N/A // clean out the int[] values, which are temporary
1193N/A for (int beg = 1, end = values.length-1, beg2, end2;
1193N/A beg <= end;
1193N/A beg = beg2, end = end2) {
1193N/A beg2 = end; end2 = beg-1;
1193N/A //System.out.println("CP resolve pass: "+beg+".."+end);
1193N/A for (int i = beg; i <= end; i++) {
1193N/A Object value = values[i];
1193N/A if (!(value instanceof int[]))
1193N/A continue;
1193N/A int[] array = (int[]) value;
1193N/A byte tag = tags[i];
1193N/A switch (tag) {
1193N/A case CONSTANT_String:
1193N/A String stringBody = (String) values[array[0]];
1193N/A visitor.visitConstantString(i, tag, stringBody, array[0]);
1193N/A values[i] = null;
1193N/A break;
1193N/A case CONSTANT_Class: {
1193N/A String className = (String) values[array[0]];
1193N/A // use the external form favored by Class.forName:
1193N/A className = className.replace('/', '.');
1193N/A visitor.visitConstantString(i, tag, className, array[0]);
1193N/A values[i] = className;
1193N/A break;
1193N/A }
1193N/A case CONSTANT_NameAndType: {
1193N/A String memberName = (String) values[array[0]];
1193N/A String signature = (String) values[array[1]];
1193N/A visitor.visitDescriptor(i, tag, memberName, signature,
1193N/A array[0], array[1]);
1193N/A values[i] = new String[] {memberName, signature};
1193N/A break;
1193N/A }
1193N/A case CONSTANT_Fieldref: // fall through:
1193N/A case CONSTANT_Methodref: // fall through:
1193N/A case CONSTANT_InterfaceMethodref: {
1193N/A Object className = values[array[0]];
1193N/A Object nameAndType = values[array[1]];
1193N/A if (!(className instanceof String) ||
1193N/A !(nameAndType instanceof String[])) {
1193N/A // one more pass is needed
1193N/A if (beg2 > i) beg2 = i;
1193N/A if (end2 < i) end2 = i;
1193N/A continue;
1193N/A }
1193N/A String[] nameAndTypeArray = (String[]) nameAndType;
1193N/A visitor.visitMemberRef(i, tag,
1193N/A (String)className,
1193N/A nameAndTypeArray[0],
1193N/A nameAndTypeArray[1],
1193N/A array[0], array[1]);
1193N/A values[i] = null;
1193N/A }
1193N/A break;
1193N/A default:
1193N/A continue;
1193N/A }
1193N/A }
1193N/A }
1193N/A }
1193N/A
1193N/A private static int getUnsignedByte(ByteBuffer buffer) {
1193N/A return buffer.get() & 0xFF;
1193N/A }
1193N/A
1193N/A private static int getUnsignedShort(ByteBuffer buffer) {
1193N/A int b1 = getUnsignedByte(buffer);
1193N/A int b2 = getUnsignedByte(buffer);
1193N/A return (b1 << 8) + (b2 << 0);
1193N/A }
1193N/A
1193N/A private static String getUTF8(ByteBuffer buffer, int utfLen, char[] charArray) throws InvalidConstantPoolFormatException {
1193N/A int utfLimit = buffer.position() + utfLen;
1193N/A int index = 0;
1193N/A while (buffer.position() < utfLimit) {
1193N/A int c = buffer.get() & 0xff;
1193N/A if (c > 127) {
1193N/A buffer.position(buffer.position() - 1);
1193N/A return getUTF8Extended(buffer, utfLimit, charArray, index);
1193N/A }
1193N/A charArray[index++] = (char)c;
1193N/A }
1193N/A return new String(charArray, 0, index);
1193N/A }
1193N/A
1193N/A private static String getUTF8Extended(ByteBuffer buffer, int utfLimit, char[] charArray, int index) throws InvalidConstantPoolFormatException {
1193N/A int c, c2, c3;
1193N/A while (buffer.position() < utfLimit) {
1193N/A c = buffer.get() & 0xff;
1193N/A switch (c >> 4) {
1193N/A case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
1193N/A /* 0xxxxxxx*/
1193N/A charArray[index++] = (char)c;
1193N/A break;
1193N/A case 12: case 13:
1193N/A /* 110x xxxx 10xx xxxx*/
1193N/A c2 = buffer.get();
1193N/A if ((c2 & 0xC0) != 0x80)
1193N/A throw new InvalidConstantPoolFormatException(
1193N/A "malformed input around byte " + buffer.position());
1193N/A charArray[index++] = (char)(((c & 0x1F) << 6) |
1193N/A (c2 & 0x3F));
1193N/A break;
1193N/A case 14:
1193N/A /* 1110 xxxx 10xx xxxx 10xx xxxx */
1193N/A c2 = buffer.get();
1193N/A c3 = buffer.get();
1193N/A if (((c2 & 0xC0) != 0x80) || ((c3 & 0xC0) != 0x80))
1193N/A throw new InvalidConstantPoolFormatException(
1193N/A "malformed input around byte " + (buffer.position()));
1193N/A charArray[index++] = (char)(((c & 0x0F) << 12) |
1193N/A ((c2 & 0x3F) << 6) |
1193N/A ((c3 & 0x3F) << 0));
1193N/A break;
1193N/A default:
1193N/A /* 10xx xxxx, 1111 xxxx */
1193N/A throw new InvalidConstantPoolFormatException(
1193N/A "malformed input around byte " + buffer.position());
1193N/A }
1193N/A }
1193N/A // The number of chars produced may be less than utflen
1193N/A return new String(charArray, 0, index);
1193N/A }
1193N/A}