???????????????????????
?????????????????????????????
????public abstract class Foo<T> {
????//content
????}
????public class FooChild extends Foo<Bar> {
????//content
????}
????????????????Foo?д???????Class???????
??????????????????????????
???????FooChild?????????class????(???????????????)??
????public abstract class Foo<T> {
????private Class<T> tClass;
????public Foo(Class<T> tClass) {
????this.tClass = tClass;
????}
????//content
????}
????public class FooChild extends Foo<Bar> {
????public FooChild() {
????super(FooChild.class);
????}
????//content
????}
???????÷???????
????public static Type[] getParameterizedTypes(Object object) {
????Type superclassType = object.getClass().getGenericSuperclass();
????if (!ParameterizedType.class.isAssignableFrom(superclassType.getClass())) {
????return null;
????}
????return ((ParameterizedType)superclassType).getActualTypeArguments();
????}
????????ReflectionUtil#getParameterizedTypes(Object)???÷????????????????????????????????????????????????????T?????Type???顣
???????Foo???T??????????????????????????
????...
????Type[] parameterizedTypes = ReflectionUtil.getParameterizedTypes(this);
????Class<T> clazz = (Class<T>)ReflectionUtil.getClass(parameterizedTypes[0]);
????...
???????:
??????java.lang.reflect.ParameterizedType#getActualTypeArguments() documentation:???????????????????????
????in some cases?? the returned array can be empty. This can occur. if this type represents
????a non-parameterized type nested within a parameterized type.
????????????????????????????????????????
????????????????????????
????public static boolean hasDefaultConstructor(Class<?> clazz) throws SecurityException {
????Class<?>[] empty = {};
????try {
????clazz.getConstructor(empty);
????} catch (NoSuchMethodException e) {
????return false;
????}
????return true;
????}
????????ReflectionUtil#hasDefaultConstructor????java.lang.reflect.Constructor???????????????ι???????
???????????????е????field????
public static Class<?> getFieldClass(Class<?> clazz?? String name) {
if (clazz==null || name==null || name.isEmpty()) {
return null;
}
name = name.toLowerCase();
Class<?> propertyClass = null;
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
if (field.getName().equals(name)) {
propertyClass = field.getType();
break;
}
}
return propertyClass;
}
???????Щ??????????????????????????????????????????????ε?????????ReflectionUtil#getFieldClass(Class<?>?? String)?????????ReflectionUtil#getFieldClass(Class<?>?? String) ????Class#getDeclaredFields()?????β???????java.lang.reflect.Field#getName()?????????????????????????????