From af0e0a110e7187bf008655f7510199a0c0b25ec4 Mon Sep 17 00:00:00 2001 From: Nymph2333 <498092988@qq.com> Date: Mon, 10 Apr 2023 06:27:40 +0000 Subject: [PATCH] =?UTF-8?q?newInstance()=20=E5=B7=B2=E5=BC=83=E7=94=A8,?= =?UTF-8?q?=E4=BD=BF=E7=94=A8clazz.getDeclaredConstructor().newInstance()?= =?UTF-8?q?=20This=20method=20propagates=20any=20exception=20thrown=20by?= =?UTF-8?q?=20the=20nullary=20constructor,=20including=20a=20checked=20exc?= =?UTF-8?q?eption.=20Use=20of=20this=20method=20effectively=20bypasses=20t?= =?UTF-8?q?he=20compile-time=20exception=20checking=20that=20would=20other?= =?UTF-8?q?wise=20be=20performed=20by=20the=20compiler.=20The=20Constructo?= =?UTF-8?q?r.newInstance=20method=20avoids=20this=20problem=20by=20wrappin?= =?UTF-8?q?g=20any=20exception=20thrown=20by=20the=20constructor=20in=20a?= =?UTF-8?q?=20(checked)=20InvocationTargetException.=20The=20call=20=20cla?= =?UTF-8?q?zz.newInstance()=20can=20be=20replaced=20by=20=20clazz.getDecla?= =?UTF-8?q?redConstructor().newInstance()=20The=20latter=20sequence=20of?= =?UTF-8?q?=20calls=20is=20inferred=20to=20be=20able=20to=20throw=20the=20?= =?UTF-8?q?additional=20exception=20types=20InvocationTargetException=20an?= =?UTF-8?q?d=20NoSuchMethodException.=20Both=20of=20these=20exception=20ty?= =?UTF-8?q?pes=20are=20subclasses=20of=20ReflectiveOperationException.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nymph2333 <498092988@qq.com> --- .../src/main/java/com/ruoyi/quartz/util/JobInvokeUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/JobInvokeUtil.java b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/JobInvokeUtil.java index 57808fa..9a90d7b 100644 --- a/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/JobInvokeUtil.java +++ b/ruoyi-quartz/src/main/java/com/ruoyi/quartz/util/JobInvokeUtil.java @@ -34,7 +34,7 @@ public class JobInvokeUtil } else { - Object bean = Class.forName(beanName).newInstance(); + Object bean = Class.forName(beanName).getDeclaredConstructor().newInstance(); invokeMethod(bean, methodName, methodParams); } }