Skip to content

Commit 4d7fd88

Browse files
committed
JavaType対応
1 parent dc0d8a5 commit 4d7fd88

File tree

7 files changed

+137
-11
lines changed

7 files changed

+137
-11
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class JavaType {
1313
*
1414
* @param mixed $value 値を指定します
1515
*/
16-
public function setValue ($value) {
16+
public function __construct ($value) {
1717
$this->value = $value;
1818
}
1919

@@ -29,10 +29,10 @@ public function getValue () {
2929
/**
3030
* 値を出力します。
3131
*
32-
* @return mixed
32+
* @return string
3333
*/
3434
public function __toString() {
35-
return $this->getValue();
35+
return (string) $this->getValue();
3636
}
3737

3838
}

PHPJava/Core/JavaClass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function __construct ($file, $byteCode = null) {
136136

137137
$this->ClassFile = $file;
138138

139-
foreach (glob(__DIR__ . '/../{Common,Exceptions,Enums,Stream,Invoker,Attributes,Structures,Utils}/*.php', GLOB_BRACE) as $loadFile) {
139+
foreach (glob(__DIR__ . '/../{Common,Exceptions,Enums,Stream,Invoker,Attributes,Structures,Types,Utils}/*.php', GLOB_BRACE) as $loadFile) {
140140

141141
require_once($loadFile);
142142

PHPJava/Invoker/JavaMethodInvoker.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function __call ($methodName, $arguments) {
2525

2626
}
2727

28+
// メソッドのシグネチャを取得する
2829
$javaArguments = JavaClass::parseSignature($cpInfo[$methodInfo->getDescriptorIndex()]->getString());
2930

3031
if (sizeof($arguments) !== $javaArguments['argumentsCount']) {
@@ -68,7 +69,7 @@ public function __call ($methodName, $arguments) {
6869
3 => null
6970
);
7071

71-
$i = 0;
72+
$i = 1;
7273
foreach ($arguments as $argument) {
7374

7475
$localstorage[$i] = $argument;
@@ -92,14 +93,23 @@ public function __call ($methodName, $arguments) {
9293

9394
$statement = new $name($methodName, $this, $byteCodeStream, $stacks, $localstorage, $cpInfo, $attributeData, $pointer);
9495
$returnValue = $statement->execute();
95-
96+
9697
// write trace
9798
$this->getClass()->appendTrace($opcode, $pointer, $stacks, $byteCodeStream->getOperands());
9899

99100
if ($returnValue !== null) {
100101

101102
$this->getClass()->traceCompletion();
102-
return $returnValue;
103+
if ($javaArguments[0]['type'] !== 'class') {
104+
// java typeの取得
105+
$javaType = 'JavaType' . ucfirst($javaArguments[0]['type']);
106+
return new $javaType($returnValue);
107+
}
108+
109+
// javaのオブジェクトの場合
110+
111+
$javaType = '\\' . str_replace('/', '\\', $javaArguments[0]['className']);
112+
return new $javaType($returnValue);
103113

104114
}
105115

@@ -118,7 +128,7 @@ public function __call ($methodName, $arguments) {
118128

119129
}
120130

121-
throw new JavaInvokerException('Unknown Error');
131+
throw new JavaInvokerException('undefined method "' . $methodName . '"');
122132

123133
}
124134

PHPJava/Platform/java/lang/String.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ public function rawObject () {
5151
// php magic method
5252
public function __toString () {
5353

54+
if (!($this->Object instanceof \JavaStructureUtf8)) {
55+
return (string) $this->Object;
56+
}
57+
5458
return $this->Object->getString();
5559

5660
}

PHPJava/Statements/JavaStatement_ireturn.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
class JavaStatement_ireturn extends JavaStatement {
44

5+
/***
6+
* return an integer from a method
7+
*/
58
public function execute () {
69

7-
throw new JavaStatementException(__CLASS__ . ' hasnot statement.');
10+
return $this->getStack();
811

912
}
1013

test.java

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,102 @@ class Test {
33
long z = -22222222222222222L;
44
static int c = 100;
55
static String b = "Hello World";
6+
7+
/**
8+
* test for "Integer" value
9+
*
10+
* @param value
11+
* @return
12+
*/
13+
public int testInt (int value) {
14+
return value;
15+
}
16+
17+
/**
18+
* test for "Short" value
19+
*
20+
* @param value
21+
* @return
22+
*/
23+
public short testShort (short value) {
24+
return value;
25+
}
26+
27+
/**
28+
* test for "Long" value
29+
*
30+
* @param value
31+
* @return
32+
*/
33+
public long testLong (long value) {
34+
return value;
35+
}
36+
37+
/**
38+
* test for "Float" value
39+
*
40+
* @param value
41+
* @return
42+
*/
43+
public float testLong (float value) {
44+
return value;
45+
}
46+
47+
/**
48+
* test for "Double" value
49+
*
50+
* @param value
51+
* @return
52+
*/
53+
public double testLong (double value) {
54+
return value;
55+
}
56+
57+
/**
58+
* test for "Char" value
59+
*
60+
* @param value
61+
* @return
62+
*/
63+
public char testChar (char value) {
64+
return value;
65+
}
66+
67+
/**
68+
* test for "Byte" value
69+
*
70+
* @param value
71+
* @return
72+
*/
73+
public byte testByte (byte value) {
74+
return value;
75+
}
76+
77+
/**
78+
* test for "Boolean" value
79+
*
80+
* @param value
81+
* @return
82+
*/
83+
public boolean testBoolean (boolean value) {
84+
return value;
85+
}
86+
87+
/**
88+
* test for "String" value
89+
*
90+
* @param value
91+
* @return
92+
*/
93+
public String testString (String value) {
94+
return value;
95+
}
696

97+
/**
98+
* main method
99+
*
100+
* @param args
101+
*/
7102
public static void main (String[] args) {
8103

9104
String x = "String";
@@ -130,7 +225,7 @@ public static void main (String[] args) {
130225

131226
}
132227

133-
double[] test4 = {3.4, 3.5, 3.6};
228+
double[] test4 = {3.4, 3.5, 3.6, 81263.12312321, -99};
134229
for (double i : test4) {
135230

136231
System.out.println(i);

test.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,26 @@
1414
});
1515

1616
try {
17+
// ini_set('memory_limit', '1024M');
1718
system('rm test.class');
1819
system('javac -encoding UTF8 test.java');
1920

2021
$invoker = new JavaClass('test.class');
2122

22-
$invoker->getMethodInvoker()->main(array(999, 888));
23+
// メインメソッドを呼ぶ
24+
// $invoker->getMethodInvoker()->main(array(999, 888));
25+
26+
// testIntを呼ぶ
27+
var_dump($invoker->getMethodInvoker()->testInt(1111));
28+
29+
// testIntを呼ぶ
30+
var_dump((string) $invoker->getMethodInvoker()->testInt(1111));
31+
32+
// testString(java/lang/String)を呼ぶ
33+
var_dump($invoker->getMethodInvoker()->testString("8888"));
34+
//
35+
// testString(java/lang/String)を呼ぶ
36+
var_dump((string) $invoker->getMethodInvoker()->testString("8888"));
2337

2438
$invoker->trace();
2539

0 commit comments

Comments
 (0)