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