01: /*
02: * Created on Feb 23, 2004
03: */
04: package net.sourceforge.orbroker;
05:
06: import java.lang.reflect.Method;
07: import java.lang.reflect.Modifier;
08:
09: import net.kildenpedersen.reflect.Reflector;
10:
11: /**
12: * @author Nils Kilden-Pedersen
13: */
14: class ResultObjectFactoryMethod extends ResultObjectInstantiator {
15:
16: private static Method buildStaticMethod(Class type, String name,
17: ArgumentList parameters) {
18: parameters.close();
19: Class[] parmClasses = parameters.getArgumentTypes();
20: Method method = Reflector.getAccessibleMethod(type, name,
21: parmClasses);
22: if (method == null) {
23: String msg = "Unknown factory method: "
24: + Reflector.getClassName(type) + ". " + name
25: + parameters.toString();
26: throw new ConfigurationException(msg);
27: }
28: if (!Modifier.isStatic(method.getModifiers())) {
29: String msg = "Factory method not static: "
30: + Reflector.getClassName(type) + "." + name
31: + parameters.toString();
32: throw new ConfigurationException(msg);
33: }
34: return method;
35: }
36:
37: ResultObjectFactoryMethod(Class type, String methodName,
38: ArgumentList parameters) {
39: super (buildStaticMethod(type, methodName, parameters),
40: parameters);
41: }
42:
43: private Method getMethod() {
44: return (Method) getMember();
45: }
46:
47: /**
48: * @inheritDoc
49: * @see net.sourceforge.orbroker.ResultObjectInstantiator#copy(Class)
50: */
51: ResultObjectInstantiator copy(Class forType) {
52: return new ResultObjectFactoryMethod(forType, getMethod()
53: .getName(), getArgumentList());
54: }
55:
56: /**
57: * Get new instance.
58: * @param row
59: * @return result object
60: */
61: Object newInstance(ResultRow row) {
62: Object[] parmObjects = this .getArgumentList()
63: .getArgumentValues(row);
64: try {
65: return getMethod().invoke(null, parmObjects);
66: } catch (Exception e) {
67: String msg = "Cannot construct "
68: + Reflector.getClassName(getType())
69: + " using factory method: "
70: + getMethod().toString();
71: throw new ReflectionException(msg, e);
72: }
73: }
74: }
|