I am using Netbeans and upgraded to JDK 15. Since then, I am having problems with reflection. The very same reflection works fine if I do the same code as application. However, if I want to use the same code in servlet init then it failes. Throwing an exception of TargetInvocation exception.
(These codes works perfectly fine with JDK 8)
My code is below (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;
}
The exception stack 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...
Exception Details
e = (java.lang.reflect.InvocationTargetException) java.lang.reflect.InvocationTargetException
Cannot suppress a null exception
And the class I am trying to create the instance of
/*
* 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";
}
}
Aucun commentaire:
Enregistrer un commentaire