001: /*
002: * Copyright 2004,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.bsf.util.event.generator;
018:
019: /**
020: * Bytecode handling utilities
021: *
022: * Handle standard byte arrays as defined in Java VM and Class File
023: *
024: * 5 April 1999 - functions to append Class File byte subarrays
025: * into a Class File byte array
026: *
027: * @author Richard F. Boehme
028: *
029: */
030: public class Bytecode {
031: // Constant Pool Item Codes
032: public static final byte C_Utf8 = 0x01; // 1
033: public static final byte C_Integer = 0x03; // 3
034: public static final byte C_Float = 0x04; // 4
035: public static final byte C_Long = 0x05; // 5
036: public static final byte C_Double = 0x06; // 6
037: public static final byte C_Class = 0x07; // 7
038: public static final byte C_String = 0x08; // 8
039: public static final byte C_FieldRef = 0x09; // 9
040: public static final byte C_MethodRef = 0x0A; // 10
041: public static final byte C_InterfaceMethodRef = 0x0B; // 11
042: public static final byte C_NameAndType = 0x0C; // 12
043:
044: //public static byte[] addDouble(byte[] array,double value)
045: //{
046: // array = ByteUtility.addBytes(array,C_Double);
047: // array = ByteUtility.addBytes(array,value);
048: // return array;
049: //}
050:
051: public static byte[] addClass(byte[] array, short value) {
052: return addRef(C_Class, array, value);
053: }
054:
055: public static byte[] addFieldRef(byte[] array, short value1,
056: short value2) {
057: return addRef(C_FieldRef, array, value1, value2);
058: }
059:
060: public static byte[] addInteger(byte[] array, int value) {
061: array = ByteUtility.addBytes(array, C_Integer);
062: array = ByteUtility.addBytes(array, value);
063: return array;
064: }
065:
066: public static byte[] addInterfaceMethodRef(byte[] array,
067: short value1, short value2) {
068: return addRef(C_InterfaceMethodRef, array, value1, value2);
069: }
070:
071: //public static byte[] addFloat(byte[] array,float value)
072: //{
073: // array = ByteUtility.addBytes(array,C_Float);
074: // array = ByteUtility.addBytes(array,value);
075: // return array;
076: //}
077:
078: public static byte[] addLong(byte[] array, long value) {
079: array = ByteUtility.addBytes(array, C_Long);
080: array = ByteUtility.addBytes(array, value);
081: return array;
082: }
083:
084: public static byte[] addMethodRef(byte[] array, short value1,
085: short value2) {
086: return addRef(C_MethodRef, array, value1, value2);
087: }
088:
089: public static byte[] addNameAndType(byte[] array, short value1,
090: short value2) {
091: return addRef(C_NameAndType, array, value1, value2);
092: }
093:
094: public static byte[] addRef(byte refType, byte[] array, short value) {
095: array = ByteUtility.addBytes(array, refType);
096: array = ByteUtility.addBytes(array, value);
097: return array;
098: }
099:
100: // Generic Bytecode Methods
101: public static byte[] addRef(byte refType, byte[] array,
102: short value1, short value2) {
103: array = ByteUtility.addBytes(array, refType);
104: array = ByteUtility.addBytes(array, value1);
105: array = ByteUtility.addBytes(array, value2);
106: return array;
107: }
108:
109: public static byte[] addString(byte[] array, short value) {
110: return addRef(C_String, array, value);
111: }
112:
113: // Constant Pool Item Methods
114: public static byte[] addUtf8(byte[] array, String value) {
115: array = ByteUtility.addBytes(array, C_Utf8);
116: array = ByteUtility.addBytes(array, (short) value.length());
117: array = ByteUtility.addBytes(array, value);
118: return array;
119: }
120: }
|