classutils.java
来自「有关此类编程有心德的高手 希望能够多多给予指教」· Java 代码 · 共 835 行 · 第 1/3 页
JAVA
835 行
/**
* Return the user-defined class for the given instance: usually simply
* the class of the given instance, but the original class in case of a
* CGLIB-generated subclass.
* @param instance the instance to check
* @return the user-defined class
*/
public static Class getUserClass(Object instance) {
Assert.notNull(instance, "Instance must not be null");
return getUserClass(instance.getClass());
}
/**
* Return the user-defined class for the given class: usually simply the given
* class, but the original class in case of a CGLIB-generated subclass.
* @param clazz the class to check
* @return the user-defined class
*/
public static Class getUserClass(Class clazz) {
return (clazz != null && clazz.getName().indexOf(CGLIB_CLASS_SEPARATOR) != -1 ?
clazz.getSuperclass() : clazz);
}
/**
* Get the class name without the qualified package name.
* @param className the className to get the short name for
* @return the class name of the class without the package name
* @throws IllegalArgumentException if the className is empty
*/
public static String getShortName(String className) {
Assert.hasLength(className, "Class name must not be empty");
int lastDotIndex = className.lastIndexOf(PACKAGE_SEPARATOR);
int nameEndIndex = className.indexOf(CGLIB_CLASS_SEPARATOR);
if (nameEndIndex == -1) {
nameEndIndex = className.length();
}
String shortName = className.substring(lastDotIndex + 1, nameEndIndex);
shortName = shortName.replace(INNER_CLASS_SEPARATOR, PACKAGE_SEPARATOR);
return shortName;
}
/**
* Get the class name without the qualified package name.
* @param clazz the class to get the short name for
* @return the class name of the class without the package name
*/
public static String getShortName(Class clazz) {
return getShortName(getQualifiedName(clazz));
}
/**
* Return the short string name of a Java class in decapitalized
* JavaBeans property format.
* @param clazz the class
* @return the short name rendered in a standard JavaBeans property format
* @see java.beans.Introspector#decapitalize(String)
*/
public static String getShortNameAsProperty(Class clazz) {
return Introspector.decapitalize(getShortName(clazz));
}
/**
* Determine the name of the class file, relative to the containing
* package: e.g. "String.class"
* @param clazz the class
* @return the file name of the ".class" file
*/
public static String getClassFileName(Class clazz) {
Assert.notNull(clazz, "Class must not be null");
String className = clazz.getName();
int lastDotIndex = className.lastIndexOf(PACKAGE_SEPARATOR);
return className.substring(lastDotIndex + 1) + CLASS_FILE_SUFFIX;
}
/**
* Determine the name of the package of the given class:
* e.g. "java.lang" for the <code>java.lang.String</code> class.
* @param clazz the class
* @return the package name, or the empty String if the class
* is defined in the default package
*/
public static String getPackageName(Class clazz) {
Assert.notNull(clazz, "Class must not be null");
String className = clazz.getName();
int lastDotIndex = className.lastIndexOf(PACKAGE_SEPARATOR);
return (lastDotIndex != -1 ? className.substring(0, lastDotIndex) : "");
}
/**
* Return the qualified name of the given class: usually simply
* the class name, but component type class name + "[]" for arrays.
* @param clazz the class
* @return the qualified name of the class
*/
public static String getQualifiedName(Class clazz) {
Assert.notNull(clazz, "Class must not be null");
if (clazz.isArray()) {
return getQualifiedNameForArray(clazz);
}
else {
return clazz.getName();
}
}
/**
* Build a nice qualified name for an array:
* component type class name + "[]".
* @param clazz the array class
* @return a qualified name for the array class
*/
private static String getQualifiedNameForArray(Class clazz) {
StringBuffer buffer = new StringBuffer();
while (clazz.isArray()) {
clazz = clazz.getComponentType();
buffer.append(ClassUtils.ARRAY_SUFFIX);
}
buffer.insert(0, clazz.getName());
return buffer.toString();
}
/**
* Return the qualified name of the given method, consisting of
* fully qualified interface/class name + "." + method name.
* @param method the method
* @return the qualified name of the method
*/
public static String getQualifiedMethodName(Method method) {
Assert.notNull(method, "Method must not be null");
return method.getDeclaringClass().getName() + "." + method.getName();
}
/**
* Determine whether the given class has a constructor with the given signature.
* <p>Essentially translates <code>NoSuchMethodException</code> to "false".
* @param clazz the clazz to analyze
* @param paramTypes the parameter types of the method
* @return whether the class has a corresponding constructor
* @see java.lang.Class#getMethod
*/
public static boolean hasConstructor(Class clazz, Class[] paramTypes) {
return (getConstructorIfAvailable(clazz, paramTypes) != null);
}
/**
* Determine whether the given class has a constructor with the given signature,
* and return it if available (else return <code>null</code>).
* <p>Essentially translates <code>NoSuchMethodException</code> to <code>null</code>.
* @param clazz the clazz to analyze
* @param paramTypes the parameter types of the method
* @return the constructor, or <code>null</code> if not found
* @see java.lang.Class#getConstructor
*/
public static Constructor getConstructorIfAvailable(Class clazz, Class[] paramTypes) {
Assert.notNull(clazz, "Class must not be null");
try {
return clazz.getConstructor(paramTypes);
}
catch (NoSuchMethodException ex) {
return null;
}
}
/**
* Determine whether the given class has a method with the given signature.
* <p>Essentially translates <code>NoSuchMethodException</code> to "false".
* @param clazz the clazz to analyze
* @param methodName the name of the method
* @param paramTypes the parameter types of the method
* @return whether the class has a corresponding method
* @see java.lang.Class#getMethod
*/
public static boolean hasMethod(Class clazz, String methodName, Class[] paramTypes) {
return (getMethodIfAvailable(clazz, methodName, paramTypes) != null);
}
/**
* Determine whether the given class has a method with the given signature,
* and return it if available (else return <code>null</code>).
* <p>Essentially translates <code>NoSuchMethodException</code> to <code>null</code>.
* @param clazz the clazz to analyze
* @param methodName the name of the method
* @param paramTypes the parameter types of the method
* @return the method, or <code>null</code> if not found
* @see java.lang.Class#getMethod
*/
public static Method getMethodIfAvailable(Class clazz, String methodName, Class[] paramTypes) {
Assert.notNull(clazz, "Class must not be null");
Assert.notNull(methodName, "Method name must not be null");
try {
return clazz.getMethod(methodName, paramTypes);
}
catch (NoSuchMethodException ex) {
return null;
}
}
/**
* Return the number of methods with a given name (with any argument types),
* for the given class and/or its superclasses. Includes non-public methods.
* @param clazz the clazz to check
* @param methodName the name of the method
* @return the number of methods with the given name
*/
public static int getMethodCountForName(Class clazz, String methodName) {
Assert.notNull(clazz, "Class must not be null");
Assert.notNull(methodName, "Method name must not be null");
int count = 0;
for (int i = 0; i < clazz.getDeclaredMethods().length; i++) {
Method method = clazz.getDeclaredMethods()[i];
if (methodName.equals(method.getName())) {
count++;
}
}
Class[] ifcs = clazz.getInterfaces();
for (int i = 0; i < ifcs.length; i++) {
count += getMethodCountForName(ifcs[i], methodName);
}
if (clazz.getSuperclass() != null) {
count += getMethodCountForName(clazz.getSuperclass(), methodName);
}
return count;
}
/**
* Does the given class and/or its superclasses at least have one or more
* methods (with any argument types)? Includes non-public methods.
* @param clazz the clazz to check
* @param methodName the name of the method
* @return whether there is at least one method with the given name
*/
public static boolean hasAtLeastOneMethodWithName(Class clazz, String methodName) {
Assert.notNull(clazz, "Class must not be null");
Assert.notNull(methodName, "Method name must not be null");
for (int i = 0; i < clazz.getDeclaredMethods().length; i++) {
Method method = clazz.getDeclaredMethods()[i];
if (method.getName().equals(methodName)) {
return true;
}
}
Class[] ifcs = clazz.getInterfaces();
for (int i = 0; i < ifcs.length; i++) {
if (hasAtLeastOneMethodWithName(ifcs[i], methodName)) {
return true;
}
}
return (clazz.getSuperclass() != null && hasAtLeastOneMethodWithName(clazz.getSuperclass(), methodName));
}
/**
* Given a method, which may come from an interface, and a target class used
* in the current reflective invocation, find the corresponding target method
* if there is one. E.g. the method may be <code>IFoo.bar()</code> and the
* target class may be <code>DefaultFoo</code>. In this case, the method may be
* <code>DefaultFoo.bar()</code>. This enables attributes on that method to be found.
* <p><b>NOTE:</b> In contrast to {@link org.springframework.aop.support.AopUtils#getMostSpecificMethod},
* this method does <i>not</i> resolve Java 5 bridge methods automatically.
* Call {@link org.springframework.core.BridgeMethodResolver#findBridgedMethod}
* if bridge method resolution is desirable (e.g. for obtaining metadata from
* the original method definition).
* @param method the method to be invoked, which may come from an interface
* @param targetClass the target class for the current invocation.
* May be <code>null</code> or may not even implement the method.
* @return the specific target method, or the original method if the
* <code>targetClass</code> doesn't implement it or is <code>null</code>
* @see org.springframework.aop.support.AopUtils#getMostSpecificMethod
*/
public static Method getMostSpecificMethod(Method method, Class targetClass) {
if (method != null && targetClass != null) {
try {
method = targetClass.getMethod(method.getName(), method.getParameterTypes());
}
catch (NoSuchMethodException ex) {
// Perhaps the target class doesn't implement this method:
// that's fine, just use the original method.
}
}
return method;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?