66use PHPJava \Core \JVM \FieldPool ;
77use PHPJava \Core \JVM \InterfacePool ;
88use PHPJava \Core \JVM \MethodPool ;
9- use PHPJava \Core \JVM \Parameters \GlobalOptions ;
10- use PHPJava \Core \JVM \Parameters \Runtime ;
119use PHPJava \Core \JVM \Validations \MagicByte ;
1210use PHPJava \Core \Stream \Reader \ReaderInterface ;
13- use PHPJava \Exceptions \DebugTraceIsDisabledException ;
11+ use PHPJava \Core \Traits \Classifiable ;
12+ use PHPJava \Core \Traits \ClassInvokable ;
13+ use PHPJava \Core \Traits \Debuggable ;
14+ use PHPJava \Core \Traits \Finalizable ;
15+ use PHPJava \Core \Traits \OptionExtendable ;
16+ use PHPJava \Core \Traits \ParentClassExtendable ;
1417use PHPJava \Exceptions \ValidatorException ;
1518use PHPJava \Kernel \Attributes \InnerClassesAttribute ;
1619use PHPJava \Kernel \Resolvers \ClassResolver ;
1720use PHPJava \Kernel \Resolvers \SDKVersionResolver ;
1821use PHPJava \Kernel \Structures \_MethodInfo ;
1922use PHPJava \Kernel \Structures \_Utf8 ;
20- use PHPJava \Packages \java \lang \_Class ;
2123use PHPJava \Utilities \DebugTool ;
2224use PHPJava \Utilities \Formatter ;
2325
2426class JavaClass implements JavaClassInterface
2527{
2628 use \PHPJava \Kernel \Core \ConstantPool;
29+ use ParentClassExtendable;
30+ use Classifiable;
31+ use Debuggable;
32+ use OptionExtendable;
33+ use Finalizable;
34+ use ClassInvokable;
2735
2836 /**
2937 * @var int[]
@@ -78,11 +86,6 @@ class JavaClass implements JavaClassInterface
7886 */
7987 private $ className ;
8088
81- /**
82- * @var mixed[]
83- */
84- private $ debugTraces = [];
85-
8689 /**
8790 * @var JavaClassInvoker
8891 */
@@ -103,16 +106,6 @@ class JavaClass implements JavaClassInterface
103106 */
104107 private $ superClass ;
105108
106- /**
107- * @var array
108- */
109- private $ options = [];
110-
111- /**
112- * @var DebugTool
113- */
114- private $ debugTool ;
115-
116109 /**
117110 * @var float
118111 */
@@ -128,7 +121,7 @@ public function __construct(ReaderInterface $reader, array $options = [])
128121 $ this ->startTime = microtime (true );
129122
130123 // Validate Java file
131- if (!(new MagicByte ($ reader ->getBinaryReader ()->readUnsignedInt ()))->isValid ()) {
124+ if (!(new MagicByte ($ reader ->getReader ()->readUnsignedInt ()))->isValid ()) {
132125 throw new ValidatorException ($ reader . ' has broken or not Java class. ' );
133126 }
134127
@@ -155,12 +148,12 @@ public function __construct(ReaderInterface $reader, array $options = [])
155148 $ this ->debugTool ->getLogger ()->info ('Start class emulation ' );
156149
157150 // read minor version
158- $ this ->versions ['minor ' ] = $ reader ->getBinaryReader ()->readUnsignedShort ();
151+ $ this ->versions ['minor ' ] = $ reader ->getReader ()->readUnsignedShort ();
159152
160153 $ this ->debugTool ->getLogger ()->info ('Minor version: ' . $ this ->versions ['minor ' ]);
161154
162155 // read major version
163- $ this ->versions ['major ' ] = $ reader ->getBinaryReader ()->readUnsignedShort ();
156+ $ this ->versions ['major ' ] = $ reader ->getReader ()->readUnsignedShort ();
164157
165158 $ this ->debugTool ->getLogger ()->info ('Major version: ' . $ this ->versions ['major ' ]);
166159
@@ -169,21 +162,21 @@ public function __construct(ReaderInterface $reader, array $options = [])
169162 // read constant pool size
170163 $ this ->constantPool = new ConstantPool (
171164 $ reader ,
172- $ reader ->getBinaryReader ()->readUnsignedShort ()
165+ $ reader ->getReader ()->readUnsignedShort ()
173166 );
174167
175168 $ this ->debugTool ->getLogger ()->info ('Constant Pools: ' . count ($ this ->constantPool ));
176169
177170 // read access flag
178- $ this ->accessFlag = $ reader ->getBinaryReader ()->readUnsignedShort ();
171+ $ this ->accessFlag = $ reader ->getReader ()->readUnsignedShort ();
179172
180173 // read this class
181- $ this ->thisClass = $ reader ->getBinaryReader ()->readUnsignedShort ();
174+ $ this ->thisClass = $ reader ->getReader ()->readUnsignedShort ();
182175
183176 $ this ->className = $ this ->constantPool [$ this ->constantPool [$ this ->thisClass ]->getClassIndex ()];
184177
185178 // read super class
186- $ this ->superClassIndex = $ reader ->getBinaryReader ()->readUnsignedShort ();
179+ $ this ->superClassIndex = $ reader ->getReader ()->readUnsignedShort ();
187180
188181 $ cpInfo = $ this ->getConstantPool ();
189182 [$ resolvedType , $ superClass ] = $ this ->options ['class_resolver ' ]->resolve (
@@ -208,7 +201,7 @@ public function __construct(ReaderInterface $reader, array $options = [])
208201 // read interfaces
209202 $ this ->interfacePool = new InterfacePool (
210203 $ reader ,
211- $ reader ->getBinaryReader ()->readUnsignedShort (),
204+ $ reader ->getReader ()->readUnsignedShort (),
212205 $ this ->constantPool ,
213206 $ this ->debugTool
214207 );
@@ -218,7 +211,7 @@ public function __construct(ReaderInterface $reader, array $options = [])
218211 // read fields
219212 $ this ->fieldPool = new FieldPool (
220213 $ reader ,
221- $ reader ->getBinaryReader ()->readUnsignedShort (),
214+ $ reader ->getReader ()->readUnsignedShort (),
222215 $ this ->constantPool ,
223216 $ this ->debugTool
224217 );
@@ -228,7 +221,7 @@ public function __construct(ReaderInterface $reader, array $options = [])
228221 // read methods
229222 $ this ->methodPool = new MethodPool (
230223 $ reader ,
231- $ reader ->getBinaryReader ()->readUnsignedShort (),
224+ $ reader ->getReader ()->readUnsignedShort (),
232225 $ this ->constantPool ,
233226 $ this ->debugTool
234227 );
@@ -238,7 +231,7 @@ public function __construct(ReaderInterface $reader, array $options = [])
238231 // read Attributes
239232 $ this ->attributePool = new AttributePool (
240233 $ reader ,
241- $ reader ->getBinaryReader ()->readUnsignedShort (),
234+ $ reader ->getReader ()->readUnsignedShort (),
242235 $ this ->constantPool ,
243236 $ this ->debugTool
244237 );
@@ -292,34 +285,6 @@ function (_MethodInfo $method) {
292285 ];
293286 }
294287
295- public function getClass ()
296- {
297- return new _Class ($ this );
298- }
299-
300- public function __invoke (...$ arguments ): JavaClass
301- {
302- return $ this
303- ->getInvoker ()
304- ->construct (...$ arguments )
305- ->getJavaClass ();
306- }
307-
308- public function getOptions ($ key = null )
309- {
310- if (isset ($ key )) {
311- return $ this ->options [$ key ];
312- }
313- return $ this ->options ;
314- }
315-
316- public function __destruct ()
317- {
318- $ this ->debugTool ->getLogger ()->info (
319- 'Spent time: ' . (microtime (true ) - $ this ->startTime ) . ' sec. '
320- );
321- }
322-
323288 public function getClassName (bool $ shortName = false ): string
324289 {
325290 $ className = $ this ->className ->getString ();
@@ -368,28 +333,6 @@ public function getInvoker(): JavaClassInvoker
368333 return $ this ->invoker ;
369334 }
370335
371- public function appendDebug ($ log ): self
372- {
373- $ this ->debugTraces [] = $ log ;
374- return $ this ;
375- }
376-
377- public function hasParentClass (): bool
378- {
379- return isset ($ this ->parentClass );
380- }
381-
382- public function setParentClass (JavaClass $ class ): self
383- {
384- $ this ->parentClass = $ class ;
385- return $ this ;
386- }
387-
388- public function getParentClass (): JavaClass
389- {
390- return $ this ->parentClass ;
391- }
392-
393336 public function getSuperClass ()
394337 {
395338 return $ this ->superClass ;
@@ -402,82 +345,4 @@ public function getAttributes(): array
402345 {
403346 return $ this ->attributePool ->getEntries ();
404347 }
405-
406- public function debug (): void
407- {
408- $ isEnabledTrace = $ this ->options ['operations ' ]['enable_trace ' ] ?? GlobalOptions::get ('operations.enable_trace ' ) ?? Runtime::OPERATIONS_ENABLE_TRACE ;
409- if (!$ isEnabledTrace ) {
410- throw new DebugTraceIsDisabledException (
411- 'Debug trace is disabled. If you want to show debug trace then enable to `enable_trace` option. '
412- );
413- }
414- $ cpInfo = $ this ->getConstantPool ();
415- foreach ($ this ->debugTraces as $ debugTraces ) {
416- printf ("[method] \n" );
417- printf (Formatter::beatifyMethodFromConstantPool ($ debugTraces ['method ' ], $ this ->getConstantPool ()) . "\n" );
418- printf ("\n" );
419- printf ("[code] \n" );
420-
421- $ codeCounter = 0 ;
422- printf (
423- "%s \n" ,
424- implode (
425- "\n" ,
426- array_map (
427- function ($ codes ) use (&$ codeCounter , &$ debugTraces ) {
428- return implode (
429- ' ' ,
430- array_map (
431- function ($ code ) use (&$ codeCounter , &$ debugTraces ) {
432- $ isMnemonic = in_array ($ codeCounter , $ debugTraces ['mnemonic_indexes ' ]);
433- $ codeCounter ++;
434- return ($ isMnemonic ? "\e[1m \e[35m " : '' ) . "<0x {$ code }> " . ($ isMnemonic ? "\e[m " : '' );
435- },
436- $ codes
437- )
438- );
439- },
440- array_chunk (str_split (bin2hex ($ debugTraces ['raw_code ' ]), 2 ), 20 )
441- )
442- )
443- );
444- printf ("\n" );
445- printf ("[executed] \n" );
446-
447- printf (
448- "% 8s | %-6.6s | %-20.20s | %-10.10s | %-15.15s \n" ,
449- 'PC ' ,
450- 'OPCODE ' ,
451- 'MNEMONIC ' ,
452- 'OPERANDS ' ,
453- 'LOCAL STORAGE '
454- );
455-
456- $ line = sprintf (
457- "%8s+%8s+%22s+%12s+%17s \n" ,
458- '--------- ' ,
459- '-------- ' ,
460- '---------------------- ' ,
461- '------------ ' ,
462- '----------------- '
463- );
464-
465- printf ($ line );
466-
467- foreach ($ debugTraces ['executed ' ] as [$ opcode , $ mnemonic , $ localStorage , $ stacks , $ pointer ]) {
468- printf (
469- "% 8s | 0x%02X | %-20.20s | %-10.10s | %-15.15s \n" ,
470- (int ) $ pointer ,
471- $ opcode ,
472- // Remove prefix
473- ltrim ($ mnemonic , '_ ' ),
474- count ($ stacks ),
475- count ($ localStorage )
476- );
477- }
478-
479- printf ($ line );
480- printf ("\n" );
481- }
482- }
483348}
0 commit comments