11<?php
22
33class JavaMethodInvoker extends JavaInvoker {
4+
5+ /**
6+ * @var bool コンストラクターが実行されているかどうかを判定します。
7+ */
8+ private $ _constructed = false ;
9+
10+ /**
11+ *
12+ * @access private
13+ * @var bool インスタンス化されているかどうか判定します
14+ */
15+ public function __construct (\JavaClass &$ Class , $ constructed = false ) {
16+ parent ::__construct ($ Class );
17+ $ this ->_constructed = $ constructed ;
18+ }
19+
20+ /**
21+ * Javaのメンバをコールします。
22+ *
23+ * @param $fieldName フィールド名
24+ * @return mixed
25+ */
26+ public function __get ($ fieldName ) {
27+
28+ $ cpInfo = $ this ->getClass ()->getCpInfo ();
429
5- public function __call ( $ methodName , $ arguments ) {
30+ foreach ( $ this -> getClass ()-> getFields () as $ fieldInfo ) {
631
7- $ cpInfo = $ this -> getClass ()-> getCpInfo ();
32+ $ cpFieldName = $ cpInfo [ $ fieldInfo -> getNameIndex ()]-> getString ();
833
9- foreach ($ this ->getClass ()->getMethods () as $ methodInfo ) {
34+ if ($ fieldName === $ cpFieldName ) {
35+
36+ $ accessibility = $ this ->_getAccessiblity ($ fieldInfo );
37+ $ fieldSignature = JavaClass::parseSignature ($ cpInfo [$ fieldInfo ->getDescriptorIndex ()]->getString ());
38+
39+ // 静的メンバの場合
40+ if (in_array ('static ' , $ accessibility )) {
41+ return $ this ->getClass ()->getStatic ($ fieldName );
42+ }
43+ $ type = 'JavaType ' . ucfirst ($ fieldSignature [0 ]['type ' ]);
44+ return new $ type ($ this ->getClass ()->getInstance ($ fieldName ));
45+ }
46+ }
47+
48+ throw new JavaInvokerException ('undefined field " ' . $ fieldName . '" ' );
49+
50+ }
51+
52+ /**
53+ * Javaのメンバの値を設定します。
54+ *
55+ * @param string $fieldName フィールド名
56+ * @param mixed $value 書き換える値
57+ * @return mixed
58+ */
59+ public function __set ($ fieldName , $ value ) {
60+
61+ $ cpInfo = $ this ->getClass ()->getCpInfo ();
1062
11- $ cpMethodName = $ cpInfo [ $ methodInfo -> getNameIndex ()]-> getString ();
63+ foreach ( $ this -> getClass ()-> getFields () as $ fieldInfo ) {
1264
13- if ( $ methodName === $ cpMethodName ) {
65+ $ cpFieldName = $ cpInfo [ $ fieldInfo -> getNameIndex ()]-> getString ();
1466
15- $ accessFlag = new JavaMethodAccessFlagEnum ();
16- $ accessibility = array ();
67+ if ($ fieldName === $ cpFieldName ) {
68+
69+ $ accessibility = $ this ->_getAccessiblity ($ fieldInfo );
70+ $ fieldSignature = JavaClass::parseSignature ($ cpInfo [$ fieldInfo ->getDescriptorIndex ()]->getString ());
71+
72+ // 静的メンバの場合
73+ if (in_array ('static ' , $ accessibility )) {
74+ $ this ->getClass ()->setStatic ($ fieldName , $ value );
75+ return ;
76+ }
77+ $ this ->getClass ()->setInstance ($ fieldName , $ value );
78+ return ;
79+ }
80+ }
81+
82+ throw new JavaInvokerException ('undefined field " ' . $ fieldName . '" ' );
83+ }
84+
85+ /**
86+ * Javaのメソッドをエミュレートします。
87+ *
88+ * @param $fieldName フィールド名
89+ * @return mixed
90+ */
91+ public function __call ($ methodName , $ arguments ) {
1792
18- foreach ( $ accessFlag -> getValues () as $ value ) {
93+ $ cpInfo = $ this -> getClass ()-> getCpInfo ();
1994
20- if (( $ methodInfo -> getAccessFlag () & $ value ) != 0 ) {
95+ foreach ( $ this -> getClass ()-> getMethods () as $ methodInfo ) {
2196
22- $ accessibility [] = strtolower ( preg_replace ( ' /^CONSTANT_/ ' , '' , $ accessFlag -> getName ( $ value )) );
97+ $ cpMethodName = $ cpInfo [ $ methodInfo -> getNameIndex ()]-> getString ( );
2398
24- }
99+ if ( $ methodName === $ cpMethodName ) {
25100
26- }
101+ $ accessibility = $ this -> _getAccessiblity ( $ methodInfo );
27102
28103 // メソッドのシグネチャを取得する
29104 $ javaArguments = JavaClass::parseSignature ($ cpInfo [$ methodInfo ->getDescriptorIndex ()]->getString ());
@@ -98,13 +173,13 @@ public function __call ($methodName, $arguments) {
98173 $ this ->getClass ()->appendTrace ($ opcode , $ pointer , $ stacks , $ byteCodeStream ->getOperands ());
99174
100175 if ($ returnValue !== null ) {
176+ $ this ->getClass ()->traceCompletion ();
101177 return $ returnValue ;
102178 }
103179
104180 }
105181
106182 $ this ->getClass ()->traceCompletion ();
107-
108183 return ;
109184
110185 }
@@ -120,10 +195,40 @@ public function __call ($methodName, $arguments) {
120195
121196 }
122197
198+ /**
199+ * 型の変換を行います
200+ *
201+ * @param mixed $value 変換対象を指定
202+ * @return int 変換された型を返します。
203+ */
123204 public function valueOf ($ value ) {
124205
125206 return (int ) $ value ;
126207
127208 }
128209
210+ /**
211+ * アクセス修飾子を取得します。
212+ *
213+ * @param JavaMethodInfo|JavaFieldInfo $info アクセス修飾子を取得したいストラクチャを指定
214+ * @return array アクセス修飾子を返します。
215+ */
216+ private function _getAccessiblity ($ info ) {
217+
218+ $ accessFlag = new JavaAccessFlagEnum ();
219+ $ accessibility = array ();
220+
221+ foreach ($ accessFlag ->getValues () as $ value ) {
222+
223+ if (($ info ->getAccessFlag () & $ value ) != 0 ) {
224+
225+ $ accessibility [] = strtolower (preg_replace ('/^CONSTANT_/ ' , '' , $ accessFlag ->getName ($ value )));
226+
227+ }
228+
229+ }
230+
231+ return $ accessibility ;
232+ }
233+
129234}
0 commit comments