如果用工具的话 偶知道 eclipse 等等..runtime取得 应该是reflect

解决方案 »

  1.   

    那么,自己如何编写相应的java代码呢?
      

  2.   

    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();

    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";
    }
    }
    }
      

  3.   

    可能我没有说明白我的意思。以songlean(乐乐)兄的程序来说,当运行到printConstructors()方法的
    “strFile += "\t" + Modifier.toString(c.getModifiers());”
    这一行时,我让程序pause掉。然后该怎么写代码,才能取出在其上一行“String name = c.getName();”中得到的“name”的值?
      

  4.   

    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: }
      

  5.   

    更简单一点,以上面的程序来说,在程序运行到line 6的时候,将其pause。此时,如何用一个其他的线程将val的值取出?
      

  6.   

    需要用一个同步方法将val返回给一个共享对象。
      

  7.   

    to admiralsf(甲丁):
    1、如果“用一个同步方法将val返回给一个共享对象”的话,调试器的代码是如何写出来的呢?
    2、不同的IDE(或Debuger)的调试方法都不一样。