📄 classutil.java
字号:
/* $Id: ClassUtil.java,v 1.21 2004/08/15 12:39:30 eric Exp $ * * ProGuard -- shrinking, optimization, and obfuscation of Java class files. * * Copyright (c) 2002-2004 Eric Lafortune (eric@graphics.cornell.edu) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package proguard.classfile.util;import proguard.classfile.*;import java.io.*;import java.util.*;/** * Utility methods for converting between internal and external representations * of names and descriptions. * * @author Eric Lafortune */public class ClassUtil{ private static final String EMPTY_STRING = ""; private static final InternalTypeEnumeration internalTypeEnumeration = new InternalTypeEnumeration(); private static final ExternalTypeEnumeration externalTypeEnumeration = new ExternalTypeEnumeration(); /** * Checks whether the given class file magic number is correct. * @param magicNumber the magic number. * @throws IOException when the magic number is incorrect. */ public static void checkMagicNumber(int magicNumber) throws IOException { if (magicNumber != ClassConstants.MAGIC) { throw new IOException("Invalid magic number ["+Integer.toHexString(magicNumber)+"] in class file"); } } /** * Checks whether the given class file version numbers are supported. * @param majorVersionNumber the major version number. * @param minorVersionNumber the minor version number. * @throws IOException when the version is not supported. */ public static void checkVersionNumbers(int majorVersionNumber, int minorVersionNumber) throws IOException { if (majorVersionNumber < ClassConstants.MAJOR_VERSION_MIN || (majorVersionNumber == ClassConstants.MAJOR_VERSION_MIN && minorVersionNumber < ClassConstants.MINOR_VERSION_MIN) || (majorVersionNumber == ClassConstants.MAJOR_VERSION_MAX && minorVersionNumber > ClassConstants.MINOR_VERSION_MAX) || majorVersionNumber > ClassConstants.MAJOR_VERSION_MAX) { throw new IOException("Unsupported version number ["+majorVersionNumber+"."+minorVersionNumber+"] for class file format"); } } /** * Converts an external class name into an internal class name. * @param externalClassName the external class name, * e.g. "<code>java.lang.Object</code>" * @return the internal class name, * e.g. "<code>java/lang/Object</code>". */ public static String internalClassName(String externalClassName) { return externalClassName.replace(ClassConstants.EXTERNAL_PACKAGE_SEPARATOR, ClassConstants.INTERNAL_PACKAGE_SEPARATOR); } /** * Converts an internal class description into an external class description. * @param accessFlags the access flags of the class. * @param internalClassName the internal class name, * e.g. "<code>java/lang/Object</code>". * @return the external class description, * e.g. "<code>public java.lang.Object</code>". */ public static String externalFullClassDescription(int accessFlags, String internalClassName) { return externalClassAccessFlags(accessFlags) + externalClassName(internalClassName); } /** * Converts an internal class name into an external class name. * @param internalClassName the internal class name, * e.g. "<code>java/lang/Object</code>". * @return the external class name, * e.g. "<code>java.lang.Object</code>". */ public static String externalClassName(String internalClassName) { return //internalClassName.startsWith(ClassConstants.INTERNAL_PACKAGE_JAVA_LANG) && //internalClassName.indexOf(ClassConstants.INTERNAL_PACKAGE_SEPARATOR, ClassConstants.INTERNAL_PACKAGE_JAVA_LANG.length() + 1) < 0 ? //internalClassName.substring(ClassConstants.INTERNAL_PACKAGE_JAVA_LANG.length()) : internalClassName.replace(ClassConstants.INTERNAL_PACKAGE_SEPARATOR, ClassConstants.EXTERNAL_PACKAGE_SEPARATOR); } /** * Converts an internal class name into an external short class name, without * package specification. * @param externalClassName the external class name, * e.g. "<code>java.lang.Object</code>" * @return the external short class name, * e.g. "<code>Object</code>". */ public static String externalShortClassName(String externalClassName) { int index = externalClassName.lastIndexOf(ClassConstants.EXTERNAL_PACKAGE_SEPARATOR); return externalClassName.substring(index+1); } /** * Returns whether the given internal type is an array type. * @param internalType the internal type, * e.g. "<code>[[Ljava/lang/Object;</code>". * @return <code>true</code> if the given type is an array type, * <code>false</code> otherwise. */ public static boolean isInternalArrayType(String internalType) { return internalType.length() > 1 && internalType.charAt(0) == ClassConstants.INTERNAL_TYPE_ARRAY; } /** * Returns the number of dimensions of the given internal type. * @param internalType the internal type, * e.g. "<code>[[Ljava/lang/Object;</code>". * @return the number of dimensions, e.g. 2. */ public static int internalArrayTypeDimensionCount(String internalType) { int dimensions = 0; while (internalType.charAt(dimensions) == ClassConstants.INTERNAL_TYPE_ARRAY) { dimensions++; } return dimensions; } /** * Returns whether the given internal type is a plain primitive type * (not void). * @param internalType the internal type, * e.g. "<code>I</code>". * @return <code>true</code> if the given type is a class type, * <code>false</code> otherwise. */ public static boolean isInternalPrimitiveType(char internalType) { return internalType == ClassConstants.INTERNAL_TYPE_BOOLEAN || internalType == ClassConstants.INTERNAL_TYPE_BYTE || internalType == ClassConstants.INTERNAL_TYPE_CHAR || internalType == ClassConstants.INTERNAL_TYPE_SHORT || internalType == ClassConstants.INTERNAL_TYPE_INT || internalType == ClassConstants.INTERNAL_TYPE_FLOAT || internalType == ClassConstants.INTERNAL_TYPE_LONG || internalType == ClassConstants.INTERNAL_TYPE_DOUBLE; } /** * Returns whether the given internal type is a plain class type * (not an array type). * @param internalType the internal type, * e.g. "<code>Ljava/lang/Object;</code>". * @return <code>true</code> if the given type is a class type, * <code>false</code> otherwise. */ public static boolean isInternalClassType(String internalType) { int length = internalType.length(); return length > 1 && internalType.charAt(0) == ClassConstants.INTERNAL_TYPE_CLASS_START && internalType.charAt(length-1) == ClassConstants.INTERNAL_TYPE_CLASS_END; } /** * Returns the internal element type of a given internal array type. * @param internalArrayType the internal array type, * e.g. "<code>[[Ljava/lang/Object;</code>" or * "<code>[I</code>". * @return the internal type of the array elements, * e.g. "<code>Ljava/lang/Object;</code>" or * "<code>I</code>". */ public static String internalTypeFromArrayType(String internalArrayType) { int index = internalArrayType.lastIndexOf(ClassConstants.INTERNAL_TYPE_ARRAY); return internalArrayType.substring(index+1); } /** * Returns the internal class name of a given internal class type. * @param internalClassType the internal class type, * e.g. "<code>Ljava/lang/Object;</code>". * @return the internal class name, * e.g. "<code>java/lang/Object</code>". */ public static String internalClassNameFromClassType(String internalClassType) { return internalClassType.substring(1, internalClassType.length()-1); } /** * Returns the internal class name of any given internal type. * The returned class name for primitive array types is * "<code>java/lang/Object</code>". * @param internalClassType the internal class type, * e.g. "<code>Ljava/lang/Object;</code>" or * "<code>[[I</code>". * @return the internal class name, * e.g. "<code>java/lang/Object</code>". */ public static String internalClassNameFromType(String internalClassType) { // Is it an array type? if (isInternalArrayType(internalClassType)) { internalClassType = internalTypeFromArrayType(internalClassType); // Is the array of a non-primitive type? if (isInternalClassType(internalClassType)) { internalClassType = internalClassNameFromClassType(internalClassType); } else { internalClassType = ClassConstants.INTERNAL_NAME_JAVA_LANG_OBJECT; } } return internalClassType; } /** * Returns the internal type of the given internal method descriptor. * @param internalMethodDescriptor the internal method descriptor, * e.g. "<code>(II)Z</code>". * @return the internal return type, * e.g. "<code>Z</code>". */ public static String internalMethodReturnType(String internalMethodDescriptor) { int index = internalMethodDescriptor.indexOf(ClassConstants.INTERNAL_METHOD_ARGUMENTS_CLOSE); return internalMethodDescriptor.substring(index + 1); } /** * Returns the number of parameters of the given internal method descriptor. * @param internalMethodDescriptor the internal method descriptor, * e.g. "<code>(ID)Z</code>". * @return the number of parameters, * e.g. 2. */ public static int internalMethodParameterCount(String internalMethodDescriptor) { internalTypeEnumeration.setDescriptor(internalMethodDescriptor); int counter = 0; while (internalTypeEnumeration.hasMoreTypes()) { internalTypeEnumeration.nextType(); counter++; } return counter; } /** * 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. * @param internalMethodDescriptor the internal method descriptor, * e.g. "<code>(ID)Z</code>".
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -