????4.??class?????????
????//??????? Class???????newInstance()????????
????Object obj = class1.newInstance();
????//??????? ??????????Constructor???????????Constructor?????newInstance()????????
????Constructor<?> constructor = class1.getDeclaredConstructor(new Class[]{String.class});//????????????????
????obj = constructor.newInstance(new Object[]{"lcj"});
????5.????????ú???
try {
// ?????μ??????newInstance()????
Object obj = class1.newInstance();
//?ж??????????Person??????
boolean isInstanceOf = obj instanceof Person;
//????????????÷????????Method????
Method method = class1.getDeclaredMethod("setAge"?? new Class[]{int.class});
//?????????????????????
method.invoke(obj?? 28);
method = class1.getDeclaredMethod("getAge");
Object result = method.invoke(obj?? new Class[]{});
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
????6.)????????????????????
?????????????????
????//People??
????public class People<T> {}
????//Person????People??
????public class Person<T> extends People<String> implements PersonInterface<Integer> {}
????//PersonInterface???
????public interface PersonInterface<T> {}
???????????????
Person<String> person = new Person<>();
//??????? ???????getClass????
Class<?> class1 = person.getClass();
Type genericSuperclass = class1.getGenericSuperclass();//???class???????????? Type
Type[] interfaceTypes = class1.getGenericInterfaces();//???class????????н???Type????
getComponentType(genericSuperclass);
getComponentType(interfaceTypes[0]);
getComponentType???????
private Class<?> getComponentType(Type type) {
Class<?> componentType = null;
if (type instanceof ParameterizedType) {
//getActualTypeArguments()???????????????????????? Type ????????顣
Type[] actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments();
if (actualTypeArguments != null && actualTypeArguments.length > 0) {
componentType = (Class<?>) actualTypeArguments[0];
}
} else if (type instanceof GenericArrayType) {
// ????????????????????????????????????????????
componentType = (Class<?>) ((GenericArrayType) type).getGenericComponentType();
} else {
componentType = (Class<?>) type;
}
return componentType;
}
????7.)??????????????????
???????????????Method???????????
try {
//????????????÷????????Method????
Method method = class1.getDeclaredMethod("jumpToGoodsDetail"?? new Class[]{String.class?? String.class});
Annotation[] annotations1 = method.getAnnotations();//??????е??????????
Annotation annotation1 = method.getAnnotation(RouterUri.class);//??????????????
TypeVariable[] typeVariables1 = method.getTypeParameters();
Annotation[][] parameterAnnotationsArray = method.getParameterAnnotations();//??????в?????????
Class<?>[] parameterTypes = method.getParameterTypes();//??????в???class????
Type[] genericParameterTypes = method.getGenericParameterTypes();//??????в?????type????
Class<?> returnType = method.getReturnType();//????????????????
int modifiers = method.getModifiers();//???????????????
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
??????????????ó?????
????· ??????? ?????練????
????· ???????????? ????Retrofit
????· ????????????????? ????EventBus 2.x
????· ??????????? ????Gson
?????????????????
???????
????????????????ж????????????????????÷???
???????
????????????????????????????н??????????jvm????????飬?????????java??????????
???????
????Java????????????????????????к????????????????Щ?????????????÷???????????????????£????к??δ?????????????????????
?????????????У?????и??????ζ?????????????????????ζ???????????????壬????????????