1
|
|
#region Copyright
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
#endregion
|
18
|
|
|
19
|
|
using System;
|
20
|
|
using System.Collections;
|
21
|
|
using System.Reflection;
|
22
|
|
using Seasar.Framework.Util;
|
23
|
|
using Seasar.Framework.Beans;
|
24
|
|
using Seasar.Framework.Container.Util;
|
25
|
|
|
26
|
|
|
27
|
|
namespace Seasar.Framework.Container.Assembler
|
28
|
|
{
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
public abstract class AbstractMethodAssembler : AbstractAssembler,IMethodAssembler
|
33
|
|
{
|
34
|
943
|
public AbstractMethodAssembler(IComponentDef componentDef)
|
35
|
|
: base(componentDef)
|
36
|
|
{
|
37
|
|
}
|
38
|
|
|
39
|
32
|
protected void Invoke(Type type,object component,IMethodDef methodDef)
|
40
|
|
{
|
41
|
32
|
string expression = methodDef.Expression;
|
42
|
32
|
string methodName = methodDef.MethodName;
|
43
|
32
|
if(methodName != null)
|
44
|
|
{
|
45
|
25
|
object[] args = new object[0];
|
46
|
25
|
MethodInfo method = null;
|
47
|
25
|
try
|
48
|
|
{
|
49
|
25
|
if(methodDef.ArgDefSize > 0)
|
50
|
|
{
|
51
|
13
|
args = methodDef.Args;
|
52
|
|
}
|
53
|
|
else
|
54
|
|
{
|
55
|
12
|
MethodInfo[] methods = type.GetMethods();
|
56
|
12
|
method = this.GetSuitableMethod(methods,methodName);
|
57
|
12
|
if(method != null)
|
58
|
|
{
|
59
|
10
|
ParameterInfo[] parameters = method.GetParameters();
|
60
|
10
|
Type[] argTypes = new Type[parameters.Length];
|
61
|
12
|
for(int i = 0; i < parameters.Length; ++i)
|
62
|
|
{
|
63
|
2
|
argTypes[i] = parameters[i].ParameterType;
|
64
|
|
}
|
65
|
10
|
args = this.GetArgs(argTypes);
|
66
|
|
}
|
67
|
|
}
|
68
|
|
}
|
69
|
|
catch(ComponentNotFoundRuntimeException cause)
|
70
|
|
{
|
71
|
0
|
throw new IllegalMethodRuntimeException(
|
72
|
|
this.GetComponentType(component),methodName,cause);
|
73
|
|
}
|
74
|
25
|
if(method != null)
|
75
|
|
{
|
76
|
10
|
MethodUtil.Invoke(method,component,args);
|
77
|
|
}
|
78
|
|
else
|
79
|
|
{
|
80
|
15
|
this.Invoke(type,component,methodName,args);
|
81
|
|
}
|
82
|
|
}
|
83
|
|
else
|
84
|
|
{
|
85
|
7
|
InvokeExpression(component,expression);
|
86
|
|
}
|
87
|
|
}
|
88
|
|
|
89
|
7
|
private void InvokeExpression(object component,string expression)
|
90
|
|
{
|
91
|
7
|
Hashtable ctx = new Hashtable();
|
92
|
7
|
ctx["self"] = component;
|
93
|
7
|
ctx["out"] = Console.Out;
|
94
|
7
|
ctx["err"] = Console.Error;
|
95
|
7
|
JScriptUtil.Evaluate(expression,ctx,null);
|
96
|
|
}
|
97
|
|
|
98
|
12
|
private MethodInfo GetSuitableMethod(MethodInfo[] methods, string methodName)
|
99
|
|
{
|
100
|
12
|
int argSize = -1;
|
101
|
12
|
MethodInfo method = null;
|
102
|
128
|
for(int i = 0; i < methods.Length; ++i)
|
103
|
|
{
|
104
|
116
|
int tempArgSize = methods[i].GetParameters().Length;
|
105
|
116
|
if (methods[i].Name.Equals(methodName)
|
106
|
|
&& tempArgSize > argSize
|
107
|
|
&& AutoBindingUtil.IsSuitable(methods[i].GetParameters()))
|
108
|
|
{
|
109
|
10
|
method = methods[i];
|
110
|
10
|
argSize = tempArgSize;
|
111
|
|
}
|
112
|
|
}
|
113
|
12
|
return method;
|
114
|
|
}
|
115
|
|
|
116
|
15
|
private void Invoke(Type type, object component, string methodName, object[] args)
|
117
|
|
{
|
118
|
15
|
try
|
119
|
|
{
|
120
|
15
|
type.InvokeMember(methodName,BindingFlags.InvokeMethod,
|
121
|
|
null,component,args);
|
122
|
|
}
|
123
|
|
catch(MissingMethodException ex)
|
124
|
|
{
|
125
|
3
|
ex.ToString();
|
126
|
3
|
throw new MethodNotFoundRuntimeException(type,methodName,args);
|
127
|
|
}
|
128
|
|
catch(Exception ex)
|
129
|
|
{
|
130
|
0
|
throw new IllegalMethodRuntimeException(this.ComponentDef.ComponentType,
|
131
|
|
methodName,ex);
|
132
|
|
}
|
133
|
|
}
|
134
|
|
|
135
|
|
#region MethodAssembler メンバ
|
136
|
|
|
137
|
0
|
public virtual void Assemble(object component)
|
138
|
|
{
|
139
|
|
|
140
|
|
}
|
141
|
|
|
142
|
|
#endregion
|
143
|
|
}
|
144
|
|
}
|
145
|
|
|