001: /*
002: * Created on May 12, 2004
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.xdev.base.xssl.reflection;
008:
009: import java.util.HashMap;
010: import java.util.List;
011:
012: import org.xdev.base.xssl.XSSLAction;
013: import org.xdev.base.xssl.XSSLActionIterator;
014: import org.xdev.base.xssl.XSSLReturn;
015:
016: import java.lang.reflect.*;
017:
018: /**
019: * @author AYegorov
020: *
021: * Company: xDev Group.org
022: *
023: * URL: <a>http://activexml.dev.java.net</a>
024: *
025: * File name: VirtualObject.java
026: *
027: * <p>Is bound: true | false</p>
028: *
029: * <table>
030: * <tr>
031: * <td>Description:</td>
032: * <td>
033: * <!--Your description here-->
034: * </td>
035: * </tr>
036: * </table>
037: * <table>
038: * <tr>
039: * <td colspan="3">
040: * Properties
041: * </td>
042: * </tr>
043: * <tr>
044: * <td>Name</td>
045: * <td>Value</td>
046: * <td>Description</td>
047: * </tr>
048: * <tr>
049: * <td></td>
050: * <td></td>
051: * <td></td>
052: * </tr>
053: * </table>
054: */
055: public abstract class AbstractVirtual extends XSSLActionIterator {
056:
057: protected Class cls = null;
058:
059: protected Object returnObject = null;
060:
061: protected Class[] parameterClasses = null;
062: protected Object[] parameterObjects = null;
063:
064: private int count = 0;
065:
066: /**
067: * @param id
068: */
069: public AbstractVirtual(String id) {
070: super (id);
071: // XXX Auto-generated constructor stub
072: }
073:
074: /**
075: * @param id
076: * @param properties
077: */
078: public AbstractVirtual(String id, HashMap properties) {
079: super (id, properties);
080: // XXX Auto-generated constructor stub
081: }
082:
083: /* (non-Javadoc)
084: * @see org.xdev.base.xssl.XSSLActionIterator#invoke(org.xdev.base.xssl.XSSLAction)
085: */
086: protected void invoke(XSSLAction component) throws Exception {
087: Object obj = ((XSSLReturn) component).getObjectValue();
088:
089: this .parameterClasses[this .count] = component
090: .hasProperty("param-class") ? component
091: .getClassProperty("param-class") : obj.getClass();
092:
093: this .parameterObjects[this .count++] = obj;
094: }
095:
096: /* (non-Javadoc)
097: * @see org.xdev.base.xssl.XSSLAction#set()
098: */
099: protected void set() throws Exception {
100: this .cls = this .getClassProperty("class");
101:
102: this .count = 0;
103:
104: int size = this .getCount();
105:
106: List params = null;
107:
108: if (this .hasProperty("params")) {
109: params = this .getTokenListProperty("params");
110:
111: size += params.size();
112:
113: }
114:
115: this .parameterClasses = new Class[size];
116:
117: this .parameterObjects = new Object[size];
118:
119: if (params != null) {
120:
121: int sz = params.size();
122:
123: Object obj = null;
124:
125: Class cls = null;
126:
127: for (; this .count < sz; this .count++) {
128: obj = this .getPropertyAsObject((String) params
129: .get(this .count));
130:
131: this .parameterClasses[this .count] = obj.getClass();
132:
133: this .parameterObjects[this .count] = obj;
134: }
135: }
136: }
137:
138: protected void invoke() throws Exception {
139:
140: super .invoke();
141:
142: this .returnObject = this .reflect(this .cls,
143: this .parameterClasses, this .parameterObjects);
144: }
145:
146: protected abstract Object reflect(Class cls,
147: Class[] parameterClasses, Object[] parameterObjects)
148: throws Exception;
149:
150: public Object getObjectValue() {
151: return this .returnObject;
152: }
153:
154: protected Class isPrimitive(Class cls) {
155:
156: Class primitive = null;
157:
158: if (cls.equals(Integer.class)) {
159: primitive = int.class;
160: } else if (cls.equals(Long.class)) {
161: primitive = long.class;
162: } else if (cls.equals(Short.class)) {
163: primitive = short.class;
164: } else if (cls.equals(Byte.class)) {
165: primitive = byte.class;
166: } else if (cls.equals(Double.class)) {
167: primitive = double.class;
168: } else if (cls.equals(Float.class)) {
169: primitive = float.class;
170: } else if (cls.equals(Boolean.class)) {
171: primitive = boolean.class;
172: }
173:
174: return primitive;
175: }
176:
177: protected Class isPrimitiveWrapper(Class cls) {
178:
179: Class primitive = null;
180:
181: if (cls.equals(int.class)) {
182: primitive = Integer.class;
183: } else if (cls.equals(long.class)) {
184: primitive = Long.class;
185: } else if (cls.equals(short.class)) {
186: primitive = Short.class;
187: } else if (cls.equals(byte.class)) {
188: primitive = Byte.class;
189: } else if (cls.equals(double.class)) {
190: primitive = Double.class;
191: } else if (cls.equals(float.class)) {
192: primitive = Float.class;
193: } else if (cls.equals(boolean.class)) {
194: primitive = Boolean.class;
195: }
196:
197: return primitive;
198: }
199:
200: protected Object resolveSignature(Class cls, Class[] paramClasses)
201: throws Exception {
202:
203: Object m = null;
204:
205: Class sp = null;
206:
207: Class temp = null;
208:
209: Class[] variation = null;
210:
211: boolean unWrapped = false;
212:
213: while (m == null) {
214: for (int j = 0; j < paramClasses.length; j++) {
215: for (int i = 0; i < paramClasses.length && m == null; i++) {
216:
217: variation = paramClasses;
218:
219: sp = paramClasses[i];
220:
221: do {
222: variation[i] = sp;
223:
224: try {
225: m = this .returnSignedObject(cls, variation);
226: } catch (Exception ex) {
227: }
228:
229: if (m == null) {
230: if ((temp = this .isPrimitive(variation[i])) != null
231: || (temp = this
232: .isPrimitiveWrapper(variation[i])) != null) {
233:
234: sp = temp;
235:
236: unWrapped = unWrapped ? false : true;
237: }
238:
239: if (!unWrapped) {
240: sp = sp.getSuperclass();
241: }
242: } else {
243: break;
244: }
245: } while (!sp.equals(Object.class));
246: }
247:
248: if (m == null) {
249:
250: paramClasses[j] = paramClasses[j].getSuperclass();
251: } else {
252: break;
253: }
254: }
255: }
256:
257: return m;
258:
259: }
260:
261: protected abstract Object returnSignedObject(Class cls,
262: Class[] variation) throws Exception;
263: }
|