调用:通过反射调用同一个类型的返回值

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反射制造唯一结果的更多相关文章

  1. .NET面试题系列[6] - 反射

    反射 - 定义,实例与优化 在面试中,通常会考察反射的定义(操作元数据),可以用反射做什么(获得程序集及其各个部件),反射有什么使用场景(ORM,序列化,反序列化,值类型比较等).如果答得好,还可能会 ...

  2. C# 之 反射性能优化2

    问题回顾 在上篇博客中,我介绍了优化反射的第一个步骤:用委托调用代替直接反射调用. 然而,那只是反射优化过程的开始,因为新的问题出现了:如何保存大量的委托? 如果我们将委托保存在字典集合中,会发现这种 ...

  3. 《C#从现象到本质》读书笔记(八)第10章反射

    <C#从现象到本质>读书笔记(八)第10章反射 个人感觉,反射其实就是为了能够在程序运行期间动态的加载一个外部的DLL集合,然后通过某种办法找到这个DLL集合中的某个空间下的某个类的某个成 ...

  4. 深入理解Java String#intern() 内存模型

    原文出处: codelog.me 大家知道,Java中string.intern()方法调用会先去字符串常量池中查找相应的字符串,如果字符串不存在,就会在字符串常量池中创建该字符串然后再返回. 字符串 ...

  5. JVM学习六:JVM之类加载器之双亲委派机制

    前面我们知道类加载有系统自带的3种加载器,也有自定义的加载器,那么这些加载器之间的关系是什么,已经在加载类的时候,谁去加载呢?这节,我们将进行讲解. 一.双亲委派机制 JVM的ClassLoader采 ...

  6. JVM体系结构之七:持久代、元空间(Metaspace) 常量池==了解String类的intern()方法、常量池介绍、常量池从Perm-->Heap

    一.intern()定义及使用 相信绝大多数的人不会去用String类的intern方法,打开String类的源码发现这是一个本地方法,定义如下: public native String inter ...

  7. Java Exception & RTTI

    Exception Try { ... ... } catch (Exception ex) { …; throw new Throwable(ex); } catch (Throwable ex) ...

  8. JAVA RTTI

    基础类可接收我们发给派生类的任何消息,因为两者拥有完全一致的接口.我们要做的全部事情就是从派生上溯造型,而且永远不需要回过头来检查对象的准确类型是什么.所有细节都已通过多态性获得了完美的控制. 但经过 ...

  9. 常见DDoS攻击

    导航: 这里将一个案例事项按照流程进行了整合,这样观察起来比较清晰.部分资料来自于Cloudflare 1.DDoS介绍 2.常用DDoS攻击 3.DDoS防护方式以及产品 4.Cloudflare ...

随机推荐

  1. #在lua中的运用

    在lua中"#"表示返回表长度或字符串长度 例子一: a = "Hello " b = "World" print("Concat ...

  2. CentOS 安装Redis

    redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcached类似,但很大程度补偿了memcached的不足,它支持存储的value类型相对更多,包括strin ...

  3. 加载页面(Loading)

    /* 文件说明:页面加载时Loading JS 文件描述:解决IE或FF下,初始化加载时,页面布局乱掉的问题,参考:*/var width = $(window).width();var height ...

  4. java--4种内部类

    内部类: 一 非静态内部类 //非静态内部类 //非静态内部类可任意调用外部类的局部变量,无论是否private //在外部类中要实例化内部类:InnerClass inner = new Inner ...

  5. python学习笔记六 初识面向对象上(基础篇)

    python面向对象   面向对象编程(Object-Oriented Programming )介绍   对于编程语言的初学者来讲,OOP不是一个很容易理解的编程方式,虽然大家都知道OOP的三大特性 ...

  6. Json 入门例子【2】

    <script> var json1 = { "id": 1, "tagName": "apple" }; $("#f ...

  7. 获取指定的系统路径 SHGetSpecialFolderPath

    1.获取桌面的系统路径 TCHAR szLink[MAX_PATH + ] = { }; SHGetSpecialFolderPath(,szLink,CSIDL_DESKTOPDIRECTORY,) ...

  8. Oracle重置过期的密码

    过期的原因一般有两种可能: 一.由于Oracle 11g在默认的default概要文件中设置了“PASSWORD_LIFE_TIME=180”天导致:   这种情况的解决办法: 1.查看用户的proi ...

  9. [51NOD1007] 正整数分组(DP,记忆化搜索)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1007 dp(id, s)表示第id个数之前,其中一个集合和为s ...

  10. FJNU 1159 Fat Brother’s new way(胖哥的新姿势)

    FJNU 1159 Fat Brother’s new way(胖哥的新姿势) Time Limit: 1000MS   Memory Limit: 257792K [Description] [题目 ...