author | hlopkmar |
Fri, 13 May 2011 15:50:43 +0000 | |
branch | jk_new_structure |
changeset 784 | 1f70ce9d0fee |
parent 774 | ac54730a2b74 |
child 796 | 9b3e00dd3196 |
permissions | -rw-r--r-- |
758 | 1 |
"{ Package: 'stx:libjava' }" |
2 |
||
3 |
Object subclass:#JavaResolver |
|
761 | 4 |
instanceVariableNames:'exceptionThrower' |
758 | 5 |
classVariableNames:'uniqueInstance' |
6 |
poolDictionaries:'' |
|
7 |
category:'Languages-Java-Reader-Support-new' |
|
8 |
! |
|
9 |
||
10 |
||
11 |
!JavaResolver class methodsFor:'initialization'! |
|
12 |
||
13 |
initialize |
|
14 |
uniqueInstance := JavaResolver new. |
|
761 | 15 |
uniqueInstance exceptionThrower: JavaVM. |
16 |
||
758 | 17 |
"/has methods at: and at: put: |
759
66d28d2fa62e
JavaResolver: fixed bug in JavaResolver class>>#initialize
vranyj1
parents:
758
diff
changeset
|
18 |
"/uniqueInstance resolvedClasses: Java. |
758 | 19 |
|
759
66d28d2fa62e
JavaResolver: fixed bug in JavaResolver class>>#initialize
vranyj1
parents:
758
diff
changeset
|
20 |
"Modified: / 10-04-2011 / 10:23:43 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
761 | 21 |
"Modified: / 13-04-2011 / 14:07:35 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
758 | 22 |
! ! |
23 |
||
24 |
!JavaResolver class methodsFor:'instance creation'! |
|
25 |
||
26 |
uniqueInstance |
|
761 | 27 |
^uniqueInstance. |
758 | 28 |
|
29 |
"Created: / 08-04-2011 / 17:36:37 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
761 | 30 |
"Modified: / 11-04-2011 / 19:06:49 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
31 |
! ! |
|
32 |
||
33 |
!JavaResolver methodsFor:'accessing'! |
|
34 |
||
35 |
exceptionThrower |
|
36 |
^ exceptionThrower |
|
37 |
! |
|
38 |
||
39 |
exceptionThrower:something |
|
40 |
exceptionThrower := something. |
|
758 | 41 |
! ! |
42 |
||
43 |
!JavaResolver methodsFor:'class resolving'! |
|
44 |
||
761 | 45 |
resolveClassIndentifiedByRef: aJavaClassRef |
758 | 46 |
| classLoader result | |
47 |
||
761 | 48 |
self validateClassRef: aJavaClassRef. |
49 |
result := self lookupClassIfAlreadyResolved: aJavaClassRef javaClassName. |
|
758 | 50 |
result ifNotNil: |
51 |
[ "/wrap result with array(s) if needed and return it |
|
761 | 52 |
self checkIfArrayRef: aJavaClassRef andWrap: result ]. |
758 | 53 |
" |
54 |
To resolve an unresolved symbolic reference from D to a class or interface C denoted by N, |
|
55 |
the following steps are performed: |
|
56 |
The defining class loader of D is used to create a class or interface denoted by N. |
|
57 |
This class or interface is C. Any exception that can be thrown as a result of failure |
|
58 |
of class or interface creation can thus be thrown as a result of failure of class and |
|
59 |
interface resolution. The details of the process are given in Section 5.3. |
|
60 |
" |
|
761 | 61 |
classLoader := aJavaClassRef classLoader. |
758 | 62 |
classLoader isNil |
784 | 63 |
ifTrue: |
64 |
[ |
|
65 |
result := self loadUnresolvedClass: aJavaClassRef ] |
|
758 | 66 |
ifFalse: |
761 | 67 |
[ result := self loadUnresolvedClass: aJavaClassRef |
68 |
withJavaClassLoader: classLoader. ]. |
|
774 | 69 |
result ifNil: |
784 | 70 |
[ self halt. |
774 | 71 |
"tell mh, he will throw exception instead, for now, he wants to halt." |
72 |
]. |
|
758 | 73 |
" |
74 |
If C is an array class and its element type is a reference type, then the symbolic reference |
|
75 |
to the class or interface representing the element type is resolved by invoking the algorithm |
|
76 |
in Section 5.4.3.1 recursively." |
|
761 | 77 |
result := self checkIfArrayRef: aJavaClassRef andWrap: result. |
758 | 78 |
"Finally, access permissions to C are checked: |
761 | 79 |
If C is not accessible (5.4.4) to D, class or interface resolution throws an IllegalAccessError. |
758 | 80 |
This condition can occur, for example, if C is a class that was originally declared to be |
81 |
public but was changed to be non-public after D was compiled. |
|
82 |
||
83 |
If steps 1 and 2 succeed but step 3 fails, C is still valid and usable. Nevertheless, resolution |
|
84 |
fails, and D is prohibited from accessing C." |
|
761 | 85 |
(self checkPermissionsFrom: aJavaClassRef owner to: result) |
86 |
ifTrue: [ ^ result ] |
|
87 |
ifFalse: [ self throwIllegalAccessError ]. |
|
758 | 88 |
|
761 | 89 |
"Created: / 11-04-2011 / 19:07:19 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
784 | 90 |
"Modified: / 13-05-2011 / 17:51:20 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
758 | 91 |
! ! |
92 |
||
93 |
!JavaResolver methodsFor:'class resolving helpers'! |
|
94 |
||
761 | 95 |
checkIfArrayRef: aJavaClassRef andWrap: result |
96 |
"/wrap result with array(s) if needed and return it |
|
97 |
^ aJavaClassRef isJavaArrayClassRef |
|
98 |
ifFalse: [ result ] |
|
99 |
ifTrue: [ self wrapJavaClass: result withArrayAsSpecifiedIn: aJavaClassRef ] |
|
100 |
||
101 |
"Modified: / 11-04-2011 / 19:31:43 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
102 |
! |
|
103 |
||
104 |
checkPermissionsFrom: refOwner to: resolvedClass |
|
105 |
" |
|
106 |
A class or interface C is accessible to a class or interface D if and only if either of the following conditions are true: |
|
107 |
C is public. |
|
108 |
C and D are members of the same runtime package (§5.3). |
|
109 |
Finally, access permissions to C are checked: |
|
110 |
If C is not accessible (§5.4.4) to D, class or interface resolution throws an IllegalAccessError. |
|
111 |
This condition can occur, for example, if C is a class that was originally declared to be |
|
112 |
public but was changed to be non-public after D was compiled. |
|
113 |
At run time, a class or interface is determined not by its name alone, |
|
114 |
but by a pair: its fully qualified name and its defining class loader. |
|
115 |
Each such class or interface belongs to a single runtime package. The runtime |
|
116 |
package of a class or interface is determined by the package name and |
|
117 |
defining class loader of the class or interface." |
|
118 |
||
119 |
resolvedClass isPublic ifTrue: [ ^ true ]. |
|
120 |
resolvedClass classLoader = refOwner classLoader ifFalse: [ ^ false ]. |
|
121 |
^ refOwner javaPackage = resolvedClass javaPackage. |
|
122 |
||
123 |
"Created: / 11-04-2011 / 19:35:21 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
124 |
"Modified: / 13-04-2011 / 23:13:39 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
125 |
! |
|
126 |
||
127 |
loadUnresolvedClass: aJavaClassRef |
|
773 | 128 |
^ JavaClassReader loadClass: aJavaClassRef javaClassName. |
761 | 129 |
|
130 |
"Created: / 11-04-2011 / 19:27:10 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
773 | 131 |
"Modified: / 12-05-2011 / 16:17:59 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
761 | 132 |
! |
133 |
||
134 |
loadUnresolvedClass: aJavaClassRef withJavaClassLoader: classLoader |
|
135 |
^ classLoader perform: #'loadClass(Ljava/lang/String;)Ljava/lang/Class;' |
|
136 |
with: (Java as_String: (aJavaClassRef name)) |
|
137 |
! |
|
138 |
||
139 |
lookupClassIfAlreadyResolved: javaClassName |
|
140 |
^ nil |
|
141 |
||
142 |
"Modified: / 13-04-2011 / 14:05:25 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
143 |
! |
|
144 |
||
145 |
validateClassRef: aJavaClassRef |
|
146 |
aJavaClassRef isJavaRef ifFalse: [ self halt: 'Argument type not valid' ]. |
|
147 |
aJavaClassRef isJavaClassRef |
|
148 |
ifFalse: [ self halt: 'Only class refs please' ]. |
|
149 |
! |
|
150 |
||
758 | 151 |
wrapJavaClass: aJavaClass withArrayAsSpecifiedIn: aJavaClassRef |
152 |
||
153 |
"wraps aJavaClass with as many array dimensions as needed (zero means no array)" |
|
154 |
| wrappedResult | |
|
155 |
||
156 |
aJavaClassRef isJavaArrayClassRef ifFalse: [ ^ aJavaClass ]. |
|
157 |
aJavaClassRef arrayDimensions timesRepeat: |
|
158 |
[:counter | |
|
159 |
wrappedResult := aJavaClass. |
|
160 |
wrappedResult := JavaArray javaArrayClassFor: wrappedResult. |
|
161 |
^ wrappedResult ]. |
|
162 |
||
163 |
"Created: / 08-04-2011 / 18:40:00 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
164 |
! ! |
|
165 |
||
761 | 166 |
!JavaResolver methodsFor:'common helpers'! |
167 |
||
168 |
checkPermissionsForMethodOrField: aJavaMethod from: accessingJavaClass to: resolvedJavaClass |
|
169 |
"A class or interface C is accessible to a class or interface D if |
|
170 |
and only if either of the following conditions are true: |
|
171 |
C is public. |
|
172 |
C and D are members of the same runtime package (§5.3). |
|
173 |
A field or method R is accessible to a class or interface D if and only |
|
174 |
if any of the following conditions is true: |
|
175 |
R is public. |
|
176 |
R is protected and is declared in a class C, and D is either a subclass |
|
177 |
of C or C itself. |
|
178 |
R is either protected or package private (that is, neither public nor |
|
179 |
protected nor private), and is declared by a class in the same runtime |
|
180 |
package as D. |
|
181 |
R is private and is declared in D. |
|
182 |
This discussion of access control omits a related restriction on the target |
|
183 |
of a protected field access or method invocation (the target must be of class |
|
184 |
D or a subtype of D). That requirement is checked as part of the verification |
|
185 |
process (§5.4.1); it is not part of link-time access control." |
|
186 |
||
187 |
(self checkPermissionsFrom: accessingJavaClass to: resolvedJavaClass) |
|
188 |
ifFalse: [ ^ false ]. |
|
189 |
aJavaMethod isPublic ifTrue: [ ^ true ]. |
|
190 |
((aJavaMethod isProtected |
|
191 |
and: [ aJavaMethod javaClass = resolvedJavaClass ]) and: |
|
192 |
[ (accessingJavaClass isSubclassOf: resolvedJavaClass) |
|
193 |
or: [ accessingJavaClass = resolvedJavaClass ] ]) |
|
194 |
ifTrue: [ ^ true ]. |
|
195 |
((aJavaMethod isPrivate not |
|
196 |
and: [ resolvedJavaClass javaPackage = accessingJavaClass javaPackage ]) |
|
197 |
and: [ resolvedJavaClass classLoader = accessingJavaClass classLoader ]) |
|
198 |
ifTrue: [ ^ true ]. |
|
199 |
(aJavaMethod isPrivate and: [ aJavaMethod javaClass = accessingJavaClass ]) |
|
200 |
ifTrue: [ ^ true ]. |
|
201 |
^ false. |
|
202 |
||
203 |
"Created: / 14-04-2011 / 14:19:33 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
204 |
"Modified: / 14-04-2011 / 15:42:15 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
205 |
! ! |
|
206 |
||
207 |
!JavaResolver methodsFor:'exceptions'! |
|
208 |
||
209 |
throwAbstractMethodError |
|
210 |
exceptionThrower throwAbstractMethodError. |
|
211 |
||
212 |
"Created: / 11-04-2011 / 20:19:42 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
213 |
"Modified: / 13-04-2011 / 14:07:46 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
214 |
! |
|
215 |
||
216 |
throwIllegalAccessError |
|
217 |
exceptionThrower throwIllegalAccessError. |
|
218 |
||
219 |
"Created: / 11-04-2011 / 19:39:16 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
220 |
"Modified: / 13-04-2011 / 23:06:58 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
221 |
! |
|
222 |
||
223 |
throwIncompatibleClassChangeError |
|
224 |
exceptionThrower throwIncompatibleClassChangeError. |
|
225 |
||
226 |
"Created: / 11-04-2011 / 20:02:01 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
227 |
"Modified: / 13-04-2011 / 14:07:54 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
228 |
! |
|
229 |
||
230 |
throwNoSuchFieldException |
|
231 |
exceptionThrower throwNoSuchFieldException. |
|
232 |
||
233 |
"Created: / 11-04-2011 / 21:35:02 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
234 |
"Modified: / 13-04-2011 / 14:07:57 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
235 |
! |
|
236 |
||
237 |
throwNoSuchMethodError |
|
238 |
exceptionThrower throwNoSuchMethodError. |
|
239 |
||
240 |
"Created: / 11-04-2011 / 20:19:41 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
241 |
"Modified: / 13-04-2011 / 14:08:01 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
242 |
! ! |
|
243 |
||
244 |
!JavaResolver methodsFor:'field resolving'! |
|
245 |
||
246 |
resolveFieldIndentifiedByRef: aJavaFieldRef |
|
247 |
| result class | |
|
248 |
||
249 |
self validateFieldRef: aJavaFieldRef. |
|
250 |
result := self lookupFieldIfAlreadyResolved: aJavaFieldRef. |
|
251 |
result ifNotNil: [ ^ result ]. |
|
252 |
class := aJavaFieldRef classRef resolve. |
|
253 |
class ifNil: [ self halt: 'should not happen - tell mh' ]. |
|
254 |
result := class lookupFieldByNameAndType: aJavaFieldRef nameAndType. |
|
255 |
result ifNil: [ self throwNoSuchFieldException ]. |
|
256 |
(self |
|
257 |
checkPermissionsForField: result |
|
258 |
from: aJavaFieldRef classRef owner |
|
259 |
to: class) ifFalse: [ self throwIllegalAccessError ]. |
|
260 |
^ result. |
|
261 |
||
262 |
" |
|
263 |
To resolve an unresolved symbolic reference from D to a field in a class |
|
264 |
or interface C, the symbolic reference to C given by the field reference |
|
265 |
must first be resolved (§5.4.3.1). Therefore, any exception that can be |
|
266 |
thrown as a result of failure of resolution of a class or interface reference |
|
267 |
can be thrown as a result of failure of field resolution. If the reference |
|
268 |
to C can be successfully resolved, an exception relating to the failure of |
|
269 |
resolution of the field reference itself can be thrown. |
|
270 |
When resolving a field reference, field resolution first attempts to look |
|
271 |
up the referenced field in C and its superclasses: |
|
272 |
||
273 |
If C declares a field with the name and descriptor specified by the field |
|
274 |
reference, field lookup succeeds. The declared field is the result of the |
|
275 |
field lookup. |
|
276 |
||
277 |
Otherwise, field lookup is applied recursively to the direct superinterfaces |
|
278 |
of the specified class or interface C. |
|
279 |
||
280 |
Otherwise, if C has a superclass S, field lookup is applied recursively to S. |
|
281 |
||
282 |
Otherwise, field lookup fails. |
|
283 |
If field lookup fails, field resolution throws a NoSuchFieldError. Otherwise, |
|
284 |
if field lookup succeeds but the referenced field is not accessible (§5.4.4) |
|
285 |
to D, field resolution throws an IllegalAccessError. |
|
286 |
Otherwise, let <E, L1> be the class or interface in which the referenced |
|
287 |
field is actually declared and let L2 be the defining loader of D. Let T be |
|
288 |
the name of the type of the referenced field. The Java virtual machine must |
|
289 |
impose the loading constraint that TL1=TL2(§5.3.4)." |
|
290 |
||
291 |
"Created: / 11-04-2011 / 21:15:20 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
292 |
"Modified: / 13-04-2011 / 11:57:24 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
293 |
! |
|
294 |
||
295 |
resolveStaticFieldIndentifiedByRef: aJavaFieldRef |
|
296 |
| result class | |
|
297 |
||
298 |
self validateFieldRef: aJavaFieldRef. |
|
299 |
result := self lookupFieldIfAlreadyResolved: aJavaFieldRef. |
|
300 |
result ifNotNil: [ ^ result ]. |
|
301 |
class := aJavaFieldRef classRef resolve. |
|
302 |
class ifNil: [ self halt: 'should not happen - tell mh' ]. |
|
303 |
result := class lookupStaticFieldByNameAndType: aJavaFieldRef nameAndType. |
|
304 |
result ifNil: [ self throwNoSuchFieldException ]. |
|
305 |
(self |
|
306 |
checkPermissionsForField: result |
|
307 |
from: aJavaFieldRef classRef owner |
|
308 |
to: class) ifFalse: [ self throwIllegalAccessError ]. |
|
309 |
^ result. |
|
310 |
||
311 |
" |
|
312 |
To resolve an unresolved symbolic reference from D to a field in a class |
|
313 |
or interface C, the symbolic reference to C given by the field reference |
|
314 |
must first be resolved (§5.4.3.1). Therefore, any exception that can be |
|
315 |
thrown as a result of failure of resolution of a class or interface reference |
|
316 |
can be thrown as a result of failure of field resolution. If the reference |
|
317 |
to C can be successfully resolved, an exception relating to the failure of |
|
318 |
resolution of the field reference itself can be thrown. |
|
319 |
When resolving a field reference, field resolution first attempts to look |
|
320 |
up the referenced field in C and its superclasses: |
|
321 |
||
322 |
If C declares a field with the name and descriptor specified by the field |
|
323 |
reference, field lookup succeeds. The declared field is the result of the |
|
324 |
field lookup. |
|
325 |
||
326 |
Otherwise, field lookup is applied recursively to the direct superinterfaces |
|
327 |
of the specified class or interface C. |
|
328 |
||
329 |
Otherwise, if C has a superclass S, field lookup is applied recursively to S. |
|
330 |
||
331 |
Otherwise, field lookup fails. |
|
332 |
If field lookup fails, field resolution throws a NoSuchFieldError. Otherwise, |
|
333 |
if field lookup succeeds but the referenced field is not accessible (§5.4.4) |
|
334 |
to D, field resolution throws an IllegalAccessError. |
|
335 |
Otherwise, let <E, L1> be the class or interface in which the referenced |
|
336 |
field is actually declared and let L2 be the defining loader of D. Let T be |
|
337 |
the name of the type of the referenced field. The Java virtual machine must |
|
338 |
impose the loading constraint that TL1=TL2(§5.3.4)." |
|
339 |
||
340 |
"Created: / 28-04-2011 / 22:31:34 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
341 |
! ! |
|
342 |
||
343 |
!JavaResolver methodsFor:'field resolving helpers'! |
|
344 |
||
345 |
checkPermissionsForField: aJavaField from: accessingJavaClass to: resolvedJavaClass |
|
346 |
"A class or interface C is accessible to a class or interface D if and only |
|
347 |
if either of the following conditions are true: |
|
348 |
C is public. |
|
349 |
C and D are members of the same runtime package (5.3). |
|
350 |
A field or method R is accessible to a class or interface D if and only |
|
351 |
if any of the following conditions is true: |
|
352 |
R is public. |
|
353 |
R is protected and is declared in a class C, and D is either a subclass of |
|
354 |
C or C itself. |
|
355 |
R is either protected or package private (that is, neither public nor protected |
|
356 |
nor private), and is declared by a class in the same runtime package as D. |
|
357 |
R is private and is declared in D. |
|
358 |
This discussion of access control omits a related restriction on the target of |
|
359 |
a protected field access or method invocation (the target must be of class D or |
|
360 |
a subtype of D). That requirement is checked as part of the verification process |
|
361 |
(5.4.1); it is not part of link-time access control." |
|
362 |
||
363 |
^ self |
|
364 |
checkPermissionsForMethodOrField: aJavaField from: accessingJavaClass |
|
365 |
to: resolvedJavaClass. |
|
366 |
||
367 |
"Created: / 11-04-2011 / 21:46:29 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
368 |
"Modified: / 14-04-2011 / 14:21:05 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
369 |
! |
|
370 |
||
371 |
lookupFieldIfAlreadyResolved: aJavaFieldRef |
|
372 |
^ nil. |
|
373 |
||
374 |
"Created: / 11-04-2011 / 21:16:45 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
375 |
"Modified: / 13-04-2011 / 11:57:13 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
376 |
! |
|
377 |
||
378 |
validateFieldRef: aJavaFieldRef |
|
379 |
aJavaFieldRef isJavaRef ifFalse:[self halt:'I expected Java Ref']. |
|
380 |
aJavaFieldRef isJavaFieldRef ifFalse:[self halt:'I expected Java Field Ref']. |
|
381 |
||
382 |
"Created: / 11-04-2011 / 21:16:44 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
383 |
! ! |
|
384 |
||
385 |
!JavaResolver methodsFor:'interface method resolving'! |
|
386 |
||
387 |
resolveInterfaceMethodIdentifiedByRef: aJavaInterfaceMethodRef |
|
388 |
| result class | |
|
389 |
||
390 |
self validateInterfaceMethodRef: aJavaInterfaceMethodRef. |
|
391 |
result := self |
|
392 |
lookupInterfaceMethodIfAlreadyResolved: aJavaInterfaceMethodRef. |
|
393 |
result ifNotNil: [ ^ result ]. |
|
394 |
class := aJavaInterfaceMethodRef classRef resolve. |
|
395 |
class ifNil: [ self halt: 'should not happen - tell mh' ]. |
|
396 |
class isInterface ifFalse:[self throwIncompatibleClassChangeError]. |
|
397 |
result := class lookupMethodByNameAndType: aJavaInterfaceMethodRef nameAndType. |
|
398 |
result ifNil:[self throwNoSuchMethodError]. |
|
399 |
^ result. |
|
400 |
" |
|
401 |
To resolve an unresolved symbolic reference from D to an interface method in an |
|
402 |
interface C, the symbolic reference to C given by the interface method reference is |
|
403 |
first resolved (§5.4.3.1). Therefore, any exceptions that can be thrown as a result |
|
404 |
of failure of resolution of an interface reference can be thrown as a result of |
|
405 |
failure of interface method resolution. If the reference to C can be successfully |
|
406 |
resolved, exceptions relating to the resolution of the interface method reference |
|
407 |
itself can be thrown. |
|
408 |
When resolving an interface method reference: |
|
409 |
||
410 |
If C is not an interface, interface method resolution throws an IncompatibleClassChangeError. |
|
411 |
Otherwise, if the referenced method does not have the same name and descriptor as |
|
412 |
a method in C or in one of the superinterfaces of C, or in class Object, interface |
|
413 |
method resolution throws a NoSuchMethodError. |
|
414 |
Otherwise, let <E, L1> be the interface in which the referenced interface method is |
|
415 |
actually declared and let L2 be the defining loader of D. Let T0 be the name of |
|
416 |
the type returned by the referenced method, and let T1, ..., Tn be the names of the |
|
417 |
argument types of the referenced method. The Java virtual machine must impose the |
|
418 |
loading constraints TiL1 = TiL2 for i = 0 to n (§5.3.4)." |
|
419 |
||
420 |
"Modified: / 13-04-2011 / 12:16:01 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
421 |
! ! |
|
422 |
||
423 |
!JavaResolver methodsFor:'interface method resolving helpers'! |
|
424 |
||
425 |
lookupInterfaceMethodIfAlreadyResolved: aJavaInterfaceMethodRef |
|
426 |
^ nil. |
|
427 |
||
428 |
"Created: / 13-04-2011 / 11:53:36 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
429 |
! |
|
430 |
||
431 |
validateInterfaceMethodRef: aJavaInterfaceMethodRef |
|
432 |
aJavaInterfaceMethodRef isJavaRef |
|
433 |
ifFalse: [ self halt: 'I expected JavaRef instance as an argument' ]. |
|
434 |
aJavaInterfaceMethodRef isJavaInterfaceMethodRef |
|
435 |
ifFalse: [ self halt: 'I expected JavaMethodRef instance as an argument' ]. |
|
436 |
||
437 |
"Created: / 13-04-2011 / 11:53:34 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
438 |
! ! |
|
439 |
||
440 |
!JavaResolver methodsFor:'method resolving'! |
|
441 |
||
442 |
resolveMethodIndentifiedByRef: aJavaMethodRef |
|
443 |
| result class | |
|
444 |
||
445 |
self validateMethodRef: aJavaMethodRef. |
|
446 |
result := self lookupMethodIfAlreadyResolved: aJavaMethodRef. |
|
447 |
result ifNotNil: [ ^ result ]. |
|
448 |
class := aJavaMethodRef classRef resolve. |
|
449 |
class ifNil: [ self halt: 'should not happen - tell mh' ]. |
|
450 |
" |
|
451 |
To resolve an unresolved symbolic reference from D to a method in |
|
452 |
a class C, the symbolic reference to C given by the method reference |
|
453 |
is first resolved (§5.4.3.1). Therefore, any exceptions that can be |
|
454 |
thrown due to resolution of a class reference can be thrown as a result |
|
455 |
of method resolution. If the reference to C can be successfully resolved, |
|
456 |
exceptions relating to the resolution of the method reference itself |
|
457 |
can be thrown. |
|
458 |
When resolving a method reference: |
|
459 |
||
460 |
Method resolution checks whether C is a class or an interface. |
|
461 |
If C is an interface, method resolution throws an IncompatibleClassChangeError." |
|
462 |
class isInterface ifTrue: [ self throwIncompatibleClassChangeError ]. |
|
463 |
"Method resolution attempts to look up the referenced method in C and its |
|
464 |
superclasses: |
|
465 |
If C declares a method with the name and descriptor specified by the method |
|
466 |
reference, method lookup succeeds. |
|
467 |
Otherwise, if C has a superclass, step 2 of method lookup is recursively |
|
468 |
invoked on the direct superclass of C. |
|
469 |
||
470 |
Otherwise, method lookup attempts to locate the referenced method in any of |
|
471 |
the superinterfaces of the specified class C. |
|
472 |
If any superinterface of C declares a method with the name and descriptor |
|
473 |
specified by the method reference, method lookup succeeds. |
|
474 |
Otherwise, method lookup fails. |
|
475 |
If method lookup fails, method resolution throws a NoSuchMethodError. If method |
|
476 |
lookup succeeds and the method is abstract, but C is not abstract, method resolution |
|
477 |
throws an AbstractMethodError. Otherwise, if the referenced method is not accessible |
|
478 |
(§5.4.4) to D, method resolution throws an IllegalAccessError." |
|
479 |
result := class lookupMethodByNameAndType: aJavaMethodRef nameAndType. |
|
480 |
result ifNil: [ self throwNoSuchMethodError ]. |
|
481 |
(result isAbstract and: [ class isAbstract not ]) |
|
482 |
ifTrue: [ self throwAbstractMethodError ]. |
|
483 |
(self |
|
484 |
checkPermissionsForMethod: result |
|
485 |
from: aJavaMethodRef classRef owner |
|
486 |
to: class) ifFalse: [ self throwIllegalAccessError ]. |
|
487 |
^ result. |
|
488 |
||
489 |
"Otherwise, let <E, L1> be the class or interface in which the referenced method is |
|
490 |
actually declared and let L2 be the defining loader of D. Let T0 be the name of |
|
491 |
the type returned by the referenced method, and let T1, ..., Tn be the names of |
|
492 |
the argument types of the referenced method. The Java virtual machine must impose |
|
493 |
the loading constraints TiL1=TiL2 for i = 0 to n (§5.3.4)." |
|
494 |
||
495 |
"Created: / 11-04-2011 / 19:45:34 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
496 |
"Modified: / 14-04-2011 / 00:01:34 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
497 |
! |
|
498 |
||
499 |
resolveStaticMethodIndentifiedByRef: aJavaMethodRef |
|
500 |
| result class | |
|
501 |
||
502 |
self validateMethodRef: aJavaMethodRef. |
|
503 |
result := self lookupMethodIfAlreadyResolved: aJavaMethodRef. |
|
504 |
result ifNotNil: [ ^ result ]. |
|
505 |
class := aJavaMethodRef classRef resolve. |
|
506 |
class ifNil: [ self halt: 'should not happen - tell mh' ]. |
|
507 |
" |
|
508 |
To resolve an unresolved symbolic reference from D to a method in |
|
509 |
a class C, the symbolic reference to C given by the method reference |
|
510 |
is first resolved (§5.4.3.1). Therefore, any exceptions that can be |
|
511 |
thrown due to resolution of a class reference can be thrown as a result |
|
512 |
of method resolution. If the reference to C can be successfully resolved, |
|
513 |
exceptions relating to the resolution of the method reference itself |
|
514 |
can be thrown. |
|
515 |
When resolving a method reference: |
|
516 |
||
517 |
Method resolution checks whether C is a class or an interface. |
|
518 |
If C is an interface, method resolution throws an IncompatibleClassChangeError." |
|
519 |
class isInterface ifTrue: [ self throwIncompatibleClassChangeError ]. |
|
520 |
"Method resolution attempts to look up the referenced method in C and its |
|
521 |
superclasses: |
|
522 |
If C declares a method with the name and descriptor specified by the method |
|
523 |
reference, method lookup succeeds. |
|
524 |
Otherwise, if C has a superclass, step 2 of method lookup is recursively |
|
525 |
invoked on the direct superclass of C. |
|
526 |
||
527 |
Otherwise, method lookup attempts to locate the referenced method in any of |
|
528 |
the superinterfaces of the specified class C. |
|
529 |
If any superinterface of C declares a method with the name and descriptor |
|
530 |
specified by the method reference, method lookup succeeds. |
|
531 |
Otherwise, method lookup fails. |
|
532 |
If method lookup fails, method resolution throws a NoSuchMethodError. If method |
|
533 |
lookup succeeds and the method is abstract, but C is not abstract, method resolution |
|
534 |
throws an AbstractMethodError. Otherwise, if the referenced method is not accessible |
|
535 |
(§5.4.4) to D, method resolution throws an IllegalAccessError." |
|
536 |
result := class lookupStaticMethodByNameAndType: aJavaMethodRef nameAndType. |
|
537 |
result ifNil: [ self throwNoSuchMethodError ]. |
|
538 |
(result isAbstract and: [ class isAbstract not ]) |
|
539 |
ifTrue: [ self throwAbstractMethodError ]. |
|
540 |
(self |
|
541 |
checkPermissionsForMethod: result |
|
542 |
from: aJavaMethodRef classRef owner |
|
543 |
to: class) ifFalse: [ self throwIllegalAccessError ]. |
|
544 |
^ result. |
|
545 |
||
546 |
"Otherwise, let <E, L1> be the class or interface in which the referenced method is |
|
547 |
actually declared and let L2 be the defining loader of D. Let T0 be the name of |
|
548 |
the type returned by the referenced method, and let T1, ..., Tn be the names of |
|
549 |
the argument types of the referenced method. The Java virtual machine must impose |
|
550 |
the loading constraints TiL1=TiL2 for i = 0 to n (§5.3.4)." |
|
551 |
||
552 |
"Created: / 28-04-2011 / 22:50:04 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
553 |
! ! |
|
554 |
||
555 |
!JavaResolver methodsFor:'method resolving helpers'! |
|
556 |
||
557 |
checkPermissionsForMethod: aJavaMethod from: accessingJavaClass to: resolvedJavaClass |
|
558 |
"A class or interface C is accessible to a class or interface D if |
|
559 |
and only if either of the following conditions are true: |
|
560 |
C is public. |
|
561 |
C and D are members of the same runtime package (§5.3). |
|
562 |
A field or method R is accessible to a class or interface D if and only |
|
563 |
if any of the following conditions is true: |
|
564 |
R is public. |
|
565 |
R is protected and is declared in a class C, and D is either a subclass |
|
566 |
of C or C itself. |
|
567 |
R is either protected or package private (that is, neither public nor |
|
568 |
protected nor private), and is declared by a class in the same runtime |
|
569 |
package as D. |
|
570 |
R is private and is declared in D. |
|
571 |
This discussion of access control omits a related restriction on the target |
|
572 |
of a protected field access or method invocation (the target must be of class |
|
573 |
D or a subtype of D). That requirement is checked as part of the verification |
|
574 |
process (§5.4.1); it is not part of link-time access control." |
|
575 |
||
576 |
^ self checkPermissionsForMethodOrField: aJavaMethod from: accessingJavaClass to: resolvedJavaClass. |
|
577 |
||
578 |
"Created: / 11-04-2011 / 20:20:12 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
579 |
"Modified: / 14-04-2011 / 14:20:27 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
580 |
! |
|
581 |
||
582 |
lookupMethodIfAlreadyResolved: aJavaMethodRef |
|
583 |
^ nil. |
|
584 |
||
585 |
"Created: / 11-04-2011 / 19:50:38 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
586 |
"Modified: / 13-04-2011 / 11:57:00 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
587 |
! |
|
588 |
||
589 |
validateMethodRef: aJavaMethodRef |
|
590 |
aJavaMethodRef isJavaRef ifFalse:[self halt: 'I expected JavaRef instance as an argument']. |
|
591 |
aJavaMethodRef isJavaMethodRef ifFalse: [self halt: 'I expected JavaMethodRef instance as an argument']. |
|
592 |
||
593 |
"Created: / 11-04-2011 / 19:47:25 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
594 |
! ! |
|
595 |
||
758 | 596 |
!JavaResolver class methodsFor:'documentation'! |
597 |
||
598 |
version_SVN |
|
599 |
^ '$Id$' |
|
600 |
! ! |
|
601 |
||
602 |
JavaResolver initialize! |