反射的常用操作及一些使用特征

- 日理万妓 2022-04-06 13:52 232阅读 0赞

反射:

反射获取类的实体:

class Factory{

  1. public static fruit getInstance(String ClassName)\{
  2. fruit f=null;
  3. try\{
  4. f=(fruit)Class.forName(ClassName).newInstance();
  5. \}catch (Exception e) \{
  6. e.printStackTrace();
  7. \}
  8. return f;
  9. \}

}

class hello{

  1. public static void main(String\[\] a)\{
  2. fruit f=Factory.getInstance("Reflect.Apple");
  3. if(f!=null)\{
  4. f.eat();
  5. \}
  6. \}

反射调用属性的操作方法:

public class Person implements Serializable {

  1. private String name;
  2. private int age;

// get/set方法

}

public static void main(String[] args) {

  1. Person person = new Person("luoxn28", 23);
  2. Class clazz = person.getClass();
  3. Field\[\] fields = clazz.getDeclaredFields();
  4. for (Field field : fields) \{
  5. String key = field.getName();
  6. PropertyDescriptor descriptor = new PropertyDescriptor(key, clazz);
  7. Method method = descriptor.getReadMethod();
  8. Object value = method.invoke(person);
  9. System.out.println(key + ":" + value);
  10. \}

}

反射获取具体实体的某一个字段值的快速方法(包括list):

field.get(object);

/**

  1. \* 检查bean中含有List的必要参数
  2. \*
  3. \* @param object
  4. \* @return
  5. \* @throws Exception
  6. \*/
  7. public static String checkBeans(Object object) throws Exception \{
  8. Class cls = object.getClass();
  9. StringBuilder stringBuilder = new StringBuilder();
  10. for (; cls != Object.class; cls = cls.getSuperclass()) \{ //从本类一直遍历到父类
  11. Field\[\] fieldes = cls.getDeclaredFields();
  12. for (Field field : fieldes) \{
  13. field.setAccessible(true);
  14. NotEmpty annotation = field.getAnnotation(NotEmpty.class);
  15. IsJsonArray isJsonArray = field.getAnnotation(IsJsonArray.class);
  16. Valid valid = field.getAnnotation(Valid.class);
  17. if (annotation != null
  18. && StringUtils.isBlank((String) field.get(object))) \{
  19. stringBuilder.append(field.getName());
  20. stringBuilder.append(";");
  21. \}
  22. if (isJsonArray != null
  23. && !JsonUtils.isJsonArrayString((String) field.get(object))) \{
  24. stringBuilder.append(field.getName());
  25. stringBuilder.append(";");
  26. \}
  27. if (valid != null
  28. && !ArrayIsNotNull.isNull( (List<Object>)field.get(object))) \{
  29. List<Object> os=(List<Object>) field.get(object);
  30. for(Object o : os)\{
  31. checkBean(stringBuilder,o);
  32. \}
  33. \}else if(valid != null && ArrayIsNotNull.isNull( (List<Object>)field.get(object)))\{
  34. stringBuilder.append(field.getName());
  35. stringBuilder.append(";");
  36. \}
  37. \}
  38. \}
  39. return stringBuilder.toString();
  40. \}

反射的一些使用特征,1,基于目标对象,方法对象操作 2,和常规的对象.方法相反(方法对象.对象)

method.invoke(TYZDSxPTAPI.instance();

public ApiResponse apiSend(FacadeSendRequest facadeSendRequest) throws Exception{

String methodName = urlEnum.getUrl();

//将packet中的json字符串转成接口参数map

log.info(“解析api方法名为:[{}]“,methodName);

String jsonStr = facadeSendRequest.getPacket();

Map paramMap = (Map)JSON.parse(jsonStr);

Method method = TYZDSxPTAPI.class.getMethod(methodName, Map.class);

if(method == null){

log.error(“类TYZDSxPTAPI中未找到对应方法函数:[{}]“,methodName);

}

String logStr = JSONObject.toJSONString(paramMap);

log.info(“开始调用TYZDSxPTAPI.{}:参数:{}“,methodName,logStr.length() > 10000 ? (logStr.substring(0, 10000) + “…”) : logStr);

Map returnMap;

if (“TYZDAdvceListQry”.equals(methodName)) {//临时注释

Map rspMap = TYZDSxPTAPI.instance().sendRequest(“”, “ZDSupLstQry”, paramMap);

returnMap = DataUtil.dealResult(rspMap);

} else {

returnMap = (Map)method.invoke(TYZDSxPTAPI.instance(), paramMap);

}

log.info(“结束调用TYZDSxPTAPI.{}:返回:{}“,methodName,JSONObject.toJSONString(returnMap));

return afterSend(facadeSendRequest.getTradeCode(),JSONObject.toJSONString(returnMap));

}

发表评论

表情:
评论列表 (有 0 条评论,232人围观)

还没有评论,来说两句吧...

相关阅读

    相关 使用

    概述     JAVA反射机制是在运行中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态

    相关 操作方法

    目标:通过反射获得User类的Class对象,得到该类的所有信息,构造User类的对象,调用普通方法,操作属性。  \创建User类(方便演示) package R