/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache BCEL" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache BCEL", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*/
/**
* Utility functions that do not really belong to any class in particular.
*
* @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
*/
public abstract class Utility {
* during parsing in signatureToString().
* Read by methodSignatureToString().
* Set by side effect,but only internally.
*/
* byte code to allow 16-bit wide indices
* for local variables. This opcode
* precedes an `ILOAD', e.g.. The opcode
* immediately following takes an extra
* byte which is combined with the
* following byte to form a
* 16-bit value.
*/
/**
* Convert bit field of flags into string such as `static final'.
*
* @param access_flags Access flags
* @return String representation of flags
*/
return accessToString(access_flags, false);
}
/**
* Convert bit field of flags into string such as `static final'.
*
* Special case: Classes compiled with new compilers and with the
* `ACC_SUPER' flag would be said to be "synchronized". This is
* because SUN used the same value for the flags `ACC_SUPER' and
* `ACC_SYNCHRONIZED'.
*
* @param access_flags Access flags
* @param for_class access flags are for class qualifiers ?
* @return String representation of flags
*/
boolean for_class)
{
int p = 0;
p = pow2(i);
if((access_flags & p) != 0) {
/* Special case: Classes compiled with new compilers and with the
* `ACC_SUPER' flag would be said to be "synchronized". This is
* because SUN used the same value for the flags `ACC_SUPER' and
* `ACC_SYNCHRONIZED'.
*/
continue;
}
}
}
/**
* @return "class" or "interface", depending on the ACC_INTERFACE flag
*/
}
/**
* Disassemble a byte array of JVM byte codes starting from code line
* `index' and return the disassembled string representation. Decode only
* `num' opcodes (including their operands), use -1 if you want to
* decompile everything.
*
* @param code byte code array
* @param constant_pool Array of constants
* @param index offset in `code' array
* <EM>(number of opcodes, not bytes!)</EM>
* @param length number of opcodes to decompile, -1 for all
* @param verbose be verbose, e.g. print constant pool index
* @return String representation of byte codes
*/
{
try {
}
}
} catch(IOException e) {
e.printStackTrace();
throw new ClassFormatException("Byte code error: " + e);
}
}
}
/**
* Disassemble a stream of byte codes and return the
* string representation.
*
* @param bytes stream of bytes
* @param constant_pool Array of constants
* @param verbose be verbose, e.g. print constant pool index
* @return String representation of byte code
*/
throws IOException
{
int[] match, jump_table;
/* Special case: Skip (0-3) padding bytes, i.e., the
* following bytes are 4-byte-aligned
*/
for(int i=0; i < no_pad_bytes; i++) {
byte b;
}
// Both cases have a field default_offset in common
}
switch(opcode) {
/* Table switch has variable length arguments.
*/
case Constants.TABLESWITCH:
default_offset += offset;
}
break;
/* Lookup switch has variable length arguments.
*/
case Constants.LOOKUPSWITCH: {
jump_table = new int[npairs];
default_offset += offset;
" (");
for(int i=0; i < npairs; i++) {
if(i < npairs - 1)
}
}
break;
/* Two address bytes + offset from start of byte stream form the
* jump target
*/
case Constants.IF_ACMPNE: case Constants.IF_ICMPEQ: case Constants.IF_ICMPGE: case Constants.IF_ICMPGT:
break;
/* 32-bit wide jumps
*/
break;
/* Index byte references local variable (register)
*/
case Constants.ALOAD: case Constants.ASTORE: case Constants.DLOAD: case Constants.DSTORE: case Constants.FLOAD:
case Constants.FSTORE: case Constants.ILOAD: case Constants.ISTORE: case Constants.LLOAD: case Constants.LSTORE:
if(wide) {
wide=false; // Clear flag
}
else
break;
/*
* Remember wide byte which is used to form a 16-bit address in the
* following instruction. Relies on that the method is called again with
* the following opcode.
*/
wide = true;
break;
/* Array of basic type.
*/
break;
*/
case Constants.GETFIELD: case Constants.GETSTATIC: case Constants.PUTFIELD: case Constants.PUTSTATIC:
break;
/* Operands are references to classes in constant pool
*/
case Constants.INSTANCEOF:
break;
/* Operands are references to methods in constant pool
*/
break;
case Constants.INVOKEINTERFACE:
break;
/* Operands are references to items in constant pool
*/
break;
break;
/* Array of references.
*/
break;
/* Multidimensional array of references.
*/
case Constants.MULTIANEWARRAY: {
}
break;
/* Increment local variable.
*/
if(wide) {
wide = false;
}
else {
}
break;
default:
default: // Never reached
}
}
}
}
}
throws IOException
{
}
/**
* <em>String</em>.
*
* @param str The long class name
* @return Compacted class name
*/
return compactClassName(str, true);
}
/**
* Shorten long class name <em>str</em>, i.e., chop off the <em>prefix</em>,
* if the
* class name starts with this string and the flag <em>chopit</em> is true.
* Slashes <em>/</em> are converted to dots <em>.</em>.
*
* @param str The long class name
* @param prefix The prefix the get rid off
* @param chopit Flag that determines whether chopping is executed or not
* @return Compacted class name
*/
boolean chopit)
{
if(chopit) {
// If string starts with `prefix' and contains no further dots
}
return str;
}
/**
* <em>java.lang.String</em>,
* e.g.. If <em>chopit</em> is <em>true</em> the prefix <em>java.lang</em>
* is also removed.
*
* @param str The long class name
* @param chopit Flag that determines whether chopping is executed or not
* @return Compacted class name
*/
}
}
}
/**
* @return `flag' with bit `i' set to 1
*/
}
/**
* @return `flag' with bit `i' set to 0
*/
}
/**
* @return true, if bit `i' in `flag' is set
*/
}
/**
* Converts string containing the method return and argument types
* to a byte code method signature.
*
* @param ret Return type of method
* @param argv Types of method arguments
* @return Byte code representation of method signature
*/
throws ClassFormatException
{
}
}
/**
* @param signature Method signature
* @return Array of argument types
* @throws ClassFormatException
*/
throws ClassFormatException
{
return methodSignatureArgumentTypes(signature, true);
}
/**
* @param signature Method signature
* @param chopit Shorten class names ?
* @return Array of argument types
* @throws ClassFormatException
*/
boolean chopit)
throws ClassFormatException
{
int index;
try { // Read all declarations between for `(' and `)'
}
} catch(StringIndexOutOfBoundsException e) { // Should never occur
}
return types;
}
/**
* @param signature Method signature
* @return return type of method
* @throws ClassFormatException
*/
throws ClassFormatException
{
return methodSignatureReturnType(signature, true);
}
/**
* @param signature Method signature
* @param chopit Shorten class names ?
* @return return type of method
* @throws ClassFormatException
*/
boolean chopit)
throws ClassFormatException
{
int index;
try {
// Read return type after `)'
} catch(StringIndexOutOfBoundsException e) { // Should never occur
}
return type;
}
/**
* Converts method signature to string with all class names compacted.
*
* @param signature to convert
* @param name of method
* @param access flags of method
* @return Human readable signature
*/
}
boolean chopit) {
}
/**
* A return type signature represents the return value from a method.
* It is a series of bytes in the following grammar:
*
* <return_signature> ::= <field_type> | V
*
* The character V indicates that the method returns no value. Otherwise, the
* signature indicates the type of the return value.
* An argument signature represents an argument passed to a method:
*
* <argument_signature> ::= <field_type>
*
* A method signature represents the arguments that the method expects, and
* the value that it returns.
* <method_signature> ::= (<arguments_signature>) <return_signature>
* <arguments_signature>::= <argument_signature>*
*
* This method converts such a string into a Java type declaration like
* `void _main(String[])' and throws a `ClassFormatException' when the parsed
* type is invalid.
*
* @param signature Method signature
* @param name Method name
* @param access Method access rights
* @return Java type declaration
* @throws ClassFormatException
*/
boolean chopit,
throws ClassFormatException
{
int index;
try { // Read all declarations between for `(' and `)'
if(l != null)
} else
var_index += 2;
else
var_index++;
}
index++; // update position
// Read return type after `)'
} catch(StringIndexOutOfBoundsException e) { // Should never occur
}
}
// Guess what this does
private static final int pow2(int n) {
return 1 << n;
}
/**
* Replace all occurences of <em>old</em> in <em>str</em> with <em>new</em>.
*
* @param str String to permute
* @param old String to be replaced
* @param new Replacement string
* @return new String object
*/
try {
// While we have something to replace
}
}
} catch(StringIndexOutOfBoundsException e) { // Should not occur
}
return str;
}
/**
* Converts signature to string with all class names compacted.
*
* @param signature to convert
* @return Human readable signature
*/
return signatureToString(signature, true);
}
/**
* The field signature represents the value of an argument to a function or
* the value of a variable. It is a series of bytes generated by the
* following grammar:
*
* <PRE>
* <field_signature> ::= <field_type>
* <field_type> ::= <base_type>|<object_type>|<array_type>
* <base_type> ::= B|C|D|F|I|J|S|Z
* <object_type> ::= L<fullclassname>;
* <array_type> ::= [<field_type>
*
* The meaning of the base types is as follows:
* B byte signed byte
* C char character
* D double double precision IEEE float
* F float single precision IEEE float
* I int integer
* J long long integer
* L<fullclassname>; ... an object of the given class
* S short signed short
* Z boolean true or false
* [<field sig> ... array
* </PRE>
*
* This method converts this string into a Java type declaration such as
* `String[]' and throws a `ClassFormatException' when the parsed type is
* invalid.
*
* @param signature Class signature
* @param chopit Flag that determines whether chopping is executed or not
* @return Java type declaration
* @throws ClassFormatException
*/
boolean chopit)
{
try {
case 'B' : return "byte";
case 'C' : return "char";
case 'D' : return "double";
case 'F' : return "float";
case 'I' : return "int";
case 'J' : return "long";
case 'L' : { // Full class name
if(index < 0)
}
case 'S' : return "short";
case 'Z' : return "boolean";
case '[' : { // Array declaration
int n;
char ch;
int consumed_chars; // Shadows global var
// Count opening brackets and look for optional size argument
consumed_chars = n; // Remember value
// The rest of the string denotes a `<field_type>'
}
case 'V' : return "void";
default : throw new ClassFormatException("Invalid signature: `" +
signature + "'");
}
} catch(StringIndexOutOfBoundsException e) { // Should never occur
}
}
/** Parse Java type such as "char", or "java.lang.String[]" and return the
*
* @param type Java type
* @return byte code signature
*/
boolean char_found = false, delim = false;
int index = -1;
loop:
switch(chars[i]) {
case ' ': case '\t': case '\n': case '\r': case '\f':
if(char_found)
delim = true;
break;
case '[':
if(!char_found)
index = i;
break loop;
default:
char_found = true;
if(!delim)
}
}
int brackets = 0;
if(index > 0)
for(int i=0; i < brackets; i++)
boolean found = false;
found = true;
}
}
if(!found) // Class name
}
int count = 0;
boolean open = false;
switch(chars[i]) {
case '[':
if(open)
open = true;
break;
case ']':
if(!open)
open = false;
count++;
break;
default:
// Don't care
}
}
if(open)
return count;
}
/**
* Return type of method signature as a byte value as defined in <em>Constants</em>
*
* @param signature in format described above
* @return type of method signature
* @see Constants
*/
throws ClassFormatException
{
int index;
try {
} catch(StringIndexOutOfBoundsException e) {
}
}
/**
* Return type of signature as a byte value as defined in <em>Constants</em>
*
* @param signature in format described above
* @return type of signature
* @see Constants
*/
throws ClassFormatException
{
try {
default:
}
} catch(StringIndexOutOfBoundsException e) {
}
}
/** Map opcode names to opcode numbers. E.g., return Constants.ALOAD for "aload"
*/
return i;
return -1;
}
/**
* Convert (signed) byte to (unsigned) short value, i.e., all negative
* values become positive.
*/
private static final short byteToShort(byte b) {
return (b < 0)? (short)(256 + b) : (short)b;
}
/** Convert bytes into hexidecimal string
*
* @return bytes as hexidecimal string, e.g. 00 FA 12 ...
*/
short b = byteToShort(bytes[i]);
if(b < 0x10) // just one digit, prepend '0'
}
}
/**
* Return a string for an integer justified left or right and filled up with
* `fill' characters if necessary.
*
* @param i integer to format
* @param length length of desired string
* @param left_justify format left or right
* @param fill fill character
* @return formatted int
*/
}
/**
* Fillup char with up to length characters with char `fill' and justify it left or right.
*
* @param str string to format
* @param length length of desired string
* @param left_justify format left or right
* @param fill fill character
* @return formatted string
*/
if(left_justify)
else
}
static final boolean equals(byte[] a, byte[] b) {
int size;
return false;
for(int i=0; i < size; i++)
if(a[i] != b[i])
return false;
return true;
}
}
}
return printArray(obj, true);
}
}
boolean quote) {
return null;
if(braces)
} else {
}
}
}
if(braces)
}
/** @return true, if character is one of (a, ... z, A, ... Z, 0, ... 9, _)
*/
(ch == '_');
}
/** Encode byte array it into Java identifier string, i.e., a string
* that only contains the following characters: (a, ... z, A, ... Z,
* 0, ... 9, _, $). The encoding algorithm itself is not too
* clever: if the current byte's ASCII value already is a valid Java
* identifier part, leave it as it is. Otherwise it writes the
* escape character($) followed by <p><ul><li> the ASCII value as a
* hexadecimal string, if the value is not in the range
* 200..247</li> <li>a Java identifier char not used in a lowercase
* hexadecimal string, if the value is in the range
* 200..247</li><ul></p>
*
* <p>This operation inflates the original byte array by roughly 40-50%</p>
*
* @param bytes the byte array to convert
* @param compress use gzip to minimize string
*/
if(compress) {
}
}
}
/** Decode a string back to a byte array.
*
* @param bytes the byte array to convert
* @param uncompress use gzip to uncompress the stream of bytes
*/
char[] chars = s.toCharArray();
int ch;
}
if(uncompress) {
int count = 0;
int b;
}
return bytes;
}
// A-Z, g-z, _, $
static {
int j = 0, k = 0;
for(int i='A'; i <= 'Z'; i++) {
CHAR_MAP[j] = i;
MAP_CHAR[i] = j;
j++;
}
for(int i='g'; i <= 'z'; i++) {
CHAR_MAP[j] = i;
MAP_CHAR[i] = j;
j++;
}
CHAR_MAP[j] = '$';
MAP_CHAR['$'] = j;
j++;
CHAR_MAP[j] = '_';
MAP_CHAR['_'] = j;
}
/** Decode characters into bytes.
* Used by <a href="Utility.html#decode(java.lang.String, boolean)">decode()</a>
*/
super(in);
}
if(b != ESCAPE_CHAR) {
return b;
} else {
if(i < 0)
return -1;
if(((i >= '0') && (i <= '9')) || ((i >= 'a') && (i <= 'f'))) { // Normal escape
if(j < 0)
return -1;
char[] tmp = { (char)i, (char)j };
return s;
} else { // Special escape
return MAP_CHAR[i];
}
}
}
for(int i=0; i < len; i++)
return len;
}
}
/** Encode bytes into valid java identifier characters.
* Used by <a href="Utility.html#encode(byte[], boolean)">encode()</a>
*/
super(out);
}
if(isJavaIdentifierPart((char)b) && (b != ESCAPE_CHAR)) {
} else {
// Special escape
if(b >= 0 && b < FREE_CHARS) {
} else { // Normal escape
} else {
}
}
}
}
for(int i=0; i < len; i++)
}
}
}
/**
* Escape all occurences of newline chars '\n', quotes \", etc.
*/
switch(ch[i]) {
case '\n':
case '\r':
case '\"':
case '\'':
case '\\':
default:
}
}
}
}