Skip to content

JavaClass

memory-agape edited this page Apr 18, 2019 · 2 revisions

PHPJava\Core\JavaClass

Introduction

JavaClass class is provided APIs for reading Java class which implemented PHPJava\Core\Stream\Reader\ReaderInterface.

How to use

For an example as follows:

  1. Write Java:
class HelloWorld 
{
    public static void main(String[] args)
    {
        System.out.println(args[0]);
    }
}
  1. Compile Java:
$ javac -UTF8 /path/to/HelloWorld.java
  1. Call the main method as follows:
<?php
use PHPJava\Core\JavaClass;
use PHPJava\Core\Stream\Reader\FileReader;

(new JavaClass(new FileReader('/path/to/HelloWorld.class')))
    ->getInvoker()
    ->getStatic()
    ->getMethods()
    ->call(
        'main',
        ["Hello World!"]
    );
  1. Get the result
$ php /path/to/HelloWorld.php
Hello World!

Provided methods

Name

public function __construct(ReaderInterface $reader, array $options = [])
Introduction

Initiate a Java Class.

Parameters
Name Type Description
$reader ReaderInterface Specify reader as such as FileReader or InlineReader
$options array Set JavaClass runtime options.

Name

public function getOptions(): array
Introduction

Return set runtime options on a JavaClass.

Parameters

No parameters.

Name

public function getClassName(bool $shortName = false): string
Introduction

Return running Java's class name.

Parameters
Name Type Description
$shortName bool Specify to return value which fully qualified class name or short name. (NOTE: Fully qualified return value is including $ if it is a inner class)

Name

public function getInnerClasses(): array
Introduction

Return included inner classes in JavaClass.

Parameters

No parameter.

Name

public function getFields(): array
Introduction

Return specified fields in JavaClass.

Parameters

No parameter.

Name

public function getMethods(): array
Introduction

Return specified methods in JavaClass.

Parameters

No parameter.

Name

public function getInvoker(): JavaClassInvoker
Introduction

Return an Invoker which is caller for JavaClass's method.

Parameters

No parameter.

Name

public function appendDebug(mixed $log): void
Introduction

Append one line trace. Maybe, you do not use this method.

Parameters
Name Type Description
$log mixed Specify a log

Name

public function hasParentClass(): bool
Introduction

Check the JavaClass's parent class.

Parameters

No parameter.

Name

public function setParentClass(JavaClass $class): JavaClass
Introduction

Set parent class.

Parameters
Name Type Description
$class JavaClass Specify JavaClass

Name

public function getParentClass(): JavaClass
Introduction

Return parent class.

Parameters

No parameter.

Name

public function getSuperClass()
Introduction

No feature.

Parameters

No feature.

Name

public function debug(): void
Introduction

Output debug trace.

Parameters

No parameter.

Clone this wiki locally