import java.io.*; import java.lang.reflect.*; import java.text.*;public class UseReflection {
private static String strFile = ""; private Object test = "test"; public static void main(String[] args) { System.out.println("Please enter a class name: "); String name = new String(); try { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); name = br.readLine();
public static void printConstructors(Class cl) { Constructor[] constructors = cl.getDeclaredConstructors();
for (int i = 0; i < constructors.length; i++) { Constructor c = constructors[i]; Class[] paramTypes = c.getParameterTypes(); String name = c.getName(); strFile += "\t" + Modifier.toString(c.getModifiers()); strFile += " " + name + "("; for (int j = 0; j < paramTypes.length; j++) { if (j > 0) { strFile += ", "; } strFile += paramTypes[j].getName(); } strFile += ");\r\n"; } }
public static void printMethods(Class cl) { Method[] methods = cl.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) { Method m = methods[i]; Class retType = m.getReturnType(); Class[] paramTypes = m.getParameterTypes(); Class[] exceptionTypes = m.getExceptionTypes(); String name = m.getName(); strFile += "\t" + Modifier.toString(m.getModifiers()); strFile += " " + retType.getName() + " " + name + "("; for (int j = 0; j < paramTypes.length; j++) { if (j > 0) { strFile += ", "; } strFile += paramTypes[j].getName(); } strFile += ")"; if (exceptionTypes.length > 0) { strFile += " throws "; } for (int k = 0; k < exceptionTypes.length; k++) { if (k > 0) { strFile += ", "; } strFile += exceptionTypes[k].getName(); } strFile += ";\r\n"; } }
public static void printFields(Class cl) { Field[] fields = cl.getDeclaredFields(); Object value = null; for (int i = 0; i < fields.length; i++) { Field f = fields[i]; Class type = f.getType(); String name = f.getName(); try { value = f.get(new UseReflection()); } catch (Exception ex) { } strFile += "\t" + Modifier.toString(f.getModifiers()); strFile += " " + type.getName() + " " + name + "===" + value + "\r\n"; } } }
可能我没有说明白我的意思。以songlean(乐乐)兄的程序来说,当运行到printConstructors()方法的 “strFile += "\t" + Modifier.toString(c.getModifiers());” 这一行时,我让程序pause掉。然后该怎么写代码,才能取出在其上一行“String name = c.getName();”中得到的“name”的值?
line 1: class line 2: { line 3: public static void main(String[] args) line 4: { line 5: String val = getValueDummyMethod(); line 6: // do other things here. line 7: ... line 8: } line 9: }
import java.lang.reflect.*;
import java.text.*;public class UseReflection {
private static String strFile = "";
private Object test = "test";
public static void main(String[] args) {
System.out.println("Please enter a class name: ");
String name = new String();
try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
name = br.readLine();
Class cl = Class.forName(name);
Class supercl = cl.getSuperclass();
strFile = "class " + name;
if (supercl != null && !supercl.equals(Object.class)) {
strFile += " extends " + supercl.getName();
}
strFile += " {\r\n";
printConstructors(cl);
printMethods(cl);
printFields(cl);
strFile += "}\r\n";
System.out.print(strFile);
} catch (ClassNotFoundException e) {
System.out.print("Class not found.\r\n");
} catch (IOException e) {
}
}
public static void printConstructors(Class cl) {
Constructor[] constructors = cl.getDeclaredConstructors();
for (int i = 0; i < constructors.length; i++) {
Constructor c = constructors[i];
Class[] paramTypes = c.getParameterTypes();
String name = c.getName();
strFile += "\t" + Modifier.toString(c.getModifiers());
strFile += " " + name + "(";
for (int j = 0; j < paramTypes.length; j++) {
if (j > 0) {
strFile += ", ";
}
strFile += paramTypes[j].getName();
}
strFile += ");\r\n";
}
}
public static void printMethods(Class cl) {
Method[] methods = cl.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
Method m = methods[i];
Class retType = m.getReturnType();
Class[] paramTypes = m.getParameterTypes();
Class[] exceptionTypes = m.getExceptionTypes();
String name = m.getName();
strFile += "\t" + Modifier.toString(m.getModifiers());
strFile += " " + retType.getName() + " " + name + "(";
for (int j = 0; j < paramTypes.length; j++) {
if (j > 0) {
strFile += ", ";
}
strFile += paramTypes[j].getName();
}
strFile += ")";
if (exceptionTypes.length > 0) {
strFile += " throws ";
}
for (int k = 0; k < exceptionTypes.length; k++) {
if (k > 0) {
strFile += ", ";
}
strFile += exceptionTypes[k].getName();
}
strFile += ";\r\n";
}
}
public static void printFields(Class cl) {
Field[] fields = cl.getDeclaredFields();
Object value = null;
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
Class type = f.getType();
String name = f.getName();
try {
value = f.get(new UseReflection());
} catch (Exception ex) {
}
strFile += "\t" + Modifier.toString(f.getModifiers());
strFile += " " + type.getName() + " " + name + "===" + value + "\r\n";
}
}
}
“strFile += "\t" + Modifier.toString(c.getModifiers());”
这一行时,我让程序pause掉。然后该怎么写代码,才能取出在其上一行“String name = c.getName();”中得到的“name”的值?
line 2: {
line 3: public static void main(String[] args)
line 4: {
line 5: String val = getValueDummyMethod();
line 6: // do other things here.
line 7: ...
line 8: }
line 9: }
1、如果“用一个同步方法将val返回给一个共享对象”的话,调试器的代码是如何写出来的呢?
2、不同的IDE(或Debuger)的调试方法都不一样。