-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathJavaClass.php
More file actions
174 lines (152 loc) Β· 4.89 KB
/
JavaClass.php
File metadata and controls
174 lines (152 loc) Β· 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types=1);
namespace PHPJava\Core;
use PHPJava\Core\Extended\ClassInvokable;
use PHPJava\Core\Stream\Reader\FileReader;
use PHPJava\Core\Stream\Reader\PackageReader;
use PHPJava\Kernel\Internal\JavaClassDeferredLoader;
use PHPJava\Kernel\Resolvers\ClassResolver;
use PHPJava\Packages\java\lang\ClassNotFoundException;
use PHPJava\Utilities\Formatter;
class JavaClass implements JavaClassInterface
{
use ClassInvokable;
/**
* @var JavaGenericClassInterface
*/
private $genericClass;
/**
* @var array
*/
private $options = [];
/**
* @var array
*/
private static $lastOptions = [];
public function __construct(JavaGenericClassInterface $genericClass)
{
$this->genericClass = $genericClass;
}
/**
* @param $methodName
* @param $arguments
*/
public function __call($methodName, $arguments)
{
return $this->genericClass->{$methodName}(...$arguments);
}
public function isCompiledClass(): bool
{
return $this->genericClass instanceof JavaCompiledClass;
}
public function isSimpleClass(): bool
{
return $this->genericClass instanceof JavaSimpleClass;
}
/**
* @return JavaClass
*/
public static function of(JavaClassInterface $javaClass)
{
if ($javaClass instanceof JavaClass) {
return $javaClass;
}
return new JavaClass($javaClass);
}
public function is(string $className): bool
{
$className = Formatter::convertPHPNamespacesToJava($className);
// get parents
$extendedClasses = $this->genericClass->getDefinedExtendedClasses();
$interfaceClasses = $this->genericClass->getDefinedInterfaceClasses();
return in_array($className, $extendedClasses, true) ||
in_array($className, $interfaceClasses, true);
}
public function __toString(): string
{
return (string) $this
->genericClass
->getInvoker()
->getDynamic()
->getMethods()
->call(
'toString'
);
}
/**
* @throws ClassNotFoundException
* @throws \PHPJava\Exceptions\ReadEntryException
* @throws \PHPJava\Exceptions\UnknownVersionException
* @throws \PHPJava\Exceptions\ValidatorException
*/
public static function load(string $classPath, array $options = [], bool $enableInstantiated = true): JavaClass
{
static $loaded = [];
$classPath = str_replace('/', '.', Formatter::convertPHPNamespacesToJava($classPath));
if (isset($loaded[$classPath]) && !$enableInstantiated) {
return $loaded[$classPath];
}
[$type, ] = Formatter::convertJavaNamespaceToPHP(
$classPath
);
// Add resolving path.
ClassResolver::add(
[
[ClassResolver::RESOURCE_TYPE_FILE, getcwd()],
]
);
$instance = null;
if ($type === Formatter::BUILT_IN_PACKAGE) {
$instance = new JavaSimpleClass(
new PackageReader(
$classPath
),
$options
);
} else {
foreach (ClassResolver::getClassPaths() as [$resourceType, $value]) {
try {
switch ($resourceType) {
case ClassResolver::RESOURCE_TYPE_JAR:
/**
* @var JavaArchive $value
*/
$instance = $value->getClassByName($classPath);
break;
case ClassResolver::RESOURCE_TYPE_FILE:
$path = realpath(
$value . '/' . ltrim(str_replace('.', '/', $classPath) . '.class', '/')
);
if ($path === false) {
break;
}
$instance = new JavaCompiledClass(
new FileReader($path),
$options
);
break;
}
if ($instance !== null) {
break;
}
} catch (ClassNotFoundException $e) {
// do nothing
}
}
}
if ($instance === null) {
throw new ClassNotFoundException('Class ' . $classPath . ' does not exist.');
}
return $loaded[$classPath] = static::of($instance);
}
/**
* @return JavaClassDeferredLoader
*/
public static function deferred(string $classPath, array $options = [])
{
return new JavaClassDeferredLoader(
$classPath,
$options
);
}
}