我正在使用 Netbeans 并升级到 JDK 15。从那时起,我遇到了反射问题。如果我执行与应用程序相同的代码,那么同样的反射效果很好。但是,如果我想在 servlet init 中使用相同的代码,那么它会失败。抛出 TargetInvocation 异常。
(这些代码与 JDK 8 完美配合)
我的代码如下(servlet init)
package servlet;
import java.io.IOException;
import java.lang.reflect.Constructor;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Administrator
*/
public class core extends HttpServlet
{
@Override
public void init() throws ServletException
{
Object id = null;
try
{
Class<?> MyPackageClass = Class.forName("webapi.UI");
boolean rc = MyPackageClass.isAssignableFrom(MyPackageClass);
Constructor<?> constructor = MyPackageClass.getDeclaredConstructor();
Object MyClassInstance = constructor.newInstance();
String s = "";
}
catch(Exception e)
{
Throwable cause = e.getCause();
String s1 = cause.getMessage();
String s2 = backTrace(cause);
String s = "";
}
}
public String backTrace(Throwable e)
{
StackTraceElement[] stack = e.getStackTrace();
String trace = "";
for (int i=0; i<stack.length; ++i) {
trace += stack[i].toString() + "\n";
}
return trace;
}
异常堆栈跟踪
s2 = (java.lang.String) "webapi.UI.<init>(UI.java:1)
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
servlet.core.init(core.java:31)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1134)
org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1089)
org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:983)
org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4864)
org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5173)
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:717)
org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:690)
org.apache.catalina.core.StandardHost.addChild(StandardHost.java:706)
org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:631)
org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:487)
org.apache.catalina.startup.HostConfig.check(HostConfig.java:1642)
jdk.internal.reflect.GeneratedMethodAccessor45.invoke(Unknown Sourc...
异常详情
e = (java.lang.reflect.InvocationTargetException) java.lang.reflect.InvocationTargetException
Cannot suppress a null exception
我正在尝试创建实例的类
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package webapi;
/**
*
* @author Administrator
*/
public class UI
{
public int x = 0;
public UI()
{
}
public String test()
{
return "oops";
}
}