ProGuard

proguard.classfile.util
Class ClassUtil

java.lang.Object
  extended byproguard.classfile.util.ClassUtil

public class ClassUtil
extends 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 file magic number is correct.
static void checkVersionNumbers(int majorVersionNumber, int minorVersionNumber)
          Checks whether the given class file version numbers are supported.
static int externalArrayTypeDimensionCount(String externalType)
          Returns the number of dimensions of the given external type.
static String externalClassAccessFlags(int accessFlags)
          Converts internal class access flags into an external access description.
static String externalClassAccessFlags(int accessFlags, String prefix)
          Converts internal class access flags into an external access description.
static String externalClassName(String internalClassName)
          Converts an internal class name into an external class name.
static String externalFieldAccessFlags(int accessFlags)
          Converts internal field access flags into an external access description.
static String externalFieldAccessFlags(int accessFlags, String prefix)
          Converts internal field access flags into an external access description.
static String externalFullClassDescription(int accessFlags, String internalClassName)
          Converts an internal class description into an external class description.
static String externalFullFieldDescription(int accessFlags, String fieldName, String internalFieldDescriptor)
          Converts an internal field description into an external full field description.
static String externalFullMethodDescription(String internalClassName, int accessFlags, String internalMethodName, String internalMethodDescriptor)
          Converts an internal method description into an external full method description.
static String externalMethodAccessFlags(int accessFlags)
          Converts internal method access flags into an external access description.
static String externalMethodAccessFlags(int accessFlags, String prefix)
          Converts internal method access flags into an external access description.
static String externalMethodArguments(String internalMethodDescriptor)
          Converts an internal method descriptor into an external method argument description.
static String externalMethodName(String externalMethodNameAndArguments)
          Returns the name part of the given external method name and arguments.
static String externalMethodReturnType(String internalMethodDescriptor)
          Converts an internal method descriptor into an external method return type.
static String externalPackageName(String externalClassName)
          Returns the external package name of the given external class name.
static String externalShortClassName(String externalClassName)
          Converts an internal class name into an external short class name, without package specification.
static String externalType(String internalType)
          Converts an internal type into an external type.
static int internalArrayTypeDimensionCount(String internalType)
          Returns the number of dimensions of the given internal type.
static String internalClassName(String externalClassName)
          Converts an external class name into an internal class name.
static String internalClassNameFromClassType(String internalClassType)
          Returns the internal class name of a given internal class type.
static String internalClassNameFromType(String internalClassType)
          Returns the internal class name of any given internal type.
static String internalMethodDescriptor(String externalReturnType, List externalArguments)
          Converts the given external method return type and List of arguments to an internal method descriptor.
static String internalMethodDescriptor(String externalReturnType, String externalMethodNameAndArguments)
          Converts the given external method return type and name and arguments to an internal method descriptor.
static int internalMethodParameterCount(String internalMethodDescriptor)
          Returns the number of parameters of the given internal method descriptor.
static int internalMethodParameterSize(String internalMethodDescriptor)
          Returns the size taken up on the stack by the parameters of the given internal method descriptor.
static String internalMethodReturnType(String internalMethodDescriptor)
          Returns the internal type of the given internal method descriptor.
static String internalPackageName(String internalClassName)
          Returns the internal package name of the given internal class name.
static String internalType(String externalType)
          Converts an external type into an internal type.
static String internalTypeFromArrayType(String internalArrayType)
          Returns the internal element type of a given internal array type.
static int internalTypeSize(String internalType)
          Returns the size taken up on the stack by the given internal type.
static boolean isExternalMethodNameAndArguments(String externalMemberNameAndArguments)
          Returns whether the given member String represents an external method name with arguments.
static boolean isInternalArrayType(String internalType)
          Returns whether the given internal type is an array type.
static boolean isInternalClassType(String internalType)
          Returns whether the given internal type is a plain class type (not an array type).
static boolean isInternalMethodDescriptor(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 IOException
Checks whether the given class file magic number is correct.

Parameters:
magicNumber - the magic number.
Throws:
IOException - when the magic number is incorrect.

checkVersionNumbers

public static void checkVersionNumbers(int majorVersionNumber,
                                       int minorVersionNumber)
                                throws IOException
Checks whether the given class file version numbers are supported.

Parameters:
majorVersionNumber - the major version number.
minorVersionNumber - the minor version number.
Throws:
IOException - when the version is not supported.

internalClassName

public static String internalClassName(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 String externalFullClassDescription(int accessFlags,
                                                  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 String externalClassName(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 String externalShortClassName(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(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(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.

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.

isInternalClassType

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

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

internalTypeFromArrayType

public static String internalTypeFromArrayType(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 String internalClassNameFromClassType(String internalClassType)
Returns the internal class name of a given internal class type.

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

internalClassNameFromType

public static String internalClassNameFromType(String internalClassType)
Returns the internal class name of any given internal type. The returned class name for primitive array types is "java/lang/Object".

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

internalMethodReturnType

public static String internalMethodReturnType(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(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(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 spaces.

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

internalTypeSize

public static int internalTypeSize(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 String internalType(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(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 String externalType(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(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(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 String externalMethodName(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 String internalMethodDescriptor(String externalReturnType,
                                              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 String internalMethodDescriptor(String externalReturnType,
                                              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 String externalFullFieldDescription(int accessFlags,
                                                  String fieldName,
                                                  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 String externalFullMethodDescription(String internalClassName,
                                                   int accessFlags,
                                                   String internalMethodName,
                                                   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 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 String externalClassAccessFlags(int accessFlags,
                                              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 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 String externalFieldAccessFlags(int accessFlags,
                                              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 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 String externalMethodAccessFlags(int accessFlags,
                                               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 String externalMethodReturnType(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 String externalMethodArguments(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 String internalPackageName(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".

externalPackageName

public static String externalPackageName(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".

ProGuard