001: package net.sourceforge.squirrel_sql.plugins.hibernate;
002:
003: import org.hibernate.engine.SubselectFetch;
004:
005: import java.lang.reflect.InvocationHandler;
006: import java.lang.reflect.Method;
007: import java.lang.reflect.InvocationTargetException;
008: import java.lang.reflect.Constructor;
009: import java.net.URLClassLoader;
010: import java.util.List;
011: import java.util.ArrayList;
012: import java.util.Collection;
013:
014: public class ReflectionCaller {
015: private Object _callee;
016:
017: public ReflectionCaller(Object callee) {
018: _callee = callee;
019: }
020:
021: public ReflectionCaller() {
022: this (null);
023: }
024:
025: public ReflectionCaller getClass(String className, ClassLoader cl) {
026: try {
027: return new ReflectionCaller(cl.loadClass(className));
028: } catch (ClassNotFoundException e) {
029: throw new RuntimeException(e);
030: }
031: }
032:
033: public ReflectionCaller callConstructor(Class[] paramTypes,
034: Object[] params) {
035: try {
036: Constructor constr = getCalleeClass()
037: .getDeclaredConstructor(paramTypes);
038: return new ReflectionCaller(constr.newInstance(params));
039: } catch (Exception e) {
040: throw new RuntimeException(e);
041: }
042:
043: }
044:
045: public List<ReflectionCaller> callArrayMethod(String methodName) {
046: try {
047: Method meth = getDeclaredMethodIncludingSuper(methodName);
048:
049: Object[] callees = (Object[]) meth.invoke(_callee);
050:
051: List<ReflectionCaller> ret = new ArrayList<ReflectionCaller>();
052:
053: for (Object callee : callees) {
054: ret.add(new ReflectionCaller(callee));
055: }
056:
057: return ret;
058:
059: } catch (Exception e) {
060: throw new RuntimeException(e);
061: }
062: }
063:
064: public ReflectionCaller callMethod(String methodName) {
065: try {
066: return new ReflectionCaller(
067: getDeclaredMethodIncludingSuper(methodName).invoke(
068: _callee));
069: } catch (Exception e) {
070: throw new RuntimeException(e);
071: }
072: }
073:
074: public Object getCallee() {
075: return _callee;
076: }
077:
078: public Collection<ReflectionCaller> callCollectionMethod(
079: String methodName) {
080: try {
081: Method meth = getDeclaredMethodIncludingSuper(methodName);
082: meth.setAccessible(true);
083:
084: Collection callees = (Collection) meth.invoke(_callee);
085:
086: List<ReflectionCaller> ret = new ArrayList<ReflectionCaller>();
087:
088: for (Object callee : callees) {
089: ret.add(new ReflectionCaller(callee));
090: }
091:
092: return ret;
093:
094: } catch (Exception e) {
095: throw new RuntimeException(e);
096: }
097: }
098:
099: public ReflectionCaller getField(String fieldName) {
100: try {
101: return new ReflectionCaller(getCalleeClass()
102: .getDeclaredField(fieldName).get(_callee));
103: } catch (Exception e) {
104: throw new RuntimeException(e);
105: }
106: }
107:
108: public Class getCalleeClass() {
109: if (_callee instanceof Class) {
110: return (Class) _callee;
111: } else {
112: return _callee.getClass();
113: }
114:
115: }
116:
117: public ReflectionCaller callMethod(String methodName,
118: Object[] params) {
119: try {
120: Class[] paramTypes = new Class[params.length];
121:
122: for (int i = 0; i < params.length; i++) {
123: paramTypes[i] = params[i].getClass();
124: }
125:
126: Method meth = getDeclaredMethodIncludingSuper(methodName,
127: paramTypes);
128: meth.setAccessible(true);
129:
130: return new ReflectionCaller(meth.invoke(_callee, params));
131: } catch (Exception e) {
132: throw new RuntimeException(e);
133: }
134: }
135:
136: private Method getDeclaredMethodIncludingSuper(String methodName,
137: Class... paramTypes) throws NoSuchMethodException {
138: Class clazz = getCalleeClass();
139:
140: NoSuchMethodException throwBuf = new NoSuchMethodException(
141: methodName);
142:
143: while (null != clazz) {
144: try {
145: Method ret = clazz.getDeclaredMethod(methodName,
146: paramTypes);
147: ret.setAccessible(true);
148: return ret;
149: } catch (NoSuchMethodException e) {
150: throwBuf = e;
151: }
152: clazz = clazz.getSuperclass();
153: }
154:
155: throw throwBuf;
156:
157: }
158:
159: public static void main(String[] args) {
160: ReflectionCaller c = new ReflectionCaller(Integer.class);
161:
162: System.out.println(c.getCalleeClass().getName());
163:
164: }
165:
166: }
|