N
natG
Hi;
I am not new to reflection, but due to events, I feel new.
When passing a ServletContext as a parameter to the reflected method, it
throws the NoSuchMethodException.
Here is the exact simplified code that produces it. (Please excuse the
extra Invoker class, I use it to test non-servlet related reflection.)
Thank you, in advance.
-nat
I am not new to reflection, but due to events, I feel new.
When passing a ServletContext as a parameter to the reflected method, it
throws the NoSuchMethodException.
Here is the exact simplified code that produces it. (Please excuse the
extra Invoker class, I use it to test non-servlet related reflection.)
Code:
package ez.test;
import javax.servlet.ServletContext;
public class StaticMethodClass {
public static void methodA(String s1, String s2){
System.out.println("Hello from methodA: s1 s2. You passed "+ s1
+ " " + s2);
}
public static void methodA(ServletContext ctx, String s2){
System.out.print("Hello from methodA: ctx s2 . You passed "+ s2);
System.out.println(" from ServletContext: " + ctx.toString());
}
}
package ez.test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Invoker {
Method m = null;
public Invoker(String methodName, Object[] methodParameters){
runCustomMethod(methodName,methodParameters);
}
public void runCustomMethod(String methodName, Object[] methodParams){
Class c = StaticMethodClass.class;
Method m = null;
Class[] parameterTypes = null;
if (methodParams!=null){
parameterTypes = new Class[methodParams.length];
for (int i=0;i<methodParams.length;i++){
parameterTypes[i] = methodParams[i].getClass();
}
}
try {
m = c.getDeclaredMethod(methodName,parameterTypes);
m.invoke(null,methodParams);
} catch (SecurityException e) {
System.out.println("Problem with *Security* in "
+methodName +".");
//e.printStackTrace();
} catch (NoSuchMethodException e) {
System.out.println("Error with the reflective
method."+methodName + ".");
//e.printStackTrace();
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (InvocationTargetException e1) {
e1.printStackTrace();
}
}
public static void main(String[] args) {
new Invoker("methodA", new Object[]{"String-0","String-1"});
}
}
//now the real guy, the servlet.
/* Date: 08/12/2004 14:08:57 */
package test;
import ez.test.Invoker;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Nat
* @version 0.01
* 08/12/2004 14:08:57
*/
public class InvokeMethodServlet extends HttpServlet {
ServletContext ctx = null;
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String s0 = "StringFromServlet-0", s1= "StringFromServlet-1";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01
Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>Test Reflection
Servlet.</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<p>This servlet simply tests reflection calls.</p>");
out.println("Check server log for output from strings: " + s0 +
" " + s1);
out.println("Also Check server log for output from CTX &
string: " + s1);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
new Invoker("methodA", new Object[]{s0,s1}); //this works ok.
new Invoker("methodA", new Object[]{ctx,s1}); //this does NOT.
}
public void init() throws ServletException {
ctx=getServletContext();
}
}
Thank you, in advance.
-nat