Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Reflection using HTML (w/frames) + JSP + JAVA

Status
Not open for further replies.

randersonusa

Programmer
Dec 7, 2000
1
US
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=&quot;Content-Type&quot;
content=&quot;text/html; charset=iso-8859-1&quot;>
<title>Class Information</title>
</head>

<FRAMESET ROWS=&quot;25%,*&quot;>
<FRAME src=refinput.jsp name=&quot;frame1&quot;>
<FRAME src=refout.jsp name=&quot;frame2&quot;>
</FRAMESET>

<body bgcolor=&quot;#ffffff&quot; link=&quot;#9c6060&quot;>
</body>

</html>


User input

refinput.jsp
<html>

<head>
<meta http-equiv=&quot;Content-Type&quot;
content=&quot;text/html; charset=iso-8859-1&quot;>
<title>Class Information</title>
</head>

<body bgcolor=&quot;#ffffff&quot; link=&quot;#9c6060&quot;>
<h2 align=left>Class Information</h2>
<FORM name=&quot;classInput&quot; action=refout.jsp target=&quot;frame2&quot; value=&quot;&quot; method=post>
<center>
Enter class name <INPUT name=&quot;className&quot; tabindex=1 size=&quot;25&quot; style=&quot;HEIGHT: 22px; WIDTH: 203px&quot;></INPUT>
<INPUT type=submit value=&quot;Submit&quot;></INPUT>
<INPUT type=reset value=&quot;Reset&quot;></INPUT>
</center>
</FORM>
</body>

</html>

Result display
refout.jsp

<%@ page import=&quot;java.util.*,java.io.*&quot; %>
<jsp:useBean id=&quot;cInfo&quot; scope=&quot;session&quot; class=&quot;ClassInfo&quot; />

<HTML>
<BODY>
<% if (request.getMethod().equals(&quot;POST&quot;)) {
cInfo.setClass(request.getParameter(&quot;className&quot;));
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(&quot;blue&quot;);
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()) + &quot; &quot;);
}
else {
str.append(pvec[j].getName() + &quot; &quot;);
}
}
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()) + &quot; &quot;);
}
else {
str.append(evec[j].getName() + &quot; &quot;);
}
}
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(&quot;char&quot;);
break;
case 'B':
pstr.append(&quot;byte&quot;);
break;
case 'D':
pstr.append(&quot;double&quot;);
break;
case 'F':
pstr.append(&quot;float&quot;);
break;
case 'I':
pstr.append(&quot;int&quot;);
break;
case 'J':
pstr.append(&quot;long&quot;);
break;
case 'L':
pstr.append(str.substring(str.indexOf(&quot;L&quot;) + 1));
break;
case 'S':
pstr.append(&quot;short&quot;);
break;
case 'Z':
pstr.append(&quot;boolean&quot;);
break;
}
for (int i=0; i<countChar(str,'['); i++)
pstr.append(&quot;[]&quot;);
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(&quot;Class: &quot; + getClassObjectName(cls) + &quot;\r&quot;);
resultString.append(&quot;Super Class: &quot; + getSuperclassName(cls) + &quot;\r\r&quot;);
Method methlist[] = getMethods(cls);
for (int i = 0; i < methlist.length; i++) {
Method m = methlist;
resultString.append(&quot;Method: &quot; + getMethodName(m) + &quot;(&quot; + getMethodParameters(m) + &quot;)&quot; + &quot;\r&quot;);
resultString.append(&quot;Exception types: &quot; + getExceptionTypes(m) + &quot;\r&quot;);
resultString.append(&quot;Return type: &quot; + getReturnType(m) + &quot;\r&quot;);
resultString.append(&quot;\r&quot;);
}
return resultString.toString();
}

public String htmlString() {
resultString.append(&quot;<strong><font COLOR=&quot; + htmlColor + &quot;>Class: </font></strong>&quot; + getClassObjectName(cls) + &quot;<br>&quot;);
resultString.append(&quot;<strong><font COLOR=&quot; + htmlColor + &quot;>Super Class: </font></strong>&quot; + getSuperclassName(cls) + &quot;<p>&quot;);
Method methlist[] = getMethods(cls);
for (int i = 0; i < methlist.length; i++) {
Method m = methlist;
resultString.append(&quot;<strong><font COLOR=&quot; + htmlColor + &quot;>Method: </font></strong>&quot; + getMethodName(m) + &quot;(&quot; + getMethodParameters(m) + &quot;)&quot; + &quot;<br>&quot;);
resultString.append(&quot;<strong><font COLOR=&quot; + htmlColor + &quot;>Exception types: </font></strong>&quot; + getExceptionTypes(m) + &quot;<br>&quot;);
resultString.append(&quot;<strong><font COLOR=&quot; + htmlColor + &quot;>Return type: </font></strong>&quot; + getReturnType(m) + &quot;<br>&quot;);
resultString.append(&quot;<p>&quot;);
}
return resultString.toString();
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top