From 269c1c1299308582a47bcde10c37c493c8bc1e37 Mon Sep 17 00:00:00 2001 From: octopusYan <935540320@qq.com> Date: Sat, 9 Apr 2022 14:04:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20lambda=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=8E=B7=E5=8F=96=E4=B8=8D=E5=88=B0=E6=B3=9B?= =?UTF-8?q?=E5=9E=8B=E7=B1=BB=E5=9E=8B=20=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/LoginController.java | 10 ++++ .../octopusyan/controller/MainController.java | 49 +++++++++++------ .../controller/RegisterController.java | 52 ++++++++++++------- .../top/octopusyan/manager/http/Demo.java | 5 ++ .../top/octopusyan/manager/http/EasyHttp.java | 2 +- .../octopusyan/manager/http/EasyUtils.java | 30 +++++++++++ .../manager/http/callback/NormalCallback.java | 2 +- .../manager/http/listener/OnHttpListener.java | 8 ++- .../manager/http/request/IRequestHandler.java | 8 +++ .../java/top/octopusyan/utils/ProxyUtil.java | 44 +++++++++++++--- 10 files changed, 160 insertions(+), 50 deletions(-) diff --git a/src/main/java/top/octopusyan/controller/LoginController.java b/src/main/java/top/octopusyan/controller/LoginController.java index 931bda8..dca73da 100644 --- a/src/main/java/top/octopusyan/controller/LoginController.java +++ b/src/main/java/top/octopusyan/controller/LoginController.java @@ -226,6 +226,11 @@ public class LoginController extends BaseController implements Initia } }); } + + @Override + public void onFail(Exception e) { + + } }); } @@ -240,6 +245,11 @@ public class LoginController extends BaseController implements Initia public void onSucceed(String result) { ProxyUtil.setCsrf(result); } + + @Override + public void onFail(Exception e) { + + } }); } diff --git a/src/main/java/top/octopusyan/controller/MainController.java b/src/main/java/top/octopusyan/controller/MainController.java index dcdc43b..d1c3e87 100644 --- a/src/main/java/top/octopusyan/controller/MainController.java +++ b/src/main/java/top/octopusyan/controller/MainController.java @@ -27,6 +27,7 @@ import top.octopusyan.config.ProxyConfig; import top.octopusyan.config.ProxyConfig.ProxyServer; import top.octopusyan.config.ProxyConfig.ProxyType; import top.octopusyan.http.request.ProxySetup; +import top.octopusyan.manager.http.listener.OnHttpListener; import top.octopusyan.model.ProxySetupModel; import top.octopusyan.utils.AlertUtil; import top.octopusyan.utils.DomainUtil; @@ -147,21 +148,29 @@ public class MainController extends BaseController implements Initial Arrays.asList(ProxyServer.values()).forEach((server) -> proxyServerComboBox.getItems().add(server.getServerName())); // 获取用户隧道列表 - ProxyUtil.getList(setupList -> { + ProxyUtil.getList(new OnHttpListener>() { + @Override + public void onSucceed(List result) { - if (setupList != null) - proxyList = setupList; + if (result != null) + proxyList = result; + + // 如果用户隧道列表不为空 + if (proxyList != null && proxyList.size() > 0) { + // 显示隧道列表 + initProxyList(proxyList); + // 默认选中第一个(配置隧道设置模型) + proxyListView.getSelectionModel().select(0); + } else { + // 配置隧道设置模型 + proxyList = new ArrayList<>(); + proxyList.add(proxySetup); + } + } + + @Override + public void onFail(Exception e) { - // 如果用户隧道列表不为空 - if (proxyList != null && proxyList.size() > 0) { - // 显示隧道列表 - initProxyList(proxyList); - // 默认选中第一个(配置隧道设置模型) - proxyListView.getSelectionModel().select(0); - } else { - // 配置隧道设置模型 - proxyList = new ArrayList<>(); - proxyList.add(proxySetup); } }); @@ -484,9 +493,17 @@ public class MainController extends BaseController implements Initial ProxyUtil.delete(proxySetupModel.getId()); // 添加隧道 - ProxyUtil.add(result -> { - proxySetup = result; - proxySetupModel.set(proxySetup); + ProxyUtil.add(new OnHttpListener() { + @Override + public void onSucceed(ProxySetup result) { + proxySetup = result; + proxySetupModel.set(proxySetup); + } + + @Override + public void onFail(Exception e) { + + } }, proxySetupModel.get()); } diff --git a/src/main/java/top/octopusyan/controller/RegisterController.java b/src/main/java/top/octopusyan/controller/RegisterController.java index ab9a9d6..097df78 100644 --- a/src/main/java/top/octopusyan/controller/RegisterController.java +++ b/src/main/java/top/octopusyan/controller/RegisterController.java @@ -148,6 +148,11 @@ public class RegisterController extends BaseController implements Ser public void onSucceed(String result) { Platform.runLater(() -> AlertUtil.info("系统已发送一封邮件至您的邮箱,请查收。").show()); } + + @Override + public void onFail(Exception e) { + + } }); } }); @@ -167,25 +172,34 @@ public class RegisterController extends BaseController implements Ser ApplicatonStore.getEmail(), emailCheckCodeTf.getText() )) - .request(result -> { - // 注册出错 - if (JsoupUtil.isAlertSuccess(result)) { - // 注册成功 - Platform.runLater(() -> - AlertUtil.info(JsoupUtil.getHtmlMessage(result)).header(null).show() - ); - ApplicatonStore.setRegisterSuccess(true); - Platform.runLater(() -> { - try { - jumpTo(new LoginController()); - } catch (IOException e) { - e.printStackTrace(); - } - }); - } else - Platform.runLater(() -> - AlertUtil.error(JsoupUtil.getErrorMessage(result)).header(null).show() - ); + .request(new OnHttpListener() { + @Override + public void onSucceed(String result) { + + // 注册出错 + if (JsoupUtil.isAlertSuccess(result)) { + // 注册成功 + Platform.runLater(() -> + AlertUtil.info(JsoupUtil.getHtmlMessage(result)).header(null).show() + ); + ApplicatonStore.setRegisterSuccess(true); + Platform.runLater(() -> { + try { + jumpTo(new LoginController()); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } else + Platform.runLater(() -> + AlertUtil.error(JsoupUtil.getErrorMessage(result)).header(null).show() + ); + } + + @Override + public void onFail(Exception e) { + + } }); } } diff --git a/src/main/java/top/octopusyan/manager/http/Demo.java b/src/main/java/top/octopusyan/manager/http/Demo.java index a46065a..0640289 100644 --- a/src/main/java/top/octopusyan/manager/http/Demo.java +++ b/src/main/java/top/octopusyan/manager/http/Demo.java @@ -43,6 +43,11 @@ public class Demo { public void onSucceed(String result) { } + + @Override + public void onFail(Exception e) { + + } }); try { diff --git a/src/main/java/top/octopusyan/manager/http/EasyHttp.java b/src/main/java/top/octopusyan/manager/http/EasyHttp.java index 081b71e..3d0b217 100644 --- a/src/main/java/top/octopusyan/manager/http/EasyHttp.java +++ b/src/main/java/top/octopusyan/manager/http/EasyHttp.java @@ -195,7 +195,7 @@ public class EasyHttp { try { mCallProxy = new CallProxy(createCall()); Response response = mCallProxy.execute(); - return (Result) mHandler.requestSucceed(getRequestApi(), response, EasyUtils.getReflectType(responseClass)); + return (Result) mHandler.requestSucceed(getRequestApi(), response, mHandler.getType(responseClass)); } catch (Exception e) { throw mHandler.requestFail(getRequestApi(), e); } diff --git a/src/main/java/top/octopusyan/manager/http/EasyUtils.java b/src/main/java/top/octopusyan/manager/http/EasyUtils.java index 9555acd..493e1c6 100644 --- a/src/main/java/top/octopusyan/manager/http/EasyUtils.java +++ b/src/main/java/top/octopusyan/manager/http/EasyUtils.java @@ -244,6 +244,7 @@ public class EasyUtils { if (object == null) { return Void.class; } + Type[] types = object.getClass().getGenericInterfaces(); if (types.length > 0) { // 如果这个监听对象是直接实现了接口 @@ -253,6 +254,35 @@ public class EasyUtils { // 如果这个监听对象是通过类继承 return ((ParameterizedType) object.getClass().getGenericSuperclass()).getActualTypeArguments()[0]; } + /** + * 获取对象上面的泛型 + */ + public static Type getGenericType(Object object) { + if (object == null) { + return Void.class; + } + // 获取接口上面的泛型 + Type[] types = object.getClass().getGenericInterfaces(); + if (types.length > 0) { + // 如果这个对象是直接实现了接口,并且携带了泛型 + return ((ParameterizedType) types[0]).getActualTypeArguments()[0]; + } + + // 获取父类上面的泛型 + Type genericSuperclass = object.getClass().getGenericSuperclass(); + if (!(genericSuperclass instanceof ParameterizedType)) { + return Void.class; + } + + Type[] actualTypeArguments = ((ParameterizedType) genericSuperclass).getActualTypeArguments(); + if (actualTypeArguments.length == 0) { + return Void.class; + } + + // 如果这个对象是通过类继承,并且携带了泛型 + return actualTypeArguments[0]; + } + /** diff --git a/src/main/java/top/octopusyan/manager/http/callback/NormalCallback.java b/src/main/java/top/octopusyan/manager/http/callback/NormalCallback.java index 1ca7e21..5d15d8b 100644 --- a/src/main/java/top/octopusyan/manager/http/callback/NormalCallback.java +++ b/src/main/java/top/octopusyan/manager/http/callback/NormalCallback.java @@ -40,7 +40,7 @@ public final class NormalCallback extends BaseCallback { protected void onResponse(Response response) throws Exception { // 打印请求耗时时间 HttpLog.print("RequestConsuming:" + (response.receivedResponseAtMillis() - response.sentRequestAtMillis()) + " ms"); - final Object result = mRequestHandler.requestSucceed(mRequestApi, response, EasyUtils.getReflectType(mListener)); + final Object result = mRequestHandler.requestSucceed(mRequestApi, response, mRequestHandler.getType(mListener)); EasyUtils.post(() -> { if (mListener == null) { return; diff --git a/src/main/java/top/octopusyan/manager/http/listener/OnHttpListener.java b/src/main/java/top/octopusyan/manager/http/listener/OnHttpListener.java index c05022e..8be54d2 100644 --- a/src/main/java/top/octopusyan/manager/http/listener/OnHttpListener.java +++ b/src/main/java/top/octopusyan/manager/http/listener/OnHttpListener.java @@ -1,11 +1,10 @@ package top.octopusyan.manager.http.listener; -import javafx.application.Platform; -import lombok.extern.slf4j.Slf4j; import okhttp3.Call; -import top.octopusyan.utils.AlertUtil; /** + * 使用时不能为lambda 否则获取不到泛型类型 + * * @author : octopus yan * @email : octopus_yan@foxmail.com * @description : 网络请求监听 @@ -26,8 +25,7 @@ public interface OnHttpListener { /** * 请求出错 */ - default void onFail(Exception e) { - } + void onFail(Exception e); /** * 请求结束 diff --git a/src/main/java/top/octopusyan/manager/http/request/IRequestHandler.java b/src/main/java/top/octopusyan/manager/http/request/IRequestHandler.java index eae702b..06c65f7 100644 --- a/src/main/java/top/octopusyan/manager/http/request/IRequestHandler.java +++ b/src/main/java/top/octopusyan/manager/http/request/IRequestHandler.java @@ -4,6 +4,7 @@ import java.lang.reflect.Type; import okhttp3.Request; import okhttp3.Response; +import top.octopusyan.manager.http.EasyUtils; import top.octopusyan.manager.http.api.RequestApi; public interface IRequestHandler { @@ -36,4 +37,11 @@ public interface IRequestHandler { * @return 错误对象 */ Exception requestFail(RequestApi api, Exception e); + + /** + * 解析泛型 + */ + default Type getType(Object object) { + return EasyUtils.getGenericType(object); + } } \ No newline at end of file diff --git a/src/main/java/top/octopusyan/utils/ProxyUtil.java b/src/main/java/top/octopusyan/utils/ProxyUtil.java index 5015d33..433a95f 100644 --- a/src/main/java/top/octopusyan/utils/ProxyUtil.java +++ b/src/main/java/top/octopusyan/utils/ProxyUtil.java @@ -55,7 +55,16 @@ public class ProxyUtil { EasyHttp.builder() .api(Api.DeleteProxy()) .pathParam(String.valueOf(id), csrf) - .request(result -> { + .request(new OnHttpListener() { + @Override + public void onSucceed(Void result) { + + } + + @Override + public void onFail(Exception e) { + + } }); } @@ -155,15 +164,34 @@ public class ProxyUtil { EasyHttp.builder() .api(Api.AddProxy()) .param(setup) - .request(result -> getList(result1 -> { - if (result1 != null && result1.size() > 0) { - for (ProxySetup proxySetup : result1) { - if (Objects.equals(proxySetup.getSort(), proxySetup.getSort())) { - listener.onSucceed(proxySetup); + .request(new OnHttpListener() { + @Override + public void onSucceed(String result) { + getList(new OnHttpListener>() { + @Override + public void onSucceed(List result) { + + if (result != null && result.size() > 0) { + for (ProxySetup proxySetup : result) { + if (Objects.equals(proxySetup.getSort(), proxySetup.getSort())) { + listener.onSucceed(proxySetup); + } + } + } } - } + + @Override + public void onFail(Exception e) { + + } + }); } - })); + + @Override + public void onFail(Exception e) { + + } + }); } public static String getCsrf() {