Skip to content

Commit b906412

Browse files
authored
Merge pull request #104 from php-java/di-to-metho
If included `@native` annotation then dependency inject with Constant Pool and JavaClass to method.
2 parents aba49a3 + 37630a6 commit b906412

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"php-java/java-net-package": "*",
2323
"php-java/java-nio-package": "*",
2424
"gabrielelana/byte-units": "dev-master",
25-
"symfony/console": "4.2"
25+
"symfony/console": "4.2",
26+
"phpdocumentor/reflection-docblock": "4.3"
2627
},
2728
"autoload": {
2829
"psr-4": {

src/Kernel/Mnemonics/_invokevirtual.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,28 @@ public function execute(): void
4848
...$arguments
4949
);
5050
} else {
51+
$reflectionClass = new \ReflectionClass(
52+
$realInvokerClass = TypeResolver::convertPHPTypeToJavaType($invokerClass)
53+
);
54+
$methodAccessor = $reflectionClass->getMethod($methodName);
55+
56+
if ($document = $methodAccessor->getDocComment()) {
57+
// parse PHPDoc
58+
$documentBlock = \phpDocumentor\Reflection\DocBlockFactory::createInstance()
59+
->create($document);
60+
61+
// Native annotation will dependency inject.
62+
if (!empty($documentBlock->getTagsByName('native'))) {
63+
array_unshift(
64+
$arguments,
65+
$this->getConstantPool(),
66+
$this->javaClass
67+
);
68+
}
69+
}
70+
5171
$result = call_user_func_array(
52-
[
53-
TypeResolver::convertPHPTypeToJavaType($invokerClass),
54-
$methodName
55-
],
72+
[$realInvokerClass, $methodName],
5673
$arguments
5774
);
5875
}

0 commit comments

Comments
 (0)