📄 classutils.java
字号:
clazz.getMethod(methodName, paramTypes);
return true;
}
catch (NoSuchMethodException ex) {
return false;
}
}
/**
* 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;
do {
for (int i = 0; i < clazz.getDeclaredMethods().length; i++) {
Method method = clazz.getDeclaredMethods()[i];
if (methodName.equals(method.getName())) {
count++;
}
}
clazz = clazz.getSuperclass();
}
while (clazz != null);
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");
do {
for (int i = 0; i < clazz.getDeclaredMethods().length; i++) {
Method method = clazz.getDeclaredMethods()[i];
if (method.getName().equals(methodName)) {
return true;
}
}
clazz = clazz.getSuperclass();
}
while (clazz != null);
return false;
}
/**
* Return a static method of a class.
* @param methodName the static method name
* @param clazz the class which defines the method
* @param args the parameter types to the method
* @return the static method, or <code>null</code> if no static method was found
* @throws IllegalArgumentException if the method name is blank or the clazz is null
*/
public static Method getStaticMethod(Class clazz, String methodName, Class[] args) {
Assert.notNull(clazz, "Class must not be null");
Assert.notNull(methodName, "Method name must not be null");
try {
Method method = clazz.getDeclaredMethod(methodName, args);
if ((method.getModifiers() & Modifier.STATIC) != 0) {
return method;
}
}
catch (NoSuchMethodException ex) {
}
return null;
}
/**
* Check if the given class represents a primitive wrapper,
* i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double.
*/
public static boolean isPrimitiveWrapper(Class clazz) {
Assert.notNull(clazz, "Class must not be null");
return primitiveWrapperTypeMap.containsKey(clazz);
}
/**
* Check if the given class represents a primitive (i.e. boolean, byte,
* char, short, int, long, float, or double) or a primitive wrapper
* (i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double).
*/
public static boolean isPrimitiveOrWrapper(Class clazz) {
Assert.notNull(clazz, "Class must not be null");
return (clazz.isPrimitive() || isPrimitiveWrapper(clazz));
}
/**
* Check if the given class represents an array of primitives,
* i.e. boolean, byte, char, short, int, long, float, or double.
*/
public static boolean isPrimitiveArray(Class clazz) {
Assert.notNull(clazz, "Class must not be null");
return (clazz.isArray() && clazz.getComponentType().isPrimitive());
}
/**
* Check if the given class represents an array of primitive wrappers,
* i.e. Boolean, Byte, Character, Short, Integer, Long, Float, or Double.
*/
public static boolean isPrimitiveWrapperArray(Class clazz) {
Assert.notNull(clazz, "Class must not be null");
return (clazz.isArray() && isPrimitiveWrapper(clazz.getComponentType()));
}
/**
* Determine if the given target type is assignable from the given value
* type, assuming setting by reflection. Considers primitive wrapper
* classes as assignable to the corresponding primitive types.
* @param targetType the target type
* @param valueType the value type that should be assigned to the target type
* @return if the target type is assignable from the value type
*/
public static boolean isAssignable(Class targetType, Class valueType) {
Assert.notNull(targetType, "Target type must not be null");
Assert.notNull(valueType, "Value type must not be null");
return (targetType.isAssignableFrom(valueType) ||
targetType.equals(primitiveWrapperTypeMap.get(valueType)));
}
/**
* Determine if the given type is assignable from the given value,
* assuming setting by reflection. Considers primitive wrapper classes
* as assignable to the corresponding primitive types.
* @param type the target type
* @param value the value that should be assigned to the type
* @return if the type is assignable from the value
*/
public static boolean isAssignableValue(Class type, Object value) {
Assert.notNull(type, "Type must not be null");
return (value != null ? isAssignable(type, value.getClass()) : !type.isPrimitive());
}
/**
* Return a path suitable for use with <code>ClassLoader.getResource</code>
* (also suitable for use with <code>Class.getResource</code> by prepending a
* slash ('/') to the return value. Built by taking the package of the specified
* class file, converting all dots ('.') to slashes ('/'), adding a trailing slash
* if necesssary, and concatenating the specified resource name to this.
* <br/>As such, this function may be used to build a path suitable for
* loading a resource file that is in the same package as a class file,
* although {@link org.springframework.core.io.ClassPathResource} is usually
* even more convenient.
* @param clazz the Class whose package will be used as the base
* @param resourceName the resource name to append. A leading slash is optional.
* @return the built-up resource path
* @see ClassLoader#getResource
* @see Class#getResource
*/
public static String addResourcePathToPackagePath(Class clazz, String resourceName) {
Assert.notNull(resourceName, "Resource name must not be null");
if (!resourceName.startsWith("/")) {
return classPackageAsResourcePath(clazz) + "/" + resourceName;
}
return classPackageAsResourcePath(clazz) + resourceName;
}
/**
* Given an input class object, return a string which consists of the
* class's package name as a pathname, i.e., all dots ('.') are replaced by
* slashes ('/'). Neither a leading nor trailing slash is added. The result
* could be concatenated with a slash and the name of a resource, and fed
* directly to ClassLoader.getResource(). For it to be fed to Class.getResource,
* a leading slash would also have to be prepended to the return value.
* @param clazz the input class. A null value or the default (empty) package
* will result in an empty string ("") being returned.
* @return a path which represents the package name
* @see ClassLoader#getResource
* @see Class#getResource
*/
public static String classPackageAsResourcePath(Class clazz) {
if (clazz == null || clazz.getPackage() == null) {
return "";
}
return clazz.getPackage().getName().replace('.', '/');
}
/**
* Return all interfaces that the given object implements as array,
* including ones implemented by superclasses.
* @param object the object to analyse for interfaces
* @return all interfaces that the given object implements as array
*/
public static Class[] getAllInterfaces(Object object) {
Set interfaces = getAllInterfacesAsSet(object);
return (Class[]) interfaces.toArray(new Class[interfaces.size()]);
}
/**
* Return all interfaces that the given class implements as array,
* including ones implemented by superclasses.
* <p>If the class itself is an interface, it gets returned as sole interface.
* @param clazz the class to analyse for interfaces
* @return all interfaces that the given object implements as array
*/
public static Class[] getAllInterfacesForClass(Class clazz) {
Set interfaces = getAllInterfacesForClassAsSet(clazz);
return (Class[]) interfaces.toArray(new Class[interfaces.size()]);
}
/**
* Return all interfaces that the given object implements as List,
* including ones implemented by superclasses.
* @param object the object to analyse for interfaces
* @return all interfaces that the given object implements as List
*/
public static Set getAllInterfacesAsSet(Object object) {
return getAllInterfacesForClassAsSet(object.getClass());
}
/**
* Return all interfaces that the given class implements as Set,
* including ones implemented by superclasses.
* <p>If the class itself is an interface, it gets returned as sole interface.
* @param clazz the class to analyse for interfaces
* @return all interfaces that the given object implements as Set
*/
public static Set getAllInterfacesForClassAsSet(Class clazz) {
if (clazz.isInterface()) {
return Collections.singleton(clazz);
}
Set interfaces = new HashSet();
while (clazz != null) {
for (int i = 0; i < clazz.getInterfaces().length; i++) {
Class ifc = clazz.getInterfaces()[i];
interfaces.add(ifc);
}
clazz = clazz.getSuperclass();
}
return interfaces;
}
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();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -