randersonusa
Programmer
The code provided, demonstrates how to use HTML with frames to provide a simple user interface to view java class methods.
Starting jsp code that creates the frames.
reflection.jsp
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<title>Class Information</title>
</head>
<FRAMESET ROWS="25%,*">
<FRAME src=refinput.jsp name="frame1">
<FRAME src=refout.jsp name="frame2">
</FRAMESET>
<body bgcolor="#ffffff" link="#9c6060">
</body>
</html>
User input
refinput.jsp
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<title>Class Information</title>
</head>
<body bgcolor="#ffffff" link="#9c6060">
<h2 align=left>Class Information</h2>
<FORM name="classInput" action=refout.jsp target="frame2" value="" method=post>
<center>
Enter class name <INPUT name="className" tabindex=1 size="25" style="HEIGHT: 22px; WIDTH: 203px"></INPUT>
<INPUT type=submit value="Submit"></INPUT>
<INPUT type=reset value="Reset"></INPUT>
</center>
</FORM>
</body>
</html>
Result display
refout.jsp
<%@ page import="java.util.*,java.io.*" %>
<jsp:useBean id="cInfo" scope="session" class="ClassInfo" />
<HTML>
<BODY>
<% if (request.getMethod().equals("POST") {
cInfo.setClass(request.getParameter("className");
if (cInfo.loadedClass()) { %>
<%= cInfo.htmlString() %>
<% }
else { %>
<h2 align=center>Class not found</h2>
<% }
} %>
</BODY>
</HTML>
Java bean to implement the reflection class. This bean receives the class name (ie. java.lang.String), and builds a string output with information regarding the class. Method textAreaString() is used to get output that is text. Method htmlString() provides the html rendering of the output.
ClassInfo.java
/************************************
* Provide information classes *
************************************/
import java.lang.reflect.*;
public class ClassInfo {
private Class cls;
private StringBuffer resultString;
private String htmlColor = new String("blue"
private boolean classFound = false;
public void setClass (String s) throws ClassNotFoundException {
resultString = new StringBuffer();
if (s.length() != 0) {
try {
classFound = true;
cls = Class.forName(s);
}
catch (ClassNotFoundException e) {
classFound = false;
}
}
}
public boolean loadedClass() {
return classFound;
}
public Class getClassObject() {
return cls;
}
public String getClassObjectName(Class c) {
return c.getName();
}
public String getSuperclassName(Class c) {
return c.getSuperclass().getName();
}
public Method[] getMethods(Class c) {
return c.getDeclaredMethods();
}
public String getMethodName(Method m) {
return m.getName();
}
public String getDeclaringClassName(Method m) {
return m.getDeclaringClass().toString();
}
public String getMethodParameters(Method m) {
StringBuffer str = new StringBuffer();
Class pvec[] = m.getParameterTypes();
for (int j = 0; j < pvec.length; j++) {
if (pvec[j].isArray()) {
str.append(arrayList(pvec[j].getName()) + " "
}
else {
str.append(pvec[j].getName() + " "
}
}
return str.toString().trim();
}
public String getExceptionTypes(Method m) {
StringBuffer str = new StringBuffer();
Class evec[] = m.getExceptionTypes();
for (int j = 0; j < evec.length; j++) {
if (evec[j].isArray()) {
str.append(arrayList(evec[j].getName()) + " "
}
else {
str.append(evec[j].getName() + " "
}
}
return str.toString().trim();
}
public String getReturnType(Method m) {
StringBuffer str = new StringBuffer();
Class rtn = m.getReturnType();
if (rtn.isArray()) {
str.append(arrayList(rtn.getName()));
} else {
str.append(rtn.getName());
}
return str.toString();
}
public String arrayList(String str) {
StringBuffer pstr = new StringBuffer();
switch (str.charAt(str.length()-1)) {
case 'C':
pstr.append("char"
break;
case 'B':
pstr.append("byte"
break;
case 'D':
pstr.append("double"
break;
case 'F':
pstr.append("float"
break;
case 'I':
pstr.append("int"
break;
case 'J':
pstr.append("long"
break;
case 'L':
pstr.append(str.substring(str.indexOf("L" + 1));
break;
case 'S':
pstr.append("short"
break;
case 'Z':
pstr.append("boolean"
break;
}
for (int i=0; i<countChar(str,'['); i++)
pstr.append("[]"
return pstr.toString();
}
public int countChar(String s, char match) {
int count = 0;
char[] c;
c = s.toCharArray();
for (int i=0; i<c.length; i++)
if (c == match) count++;
return count;
}
public String textAreaString() {
resultString.append("Class: " + getClassObjectName(cls) + "\r"
resultString.append("Super Class: " + getSuperclassName(cls) + "\r\r"
Method methlist[] = getMethods(cls);
for (int i = 0; i < methlist.length; i++) {
Method m = methlist;
resultString.append("Method: " + getMethodName(m) + "(" + getMethodParameters(m) + "" + "\r"
resultString.append("Exception types: " + getExceptionTypes(m) + "\r"
resultString.append("Return type: " + getReturnType(m) + "\r"
resultString.append("\r"
}
return resultString.toString();
}
public String htmlString() {
resultString.append("<strong><font COLOR=" + htmlColor + ">Class: </font></strong>" + getClassObjectName(cls) + "<br>"
resultString.append("<strong><font COLOR=" + htmlColor + ">Super Class: </font></strong>" + getSuperclassName(cls) + "<p>"
Method methlist[] = getMethods(cls);
for (int i = 0; i < methlist.length; i++) {
Method m = methlist;
resultString.append("<strong><font COLOR=" + htmlColor + ">Method: </font></strong>" + getMethodName(m) + "(" + getMethodParameters(m) + "" + "<br>"
resultString.append("<strong><font COLOR=" + htmlColor + ">Exception types: </font></strong>" + getExceptionTypes(m) + "<br>"
resultString.append("<strong><font COLOR=" + htmlColor + ">Return type: </font></strong>" + getReturnType(m) + "<br>"
resultString.append("<p>"
}
return resultString.toString();
}
}
Starting jsp code that creates the frames.
reflection.jsp
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<title>Class Information</title>
</head>
<FRAMESET ROWS="25%,*">
<FRAME src=refinput.jsp name="frame1">
<FRAME src=refout.jsp name="frame2">
</FRAMESET>
<body bgcolor="#ffffff" link="#9c6060">
</body>
</html>
User input
refinput.jsp
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<title>Class Information</title>
</head>
<body bgcolor="#ffffff" link="#9c6060">
<h2 align=left>Class Information</h2>
<FORM name="classInput" action=refout.jsp target="frame2" value="" method=post>
<center>
Enter class name <INPUT name="className" tabindex=1 size="25" style="HEIGHT: 22px; WIDTH: 203px"></INPUT>
<INPUT type=submit value="Submit"></INPUT>
<INPUT type=reset value="Reset"></INPUT>
</center>
</FORM>
</body>
</html>
Result display
refout.jsp
<%@ page import="java.util.*,java.io.*" %>
<jsp:useBean id="cInfo" scope="session" class="ClassInfo" />
<HTML>
<BODY>
<% if (request.getMethod().equals("POST") {
cInfo.setClass(request.getParameter("className");
if (cInfo.loadedClass()) { %>
<%= cInfo.htmlString() %>
<% }
else { %>
<h2 align=center>Class not found</h2>
<% }
} %>
</BODY>
</HTML>
Java bean to implement the reflection class. This bean receives the class name (ie. java.lang.String), and builds a string output with information regarding the class. Method textAreaString() is used to get output that is text. Method htmlString() provides the html rendering of the output.
ClassInfo.java
/************************************
* Provide information classes *
************************************/
import java.lang.reflect.*;
public class ClassInfo {
private Class cls;
private StringBuffer resultString;
private String htmlColor = new String("blue"
private boolean classFound = false;
public void setClass (String s) throws ClassNotFoundException {
resultString = new StringBuffer();
if (s.length() != 0) {
try {
classFound = true;
cls = Class.forName(s);
}
catch (ClassNotFoundException e) {
classFound = false;
}
}
}
public boolean loadedClass() {
return classFound;
}
public Class getClassObject() {
return cls;
}
public String getClassObjectName(Class c) {
return c.getName();
}
public String getSuperclassName(Class c) {
return c.getSuperclass().getName();
}
public Method[] getMethods(Class c) {
return c.getDeclaredMethods();
}
public String getMethodName(Method m) {
return m.getName();
}
public String getDeclaringClassName(Method m) {
return m.getDeclaringClass().toString();
}
public String getMethodParameters(Method m) {
StringBuffer str = new StringBuffer();
Class pvec[] = m.getParameterTypes();
for (int j = 0; j < pvec.length; j++) {
if (pvec[j].isArray()) {
str.append(arrayList(pvec[j].getName()) + " "
}
else {
str.append(pvec[j].getName() + " "
}
}
return str.toString().trim();
}
public String getExceptionTypes(Method m) {
StringBuffer str = new StringBuffer();
Class evec[] = m.getExceptionTypes();
for (int j = 0; j < evec.length; j++) {
if (evec[j].isArray()) {
str.append(arrayList(evec[j].getName()) + " "
}
else {
str.append(evec[j].getName() + " "
}
}
return str.toString().trim();
}
public String getReturnType(Method m) {
StringBuffer str = new StringBuffer();
Class rtn = m.getReturnType();
if (rtn.isArray()) {
str.append(arrayList(rtn.getName()));
} else {
str.append(rtn.getName());
}
return str.toString();
}
public String arrayList(String str) {
StringBuffer pstr = new StringBuffer();
switch (str.charAt(str.length()-1)) {
case 'C':
pstr.append("char"
break;
case 'B':
pstr.append("byte"
break;
case 'D':
pstr.append("double"
break;
case 'F':
pstr.append("float"
break;
case 'I':
pstr.append("int"
break;
case 'J':
pstr.append("long"
break;
case 'L':
pstr.append(str.substring(str.indexOf("L" + 1));
break;
case 'S':
pstr.append("short"
break;
case 'Z':
pstr.append("boolean"
break;
}
for (int i=0; i<countChar(str,'['); i++)
pstr.append("[]"
return pstr.toString();
}
public int countChar(String s, char match) {
int count = 0;
char[] c;
c = s.toCharArray();
for (int i=0; i<c.length; i++)
if (c == match) count++;
return count;
}
public String textAreaString() {
resultString.append("Class: " + getClassObjectName(cls) + "\r"
resultString.append("Super Class: " + getSuperclassName(cls) + "\r\r"
Method methlist[] = getMethods(cls);
for (int i = 0; i < methlist.length; i++) {
Method m = methlist;
resultString.append("Method: " + getMethodName(m) + "(" + getMethodParameters(m) + "" + "\r"
resultString.append("Exception types: " + getExceptionTypes(m) + "\r"
resultString.append("Return type: " + getReturnType(m) + "\r"
resultString.append("\r"
}
return resultString.toString();
}
public String htmlString() {
resultString.append("<strong><font COLOR=" + htmlColor + ">Class: </font></strong>" + getClassObjectName(cls) + "<br>"
resultString.append("<strong><font COLOR=" + htmlColor + ">Super Class: </font></strong>" + getSuperclassName(cls) + "<p>"
Method methlist[] = getMethods(cls);
for (int i = 0; i < methlist.length; i++) {
Method m = methlist;
resultString.append("<strong><font COLOR=" + htmlColor + ">Method: </font></strong>" + getMethodName(m) + "(" + getMethodParameters(m) + "" + "<br>"
resultString.append("<strong><font COLOR=" + htmlColor + ">Exception types: </font></strong>" + getExceptionTypes(m) + "<br>"
resultString.append("<strong><font COLOR=" + htmlColor + ">Return type: </font></strong>" + getReturnType(m) + "<br>"
resultString.append("<p>"
}
return resultString.toString();
}
}