proguard.classfile.util
Class ClassUtil

java.lang.Object
  extended by proguard.classfile.util.ClassUtil

public class ClassUtil
extends java.lang.Object

Utility methods for converting between internal and external representations of names and descriptions.


Constructor Summary
ClassUtil()
           
 
Method Summary
static void checkMagicNumber(int magicNumber)
          Checks whether the given class magic number is correct.
static void checkVersionNumbers(int classVersion)
          Checks whether the given class version number is supported.
static int externalArrayTypeDimensionCount(java.lang.String externalType)
          Returns the number of dimensions of the given external type.
static java.lang.String externalClassAccessFlags(int accessFlags)
          Converts internal class access flags into an external access description.
static java.lang.String externalClassAccessFlags(int accessFlags, java.lang.String prefix)
          Converts internal class access flags into an external access description.
static java.lang.String externalClassName(java.lang.String internalClassName)
          Converts an internal class name into an external class name.
static java.lang.String externalClassVersion(int classVersion)
          Returns the minor part of the given class version number.
static java.lang.String externalFieldAccessFlags(int accessFlags)
          Converts internal field access flags into an external access description.
static java.lang.String externalFieldAccessFlags(int accessFlags, java.lang.String prefix)
          Converts internal field access flags into an external access description.
static java.lang.String externalFullClassDescription(int accessFlags, java.lang.String internalClassName)
          Converts an internal class description into an external class description.
static java.lang.String externalFullFieldDescription(int accessFlags, java.lang.String fieldName, java.lang.String internalFieldDescriptor)
          Converts an internal field description into an external full field description.
static java.lang.String externalFullMethodDescription(java.lang.String internalClassName, int accessFlags, java.lang.String internalMethodName, java.lang.String internalMethodDescriptor)
          Converts an internal method description into an external full method description.
static java.lang.String externalMethodAccessFlags(int accessFlags)
          Converts internal method access flags into an external access description.
static java.lang.String externalMethodAccessFlags(int accessFlags, java.lang.String prefix)
          Converts internal method access flags into an external access description.
static java.lang.String externalMethodArguments(java.lang.String internalMethodDescriptor)
          Converts an internal method descriptor into an external method argument description.
static java.lang.String externalMethodName(java.lang.String externalMethodNameAndArguments)
          Returns the name part of the given external method name and arguments.
static java.lang.String externalMethodReturnType(java.lang.String internalMethodDescriptor)
          Converts an internal method descriptor into an external method return type.
static java.lang.String externalPackageName(java.lang.String externalClassName)
          Returns the external package name of the given external class name.
static java.lang.String externalPackagePrefix(java.lang.String externalClassName)
          Returns the external package prefix of the given external class name.
static java.lang.String externalShortClassName(java.lang.String externalClassName)
          Converts an internal class name into an external short class name, without package specification.
static java.lang.String externalType(java.lang.String internalType)
          Converts an internal type into an external type.
static int internalArrayTypeDimensionCount(java.lang.String internalType)
          Returns the number of dimensions of the given internal type.
static java.lang.String internalArrayTypeFromClassName(java.lang.String internalClassName, int dimensionCount)
          Returns the internal array type of a given class name with a given number of dimensions.
static java.lang.String internalClassName(java.lang.String externalClassName)
          Converts an external class name into an internal class name.
static java.lang.String internalClassNameFromClassType(java.lang.String internalClassType)
          Returns the internal class name of a given internal class type (including an array type).
static java.lang.String internalClassNameFromType(java.lang.String internalClassType)
          Returns the internal class name of any given internal descriptor type, disregarding array prefixes.
static int internalClassVersion(int majorVersion, int minorVersion)
          Returns the combined class version number.
static int internalClassVersion(java.lang.String classVersion)
          Returns the internal class version number.
static int internalMajorClassVersion(int classVersion)
          Returns the major part of the given class version number.
static java.lang.String internalMethodDescriptor(java.lang.String externalReturnType, java.util.List externalArguments)
          Converts the given external method return type and List of arguments to an internal method descriptor.
static java.lang.String internalMethodDescriptor(java.lang.String externalReturnType, java.lang.String externalMethodNameAndArguments)
          Converts the given external method return type and name and arguments to an internal method descriptor.
static int internalMethodParameterCount(java.lang.String internalMethodDescriptor)
          Returns the number of parameters of the given internal method descriptor.
static int internalMethodParameterSize(java.lang.String internalMethodDescriptor)
          Returns the size taken up on the stack by the parameters of the given internal method descriptor.
static int internalMethodParameterSize(java.lang.String internalMethodDescriptor, boolean isStatic)
          Returns the size taken up on the stack by the parameters of the given internal method descriptor.
static int internalMethodParameterSize(java.lang.String internalMethodDescriptor, int accessFlags)
          Returns the size taken up on the stack by the parameters of the given internal method descriptor.
static java.lang.String internalMethodReturnType(java.lang.String internalMethodDescriptor)
          Returns the internal type of the given internal method descriptor.
static int internalMinorClassVersion(int classVersion)
          Returns the internal class version number.
static java.lang.String internalPackageName(java.lang.String internalClassName)
          Returns the internal package name of the given internal class name.
static java.lang.String internalPackagePrefix(java.lang.String internalClassName)
          Returns the internal package prefix of the given internal class name.
static java.lang.String internalType(java.lang.String externalType)
          Converts an external type into an internal type.
static java.lang.String internalTypeFromArrayType(java.lang.String internalArrayType)
          Returns the internal element type of a given internal array type.
static java.lang.String internalTypeFromClassName(java.lang.String internalClassName)
          Returns the internal type of a given class name.
static int internalTypeSize(java.lang.String internalType)
          Returns the size taken up on the stack by the given internal type.
static boolean isExternalMethodNameAndArguments(java.lang.String externalMemberNameAndArguments)
          Returns whether the given member String represents an external method name with arguments.
static boolean isInternalArrayInterfaceName(java.lang.String internalClassName)
          Returns whether the given internal class name is one of the interfaces that is implemented by all array types.
static boolean isInternalArrayType(java.lang.String internalType)
          Returns whether the given internal type is an array type.
static boolean isInternalCategory2Type(java.lang.String internalType)
          Returns whether the given internal type is a primitive Category 2 type.
static boolean isInternalClassType(java.lang.String internalType)
          Returns whether the given internal type is a plain class type (including an array type of a plain class type).
static boolean isInternalMethodDescriptor(java.lang.String internalDescriptor)
          Returns whether the given internal descriptor String represents a method descriptor.
static boolean isInternalPrimitiveType(char internalType)
          Returns whether the given internal type is a plain primitive type (not void).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassUtil

public ClassUtil()
Method Detail

checkMagicNumber

public static void checkMagicNumber(int magicNumber)
                             throws java.lang.UnsupportedOperationException
Checks whether the given class magic number is correct.

Parameters:
magicNumber - the magic number.
Throws:
java.lang.UnsupportedOperationException - when the magic number is incorrect.

internalClassVersion

public static int internalClassVersion(int majorVersion,
                                       int minorVersion)
Returns the combined class version number.

Parameters:
majorVersion - the major part of the class version number.
minorVersion - the minor part of the class version number.
Returns:
the combined class version number.

internalMajorClassVersion

public static int internalMajorClassVersion(int classVersion)
Returns the major part of the given class version number.

Parameters:
classVersion - the combined class version number.
Returns:
the major part of the class version number.

internalMinorClassVersion

public static int internalMinorClassVersion(int classVersion)
Returns the internal class version number.

Parameters:
classVersion - the external class version number.
Returns:
the internal class version number.

internalClassVersion

public static int internalClassVersion(java.lang.String classVersion)
Returns the internal class version number.

Parameters:
classVersion - the external class version number.
Returns:
the internal class version number.

externalClassVersion

public static java.lang.String externalClassVersion(int classVersion)
Returns the minor part of the given class version number.

Parameters:
classVersion - the combined class version number.
Returns:
the minor part of the class version number.

checkVersionNumbers

public static void checkVersionNumbers(int classVersion)
                                throws java.lang.UnsupportedOperationException
Checks whether the given class version number is supported.

Parameters:
classVersion - the combined class version number.
Throws:
java.lang.UnsupportedOperationException - when the version is not supported.

internalClassName

public static java.lang.String internalClassName(java.lang.String externalClassName)
Converts an external class name into an internal class name.

Parameters:
externalClassName - the external class name, e.g. "java.lang.Object"
Returns:
the internal class name, e.g. "java/lang/Object".

externalFullClassDescription

public static java.lang.String externalFullClassDescription(int accessFlags,
                                                            java.lang.String internalClassName)
Converts an internal class description into an external class description.

Parameters:
accessFlags - the access flags of the class.
internalClassName - the internal class name, e.g. "java/lang/Object".
Returns:
the external class description, e.g. "public java.lang.Object".

externalClassName

public static java.lang.String externalClassName(java.lang.String internalClassName)
Converts an internal class name into an external class name.

Parameters:
internalClassName - the internal class name, e.g. "java/lang/Object".
Returns:
the external class name, e.g. "java.lang.Object".

externalShortClassName

public static java.lang.String externalShortClassName(java.lang.String externalClassName)
Converts an internal class name into an external short class name, without package specification.

Parameters:
externalClassName - the external class name, e.g. "java.lang.Object"
Returns:
the external short class name, e.g. "Object".

isInternalArrayType

public static boolean isInternalArrayType(java.lang.String internalType)
Returns whether the given internal type is an array type.

Parameters:
internalType - the internal type, e.g. "[[Ljava/lang/Object;".
Returns:
true if the given type is an array type, false otherwise.

internalArrayTypeDimensionCount

public static int internalArrayTypeDimensionCount(java.lang.String internalType)
Returns the number of dimensions of the given internal type.

Parameters:
internalType - the internal type, e.g. "[[Ljava/lang/Object;".
Returns:
the number of dimensions, e.g. 2.

isInternalArrayInterfaceName

public static boolean isInternalArrayInterfaceName(java.lang.String internalClassName)
Returns whether the given internal class name is one of the interfaces that is implemented by all array types. These class names are "java/lang/Object", "java/lang/Cloneable", and "java/io/Serializable"

Parameters:
internalClassName - the internal class name, e.g. "java/lang/Object".
Returns:
true if the given type is an array interface name, false otherwise.

isInternalPrimitiveType

public static boolean isInternalPrimitiveType(char internalType)
Returns whether the given internal type is a plain primitive type (not void).

Parameters:
internalType - the internal type, e.g. "I".
Returns:
true if the given type is a class type, false otherwise.

isInternalCategory2Type

public static boolean isInternalCategory2Type(java.lang.String internalType)
Returns whether the given internal type is a primitive Category 2 type.

Parameters:
internalType - the internal type, e.g. "L".
Returns:
true if the given type is a Category 2 type, false otherwise.

isInternalClassType

public static boolean isInternalClassType(java.lang.String internalType)
Returns whether the given internal type is a plain class type (including an array type of a plain class type).

Parameters:
internalType - the internal type, e.g. "Ljava/lang/Object;".
Returns:
true if the given type is a class type, false otherwise.

internalTypeFromClassName

public static java.lang.String internalTypeFromClassName(java.lang.String internalClassName)
Returns the internal type of a given class name.

Parameters:
internalClassName - the internal class name, e.g. "java/lang/Object".
Returns:
the internal type, e.g. "Ljava/lang/Object;".

internalArrayTypeFromClassName

public static java.lang.String internalArrayTypeFromClassName(java.lang.String internalClassName,
                                                              int dimensionCount)
Returns the internal array type of a given class name with a given number of dimensions. If the number of dimensions is 0, the class name itself is returned.

Parameters:
internalClassName - the internal class name, e.g. "java/lang/Object".
dimensionCount - the number of array dimensions.
Returns:
the internal array type of the array elements, e.g. "Ljava/lang/Object;".

internalTypeFromArrayType

public static java.lang.String internalTypeFromArrayType(java.lang.String internalArrayType)
Returns the internal element type of a given internal array type.

Parameters:
internalArrayType - the internal array type, e.g. "[[Ljava/lang/Object;" or "[I".
Returns:
the internal type of the array elements, e.g. "Ljava/lang/Object;" or "I".

internalClassNameFromClassType

public static java.lang.String internalClassNameFromClassType(java.lang.String internalClassType)
Returns the internal class name of a given internal class type (including an array type). Types involving primitive types are returned unchanged.

Parameters:
internalClassType - the internal class type, e.g. "[Ljava/lang/Object;", "Ljava/lang/Object;", or "java/lang/Object".
Returns:
the internal class name, e.g. "java/lang/Object".

internalClassNameFromType

public static java.lang.String internalClassNameFromType(java.lang.String internalClassType)
Returns the internal class name of any given internal descriptor type, disregarding array prefixes.

Parameters:
internalClassType - the internal class type, e.g. "Ljava/lang/Object;" or "[[I".
Returns:
the internal class name, e.g. "java/lang/Object" or null.

internalMethodReturnType

public static java.lang.String internalMethodReturnType(java.lang.String internalMethodDescriptor)
Returns the internal type of the given internal method descriptor.

Parameters:
internalMethodDescriptor - the internal method descriptor, e.g. "(II)Z".
Returns:
the internal return type, e.g. "Z".

internalMethodParameterCount

public static int internalMethodParameterCount(java.lang.String internalMethodDescriptor)
Returns the number of parameters of the given internal method descriptor.

Parameters:
internalMethodDescriptor - the internal method descriptor, e.g. "(ID)Z".
Returns:
the number of parameters, e.g. 2.

internalMethodParameterSize

public static int internalMethodParameterSize(java.lang.String internalMethodDescriptor)
Returns the size taken up on the stack by the parameters of the given internal method descriptor. This accounts for long and double parameters taking up two entries.

Parameters:
internalMethodDescriptor - the internal method descriptor, e.g. "(ID)Z".
Returns:
the size taken up on the stack, e.g. 3.

internalMethodParameterSize

public static int internalMethodParameterSize(java.lang.String internalMethodDescriptor,
                                              int accessFlags)
Returns the size taken up on the stack by the parameters of the given internal method descriptor. This accounts for long and double parameters taking up two entries, and a non-static method taking up an additional entry.

Parameters:
internalMethodDescriptor - the internal method descriptor, e.g. "(ID)Z".
accessFlags - the access flags of the method, e.g. 0.
Returns:
the size taken up on the stack, e.g. 4.

internalMethodParameterSize

public static int internalMethodParameterSize(java.lang.String internalMethodDescriptor,
                                              boolean isStatic)
Returns the size taken up on the stack by the parameters of the given internal method descriptor. This accounts for long and double parameters taking up two spaces, and a non-static method taking up an additional entry.

Parameters:
internalMethodDescriptor - the internal method descriptor, e.g. "(ID)Z".
isStatic - specifies whether the method is static, e.g. false.
Returns:
the size taken up on the stack, e.g. 4.

internalTypeSize

public static int internalTypeSize(java.lang.String internalType)
Returns the size taken up on the stack by the given internal type. The size is 1, except for long and double types, for which it is 2, and for the void type, for which 0 is returned.

Parameters:
internalType - the internal type, e.g. "I".
Returns:
the size taken up on the stack, e.g. 1.

internalType

public static java.lang.String internalType(java.lang.String externalType)
Converts an external type into an internal type.

Parameters:
externalType - the external type, e.g. "java.lang.Object[][]" or "int[]".
Returns:
the internal type, e.g. "[[Ljava/lang/Object;" or "[I".

externalArrayTypeDimensionCount

public static int externalArrayTypeDimensionCount(java.lang.String externalType)
Returns the number of dimensions of the given external type.

Parameters:
externalType - the external type, e.g. "[[Ljava/lang/Object;".
Returns:
the number of dimensions, e.g. 2.

externalType

public static java.lang.String externalType(java.lang.String internalType)
Converts an internal type into an external type.

Parameters:
internalType - the internal type, e.g. "[[Ljava/lang/Object;" or "[I".
Returns:
the external type, e.g. "java.lang.Object[][]" or "int[]".

isInternalMethodDescriptor

public static boolean isInternalMethodDescriptor(java.lang.String internalDescriptor)
Returns whether the given internal descriptor String represents a method descriptor.

Parameters:
internalDescriptor - the internal descriptor String, e.g. "(II)Z".
Returns:
true if the given String is a method descriptor, false otherwise.

isExternalMethodNameAndArguments

public static boolean isExternalMethodNameAndArguments(java.lang.String externalMemberNameAndArguments)
Returns whether the given member String represents an external method name with arguments.

Parameters:
externalMemberNameAndArguments - the external member String, e.g. "myField" or e.g. "myMethod(int,int)".
Returns:
true if the given String refers to a method, false otherwise.

externalMethodName

public static java.lang.String externalMethodName(java.lang.String externalMethodNameAndArguments)
Returns the name part of the given external method name and arguments.

Parameters:
externalMethodNameAndArguments - the external method name and arguments, e.g. "myMethod(int,int)".
Returns:
the name part of the String, e.g. "myMethod".

internalMethodDescriptor

public static java.lang.String internalMethodDescriptor(java.lang.String externalReturnType,
                                                        java.lang.String externalMethodNameAndArguments)
Converts the given external method return type and name and arguments to an internal method descriptor.

Parameters:
externalReturnType - the external method return type, e.g. "boolean".
externalMethodNameAndArguments - the external method name and arguments, e.g. "myMethod(int,int)".
Returns:
the internal method descriptor, e.g. "(II)Z".

internalMethodDescriptor

public static java.lang.String internalMethodDescriptor(java.lang.String externalReturnType,
                                                        java.util.List externalArguments)
Converts the given external method return type and List of arguments to an internal method descriptor.

Parameters:
externalReturnType - the external method return type, e.g. "boolean".
externalArguments - the external method arguments, e.g. { "int", "int" }.
Returns:
the internal method descriptor, e.g. "(II)Z".

externalFullFieldDescription

public static java.lang.String externalFullFieldDescription(int accessFlags,
                                                            java.lang.String fieldName,
                                                            java.lang.String internalFieldDescriptor)
Converts an internal field description into an external full field description.

Parameters:
accessFlags - the access flags of the field.
fieldName - the field name, e.g. "myField".
internalFieldDescriptor - the internal field descriptor, e.g. "Z".
Returns:
the external full field description, e.g. "public boolean myField".

externalFullMethodDescription

public static java.lang.String externalFullMethodDescription(java.lang.String internalClassName,
                                                             int accessFlags,
                                                             java.lang.String internalMethodName,
                                                             java.lang.String internalMethodDescriptor)
Converts an internal method description into an external full method description.

Parameters:
internalClassName - the internal name of the class of the method, e.g. "mypackage/MyClass".
accessFlags - the access flags of the method.
internalMethodName - the internal method name, e.g. "myMethod" or "<init>".
internalMethodDescriptor - the internal method descriptor, e.g. "(II)Z".
Returns:
the external full method description, e.g. "public boolean myMethod(int,int)" or "public MyClass(int,int)".

externalClassAccessFlags

public static java.lang.String externalClassAccessFlags(int accessFlags)
Converts internal class access flags into an external access description.

Parameters:
accessFlags - the class access flags.
Returns:
the external class access description, e.g. "public final ".

externalClassAccessFlags

public static java.lang.String externalClassAccessFlags(int accessFlags,
                                                        java.lang.String prefix)
Converts internal class access flags into an external access description.

Parameters:
accessFlags - the class access flags.
prefix - a prefix that is added to each access modifier.
Returns:
the external class access description, e.g. "public final ".

externalFieldAccessFlags

public static java.lang.String externalFieldAccessFlags(int accessFlags)
Converts internal field access flags into an external access description.

Parameters:
accessFlags - the field access flags.
Returns:
the external field access description, e.g. "public volatile ".

externalFieldAccessFlags

public static java.lang.String externalFieldAccessFlags(int accessFlags,
                                                        java.lang.String prefix)
Converts internal field access flags into an external access description.

Parameters:
accessFlags - the field access flags.
prefix - a prefix that is added to each access modifier.
Returns:
the external field access description, e.g. "public volatile ".

externalMethodAccessFlags

public static java.lang.String externalMethodAccessFlags(int accessFlags)
Converts internal method access flags into an external access description.

Parameters:
accessFlags - the method access flags.
Returns:
the external method access description, e.g. "public synchronized ".

externalMethodAccessFlags

public static java.lang.String externalMethodAccessFlags(int accessFlags,
                                                         java.lang.String prefix)
Converts internal method access flags into an external access description.

Parameters:
accessFlags - the method access flags.
prefix - a prefix that is added to each access modifier.
Returns:
the external method access description, e.g. "public synchronized ".

externalMethodReturnType

public static java.lang.String externalMethodReturnType(java.lang.String internalMethodDescriptor)
Converts an internal method descriptor into an external method return type.

Parameters:
internalMethodDescriptor - the internal method descriptor, e.g. "(II)Z".
Returns:
the external method return type, e.g. "boolean".

externalMethodArguments

public static java.lang.String externalMethodArguments(java.lang.String internalMethodDescriptor)
Converts an internal method descriptor into an external method argument description.

Parameters:
internalMethodDescriptor - the internal method descriptor, e.g. "(II)Z".
Returns:
the external method argument description, e.g. "int,int".

internalPackageName

public static java.lang.String internalPackageName(java.lang.String internalClassName)
Returns the internal package name of the given internal class name.

Parameters:
internalClassName - the internal class name, e.g. "java/lang/Object".
Returns:
the internal package name, e.g. "java/lang".

internalPackagePrefix

public static java.lang.String internalPackagePrefix(java.lang.String internalClassName)
Returns the internal package prefix of the given internal class name.

Parameters:
internalClassName - the internal class name, e.g. "java/lang/Object".
Returns:
the internal package prefix, e.g. "java/lang/".

externalPackageName

public static java.lang.String externalPackageName(java.lang.String externalClassName)
Returns the external package name of the given external class name.

Parameters:
externalClassName - the external class name, e.g. "java.lang.Object".
Returns:
the external package name, e.g. "java.lang".

externalPackagePrefix

public static java.lang.String externalPackagePrefix(java.lang.String externalClassName)
Returns the external package prefix of the given external class name.

Parameters:
externalClassName - the external class name, e.g. "java.lang.Object".
Returns:
the external package prefix, e.g. "java.lang.".