| Code with Finding: |
class Utility {
/**
* 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
*/
public static String signatureToString( String signature, boolean chopit ) {
//corrected concurrent private static field acess
wrap(consumed_chars, 1); // This is the default, read just one char like `B'
try {
switch (signature.charAt(0)) {
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 'T': { // TypeVariableSignature
int index = signature.indexOf(';'); // Look for closing `;'
if (index < 0) {
throw new ClassFormatException("Invalid signature: " + signature);
}
//corrected concurrent private static field acess
wrap(consumed_chars, index + 1); // "Tblabla;" `T' and `;' are removed
return compactClassName(signature.substring(1, index), chopit);
}
case 'L': { // Full class name
// should this be a while loop? can there be more than
// one generic clause? (markro)
int fromIndex = signature.indexOf('<'); // generic type?
if (fromIndex < 0) {
fromIndex = 0;
} else {
fromIndex = signature.indexOf('>', fromIndex);
if (fromIndex < 0) {
throw new ClassFormatException("Invalid signature: " + signature);
}
}
int index = signature.indexOf(';', fromIndex); // Look for closing `;'
if (index < 0) {
throw new ClassFormatException("Invalid signature: " + signature);
}
// check to see if there are any TypeArguments
int bracketIndex = signature.substring(0, index).indexOf('<');
if (bracketIndex < 0) {
// just a class identifier
wrap(consumed_chars, index + 1); // "Lblabla;" `L' and `;' are removed
return compactClassName(signature.substring(1, index), chopit);
}
// we have TypeArguments; build up partial result
// as we recurse for each TypeArgument
StringBuilder type = new StringBuilder(compactClassName(signature.substring(1, bracketIndex), chopit)).append("<");
int consumed_chars = bracketIndex + 1; // Shadows global var
// check for wildcards
if (signature.charAt(consumed_chars) == '+') {
type.append("? extends ");
consumed_chars++;
} else if (signature.charAt(consumed_chars) == '-') {
type.append("? super ");
consumed_chars++;
} else if (signature.charAt(consumed_chars) == '*') {
// must be at end of signature
if (signature.charAt(consumed_chars + 1) != '>') {
throw new ClassFormatException("Invalid signature: " + signature);
}
if (signature.charAt(consumed_chars + 2) != ';') {
throw new ClassFormatException("Invalid signature: " + signature);
}
wrap(Utility.consumed_chars, consumed_chars + 3); // remove final "*>;"
return type + "?>...";
}
// get the first TypeArgument
type.append(signatureToString(signature.substring(consumed_chars), chopit));
// update our consumed count by the number of characters the for type argument
consumed_chars = unwrap(Utility.consumed_chars) + consumed_chars;
wrap(Utility.consumed_chars, consumed_chars);
// are there more TypeArguments?
while (signature.charAt(consumed_chars) != '>') {
type.append(", ").append(signatureToString(signature.substring(consumed_chars), chopit));
// update our consumed count by the number of characters the for type argument
consumed_chars = unwrap(Utility.consumed_chars) + consumed_chars;
wrap(Utility.consumed_chars, consumed_chars);
}
if (signature.charAt(consumed_chars + 1) != ';') {
throw new ClassFormatException("Invalid signature: " + signature);
}
wrap(Utility.consumed_chars, consumed_chars + 2); // remove final ">;"
return type.append(">").toString();
}
case 'S':
return "short";
case 'Z':
return "boolean";
case '[': { // Array declaration
int n;
StringBuilder brackets;
String type;
int consumed_chars; // Shadows global var
brackets = new StringBuilder(); // Accumulate []'s
// Count opening brackets and look for optional size argument
for (n = 0; signature.charAt(n) == '['; n++) {
brackets.append("[]");
}
consumed_chars = n; // Remember value
// The rest of the string denotes a `<field_type>'
type = signatureToString(signature.substring(n), chopit);
//corrected concurrent private static field acess
//Utility.consumed_chars += consumed_chars; is replaced by:
int _temp = unwrap(Utility.consumed_chars) + consumed_chars;
wrap(Utility.consumed_chars, _temp);
return type + brackets.toString();
}
case 'V':
return "void";
default:
throw new ClassFormatException("Invalid signature: `" + signature + "'");
}
} catch (StringIndexOutOfBoundsException e) { // Should never occur
throw new ClassFormatException("Invalid signature: " + signature, e);
}
}
}
|