Revision 25791 branches/v2_0_0_prep/libraries/libTools/src/org/gvsig/tools/dynobject/impl/DefaultDynObjectManager.java

View differences:

DefaultDynObjectManager.java
8 8
import java.util.Map;
9 9

  
10 10
import org.gvsig.tools.dynobject.DynClass;
11
import org.gvsig.tools.dynobject.DynMethod;
11 12
import org.gvsig.tools.dynobject.DynObject;
12 13
import org.gvsig.tools.dynobject.DynObjectManager;
13
import org.gvsig.tools.dynobject.impl.DefaultDynObject;
14
import org.gvsig.tools.dynobject.exception.DynMethodException;
15
import org.gvsig.tools.dynobject.exception.DynMethodIllegalCodeException;
16
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException;
17
import org.gvsig.tools.dynobject.exception.IllegalDynMethodException;
18
import org.gvsig.tools.dynobject.exception.IllegalDynMethodInvocationException;
19
import org.gvsig.tools.operations.Operation;
14 20

  
15 21
public class DefaultDynObjectManager implements DynObjectManager {
16 22

  
17 23
	private static DefaultDynObjectManager manager = null;
18 24
	
25
	private class MethodInfo {
26
		int code;
27
		DynClass dynClass;
28
		DynMethod dynMethod;
29
		Class theClass;
30

  
31
		MethodInfo(Class theClass, DynClass dynClass, DynMethod dynMethod, int code) {
32
			this.code = code;
33
			this.dynClass = dynClass;
34
			this.dynMethod = dynMethod;
35
			this.theClass = theClass;
36
		}
37

  
38
		String getKey() {
39
			return DefaultDynObjectManager.getKey(theClass, dynClass, dynMethod);
40
		}
41

  
42
		void check(Class theClass, int code) throws DynMethodException {
43
			if( code != this.code) {
44
				throw new DynMethodIllegalCodeException(dynMethod.getName(),this.code, code);
45
			}
46
			if( theClass != null ) {
47
				if( this.theClass == null ) {
48
					throw new IllegalDynMethodInvocationException(dynMethod.getName(), theClass);
49
				}
50
				if (!this.theClass.isAssignableFrom(theClass)) {
51
					throw new IllegalDynMethodInvocationException(dynMethod.getName(),theClass);
52
				}
53
			}
54
		}
55

  
56
		void check(DynClass dynClass, int code) throws DynMethodException  {
57
			if( code != this.code) {
58
				throw new DynMethodIllegalCodeException(dynMethod.getName(),this.code, code);
59
			}
60
			if( dynClass != null ) {
61
				if( this.dynClass == null ) {
62
					throw new IllegalDynMethodInvocationException(dynMethod.getName(), dynClass);
63
				}
64
				if( dynClass != this.dynClass || !dynClass.getName().equalsIgnoreCase(this.dynClass.getName()) ) {
65
					throw new IllegalDynMethodInvocationException(dynMethod.getName(), dynClass);
66
				}
67
			}
68
		}
69
	}
70
	
19 71
	private Map anonymousClasses;
20 72
	private Map classes;	
73
	private Map methodsMap;
74
	private MethodInfo[] methods;
21 75
	
22 76
	public static DefaultDynObjectManager getManager() {
23 77
		if (manager == null) {
......
26 80
		return manager;
27 81
	}
28 82
	
83
    static String getKey(Class theClass, DynClass dynClass, DynMethod dynMethod) {
84
    	return DefaultDynObjectManager.getKey(theClass, dynClass, dynMethod.getName());
85
    }
86
    
87
    static String getKey(Class theClass, DynClass dynClass, String methodName) {
88
		if( dynClass == null ) {
89
			return theClass.getName() + ":" + methodName;
90
		} else {
91
			return dynClass.getName() + ":" + methodName;
92
		}	    	
93
    }
94
    
95
	
29 96
	public DefaultDynObjectManager() {
30 97
		this.classes = new HashMap();
31 98
		this.anonymousClasses = new HashMap();
99
		this.methodsMap = new HashMap();
100
		this.methods = null;
32 101
	}
33 102

  
34 103
	public DynClass createDynClass(String name, String description) {
......
114 183
		}
115 184
	}
116 185

  
186
	
187
	public int registerDynMethod(DynClass dynClass, DynMethod dynMethod) {
188
		((DefaultDynClass)dynClass).addMethod(dynMethod);
189
		return registerDynMethod(null, dynClass, dynMethod); 
190
	}
191
	
192
	public int registerDynMethod(Class theClass, DynMethod dynMethod) {
193
		return registerDynMethod(theClass, null, dynMethod); 
194
	}
195

  
196
	int registerDynMethod(Class theClass, DynClass dynClass, DynMethod dynMethod) {
197
		MethodInfo info = new MethodInfo(theClass, dynClass, dynMethod, 0);
198
		MethodInfo oldInfo = (MethodInfo) methodsMap.get(info.getKey());
199
		if (oldInfo != null) {
200
			// Update the method info
201
			oldInfo.dynClass = dynClass;
202
			oldInfo.dynMethod = dynMethod;
203
			return oldInfo.code;
204
		}
205
		if (methods == null) {
206
			methods = new MethodInfo[1];
207
			info.code = 0;
208
		} else {
209
			Operation[] temp1 = new Operation[methods.length + 1];
210
			System.arraycopy(methods, 0, temp1, 0, methods.length);
211
			info.code = methods.length - 1;
212
		}
213
		methods[info.code] = info;
214
		methodsMap.put(info.getKey(), info);
215
		
216
		return info.code;
217
	}
218

  
219
	public Object invokeDynMethod(Object self, int code, DynObject context) throws DynMethodException{
220
		
221
		try {
222
			/*
223
			 * Intentamos ejecutar la operacion, y si peta ya haremos las
224
			 * comprobaciones oportunas para lanzar la excepcion que toque.
225
			 *
226
			 * Asi evitamos codigo de comprobacion para los casos que valla bien
227
			 * que deberian ser la mayoria.
228
			 */
229
			return methods[code].dynMethod.invoke(self, context);
230
		} catch (RuntimeException e) {
231
			getDynMethod(self, code);
232
			throw e;
233
		} catch (DynMethodException e) {
234
			getDynMethod(self, code);
235
			throw e;
236
		}
237

  
238
	}
239

  
240
	public int getDynMethodCode(DynClass dynClass, String methodName) throws DynMethodException  {
241
		String key = DefaultDynObjectManager.getKey(null, dynClass, methodName);
242
		MethodInfo info = (MethodInfo) methodsMap.get(key);
243
		if( info == null ) {
244
			throw new IllegalDynMethodException(methodName, dynClass);
245
		}
246
		info.check(dynClass, info.code);
247
		return info.code;
248
	}
249
	
250
	public int getDynMethodCode(Class theClass, String methodName) throws DynMethodException {
251
		String key = DefaultDynObjectManager.getKey(theClass, null, methodName);
252
		MethodInfo info = (MethodInfo) methodsMap.get(key);
253
		if( info == null ) {
254
			throw new IllegalDynMethodException(methodName, theClass);
255
		}
256
		info.check(theClass, info.code);
257
		return info.code;
258
	}
259
	
260
	public DynMethod getDynMethod(int code) throws DynMethodException {
261
		if (code >= methods.length) {
262
			throw new DynMethodNotSupportedException(code);
263
		}
264
		MethodInfo info = methods[code];
265
		info.check((Class)null, code);
266
		return info.dynMethod;
267
	}
268
	
269
	public DynMethod getDynMethod(Object obj, int code)
270
			throws DynMethodException {
271
		return getDynMethod(obj.getClass(), code);
272
	}
273

  
274
	public DynMethod getDynMethod(Class theClass, int code)
275
			throws DynMethodException {
276
		if (code >= methods.length) {
277
			throw new DynMethodNotSupportedException(code);
278
		}
279
		MethodInfo info = methods[code];
280
		info.check(theClass, code);
281
		return info.dynMethod;
282
	}
283

  
284
	public DynMethod getDynMethod(DynClass dynClass, int code)
285
			throws DynMethodException {
286
		if (code >= methods.length) {
287
			throw new DynMethodNotSupportedException(code);
288
		}
289
		MethodInfo info = methods[code];
290
		info.check(dynClass, code);
291
		return info.dynMethod;
292
	}
293

  
294
	public DynMethod getDynMethod(DynObject dynObject, int code)
295
			throws DynMethodException {
296
		return getDynMethod(dynObject.getDynClass(), code);
297
	}
298

  
117 299
}

Also available in: Unified diff