屬性或者方法為參數(shù)化類型的處理
public class User {
String name;
Integer age;
Car car;
List<String> hobbies;
List<Car> carList;
Map<String, Car> carMap;
void addCar(Car car){
carList.add(car);
}
void addHobby(List<String> hobbies){
}
void addCar(List<Car> cars){
}
}
public class Car {
String carNum;
List<String> carColor;
}
public class Test {
public static void main(String[] args) {
Class clz = User.class;
System.out.println("==========");
//解析屬性
Field[] fields = clz.getDeclaredFields();
for (Field field: fields){
String name = field.getType().getName();
System.out.println(field+": "+name);
}
System.out.println("==========");
for (Field field: fields){
String name = field.getType().getClass().getName();
System.out.println(field+": "+name);
}
System.out.println("==========");
for (Field field: fields){
String name = field.getGenericType().getTypeName();
System.out.println(field+": "+name);
}
System.out.println("==========");
for(Field field: fields){
if(field.getGenericType() instanceof ParameterizedType){
Type[] types =((ParameterizedType)(field.getGenericType())).getActualTypeArguments();
for(Type type:types){
System.out.println("方法1:"+type+": "+type.getTypeName());
}
Type type = ((ParameterizedType)(field.getGenericType())).getRawType();
System.out.println("方法2:"+type+": "+type.getTypeName());
System.out.println("==");
}
}
System.out.println("====================華麗麗的分割線=====================");
//解析方法
Method[] methods = clz.getDeclaredMethods();
for(Method method:methods){
Parameter[] parameters = method.getParameters();
for(Parameter parameter:parameters){
String name = parameter.getType().getName();
System.out.println(method+": "+name);
}
}
System.out.println("============");
for(Method method:methods){
Type[] types = method.getGenericParameterTypes();
for(Type type : types){
if(type instanceof ParameterizedType){
Type[] types1 = ((ParameterizedType)type).getActualTypeArguments();
for(Type type1 :types1){
String name = type1.getTypeName();
System.out.println(method+": "+name);
}
}
}
}
}
}
運(yùn)行結(jié)果
==========
java.lang.String reflectdemo.User.name: java.lang.String
java.lang.Integer reflectdemo.User.age: java.lang.Integer
reflectdemo.Car reflectdemo.User.car: reflectdemo.Car
java.util.List reflectdemo.User.hobbies: java.util.List
java.util.List reflectdemo.User.carList: java.util.List
java.util.Map reflectdemo.User.carMap: java.util.Map
==========
java.lang.String reflectdemo.User.name: java.lang.Class
java.lang.Integer reflectdemo.User.age: java.lang.Class
reflectdemo.Car reflectdemo.User.car: java.lang.Class
java.util.List reflectdemo.User.hobbies: java.lang.Class
java.util.List reflectdemo.User.carList: java.lang.Class
java.util.Map reflectdemo.User.carMap: java.lang.Class
==========
java.lang.String reflectdemo.User.name: java.lang.String
java.lang.Integer reflectdemo.User.age: java.lang.Integer
reflectdemo.Car reflectdemo.User.car: reflectdemo.Car
java.util.List reflectdemo.User.hobbies: java.util.List<java.lang.String>
java.util.List reflectdemo.User.carList: java.util.List<reflectdemo.Car>
java.util.Map reflectdemo.User.carMap: java.util.Map<java.lang.String, reflectdemo.Car>
==========
方法1:class java.lang.String: java.lang.String
方法2:interface java.util.List: java.util.List
==
方法1:class reflectdemo.Car: reflectdemo.Car
方法2:interface java.util.List: java.util.List
==
方法1:class java.lang.String: java.lang.String
方法1:class reflectdemo.Car: reflectdemo.Car
方法2:interface java.util.Map: java.util.Map
==
====================華麗麗的分割線=====================
void reflectdemo.User.addCar(java.util.List): java.util.List
void reflectdemo.User.addCar(reflectdemo.Car): reflectdemo.Car
void reflectdemo.User.addHobby(java.util.List): java.util.List
============
void reflectdemo.User.addCar(java.util.List): reflectdemo.Car
void reflectdemo.User.addHobby(java.util.List): java.lang.String