File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11<?php
22namespace PHPJava \Kernel \Mnemonics ;
33
4- use PHPJava \Exceptions \NotImplementedException ;
4+ use PHPJava \Kernel \Types \_Int ;
5+ use PHPJava \Utilities \Formatter ;
56
67final class _instanceof implements OperationInterface
78{
@@ -10,6 +11,22 @@ final class _instanceof implements OperationInterface
1011
1112 public function execute (): void
1213 {
13- throw new NotImplementedException (__CLASS__ );
14+ $ cp = $ this ->getConstantPool ();
15+ $ objectref = $ this ->popFromOperandStack ();
16+
17+ $ targetClass = $ cp [$ cp [$ this ->readUnsignedShort ()]->getClassIndex ()];
18+
19+ [, $ className ] = Formatter::convertJavaNamespaceToPHP ($ targetClass );
20+
21+ if ($ objectref instanceof $ className ) {
22+ $ this ->pushToOperandStack (
23+ _Int::get (1 )
24+ );
25+ return ;
26+ }
27+
28+ $ this ->pushToOperandStack (
29+ _Int::get (0 )
30+ );
1431 }
1532}
Original file line number Diff line number Diff line change 88
99class Formatter
1010{
11- const BUILT_IN_PACKAGE = 0 ;
12- const USER_DEFINED_PACKAGE = 1 ;
11+ const BUILT_IN_PACKAGE = ' BUILT_IN_PACKAGE ' ;
12+ const USER_DEFINED_PACKAGE = ' USER_DEFINED_PACKAGE ' ;
1313
1414 /**
1515 * @param $signature
Original file line number Diff line number Diff line change 1+ <?php
2+ namespace PHPJava \Tests ;
3+
4+ class InstanceOfTest extends Base
5+ {
6+ protected $ fixtures = [
7+ 'InstanceOfTest ' ,
8+ ];
9+
10+ private function call ($ method )
11+ {
12+ return $ this ->initiatedJavaClasses ['InstanceOfTest ' ]
13+ ->getInvoker ()
14+ ->getStatic ()
15+ ->getMethods ()
16+ ->call ($ method );
17+ }
18+
19+ public function testInstanceOfString ()
20+ {
21+ ob_start ();
22+ $ this ->call ('instanceOfString ' );
23+ $ result = ob_get_clean ();
24+ $ this ->assertEquals (
25+ "Hello World \n" ,
26+ $ result
27+ );
28+ }
29+
30+ public function testInstanceOfObject ()
31+ {
32+ ob_start ();
33+ $ this ->call ('instanceOfObject ' );
34+ $ result = ob_get_clean ();
35+ $ this ->assertEquals (
36+ "Hello World \n" ,
37+ $ result
38+ );
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ class InstanceOfTest
2+ {
3+ public static void instanceOfString ()
4+ {
5+ String a = "Hello World" ;
6+ if (a instanceof String ) {
7+ System .out .println (a );
8+ return ;
9+ }
10+ System .out .println ("Unreachable here." );
11+ }
12+
13+ public static void instanceOfObject ()
14+ {
15+ String a = "Hello World" ;
16+ if (a instanceof Object ) {
17+ System .out .println (a );
18+ return ;
19+ }
20+ System .out .println ("Unreachable here." );
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments