001: package isql;
002:
003: import java.io.*;
004: import java.util.*;
005: import java.lang.reflect.*;
006: import util.*;
007:
008: public class pack {
009: // takes a String and parses the import statements
010: // it maintains a list of imports it has taken care of
011: // for each import what are the class/s (could be one fixed or many)
012: // for the above what methods are available
013: //
014: /** the imports that have been processed */
015: ArrayList imports = new ArrayList();
016: /** packages and their classes
017: * Will have entried such as
018: * | java.util.Vector | java.util.Vector |
019: *or | java.util.* | Vector, Hashtable,....
020: *
021: * */
022: Map htPackageClasses = new HashMap();
023: /** classes and their methods
024: * Will have entried such as
025: * | java.util.Vector | add, put, delete
026: */
027: Map htClassMethods = new HashMap(128);
028:
029: public static void main(String args[]) {
030: String s = "package xxx;\nimport java.io.*;\n import java.util.*;import java.util.Vector;";
031: System.out.println("string:<<<" + s + ">>>");
032: pack p = new pack(s);
033: /*
034: p.print();
035: String full = p.getFullClass("Hashtable");
036: String methods[] = p.getClassMethods(full);
037: System.out.println("Fullname:"+full);
038: System.out.println("Methods:"+methods.toString());
039: for( int i = 0; i < methods.length; i++)
040: System.out.println(" :"+methods[i]);
041:
042: full = p.getFullClass("ClassMethodTabExpander");
043: System.out.println( "full:"+ full);
044: methods = p.getClassMethods(full);
045: System.out.println("2 Fullname:"+full);
046: System.out.println("2 Methods:"+methods.toString());
047: for( int i = 0; i < methods.length; i++)
048: System.out.println(" :"+methods[i]);
049:
050: */
051: ClassMethodTabExpander cmte = new ClassMethodTabExpander(
052: p.htClassMethods, p.htPackageClasses);
053: System.out.println("FileReader:rea...");
054: System.out.println(cmte.getNextExpansion("FileReader:rea"));
055: System.out.println(cmte.getNextExpansion("FileReader:rea"));
056: System.out.println(cmte.getNextExpansion("FileReader:rea"));
057: System.out.println("ClassMethodTabExpander:get...");
058: String ss[] = cmte.getExpansions("ClassMethodTabExpander:get");
059: for (int i = 0; i < ss.length; i++)
060: System.out.println(" .." + ss[i]);
061: System.out.println("java.util.Random:nextB...");
062: ss = cmte.getExpansions("java.util.Random:nextB");
063: for (int i = 0; i < ss.length; i++)
064: System.out.println(" .." + ss[i]);
065: System.out.println("java.util.Random:...");
066: ss = cmte.getExpansions("java.util.Random:");
067: for (int i = 0; i < ss.length; i++)
068: System.out.println(" .." + ss[i]);
069: }
070:
071: public pack(String s) {
072: try {
073: BufferedReader br = new BufferedReader(new StringReader(s));
074: String t;
075: while ((t = br.readLine()) != null) {
076: String tmp[] = ArrayUtil.split(t, ';');
077: // each import statement
078: for (int i = 0; i < tmp.length; i++) {
079: String path = tmp[i].trim();
080: int pos;
081: if ((pos = path.indexOf("import ")) == -1)
082: continue;
083: // if we encounter class or interface which is not
084: // in comments we should break out
085: // Also, hopefully the word import did not come in
086: // some comment
087: imports.add(path);
088: }
089: }
090: } catch (Exception exc) {
091: System.err.println("EXC:78:" + exc.toString());
092: }
093:
094: imports.add("import .");
095: Iterator it = imports.iterator();
096: while (it.hasNext()) {
097: String path = (String) it.next();
098: int pos = path.indexOf("import ");
099: String tpath = path.substring(pos + 1 + 6).trim();
100: System.out.println("import=" + tpath);
101:
102: ArrayList allp = null;
103: if (tpath.equals(".")) {
104: // get all the classes in current package
105: listpackage lp = new listpackage(tpath);
106: allp = lp.getClasses();
107: // get the classes and methods
108: /*
109: for( int i = 0; i < allp.size(); i++ ){
110: String classname = (String)allp.get(i);
111: htClassMethods.put( classname,
112: retrieveMethods(classname));
113: }
114: */
115: } else if (tpath.indexOf('*') != -1) {
116: // get all the classes in that package
117: listpackage lp = new listpackage(tpath);
118: allp = lp.getClasses();
119: } else {
120: // just one class name in import
121: allp = new ArrayList();
122: allp.add(tpath);
123: htClassMethods.put(tpath, retrieveMethods(tpath));
124: }
125: System.out.println("putting:" + tpath + ":" + allp.size());
126: htPackageClasses.put(tpath, allp);
127: } // while
128: }
129:
130: public void print() {
131: System.out.println(" IMPORTS: ");
132: for (int i = 0; i < imports.size(); i++)
133: System.out.println(" " + (String) imports.get(i));
134: Iterator e = htPackageClasses.keySet().iterator();
135: while (e.hasNext()) {
136: // can be java.io.* or java.io.IOException
137: String packpattern = (String) e.next(); // get package name
138: System.out.println("=" + packpattern);
139: ArrayList al = (ArrayList) htPackageClasses
140: .get(packpattern);
141: for (int i = 0; i < al.size(); i++) {
142: System.out.println(" -" + (String) al.get(i) + '-');
143: // retrieve all method names for class
144: String[] sarr = (String[]) (htClassMethods
145: .get((String) al.get(i)));
146: if (sarr != null)
147: for (int j = 0; j < sarr.length; j++) {
148: System.out.println(" +" + sarr[j]);
149: }
150: }
151: }
152: }
153:
154: /** given a classname without package name, returns the class name
155: * prefixed by package: given "FileReader" will return
156: * "java.io.FileReader". However, java.io must be in the import
157: * list, and the jar file containing the class should be in
158: * classpath.
159: */
160: public String getFullClass(String partclassname) {
161: Iterator e = htPackageClasses.keySet().iterator();
162: System.out.println("getFullClass:" + partclassname);
163: while (e.hasNext()) {
164: // can be java.io.* or java.io.IOException
165: String packpattern = (String) e.next(); // get package name
166: System.out.println(" PACKAGE:" + packpattern);
167: // this is a specific class import, not just a package
168: if (packpattern.indexOf('*') == -1
169: && !packpattern.equals(".")) {
170: if (packpattern.endsWith('.' + partclassname))
171: return packpattern;
172: continue;
173: }
174: ArrayList al = (ArrayList) htPackageClasses
175: .get(packpattern);
176: if (al == null)
177: System.err.println("AL WAS NULL:" + packpattern);
178: System.out.println("checking package contents :"
179: + al.size());
180: for (int i = 0; i < al.size(); i++) {
181: System.out.print(" " + i + ":" + al.get(i));
182: if (partclassname.equals((String) al.get(i)))
183: return packpattern.substring(0, packpattern
184: .length() - 1)
185: + partclassname;
186: }
187: }
188: System.out.println("Couldn t find:" + partclassname);
189: return null;
190: }
191:
192: /** given a fully qualified classname, returns the names of methods
193: * and constructors from cache, if not found it reflects and adds to
194: * cache.
195: */
196: public String[] getClassMethods(String classname) {
197: String[] sarr = (String[]) (htClassMethods.get(classname));
198: if (sarr != null)
199: return sarr;
200: else {
201: sarr = retrieveMethods(classname);
202: if (sarr != null)
203: htClassMethods.put(classname, sarr);
204: }
205: return sarr;
206: }
207:
208: /** Reflects on the fully qualified classname, returning its
209: * methods.
210: */
211: private String[] retrieveMethods(String classname) {
212:
213: try {
214: System.out.println("retrive: " + classname);
215: Class cl = Class.forName(classname);
216: Method[] methods = cl.getMethods();
217: ArrayList v = new ArrayList(methods.length);
218: for (int j = 0; j < methods.length; j++) {
219: String params = join(methods[j].getParameterTypes(),
220: ',');
221: v.add(methods[j].getName() + '(' + params + ')');
222: }
223: Constructor[] conss = cl.getConstructors();
224: for (int j = 0; j < conss.length; j++) {
225: String params = join(methods[j].getParameterTypes(),
226: ',');
227: v.add(conss[j].getName() + '(' + params + ')');
228: }
229: String[] sarr = new String[v.size()];
230: v.toArray(sarr);
231: Arrays.sort(sarr);
232: return sarr;
233: } catch (Exception exc) {
234: System.err.println("EXC:" + exc.toString());
235: return null;
236: }
237: }
238:
239: /** appends each index of string using getName and given separator.
240: * This would work only for methods having a get Name such as class
241: * and Method
242: */
243: public static String join(Class[] sarr, char csep) {
244: int len = sarr.length;
245: StringBuffer sbuf = new StringBuffer(len * 10);
246: for (int i = 0; i < len; i++) {
247: if (i == len - 1)
248: sbuf.append(sarr[i].getName());
249: else
250: sbuf.append(sarr[i].getName()).append(csep);
251: }
252: return sbuf.toString();
253: }
254:
255: }// class
|