-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
reflect.jl
203 lines (169 loc) · 4.48 KB
/
reflect.jl
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
function getclass(obj::JavaObject)
jcall(obj, "getClass", JClass, ())
end
function conventional_name(name::AbstractString)
if startswith(name, "[")
return conventional_name(name[2:end]) * "[]"
elseif name == "Z"
return "boolean"
elseif name == "B"
return "byte"
elseif name == "S"
return "short"
elseif name == "C"
return "char"
elseif name == "I"
return "int"
elseif name == "J"
return "long"
elseif name == "F"
return "float"
elseif name == "D"
return "double"
elseif name == "V"
return "void"
elseif startswith(name, "L")
return name[2:end-1]
else
return name
end
end
"""
```
getname(cls::JClass)
```
Returns the fully qualified name of the java class
### Args
* cls: The JClass object
### Returns
The fully qualified name of the java class
"""
function getname(cls::JClass)
rawname = jcall(cls, "getName", JString, ())
conventional_name(rawname)
end
"""
```
getname(method::JMethod)
```
Returns the fully qualified name of the java method
### Args
* cls: The JClass method
### Returns
The fully qualified name of the method
"""
function getname(method::JMethod)
jcall(method, "getName", JString, ())
end
"""
"""
getname(field::JField) = jcall(field, "getName", JString, ())
"""
```
listmethods(obj::JavaObject)
```
Lists the methods that are available on the java object passed
### Args
* obj: The java object
### Returns
List of methods
"""
function listmethods(obj::JavaObject)
cls = getclass(obj)
jcall(cls, "getMethods", Vector{JMethod}, ())
end
function listmethods(::Type{JavaObject{C}}) where C
cls = classforname(string(C))
jcall(cls, "getMethods", Vector{JMethod}, ())
end
function listmethods(cls::JClass)
jcall(cls, "getMethods", Vector{JMethod}, ())
end
"""
```
listmethods(obj::JavaObject, name::AbstractString)
```
Lists the methods that are available on the java object passed. The methods are filtered based on the name passed
### Args
* obj: The java object
* name: The filter (e.g. method name)
### Returns
List of methods available on the java object and matching the name passed
"""
function listmethods(obj::Union{JavaObject{C}, Type{JavaObject{C}}}, name::AbstractString) where C
allmethods = listmethods(obj)
filter(m -> getname(m) == name, allmethods)
end
"""
```
getreturntype(method::JMethod)
```
Returns the return type of the java method
### Args
* method: The java method object
### Returns
Returns the type of the return object as a JClass
"""
function getreturntype(method::JMethod)
jcall(method, "getReturnType", JClass, ())
end
"""
gettype(field::JField)
Get type of field
"""
gettype(field::JField) = jcall(field, "getType", JClass, ())
"""
```
listfields(obj::JavaObject)
```
List the fields that are available on the java object passed.
"""
listfields(cls::AbstractString) = listfields(classforname(cls))
listfields(cls::Type{JavaObject{C}}) where C = listfields(classforname(string(C)))
listfields(cls::JClass) = jcall(cls, "getFields", Vector{JField}, ())
listfields(obj::JavaObject) = listfields(getclass(obj))
function listfields(cls::Union{JavaObject{C}, Type{JavaObject{C}}}, name::AbstractString) where C
allfields = listfields(cls)
filter(f -> getname(f) == name, allfields)
end
"""
```
getparametertypes(method::JMethod)
```
Returns the parameter types of the java method
### Args
* method: The java method object
### Returns
Vector the parametertypes
"""
function getparametertypes(method::Union{JMethod,JConstructor})
jcall(method, "getParameterTypes", Vector{JClass}, ())
end
function Base.show(io::IO, method::JMethod)
name = getname(method)
rettype = getname(getreturntype(method))
argtypes = [getname(c) for c in getparametertypes(method)]
argtypestr = join(argtypes, ", ")
print(io, "$rettype $name($argtypestr)")
end
function Base.show(io::IO, field::JField)
name = getname(field)
fieldtype = getname(gettype(field))
print(io, "$fieldtype $name")
end
"""
```
classforname(name::String)
```
Create an instance of `Class<name>` (same as `Class.forName(name)` in Java)
### Args
* name: The name of a class to instantiate
### Returns
JavaObject Instance of `Class<name>`
"""
function classforname(name::String)
thread = jcall(JThread, "currentThread", JThread, ())
loader = jcall(thread, "getContextClassLoader", JClassLoader, ())
return jcall(JClass, "forName", JClass, (JString, jboolean, JClassLoader),
name, true, loader)
end