ResponseUtil反射制造唯一结果
调用:通过反射调用同一个类型的返回值
return fillResponse(response,Constants.SUCCESS,"获取数据成功","taskList",taskList);
(1)
/**
* 回复结果
* @param obj
* @param code
* @param description
* @param paramName
* @param paramValue
* @return
*/
private String fillResponse(final Object obj ,final String code , final String description,final String paramName,final Object paramValue ){
return ResponseUtil.fillResponse(obj, code, description, paramName, paramValue);
}
private String fillResponseBase(final Object obj ,final String code , final String description){
return ResponseUtil.fillResponseBase(obj, code, description);
}
(2)
package com.venustech.scanner.webservice.vulntask.util;
import java.lang.reflect.Method; import com.venustech.vwf.utils.json.JsonUtils;
/**
*
* 类说明.优化使用
* <pre>
* 2015年12月11日 songjian
* </pre>
*/
public class ResponseUtil {
/**
* 针对只有code和description的情况 +
* @param object
* @param code
* @param description
* @param paramName
* @param paramVal
* @return
*/
public static <T> String fillResponse(final T object,final String code , final String description ,final String paramName , final Object paramVal) {
if (object == null) {
return null;
}
Class<T> clazz = (Class<T>) object.getClass();
try {
Method get_Method_code = clazz.getMethod("getCode" ); //获取getMethod方法
Method set_Method_code = clazz.getMethod("setCode", get_Method_code.getReturnType());//获得属性set方法
set_Method_code.invoke(object, code); Method get_Method_des= clazz.getMethod("getDescription" ); //获取getMethod方法
Method set_Method_des = clazz.getMethod("setDescription", get_Method_des.getReturnType());//获得属性set方法
set_Method_des.invoke(object, description);
if(paramName!=""){
Method get_Method = clazz.getMethod("get" + getMethodName(paramName)); //获取getMethod方法
Method set_Method = clazz.getMethod("set" + getMethodName(paramName), get_Method.getReturnType());//获得属性set方法
set_Method.invoke(object, paramVal);
}
} catch (Exception e) {
}
return JsonUtils.toJson(object);
}
/**
* 针对只有code和description的情况
* @param object
* @param code
* @param description
* @return
*/
public static <T> String fillResponseBase(final T object,final String code , final String description) {
if (object == null) {
return null;
}
Class<T> clazz = (Class<T>) object.getClass();
try {
Method get_Method_code = clazz.getMethod("getCode" ); //获取getMethod方法
Method set_Method_code = clazz.getMethod("setCode", get_Method_code.getReturnType());//获得属性set方法
set_Method_code.invoke(object, code); Method get_Method_des= clazz.getMethod("getDescription" ); //获取getMethod方法
Method set_Method_des = clazz.getMethod("setDescription", get_Method_des.getReturnType());//获得属性set方法
set_Method_des.invoke(object, description);
} catch (Exception e) {
}
return JsonUtils.toJson(object);
} private static String getMethodName(final String fildeName) {
byte[] items = fildeName.getBytes();
items[0] = (byte) ((char) items[0] - 'a' + 'A');
return new String(items);
}
}
ResponseUtil反射制造唯一结果的更多相关文章
- .NET面试题系列[6] - 反射
反射 - 定义,实例与优化 在面试中,通常会考察反射的定义(操作元数据),可以用反射做什么(获得程序集及其各个部件),反射有什么使用场景(ORM,序列化,反序列化,值类型比较等).如果答得好,还可能会 ...
- C# 之 反射性能优化2
问题回顾 在上篇博客中,我介绍了优化反射的第一个步骤:用委托调用代替直接反射调用. 然而,那只是反射优化过程的开始,因为新的问题出现了:如何保存大量的委托? 如果我们将委托保存在字典集合中,会发现这种 ...
- 《C#从现象到本质》读书笔记(八)第10章反射
<C#从现象到本质>读书笔记(八)第10章反射 个人感觉,反射其实就是为了能够在程序运行期间动态的加载一个外部的DLL集合,然后通过某种办法找到这个DLL集合中的某个空间下的某个类的某个成 ...
- 深入理解Java String#intern() 内存模型
原文出处: codelog.me 大家知道,Java中string.intern()方法调用会先去字符串常量池中查找相应的字符串,如果字符串不存在,就会在字符串常量池中创建该字符串然后再返回. 字符串 ...
- JVM学习六:JVM之类加载器之双亲委派机制
前面我们知道类加载有系统自带的3种加载器,也有自定义的加载器,那么这些加载器之间的关系是什么,已经在加载类的时候,谁去加载呢?这节,我们将进行讲解. 一.双亲委派机制 JVM的ClassLoader采 ...
- JVM体系结构之七:持久代、元空间(Metaspace) 常量池==了解String类的intern()方法、常量池介绍、常量池从Perm-->Heap
一.intern()定义及使用 相信绝大多数的人不会去用String类的intern方法,打开String类的源码发现这是一个本地方法,定义如下: public native String inter ...
- Java Exception & RTTI
Exception Try { ... ... } catch (Exception ex) { …; throw new Throwable(ex); } catch (Throwable ex) ...
- JAVA RTTI
基础类可接收我们发给派生类的任何消息,因为两者拥有完全一致的接口.我们要做的全部事情就是从派生上溯造型,而且永远不需要回过头来检查对象的准确类型是什么.所有细节都已通过多态性获得了完美的控制. 但经过 ...
- 常见DDoS攻击
导航: 这里将一个案例事项按照流程进行了整合,这样观察起来比较清晰.部分资料来自于Cloudflare 1.DDoS介绍 2.常用DDoS攻击 3.DDoS防护方式以及产品 4.Cloudflare ...
随机推荐
- SharePoint自动化系列——Create a local user and add to SharePoint
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 实现过程:在本地创建一个local user并将该user添加到Administrators组中, ...
- Easyui主要组件用法
Easyui主要组件用法说明: 1. combogrid用法 说明:combogrid可提供翻页列表的数据选择并可进行数据的过滤查询(查询的传人参数为q,可在控制器中获取这个参数传过来的值,下面的示 ...
- CodeForces 313C Ilya and Matrix
Ilya and Matrix Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Su ...
- 《微信开发日志》之OAuth2验证接口
OAuth2接口说明: 企业应用中的URL链接(包括自定义菜单或者消息中的链接),可以通过OAuth2.0验证接口来获取员工的身份信息. 通过此接口获取用户身份会有一定的时间开销.对于频繁获取用户身份 ...
- background:linear-gradient()
文章一 http://www.runoob.com/css3/css3-gradients.html 文章二:http://www.w3cplus.com/content/css3-gradien ...
- Metasploit辅助模块
msf > show auxiliary Auxiliary ========= Name Di ...
- Timeout expired超时时间已到. 达到了最大池大小 错误及Max Pool Size设置
此文章非原创,仅为分享.学习!!! 参考数据库链接串: <add key="data" value="server=192.168.1.123; port=3306 ...
- HDU 3549 Flow Problem(最大流)
HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...
- Xcode error: conflicting types for 'XXXX'
问题描述:在main方法中调用了一个写在main方法后面的方法,比如: void main(){ A(); } void A(){} Xcode编译后就报错:conflicting types for ...
- Entity Framework 学习初级篇--基本操作:增加、更新、删除、事务(转)
摘自:http://www.cnblogs.com/xray2005/archive/2009/05/17/1458568.html 本节,直接写通过代码来学习.这些基本操作都比较简单,与这些基本操作 ...