All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.jvnet.annox.util.ClassUtils Maven / Gradle / Ivy
package org.jvnet.annox.util;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
public class ClassUtils {
private static Map, Class>> primitiveWrapperMap = new HashMap, Class>>();
static {
primitiveWrapperMap.put(Boolean.TYPE, Boolean.class);
primitiveWrapperMap.put(Byte.TYPE, Byte.class);
primitiveWrapperMap.put(Character.TYPE, Character.class);
primitiveWrapperMap.put(Short.TYPE, Short.class);
primitiveWrapperMap.put(Integer.TYPE, Integer.class);
primitiveWrapperMap.put(Long.TYPE, Long.class);
primitiveWrapperMap.put(Double.TYPE, Double.class);
primitiveWrapperMap.put(Float.TYPE, Float.class);
}
private static Map, Class>> wrapperPrimitiveMap = new HashMap, Class>>();
static {
wrapperPrimitiveMap.put(Boolean.class, Boolean.TYPE);
wrapperPrimitiveMap.put(Byte.class, Byte.TYPE);
wrapperPrimitiveMap.put(Character.class, Character.TYPE);
wrapperPrimitiveMap.put(Short.class, Short.TYPE);
wrapperPrimitiveMap.put(Integer.class, Integer.TYPE);
wrapperPrimitiveMap.put(Long.class, Long.TYPE);
wrapperPrimitiveMap.put(Double.class, Double.TYPE);
wrapperPrimitiveMap.put(Float.class, Float.TYPE);
}
private static Map> namePrimitiveMap = new HashMap>(
8);
static {
namePrimitiveMap.put("boolean", Boolean.TYPE);
namePrimitiveMap.put("byte", Byte.TYPE);
namePrimitiveMap.put("char", Character.TYPE);
namePrimitiveMap.put("short", Short.TYPE);
namePrimitiveMap.put("int", Integer.TYPE);
namePrimitiveMap.put("long", Long.TYPE);
namePrimitiveMap.put("double", Double.TYPE);
namePrimitiveMap.put("float", Float.TYPE);
}
public static Class primitiveToWrapper(Class cls) {
if (cls == null) {
throw new IllegalArgumentException("Class must not be null.");
} else if (cls.isPrimitive()) {
return primitiveWrapperMap.get(cls);
} else {
return cls;
}
}
public static Class wrapperToPrimitive(Class cls) {
if (cls == null) {
throw new IllegalArgumentException("Class must not be null.");
} else {
final Class> primitiveClass = wrapperPrimitiveMap.get(cls);
if (primitiveClass != null) {
return primitiveClass;
} else {
return cls;
}
}
}
public static Class nameToPrimitive(String name) {
if (name == null) {
return null;
} else {
return namePrimitiveMap.get(name);
}
}
public static final String ARRAY_SUFFIX = "[]";
public static Class> forName(String className)
throws ClassNotFoundException {
return forName(className, true, null);
}
public static Class> forName(String className, boolean initialize,
ClassLoader loader) throws ClassNotFoundException {
if (className == null) {
return null;
}
if (className.endsWith(ARRAY_SUFFIX)) {
final String componentClassName = className.substring(0, className
.length()
- ARRAY_SUFFIX.length());
return getArrayClass(forName(componentClassName, initialize, loader));
}
final Class primitiveClass = nameToPrimitive(className);
if (primitiveClass != null) {
return primitiveClass;
} else {
return Class.forName(className, initialize, loader);
}
}
public static Class> getArrayClass(final Class> componentClass) {
final Object componentArray = Array.newInstance(componentClass, 0);
return componentArray.getClass();
}
public static final Class>[] EMPTY_CLASS_ARRAY = new Class>[0];
public static Class>[] forNames(String names)
throws ClassNotFoundException {
return forNames(names, true, null);
}
public static Class>[] forNames(String names, boolean initialize,
ClassLoader loader) throws ClassNotFoundException {
if (names == null) {
return null;
}
final String n = names.trim();
if ("".equals(n)) {
return EMPTY_CLASS_ARRAY;
}
final String[] classNames = StringUtils.split(n, ',');
final Class>[] classes = new Class>[classNames.length];
for (int index = 0; index < classNames.length; index++) {
classes[index] = forName(classNames[index].trim(), initialize,
loader);
}
return classes;
}
}