Zamples, Inc. logo
 Home   Search   Solutions   My Zamples   FAQ   News   Contact 
Zamples ID:
Password:
   
0 anonymous users;
27 users logged in.

Live Samples
Live APIs

HTML or JSP Frag
Java Servlet Frag
HTML & Applet
Bash
C# (Mono)
C++ (gcc)
Groovy
Haskell (Hugs98)
J2SE 1.4 Class
J2SE 1.4 Fragment
J2SE 5.0 Class
J2SE 5.0 Fragment
J2SE 6.0 Class
J2SE 6.0 Fragment
Perl
Python
Ruby
 

Java Reflection Sample Code

If the Zamples security manager is configured to disable reflection then the following program fragments will not work.  The default setting for the security manager does allow for reflection, so the following program fragments should work on most installations.

File Access

List All Methods of a class

out.println("<h3>" + getClass().getName() + " Methods</h3>");
Method[] methods = getClass().getDeclaredMethods();
for (int i=0; i<methods.length; i++) {
   Method m = methods[i];
   Class retType = m.getReturnType();
   Class[] paramTypes = m.getParameterTypes();
   String name = m.getName();
   out.print(Modifier.toString(m.getModifiers()));
   out.print(" " + retType.getName() + " " + name + "(");
   for (int j=0; j<paramTypes.length; j++) {
      if (j>0) out.print(", ");
      out.print(paramTypes[j].getName());
   }
   out.println(");");
}

Sample Output

Formatted for easier reading. Note that a JSP's class has a very long name!

sessions._0002fsessions_0002fl_00030x_00034syf_00031b_00031_0002ejspl0x4syf1b1_jsp_22
Methods public void _jspService(javax.servlet.http.HttpServletRequest,
    javax.servlet.http.HttpServletResponse);
public final void _jspx_init();

Add New Code Samples Here

If you have code samples that don't fit into any of the topics above which you would like to contribute, please put them here.  We'll sort them out into their own sections as appropriate later.

0 Quality Tested Examples 0 Problems & Suggestions 0 Closed Issues
to add a new posting or reply to an existing posting.