001: package isql;
002:
003: import java.util.*;
004: import java.lang.reflect.*;
005: import java.sql.*;
006: import util.*;
007:
008: public class ReflectUtils {
009:
010: public static void main(String args[]) {
011: String cname = "java.sql.ResultSet";
012: if (args.length == 1)
013: cname = args[0];
014: if (cname.indexOf('.') == -1) {
015: cname = "java.sql." + cname;
016: System.out.println("Using:" + cname);
017: }
018: String[] ret = getClassFields(cname);
019: System.out.println("Fields:");
020: for (int i = 0; i < ret.length; i++)
021: System.out.println(ret[i]);
022:
023: ret = getClassMethods(cname);
024: System.out.println("Methods:");
025: for (int i = 0; i < ret.length; i++)
026: System.out.println(ret[i]);
027: }
028:
029: public static String[] getClassFields(String cname) {
030: if (cname.indexOf('.') == -1)
031: cname = "java.sql." + cname;
032: try {
033: Class c = Class.forName(cname);
034: Field[] fields = c.getDeclaredFields();
035: //String[] ret = new String[ fields.length ];
036: ArrayList v = new ArrayList(fields.length);
037: for (int i = 0; i < fields.length; i++) {
038: int mod = fields[i].getModifiers();
039: if (Modifier.isStatic(mod) && Modifier.isPublic(mod)) {
040: //ret[i] = (fields[i].getName () + "=" + fields[i].get(null));
041: v.add(fields[i].getName() + "="
042: + fields[i].get(null));
043: }//if static
044: }//for
045: String ret[] = new String[v.size()];
046: v.toArray(ret);
047: return ret;
048: } catch (Exception exc) {
049: System.err.println("EXC:" + exc.toString());
050: }
051: return null;
052: }
053:
054: public static String[] getClassMethods(String cname) {
055: if (cname.indexOf('.') == -1)
056: cname = "java.sql." + cname;
057: try {
058: Class c = Class.forName(cname);
059: Method[] methods = c.getDeclaredMethods();
060: //String[] ret = new String[ methods.length ];
061: ArrayList v = new ArrayList(methods.length);
062: for (int i = 0; i < methods.length; i++) {
063: int mod = methods[i].getModifiers();
064: if (Modifier.isPublic(mod)) {
065: Class[] pt = methods[i].getParameterTypes();
066: StringBuffer pname = new StringBuffer(16);
067: for (int j = 0; j < pt.length; j++) {
068: if (pt[j].isArray()) {
069: String[] s = ArrayUtil.split(pt[j]
070: .getComponentType().getName(), '.');
071: pname.append(s[s.length - 1]);
072: pname.append("[]");
073: } else {
074: String[] s = ArrayUtil.split(pt[j]
075: .getName(), '.');
076: pname.append(s[s.length - 1]);
077: }
078: if (j < pt.length - 1)
079: pname.append(',');
080: }
081: //ret[i] = (methods[i].getName()+'('+pname+')');
082: v.add(methods[i].getName() + '(' + pname + ')');
083: }//if public
084: }//for
085: String ret[] = new String[v.size()];
086: v.toArray(ret);
087: return ret;
088: } catch (Exception exc) {
089: System.err.println("EXC:" + exc.toString());
090: }
091: return null;
092: }
093:
094: /** if you have an instance of n object pass it, else send as null
095: * and give the class name fully qualified.
096: */
097: public static String[] getClassMethods(Object o, String cname) {
098: try {
099: Class c = null;
100: if (o != null) {
101: c = o.getClass();
102: } else {
103: if (cname.indexOf('.') == -1)
104: cname = "java.sql." + cname;
105: c = Class.forName(cname);
106: }
107: Method[] methods = c.getDeclaredMethods();
108: //String[] ret = new String[ methods.length ];
109: ArrayList v = new ArrayList(methods.length);
110: for (int i = 0; i < methods.length; i++) {
111: int mod = methods[i].getModifiers();
112: if (Modifier.isPublic(mod)) {
113: Class[] pt = methods[i].getParameterTypes();
114: StringBuffer pname = new StringBuffer(16);
115: for (int j = 0; j < pt.length; j++) {
116: if (pt[j].isArray()) {
117: String[] s = ArrayUtil.split(pt[j]
118: .getComponentType().getName(), '.');
119: pname.append(s[s.length - 1]);
120: pname.append("[]");
121: } else {
122: String[] s = ArrayUtil.split(pt[j]
123: .getName(), '.');
124: pname.append(s[s.length - 1]);
125: }
126: if (j < pt.length - 1)
127: pname.append(',');
128: }
129: //ret[i] = (methods[i].getName()+'('+pname+')');
130: Object res = null;
131: if (o != null) {
132: if (pt.length == 0) {
133: res = methods[i].invoke(o, null);
134: }
135: }
136: v.add(methods[i].getName() + '(' + pname + ')'
137: + (res != null ? "=" + res : ""));
138: }//if public
139: }//for
140: String ret[] = new String[v.size()];
141: v.toArray(ret);
142: return ret;
143: } catch (Exception exc) {
144: System.err.println("EXC:" + exc.toString());
145: }
146: return null;
147: }
148:
149: } // main
|