001: package isql;
002:
003: import java.util.*;
004: import java.lang.reflect.*;
005: import java.sql.*;
006: import util.*;
007:
008: public class ReflectDataBase {
009:
010: /** shoud be in format "methodname(params)"
011: * e.g. getTables(null, "root", "%", null);
012: */
013: public static void main(String args[]) {
014: String str = null;
015: if (args.length == 1)
016: str = args[0];
017: else
018: str = "getTables(null,\"root\", \"%\", null)";
019:
020: System.out.println("recvd:" + str);
021: Connection conn = null;
022: DatabaseMetaData dma = null;
023: try {
024: //commented off since i dont want this package
025: //conn = com.t4h.util.mysqlUtil.getConnection();
026: dma = conn.getMetaData();
027: ReflectDataBase rd = new ReflectDataBase(dma, str);
028: Object o = rd.getResult();
029: System.out.println(o);
030: } catch (Exception exc) {
031: System.err.println("MAIN:" + exc.toString());
032: }
033: }
034:
035: Object _result;
036:
037: // dma could just have been an object ??
038: public ReflectDataBase(DatabaseMetaData dma, String str) {
039: try {
040: //Class mainclass = Class.forName("java.sql.DatabaseMetaData");
041: Class mainclass = dma.getClass();
042: String[] sarr = PerlWrapper.perlMatch("\\s*(.*)\\((.*)\\)",
043: str);
044: // elem 0 has method name
045: // elem 1 has parameters
046: Method mainmethod = getMainMethod(mainclass, sarr[0]);
047: System.out.println(" main:" + mainmethod.getName());
048: for (int i = 0; i < sarr.length; i++) {
049: System.out.println("got:" + sarr[i]);
050: }
051: Object oparams[] = null;
052: Class[] classes = null;
053: // there are parameters
054: if (sarr.length == 2) {
055: if (sarr[1].trim().length() > 0) { // check that not blank
056: // break up params based on commas
057: String[] sparams = PerlWrapper.perlSplit(
058: "/\\s*,\\s*/", sarr[1]);
059: int length = sparams.length;
060: for (int j = 0; j < length; j++) {
061: System.out.println("gotparams:" + sparams[j]);
062: }
063: oparams = new Object[length];
064: classes = new Class[length];
065: for (int i = 0; i < length; i++) {
066: String strp = sparams[i];
067: if (PerlWrapper.isMatching("^\\d+$", strp)) {
068: //oparams[i] = Integer.valueOf(strp);
069: oparams[i] = new Integer(Integer
070: .parseInt(strp));
071: classes[i] = int.class;
072: } else if (strp.equals("true")
073: || strp.equals("false")) {
074: oparams[i] = new Boolean(strp);
075: classes[i] = boolean.class;
076: } else if (strp.equals("null")
077: || strp.equals("NULL")) {
078: System.out
079: .println("oracle driver gives null");
080: // find out the actual parameter!!!
081: Class tmpc = String.class;
082: classes[i] = String.class;
083: try {
084: classes[i] = getMethodParameterClass(
085: mainmethod, i);
086: tmpc = getMethodParameterClass(
087: mainmethod, i);
088: } catch (Exception exc) {
089: System.err.println("Reflect: "
090: + exc.toString());
091: }
092: if (!tmpc.isArray())
093: oparams[i] = tmpc.newInstance();
094: else
095: oparams[i] = java.lang.reflect.Array
096: .newInstance(tmpc
097: .getComponentType(), 0);
098: } else if (PerlWrapper.isMatching("\".*\"",
099: strp))
100: // string
101: oparams[i] = strp.substring(1, strp
102: .length() - 1);
103: else
104: // appears to be a constant
105: // which we need to reflect
106: {
107: String[] elems = ArrayUtil.split(strp, '.');
108: String classname = null;
109: String fieldname = null;
110: if (elems.length == 4) {
111: classname = elems[0] + '.' + elems[1]
112: + '.' + elems[2];
113: fieldname = elems[3];
114: } else if (elems.length == 2) {
115: classname = "java.sql." + elems[0];
116: fieldname = elems[1];
117: } else {
118: System.err
119: .println("Dont know how to handle parameter! :"
120: + strp);
121: }
122: oparams[i] = getFieldValue(classname,
123: fieldname);
124: } // constant
125: if (classes[i] == null)
126: classes[i] = oparams[i].getClass();
127: System.out.println("Class=" + classes[i] + "="
128: + oparams[i] + ".");
129: } // for i
130: } // if sarr >0
131: }// sarr == 2
132: System.out.println("reached 823");
133: Method m = mainclass.getMethod(sarr[0], classes);
134: System.out.println("reached 826" + m.getName() + ":"
135: + m.toString());
136: if (oparams != null)
137: for (int i = 0; i < oparams.length; i++) {
138: System.out.println("oparams=" + oparams[i]);
139: }
140: _result = m.invoke(dma, oparams);
141: } catch (Exception exc) {
142: System.err.println("EXC:" + exc.toString());
143: _result = null;
144: }
145: }
146:
147: public Object getResult() {
148: return _result;
149: }
150:
151: /** a public static constant has been used, which we must try to
152: * decipher.
153: */
154: public static Object getFieldValue(String classname,
155: String fieldname) throws ClassNotFoundException,
156: NoSuchFieldException, IllegalAccessException {
157: Class c = Class.forName(classname);
158: Field f = c.getDeclaredField(fieldname);
159: return f.get(null);
160: }
161:
162: /** get the class of a particular parameter
163: */
164: public static Class getMethodParameterClass(Method m, int index) {
165: System.err.println(" temp: inside getMethod param:"
166: + m.getName() + " " + index);
167: Class[] pt = m.getParameterTypes();
168: return pt[index];
169: }
170:
171: /** returns first match for method name, but what if overloaded.
172: */
173: public static Method getMainMethod(Class c, String methodname) {
174: Method[] m = c.getDeclaredMethods();
175: for (int i = 0; i < m.length; i++) {
176: if (m[i].getName().equals(methodname))
177: return m[i];
178: }
179: return null;
180: }
181: }
|