ConstantPoolParser.java 14.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
/*
 * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package sun.dyn.anon;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

import static sun.dyn.anon.ConstantPoolVisitor.*;

/** A constant pool parser.
 */
public class ConstantPoolParser {
    final byte[] classFile;
    final byte[] tags;
    final char[] firstHeader;  // maghi, maglo, minor, major, cplen

    // these are filled in on first parse:
    int endOffset;
    char[] secondHeader;       // flags, this_class, super_class, intlen

    // used to decode UTF8 array
    private char[] charArray = new char[80];

    /** Creates a constant pool parser.
     * @param classFile an array of bytes containing a class.
     * @throws InvalidConstantPoolFormatException if the header of the class has errors.
     */
    public ConstantPoolParser(byte[] classFile) throws InvalidConstantPoolFormatException {
        this.classFile = classFile;
        this.firstHeader = parseHeader(classFile);
        this.tags = new byte[firstHeader[4]];
    }

    /** Create a constant pool parser by loading the bytecodes of the
     *  class taken as argument.
     *
     * @param templateClass the class to parse.
     *
     * @throws IOException raised if an I/O occurs when loading
     *  the bytecode of the template class.
     * @throws InvalidConstantPoolFormatException if the header of the class has errors.
     *
     * @see #ConstantPoolParser(byte[])
     * @see AnonymousClassLoader#readClassFile(Class)
     */
    public ConstantPoolParser(Class<?> templateClass) throws IOException, InvalidConstantPoolFormatException {
        this(AnonymousClassLoader.readClassFile(templateClass));
    }

    /** Creates an empty patch to patch the class file
     *  used by the current parser.
     * @return a new class patch.
     */
    public ConstantPoolPatch createPatch() {
        return new ConstantPoolPatch(this);
    }

    /** Report the tag of the indicated CP entry.
     * @param index
     * @return one of {@link ConstantPoolVisitor#CONSTANT_Utf8}, etc.
     */
    public byte getTag(int index) {
        getEndOffset();  // trigger an exception if we haven't parsed yet
        return tags[index];
    }

    /** Report the length of the constant pool. */
    public int getLength() {
        return firstHeader[4];
    }

    /** Report the offset, within the class file, of the start of the constant pool. */
    public int getStartOffset() {
        return firstHeader.length * 2;
    }

    /** Report the offset, within the class file, of the end of the constant pool. */
    public int getEndOffset() {
        if (endOffset == 0)
            throw new IllegalStateException("class file has not yet been parsed");
        return endOffset;
    }

    /** Report the CP index of this class's own name. */
    public int getThisClassIndex() {
        getEndOffset();   // provoke exception if not yet parsed
        return secondHeader[1];
    }

    /** Report the total size of the class file. */
    public int getTailLength() {
        return classFile.length - getEndOffset();
    }

    /** Write the head (header plus constant pool)
     *  of the class file to the indicated stream.
     */
    public void writeHead(OutputStream out) throws IOException {
        out.write(classFile, 0, getEndOffset());
    }

    /** Write the head (header plus constant pool)
     *  of the class file to the indicated stream,
     *  incorporating the non-null entries of the given array
     *  as patches.
     */
    void writePatchedHead(OutputStream out, Object[] patchArray) {
        // this will be useful to partially emulate the class loader on old JVMs
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /** Write the tail (everything after the constant pool)
     *  of the class file to the indicated stream.
     */
    public void writeTail(OutputStream out) throws IOException {
        out.write(classFile, getEndOffset(), getTailLength());
    }

    private static char[] parseHeader(byte[] classFile) throws InvalidConstantPoolFormatException {
        char[] result = new char[5];
        ByteBuffer buffer = ByteBuffer.wrap(classFile);
        for (int i = 0; i < result.length; i++)
            result[i] = (char) getUnsignedShort(buffer);
        int magic = result[0] << 16 | result[1] << 0;
        if (magic != 0xCAFEBABE)
            throw new InvalidConstantPoolFormatException("invalid magic number "+magic);
        // skip major, minor version
        int len = result[4];
        if (len < 1)
            throw new InvalidConstantPoolFormatException("constant pool length < 1");
        return result;
    }

    /** Parse the constant pool of the class
     *  calling a method visit* each time a constant pool entry is parsed.
     *
     *  The order of the calls to visit* is not guaranteed to be the same
     *  than the order of the constant pool entry in the bytecode array.
     *
     * @param visitor
     * @throws InvalidConstantPoolFormatException
     */
    public void parse(ConstantPoolVisitor visitor) throws InvalidConstantPoolFormatException {
        ByteBuffer buffer = ByteBuffer.wrap(classFile);
        buffer.position(getStartOffset()); //skip header

        Object[] values = new Object[getLength()];
        try {
            parseConstantPool(buffer, values, visitor);
        } catch(BufferUnderflowException e) {
            throw new InvalidConstantPoolFormatException(e);
        }
        if (endOffset == 0) {
            endOffset = buffer.position();
            secondHeader = new char[4];
            for (int i = 0; i < secondHeader.length; i++) {
                secondHeader[i] = (char) getUnsignedShort(buffer);
            }
        }
        resolveConstantPool(values, visitor);
    }

    private char[] getCharArray(int utfLength) {
        if (utfLength <= charArray.length)
            return charArray;
        return charArray = new char[utfLength];
    }

    private void parseConstantPool(ByteBuffer buffer, Object[] values, ConstantPoolVisitor visitor) throws InvalidConstantPoolFormatException {
        for (int i = 1; i < tags.length; ) {
            byte tag = (byte) getUnsignedByte(buffer);
            assert(tags[i] == 0 || tags[i] == tag);
            tags[i] = tag;
            switch (tag) {
                case CONSTANT_Utf8:
                    int utfLen = getUnsignedShort(buffer);
                    String value = getUTF8(buffer, utfLen, getCharArray(utfLen));
                    visitor.visitUTF8(i, CONSTANT_Utf8, value);
                    tags[i] = tag;
                    values[i++] = value;
                    break;
                case CONSTANT_Integer:
                    visitor.visitConstantValue(i, tag, buffer.getInt());
                    i++;
                    break;
                case CONSTANT_Float:
                    visitor.visitConstantValue(i, tag, buffer.getFloat());
                    i++;
                    break;
                case CONSTANT_Long:
                    visitor.visitConstantValue(i, tag, buffer.getLong());
                    i+=2;
                    break;
                case CONSTANT_Double:
                    visitor.visitConstantValue(i, tag, buffer.getDouble());
                    i+=2;
                    break;

                case CONSTANT_Class:    // fall through:
                case CONSTANT_String:
                    tags[i] = tag;
                    values[i++] = new int[] { getUnsignedShort(buffer) };
                    break;

                case CONSTANT_Fieldref:           // fall through:
                case CONSTANT_Methodref:          // fall through:
                case CONSTANT_InterfaceMethodref: // fall through:
                case CONSTANT_NameAndType:
                    tags[i] = tag;
                    values[i++] = new int[] { getUnsignedShort(buffer), getUnsignedShort(buffer) };
                    break;
                default:
                    throw new AssertionError("invalid constant "+tag);
            }
        }
    }

    private void resolveConstantPool(Object[] values, ConstantPoolVisitor visitor) {
        // clean out the int[] values, which are temporary
        for (int beg = 1, end = values.length-1, beg2, end2;
             beg <= end;
             beg = beg2, end = end2) {
             beg2 = end; end2 = beg-1;
             //System.out.println("CP resolve pass: "+beg+".."+end);
             for (int i = beg; i <= end; i++) {
                  Object value = values[i];
                  if (!(value instanceof int[]))
                      continue;
                  int[] array = (int[]) value;
                  byte tag = tags[i];
                  switch (tag) {
                      case CONSTANT_String:
                          String stringBody = (String) values[array[0]];
                          visitor.visitConstantString(i, tag, stringBody, array[0]);
                          values[i] = null;
                          break;
                      case CONSTANT_Class: {
                          String className = (String) values[array[0]];
                          // use the external form favored by Class.forName:
                          className = className.replace('/', '.');
                          visitor.visitConstantString(i, tag, className, array[0]);
                          values[i] = className;
                          break;
                      }
                      case CONSTANT_NameAndType: {
                          String memberName = (String) values[array[0]];
                          String signature  = (String) values[array[1]];
                          visitor.visitDescriptor(i, tag, memberName, signature,
                                                  array[0], array[1]);
                          values[i] = new String[] {memberName, signature};
                          break;
                      }
                      case CONSTANT_Fieldref:           // fall through:
                      case CONSTANT_Methodref:          // fall through:
                      case CONSTANT_InterfaceMethodref: {
                              Object className   = values[array[0]];
                              Object nameAndType = values[array[1]];
                              if (!(className instanceof String) ||
                                  !(nameAndType instanceof String[])) {
                                   // one more pass is needed
                                   if (beg2 > i)  beg2 = i;
                                   if (end2 < i)  end2 = i;
                                   continue;
                              }
                              String[] nameAndTypeArray = (String[]) nameAndType;
                              visitor.visitMemberRef(i, tag,
                                  (String)className,
                                  nameAndTypeArray[0],
                                  nameAndTypeArray[1],
                                  array[0], array[1]);
                              values[i] = null;
                          }
                          break;
                      default:
                          continue;
                }
            }
        }
    }

    private static int getUnsignedByte(ByteBuffer buffer) {
        return buffer.get() & 0xFF;
    }

    private static int getUnsignedShort(ByteBuffer buffer) {
        int b1 = getUnsignedByte(buffer);
        int b2 = getUnsignedByte(buffer);
        return (b1 << 8) + (b2 << 0);
    }

    private static String getUTF8(ByteBuffer buffer, int utfLen, char[] charArray) throws InvalidConstantPoolFormatException {
      int utfLimit = buffer.position() + utfLen;
      int index = 0;
      while (buffer.position() < utfLimit) {
          int c = buffer.get() & 0xff;
          if (c > 127) {
              buffer.position(buffer.position() - 1);
              return getUTF8Extended(buffer, utfLimit, charArray, index);
          }
          charArray[index++] = (char)c;
      }
      return new String(charArray, 0, index);
    }

    private static String getUTF8Extended(ByteBuffer buffer, int utfLimit, char[] charArray, int index) throws InvalidConstantPoolFormatException {
        int c, c2, c3;
        while (buffer.position() < utfLimit) {
            c = buffer.get() & 0xff;
            switch (c >> 4) {
                case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                    /* 0xxxxxxx*/
                    charArray[index++] = (char)c;
                    break;
                case 12: case 13:
                    /* 110x xxxx   10xx xxxx*/
                    c2 = buffer.get();
                    if ((c2 & 0xC0) != 0x80)
                        throw new InvalidConstantPoolFormatException(
                            "malformed input around byte " + buffer.position());
                     charArray[index++] = (char)(((c  & 0x1F) << 6) |
                                                  (c2 & 0x3F));
                    break;
                case 14:
                    /* 1110 xxxx  10xx xxxx  10xx xxxx */
                    c2 = buffer.get();
                    c3 = buffer.get();
                    if (((c2 & 0xC0) != 0x80) || ((c3 & 0xC0) != 0x80))
                       throw new InvalidConstantPoolFormatException(
                          "malformed input around byte " + (buffer.position()));
                    charArray[index++] = (char)(((c  & 0x0F) << 12) |
                                                ((c2 & 0x3F) << 6)  |
                                                ((c3 & 0x3F) << 0));
                    break;
                default:
                    /* 10xx xxxx,  1111 xxxx */
                    throw new InvalidConstantPoolFormatException(
                        "malformed input around byte " + buffer.position());
            }
        }
        // The number of chars produced may be less than utflen
        return new String(charArray, 0, index);
    }
}