001: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
002: // Released under the terms of the GNU General Public License version 2 or later.
003:
004: package eg;
005:
006: import fit.*;
007:
008: import java.lang.reflect.Method;
009: import java.lang.reflect.Field;
010: import java.util.*;
011:
012: public class ColumnIndex extends RowFixture {
013:
014: Parse rows;
015:
016: public void doRows(Parse rows) {
017: this .rows = rows;
018: super .doRows(rows);
019: }
020:
021: public Class getTargetClass() {
022: return Column.class;
023: }
024:
025: public Object[] query() throws ClassNotFoundException {
026: // first find what classes are mentioned in the table
027: Set names = new HashSet();
028: int column = 0;
029: for (Parse cell = rows.parts; cell != null; column++, cell = cell.more) {
030: if (cell.text().equals("className")) {
031: break;
032: }
033: }
034: for (Parse row = rows.more; row != null; row = row.more) {
035: names.add(row.at(0, column).text());
036: }
037: // then find the columns in these classes
038: ArrayList columns = new ArrayList();
039: for (Iterator i = names.iterator(); i.hasNext();) {
040: Class each = Class.forName((String) i.next());
041: Field f[] = each.getFields();
042: for (int j = 0; j < f.length; j++) {
043: if (f[j].getModifiers() == 1) {
044: columns.add(new Column(f[j]));
045: }
046: }
047: Method m[] = each.getMethods();
048: for (int j = 0; j < m.length; j++) {
049: if (m[j].getParameterTypes().length == 0
050: && m[j].getModifiers() == 1) {
051: columns.add(new Column(m[j]));
052: }
053: }
054: }
055: return columns.toArray();
056: }
057:
058: public Object parse(String text, Class type) throws Exception {
059: if (type.equals(Class.class)) {
060: return parseClass(text);
061: }
062: return super .parse(text, type);
063: }
064:
065: Class parseClass(String name) throws Exception {
066: if (name.equals("byte")) {
067: return Byte.TYPE;
068: }
069: if (name.equals("short")) {
070: return Short.TYPE;
071: }
072: if (name.equals("int")) {
073: return Integer.TYPE;
074: }
075: if (name.equals("long")) {
076: return Long.TYPE;
077: }
078: if (name.equals("float")) {
079: return Float.TYPE;
080: }
081: if (name.equals("double")) {
082: return Double.TYPE;
083: }
084: if (name.equals("char")) {
085: return Character.TYPE;
086: }
087: return Class.forName(name);
088: }
089:
090: public class Column {
091: public Object column;
092: public Class className;
093: public String columnName;
094: public Class columnType;
095:
096: Column(Field f) {
097: column = f;
098: className = f.getDeclaringClass();
099: columnName = f.getName();
100: columnType = f.getType();
101: }
102:
103: Column(Method m) {
104: column = m;
105: className = m.getDeclaringClass();
106: columnName = m.getName() + "()";
107: columnType = m.getReturnType();
108: }
109: }
110:
111: }
|