????????????????????????????????????????????????????????????????????÷???????Щ???????????????????????????ReflectionUtil??????????????????????????java.lang.reflect?????????????????????????????????(java.reflect.Type)?????????
???????????????????java????????????????????á???????????java???????????????????????Oracel????γ???????????????????????.
????ReflectionUtil?а??????????????
???????Type???????class;
???????Type????????;
???????????????????????;
??????????????????????????;
???????????????е????field????;
???????????????е????method????????;
?????????????????????????;
????ReflectionUtil??????.
???????Type???????class
private static final String TYPE_NAME_PREFIX = "class ";
public static String getClassName(Type type) {
if (type==null) {
return "";
}
String className = type.toString();
if (className.startsWith(TYPE_NAME_PREFIX)) {
className = className.substring(TYPE_NAME_PREFIX.length());
}
return className;
}
public static Class<?> getClass(Type type)
throws ClassNotFoundException {
String className = getClassName(type);
if (className==null || className.isEmpty()) {
return null;
}
return Class.forName(className);
}
????????ReflectionUtil#getClass(Type)??????java.lang.reflect.Type???java.lang.Class???????????????????Type??toString????????????????class????“class some.package.Foo”?????????class???????????Class.forName(String)???class????
???????Type????????
????public static Object newInstance(Type type)
????throws ClassNotFoundException?? InstantiationException?? IllegalAccessException {
????Class<?> clazz = getClass(type);
????if (clazz==null) {
????return null;
????}
????return clazz.newInstance();
????}
????????ReflectionUtil#newInstance(Type type)??????Type???????????????????????Type????????????????????????????????????Void???????InstantiationException????