给定class对象返回该类的实例
2024-10-16 06:24:52
今天分享给大家一个实用的开发技巧, 创建一个返回值为泛型的对象构建函数, 要求是传入返回值类的 class 对象.
例如: 平时我们开发接口的时候发现很多响应类里面基本都会有`code`和`error`两个属性, 而且有的人会将常量的枚举值作为接口调用成功的返回值, 这时候每次都会在接口的末尾处 new 一个接口响应值返回给调用者, 而这时候我们可以统一将这个创建含有成功响应值对象封装为一个方法.
为了节省更多重复代码, 我们会将封装这个方法定义在一个父类中.
父类代码改造前代码:
/**
* @Title: BaseResponse
* @Description:
* @author: deWei
* @date: 2022/3/12 18:28
* @Version: 1.0
*/
@Data
@NoArgsConstructor
public class BaseResponse implements Serializable {
<p style="text-indent:2em"/>private static final long serialVersionUID = -8383361046902228836L;
<p style="text-indent:2em"/>protected String code;
<p style="text-indent:2em"/>protected String errorMsg;
}
思路: ①由于我们不能创建一个对象传入而返回, 这样就无法达到我们减少代码量的需求了, 所以就要用到 java 的反射机制去构建对象, 最后得到参数为 class 对象. ②既然是在父类中定义, 那构建的对象必须就是它的子类, 这时候就需要用到泛型去限制了.
ok, 整理好思路了就开始改造父类了.
父类代码改造后代码 :
/**
* @Title: BaseResponse
* @Description:
* @author: deWei
* @date: 2022/3/12 18:28
* @Version: 1.0
*/
@Data
@NoArgsConstructor
public class BaseResponse implements Serializable {
<p style="text-indent:2em"/>private static final long serialVersionUID = -8383361046902228836L;
<p style="text-indent:2em"/>protected String code;
<p style="text-indent:2em"/>protected String errorMsg;
<p style="text-indent:2em"/>public static <T extends BaseResponse> T setResult(Class<T> tClass, ResponseEnum responseEnum) throws Exception {
<p style="text-indent:2em"/><p style="text-indent:2em"/>Constructor<T> constructor = tClass.getConstructor(ResponseEnum.class);
<p style="text-indent:2em"/><p style="text-indent:2em"/>return constructor.newInstance(responseEnum);
<p style="text-indent:2em"/>}
<p style="text-indent:2em"/>/**
<p style="text-indent:2em"/> * 调用的子类需要重写父类的 BaseResponse(ResponseEnum responseEnum) 构造器
<p style="text-indent:2em"/> */
<p style="text-indent:2em"/>public static <T extends BaseResponse> T success(Class<T> tClass) {
<p style="text-indent:2em"/><p style="text-indent:2em"/>try {
<p style="text-indent:2em"/><p style="text-indent:2em"/><p style="text-indent:2em"/>return setResult(tClass, ResponseEnum.NORMAL_SUCCESS);
<p style="text-indent:2em"/><p style="text-indent:2em"/>} catch (Exception e) {
<p style="text-indent:2em"/><p style="text-indent:2em"/><p style="text-indent:2em"/>e.printStackTrace();
<p style="text-indent:2em"/><p style="text-indent:2em"/>}
<p style="text-indent:2em"/><p style="text-indent:2em"/>return null;
<p style="text-indent:2em"/>}
<p style="text-indent:2em"/>public static <T extends BaseResponse> T failure(Class<T> tClass) {
<p style="text-indent:2em"/><p style="text-indent:2em"/>try {
<p style="text-indent:2em"/><p style="text-indent:2em"/><p style="text-indent:2em"/>return setResult(tClass, ResponseEnum.SYSTEM_ERROR);
<p style="text-indent:2em"/><p style="text-indent:2em"/>} catch (Exception e) {
<p style="text-indent:2em"/><p style="text-indent:2em"/><p style="text-indent:2em"/>e.printStackTrace();
<p style="text-indent:2em"/><p style="text-indent:2em"/>}
<p style="text-indent:2em"/><p style="text-indent:2em"/>return null;
<p style="text-indent:2em"/>}
<p style="text-indent:2em"/>public BaseResponse(ResponseEnum responseEnum) {
<p style="text-indent:2em"/><p style="text-indent:2em"/>this.code = responseEnum.code();
<p style="text-indent:2em"/><p style="text-indent:2em"/>this.errorMsg = responseEnum.errorMsg();
<p style="text-indent:2em"/>}
}
响应枚举 ResponseEnum.java:
/**
* @Title: ResponseEnum
* @Description: 响应类实体
* @author: deWei
* @date: 2022/3/12 18:28
* @Version: 1.0
*/
public enum ResponseEnum {
<p style="text-indent:2em"/>NORMAL_SUCCESS("000000", "成功"),
<p style="text-indent:2em"/>SYSTEM_ERROR("000001", "系统异常");
<p style="text-indent:2em"/>private final String code;
<p style="text-indent:2em"/>private final String errorMsg;
<p style="text-indent:2em"/>ResponseEnum(String code, String errorMsg) {
<p style="text-indent:2em"/><p style="text-indent:2em"/>this.code = code;
<p style="text-indent:2em"/><p style="text-indent:2em"/>this.errorMsg = errorMsg;
<p style="text-indent:2em"/>}
<p style="text-indent:2em"/>public String code() {
<p style="text-indent:2em"/><p style="text-indent:2em"/>return code;
<p style="text-indent:2em"/>}
<p style="text-indent:2em"/>public String errorMsg() {
<p style="text-indent:2em"/><p style="text-indent:2em"/>return errorMsg;
<p style="text-indent:2em"/>}
}
最新文章
- JAVA并发编程J.U.C学习总结
- SEO
- css 去除 a,checkbox 框 出现虚线
- RAC NTP/CTSS
- topcoder SRM 622 DIV2 BoxesDiv2
- hdu 1284 分硬币 &;&; uva 147
- 关于javascript的误区
- Jetty和Tomcat的使用及性能测试
- 欧拉工程第51题:Prime digit replacements
- bzoj3955
- ASP.NET Identity 用户注册相关设定
- 如何获取Android系统中申请对象的信息
- C++ Config 配置文件类
- Java 小记 — Spring Boot 的实践与思考
- 笔记5 bean的作用域
- Swift中关于任意类型的数组
- Windows无人值守文件unattend制作以及自定义系统安装
- 自制操作系统Antz(10)——实现shell(上)
- 编程菜鸟的日记-初学尝试编程-寻找等长数组A与B(所含元素相同,顺序不同)相匹配的元素即a[i]=b[j]
- 基于CentOS 搭建 Seafile 专属网盘
热门文章
- RabbitMQ简介及安装
- resp.getWriter().print(categoryList)、resp.getWriter().write(String)与new ObjectMapper().writeValue(resp.getOutputStream(),categoryList)的区别
- JavaScript多元运算符
- byte溢出栗子
- 《手把手教你》系列技巧篇(六十五)-java+ selenium自动化测试 - cookie -下篇(详细教程)
- node Cheerio 获取script脚本里的数据
- Solution -「多校联训」消失的运算符
- Solution -「HDU 1788」CRT again
- SIMD编码/解码
- SpringBoot 自定义内容协商策略 configureContentNegotiation