0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/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.
0N/A */
0N/A
0N/Apackage ilib;
0N/A
0N/Aclass ClassReaderWriter implements RuntimeConstants {
0N/A
0N/A int codeAttributeIndex;
0N/A int lineNumberAttributeIndex;
0N/A int localVarAttributeIndex;
0N/A
0N/A private final byte[] orig;
0N/A private final byte[] gen;
0N/A private final int sectionLength;
0N/A
0N/A private static final int GROWTH_FACTOR = 2;
0N/A private static final int SECTIONS = 2;
0N/A private static final String codeAttributeName = "Code";
0N/A private static final String lineNumberAttributeName = "LineNumberTable";
0N/A private static final String localVarAttributeName = "LocalVariableTable";
0N/A
0N/A private int[] genSectionPos = new int[SECTIONS];
0N/A
0N/A private int inputPos = 0;
0N/A private int genPos = 0;
0N/A private int markPos = 0;
0N/A private int currentSection = 0;
0N/A
0N/A private String[] constantPool;
0N/A
0N/A ClassReaderWriter(byte[] orig) {
0N/A this.orig = orig;
0N/A sectionLength = orig.length * GROWTH_FACTOR;
0N/A gen = new byte[sectionLength * SECTIONS];
0N/A for (int section = 0; section < SECTIONS; ++section) {
0N/A genSectionPos[section] = section * sectionLength;
0N/A }
0N/A }
0N/A
0N/A int setSection(int section) {
0N/A int prevSection = currentSection;
0N/A genSectionPos[prevSection] = genPos;
0N/A genPos = genSectionPos[section];
0N/A currentSection = section;
0N/A return prevSection;
0N/A }
0N/A
0N/A byte[] result() {
0N/A int section;
0N/A int totalLength = 0;
0N/A
0N/A setSection(0); // save current section
0N/A
0N/A for (section = 0; section < SECTIONS; ++section) {
0N/A int sectionStart = section * sectionLength;
0N/A int sectionGenLength = genSectionPos[section] - sectionStart;
0N/A totalLength += sectionGenLength;
0N/A }
0N/A
0N/A byte[] newcf = new byte[totalLength];
0N/A int written = 0;
0N/A for (section = 0; section < SECTIONS; ++section) {
0N/A int sectionStart = section * sectionLength;
0N/A int sectionGenLength = genSectionPos[section] - sectionStart;
0N/A System.arraycopy(gen, sectionStart, newcf, written, sectionGenLength);
0N/A written += sectionGenLength;
0N/A }
0N/A
0N/A return newcf;
0N/A }
0N/A
0N/A int readU1() {
0N/A return ((int)orig[inputPos++]) & 0xFF;
0N/A }
0N/A
0N/A int readU2() {
0N/A int res = readU1();
0N/A return (res << 8) + readU1();
0N/A }
0N/A
0N/A short readS2() {
0N/A int res = readU1();
0N/A return (short)((res << 8) + readU1());
0N/A }
0N/A
0N/A int readU4() {
0N/A int res = readU2();
0N/A return (res << 16) + readU2();
0N/A }
0N/A
0N/A void writeU1(int val) {
0N/A gen[genPos++] = (byte)val;
0N/A }
0N/A
0N/A void writeU2(int val) {
0N/A writeU1(val >> 8);
0N/A writeU1(val & 0xFF);
0N/A }
0N/A
0N/A void writeU4(int val) {
0N/A writeU2(val >> 16);
0N/A writeU2(val & 0xFFFF);
0N/A }
0N/A
0N/A int copyU1() {
0N/A int value = readU1();
0N/A writeU1(value);
0N/A return value;
0N/A }
0N/A
0N/A int copyU2() {
0N/A int value = readU2();
0N/A writeU2(value);
0N/A return value;
0N/A }
0N/A
0N/A int copyU4() {
0N/A int value = readU4();
0N/A writeU4(value);
0N/A return value;
0N/A }
0N/A
0N/A void copy(int count) {
0N/A for (int i = 0; i < count; ++i) {
0N/A gen[genPos++] = orig[inputPos++];
0N/A }
0N/A }
0N/A
0N/A void skip(int count) {
0N/A inputPos += count;
0N/A }
0N/A
0N/A byte[] readBytes(int count) {
0N/A byte[] bytes = new byte[count];
0N/A for (int i = 0; i < count; ++i) {
0N/A bytes[i] = orig[inputPos++];
0N/A }
0N/A return bytes;
0N/A }
0N/A
0N/A void writeBytes(byte[] bytes) {
0N/A for (int i = 0; i < bytes.length; ++i) {
0N/A gen[genPos++] = bytes[i];
0N/A }
0N/A }
0N/A
0N/A byte[] inputBytes() {
0N/A return orig;
0N/A }
0N/A
0N/A int inputPosition() {
0N/A return inputPos;
0N/A }
0N/A
0N/A void setInputPosition(int pos) {
0N/A inputPos = pos;
0N/A }
0N/A
0N/A void markLocalPositionStart() {
0N/A markPos = inputPos;
0N/A }
0N/A
0N/A int localPosition() {
0N/A return inputPos - markPos;
0N/A }
0N/A
0N/A void rewind() {
0N/A setInputPosition(markPos);
0N/A }
0N/A
0N/A int generatedPosition() {
0N/A return genPos;
0N/A }
0N/A
0N/A void randomAccessWriteU2(int pos, int val) {
0N/A int savePos = genPos;
0N/A genPos = pos;
0N/A writeU2(val);
0N/A genPos = savePos;
0N/A }
0N/A
0N/A void randomAccessWriteU4(int pos, int val) {
0N/A int savePos = genPos;
0N/A genPos = pos;
0N/A writeU4(val);
0N/A genPos = savePos;
0N/A }
0N/A
0N/A String constantPoolString(int index) {
0N/A return constantPool[index];
0N/A }
0N/A
0N/A void copyConstantPool(int constantPoolCount){
0N/A // copy const pool
0N/A constantPool = new String[constantPoolCount];
0N/A // index zero not in class file
0N/A for (int i = 1; i < constantPoolCount; ++i) {
0N/A int tag = readU1();
0N/A writeU1(tag);
0N/A switch (tag) {
0N/A case CONSTANT_CLASS:
0N/A case CONSTANT_STRING:
0N/A copy(2);
0N/A break;
0N/A case CONSTANT_FIELD:
0N/A case CONSTANT_METHOD:
0N/A case CONSTANT_INTERFACEMETHOD:
0N/A case CONSTANT_INTEGER:
0N/A case CONSTANT_FLOAT:
0N/A case CONSTANT_NAMEANDTYPE:
0N/A copy(4);
0N/A break;
0N/A case CONSTANT_LONG:
0N/A case CONSTANT_DOUBLE:
0N/A copy(8);
0N/A ++i; // these take two CP entries - duh!
0N/A break;
0N/A case CONSTANT_UTF8:
0N/A int len = copyU2();
0N/A byte[] utf8 = readBytes(len);
0N/A String str = null; // null to shut the compiler up
0N/A try {
0N/A str = new String(utf8, "UTF-8");
0N/A } catch (Exception exc) {
0N/A throw new Error("CP exception: " + exc);
0N/A }
0N/A constantPool[i] = str;
0N/A if (str.equals(codeAttributeName)) {
0N/A codeAttributeIndex = i;
0N/A } else if (str.equals(lineNumberAttributeName)) {
0N/A lineNumberAttributeIndex = i;
0N/A } else if (str.equals(localVarAttributeName)) {
0N/A localVarAttributeIndex = i;
0N/A }
0N/A writeBytes(utf8);
0N/A break;
0N/A default:
0N/A throw new Error(i + " unexpected CP tag: " + tag);
0N/A }
0N/A }
0N/A }
0N/A
0N/A}