author | Claus Gittinger <cg@exept.de> |
Thu, 24 Nov 2011 11:26:08 +0100 | |
changeset 2164 | c4682bb0d828 |
parent 2156 | a8c5720d1d05 |
child 2231 | 8655662e7cc6 |
permissions | -rw-r--r-- |
749 | 1 |
" |
2 |
COPYRIGHT (c) 1996-2011 by Claus Gittinger |
|
3 |
COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko |
|
4 |
SWING Research Group, Czech Technical University in Prague |
|
5 |
||
6 |
Parts of the code written by Claus Gittinger are under following |
|
7 |
license: |
|
8 |
||
9 |
This software is furnished under a license and may be used |
|
10 |
only in accordance with the terms of that license and with the |
|
11 |
inclusion of the above copyright notice. This software may not |
|
12 |
be provided or otherwise made available to, or used by, any |
|
13 |
other person. No title to or ownership of the software is |
|
14 |
hereby transferred. |
|
15 |
||
16 |
Parts of the code written at SWING Reasearch Group [1] are MIT licensed: |
|
17 |
||
18 |
Permission is hereby granted, free of charge, to any person |
|
19 |
obtaining a copy of this software and associated documentation |
|
20 |
files (the 'Software'), to deal in the Software without |
|
21 |
restriction, including without limitation the rights to use, |
|
22 |
copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
23 |
copies of the Software, and to permit persons to whom the |
|
24 |
Software is furnished to do so, subject to the following |
|
25 |
conditions: |
|
26 |
||
27 |
The above copyright notice and this permission notice shall be |
|
28 |
included in all copies or substantial portions of the Software. |
|
29 |
||
30 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
|
31 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
32 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
33 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
34 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
35 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
36 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
37 |
OTHER DEALINGS IN THE SOFTWARE. |
|
38 |
||
39 |
[1] Code written at SWING Research Group contain a signature |
|
40 |
of one of the above copright owners. |
|
41 |
" |
|
42 |
"{ Package: 'stx:libjava' }" |
|
43 |
||
44 |
Object subclass:#JavaResolver |
|
45 |
instanceVariableNames:'exceptionThrower' |
|
46 |
classVariableNames:'uniqueInstance' |
|
47 |
poolDictionaries:'' |
|
48 |
category:'Languages-Java-Reader-Support-new' |
|
49 |
! |
|
50 |
||
51 |
!JavaResolver class methodsFor:'documentation'! |
|
52 |
||
53 |
copyright |
|
54 |
" |
|
55 |
COPYRIGHT (c) 1996-2011 by Claus Gittinger |
|
56 |
COPYRIGHT (c) 2010-2011 by Jan Vrany, Jan Kurs and Marcel Hlopko |
|
57 |
SWING Research Group, Czech Technical University in Prague |
|
58 |
||
59 |
Parts of the code written by Claus Gittinger are under following |
|
60 |
license: |
|
61 |
||
62 |
This software is furnished under a license and may be used |
|
63 |
only in accordance with the terms of that license and with the |
|
64 |
inclusion of the above copyright notice. This software may not |
|
65 |
be provided or otherwise made available to, or used by, any |
|
66 |
other person. No title to or ownership of the software is |
|
67 |
hereby transferred. |
|
68 |
||
69 |
Parts of the code written at SWING Reasearch Group [1] are MIT licensed: |
|
70 |
||
71 |
Permission is hereby granted, free of charge, to any person |
|
72 |
obtaining a copy of this software and associated documentation |
|
73 |
files (the 'Software'), to deal in the Software without |
|
74 |
restriction, including without limitation the rights to use, |
|
75 |
copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
76 |
copies of the Software, and to permit persons to whom the |
|
77 |
Software is furnished to do so, subject to the following |
|
78 |
conditions: |
|
79 |
||
80 |
The above copyright notice and this permission notice shall be |
|
81 |
included in all copies or substantial portions of the Software. |
|
82 |
||
83 |
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
|
84 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
|
85 |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
86 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|
87 |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|
88 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
89 |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
|
90 |
OTHER DEALINGS IN THE SOFTWARE. |
|
91 |
||
92 |
[1] Code written at SWING Research Group contain a signature |
|
93 |
of one of the above copright owners. |
|
94 |
||
95 |
" |
|
96 |
! ! |
|
97 |
||
98 |
!JavaResolver class methodsFor:'initialization'! |
|
99 |
||
100 |
initialize |
|
101 |
uniqueInstance := JavaResolver new. |
|
102 |
uniqueInstance exceptionThrower: JavaVM. |
|
103 |
||
104 |
"/has methods at: and at: put: |
|
105 |
"/uniqueInstance resolvedClasses: Java. |
|
106 |
||
107 |
"Modified: / 10-04-2011 / 10:23:43 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
108 |
"Modified: / 13-04-2011 / 14:07:35 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
109 |
! ! |
|
110 |
||
111 |
!JavaResolver class methodsFor:'instance creation'! |
|
112 |
||
113 |
uniqueInstance |
|
114 |
^uniqueInstance. |
|
115 |
||
116 |
"Created: / 08-04-2011 / 17:36:37 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
117 |
"Modified: / 11-04-2011 / 19:06:49 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
118 |
! ! |
|
119 |
||
120 |
!JavaResolver methodsFor:'accessing'! |
|
121 |
||
122 |
exceptionThrower |
|
123 |
^ exceptionThrower |
|
124 |
! |
|
125 |
||
126 |
exceptionThrower:something |
|
127 |
exceptionThrower := something. |
|
128 |
! ! |
|
129 |
||
130 |
!JavaResolver methodsFor:'class resolving'! |
|
131 |
||
132 |
checkIfPrimitiveArrayRef: aJavaClassRef andWrap: result |
|
133 |
||
134 |
^ (JavaDescriptor readFromString: aJavaClassRef name) javaClass. |
|
135 |
||
136 |
"Created: / 23-05-2011 / 21:12:04 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
137 |
! |
|
138 |
||
139 |
resolveClassIndentifiedByRef: aJavaClassRef init: doInit |
|
140 |
| classLoader result | |
|
141 |
||
142 |
self validateClassRef: aJavaClassRef. |
|
143 |
result := self lookupClassIfAlreadyResolved: aJavaClassRef javaClassName. |
|
144 |
"/ result notNil ifTrue:[ |
|
145 |
"/ "/wrap result with array(s) if needed and return it |
|
146 |
"/ "/FIXME: Marcel, can you have a look? Is that correct? |
|
147 |
"/ ^self checkIfArrayRef: aJavaClassRef andWrap: result |
|
148 |
"/ ]. |
|
149 |
" |
|
150 |
To resolve an unresolved symbolic reference from D to a class or interface C denoted by N, |
|
151 |
the following steps are performed: |
|
152 |
The defining class loader of D is used to create a class or interface denoted by N. |
|
153 |
This class or interface is C. Any exception that can be thrown as a result of failure |
|
154 |
of class or interface creation can thus be thrown as a result of failure of class and |
|
155 |
interface resolution. The details of the process are given in Section 5.3. |
|
156 |
" |
|
157 |
result isNil ifTrue:[ |
|
158 |
classLoader := aJavaClassRef classLoader. |
|
159 |
classLoader isNil ifTrue: [ |
|
160 |
result := self loadUnresolvedClass: aJavaClassRef |
|
161 |
] ifFalse: [ |
|
162 |
result := self loadUnresolvedClass: aJavaClassRef |
|
163 |
withJavaClassLoader: classLoader. |
|
164 |
]. |
|
165 |
]. |
|
166 |
result ifNil: [ self breakPoint: #mh ]. |
|
167 |
||
168 |
result isJavaPrimitiveType ifTrue: [ |
|
169 |
^ self checkIfPrimitiveArrayRef: aJavaClassRef andWrap: result |
|
170 |
]. |
|
171 |
doInit ifTrue:[result classInit]. |
|
172 |
" |
|
173 |
If C is an array class and its element type is a reference type, then the symbolic reference |
|
174 |
to the class or interface representing the element type is resolved by invoking the algorithm |
|
175 |
in Section 5.4.3.1 recursively." |
|
176 |
result := self checkIfArrayRef: aJavaClassRef andWrap: result. |
|
177 |
"Finally, access permissions to C are checked: |
|
178 |
If C is not accessible (5.4.4) to D, class or interface resolution throws an IllegalAccessError. |
|
179 |
This condition can occur, for example, if C is a class that was originally declared to be |
|
180 |
public but was changed to be non-public after D was compiled. |
|
181 |
||
182 |
If steps 1 and 2 succeed but step 3 fails, C is still valid and usable. Nevertheless, resolution |
|
183 |
fails, and D is prohibited from accessing C." |
|
184 |
||
185 |
||
186 |
"JV@2011-08-10: HACK FOR SAXON DEMO!!!!!!" |
|
187 |
"(self checkPermissionsFrom: aJavaClassRef owner to: result)"true |
|
188 |
ifTrue: [ ^ result ] |
|
189 |
ifFalse: [ self throwIllegalAccessError ]. |
|
190 |
||
191 |
"Created: / 11-04-2011 / 19:07:19 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
192 |
"Modified: / 23-05-2011 / 21:11:32 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
193 |
"Created: / 12-08-2011 / 22:19:51 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
194 |
! ! |
|
195 |
||
196 |
!JavaResolver methodsFor:'class resolving helpers'! |
|
197 |
||
198 |
checkIfArrayRef: aJavaClassRef andWrap: result |
|
199 |
"/wrap result with array(s) if needed and return it |
|
200 |
^ aJavaClassRef isJavaArrayClassRef |
|
201 |
ifFalse: [ result ] |
|
202 |
ifTrue: [ self wrapJavaClass: result withArrayAsSpecifiedIn: aJavaClassRef ] |
|
203 |
||
204 |
"Modified: / 11-04-2011 / 19:31:43 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
205 |
! |
|
206 |
||
207 |
checkPermissionsFrom: refOwner to: resolvedClass |
|
208 |
" |
|
209 |
A class or interface C is accessible to a class or interface D if and only if either of the following conditions are true: |
|
210 |
C is public. |
|
211 |
C and D are members of the same runtime package (§5.3). |
|
212 |
Finally, access permissions to C are checked: |
|
213 |
If C is not accessible (§5.4.4) to D, class or interface resolution throws an IllegalAccessError. |
|
214 |
This condition can occur, for example, if C is a class that was originally declared to be |
|
215 |
public but was changed to be non-public after D was compiled. |
|
216 |
At run time, a class or interface is determined not by its name alone, |
|
217 |
but by a pair: its fully qualified name and its defining class loader. |
|
218 |
Each such class or interface belongs to a single runtime package. The runtime |
|
219 |
package of a class or interface is determined by the package name and |
|
220 |
defining class loader of the class or interface." |
|
221 |
||
222 |
resolvedClass isPublic ifTrue: [ ^ true ]. |
|
223 |
resolvedClass classLoader = refOwner classLoader ifFalse: [ ^ false ]. |
|
224 |
^ refOwner javaPackage = resolvedClass javaPackage. |
|
225 |
||
226 |
"Created: / 11-04-2011 / 19:35:21 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
227 |
"Modified: / 13-04-2011 / 23:13:39 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
228 |
! |
|
229 |
||
230 |
loadUnresolvedClass: aJavaClassRef |
|
231 |
| nm | |
|
232 |
||
233 |
nm := aJavaClassRef javaClassName. |
|
234 |
nm size == 1 |
|
235 |
and: [ JavaDescriptor baseTypes at: nm first ifPresent: [:cls | ^ cls ] ]. |
|
236 |
^ Java classForName: aJavaClassRef javaClassName. |
|
237 |
||
238 |
"Created: / 11-04-2011 / 19:27:10 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
239 |
"Modified: / 22-05-2011 / 17:58:19 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
240 |
"Modified: / 23-05-2011 / 21:06:25 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
241 |
! |
|
242 |
||
243 |
loadUnresolvedClass: aJavaClassRef withJavaClassLoader: classLoader |
|
244 |
| jClass clc | |
|
245 |
||
246 |
"JV@2011-08-12: HACK HACK HACK!!!!!!" |
|
247 |
(clc := Java at:'java.lang.ClassLoader') notNil ifTrue:[ |
|
248 |
(classLoader == (clc instVarNamed: #scl)) ifTrue:[ |
|
249 |
^self loadUnresolvedClass: aJavaClassRef |
|
250 |
] |
|
251 |
]. |
|
252 |
||
253 |
jClass := classLoader |
|
254 |
perform: #'loadClass(Ljava/lang/String;)Ljava/lang/Class;' |
|
255 |
with: (Java as_String: (aJavaClassRef name)). |
|
256 |
^JavaVM reflection classForJavaClassObject: jClass. |
|
257 |
||
258 |
"Modified: / 18-05-2011 / 14:52:39 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
259 |
"Modified: / 12-08-2011 / 09:14:58 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
260 |
! |
|
261 |
||
262 |
lookupClassIfAlreadyResolved: javaClassName |
|
263 |
||
264 |
^Java at: javaClassName |
|
265 |
||
266 |
"Modified: / 13-04-2011 / 14:05:25 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
267 |
"Modified: / 06-08-2011 / 16:44:03 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
268 |
! |
|
269 |
||
270 |
validateClassRef: aJavaClassRef |
|
271 |
self assert: aJavaClassRef isJavaRef. |
|
272 |
self assert: aJavaClassRef isJavaClassRef . |
|
273 |
||
274 |
"Modified: / 23-05-2011 / 21:04:00 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
275 |
! |
|
276 |
||
277 |
wrapJavaClass:aJavaClass withArrayAsSpecifiedIn:aJavaClassRef |
|
278 |
"wraps aJavaClass with as many array dimensions as needed (zero means no array)" |
|
279 |
||
280 |
|arrayClass| |
|
281 |
||
282 |
aJavaClassRef isJavaArrayClassRef ifFalse:[ ^ aJavaClass ]. |
|
283 |
aJavaClassRef arrayDimensions timesRepeat: |
|
284 |
[ arrayClass := aJavaClass. |
|
285 |
arrayClass := arrayClass javaArrayClass. ]. |
|
286 |
^ arrayClass. |
|
287 |
||
288 |
"Created: / 08-04-2011 / 18:40:00 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
289 |
"Modified: / 22-05-2011 / 17:48:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
290 |
"Modified: / 23-05-2011 / 21:06:41 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
291 |
! ! |
|
292 |
||
293 |
!JavaResolver methodsFor:'common helpers'! |
|
294 |
||
295 |
checkPermissionsForMethodOrField: aJavaMethodOrField from: accessingJavaClass to: resolvedJavaClass |
|
296 |
"A class or interface C is accessible to a class or interface D if |
|
297 |
and only if either of the following conditions are true: |
|
298 |
C is public. |
|
299 |
C and D are members of the same runtime package (§5.3). |
|
300 |
A field or method R is accessible to a class or interface D if and only |
|
301 |
if any of the following conditions is true: |
|
302 |
R is public. |
|
303 |
R is protected and is declared in a class C, and D is either a subclass |
|
304 |
of C or C itself. |
|
305 |
R is either protected or package private (that is, neither public nor |
|
306 |
protected nor private), and is declared by a class in the same runtime |
|
307 |
package as D. |
|
308 |
R is private and is declared in D. |
|
309 |
This discussion of access control omits a related restriction on the target |
|
310 |
of a protected field access or method invocation (the target must be of class |
|
311 |
D or a subtype of D). That requirement is checked as part of the verification |
|
312 |
process (§5.4.1); it is not part of link-time access control." |
|
313 |
||
314 |
(self checkPermissionsFrom: accessingJavaClass to: resolvedJavaClass) ifFalse: [ |
|
315 |
^ false |
|
316 |
]. |
|
317 |
aJavaMethodOrField isPublic ifTrue: [ |
|
318 |
^ true |
|
319 |
]. |
|
320 |
((aJavaMethodOrField isProtected |
|
321 |
and: [ |
|
322 |
resolvedJavaClass javaComponentClass |
|
323 |
equalsOrIsSubclassOf: aJavaMethodOrField javaClass |
|
324 |
]) |
|
325 |
and: [ |
|
326 |
accessingJavaClass javaComponentClass |
|
327 |
equalsOrIsSubclassOf: aJavaMethodOrField javaClass |
|
328 |
]) |
|
329 |
ifTrue: [ ^ true ]. |
|
330 |
((aJavaMethodOrField isPrivate not |
|
331 |
and: [ resolvedJavaClass javaPackage = accessingJavaClass javaPackage ]) |
|
332 |
and: [ resolvedJavaClass classLoader = accessingJavaClass classLoader ]) |
|
333 |
ifTrue: [ ^ true ]. |
|
334 |
(aJavaMethodOrField isPrivate |
|
335 |
and: [ aJavaMethodOrField javaClass name = accessingJavaClass name ]) |
|
336 |
ifTrue: [ ^ true ]. |
|
337 |
^ false. |
|
338 |
||
339 |
"Created: / 14-04-2011 / 14:19:33 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
340 |
"Modified: / 04-06-2011 / 20:49:30 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
341 |
! ! |
|
342 |
||
343 |
!JavaResolver methodsFor:'exceptions'! |
|
344 |
||
345 |
throwAbstractMethodError |
|
346 |
exceptionThrower throwAbstractMethodError. |
|
347 |
||
348 |
"Created: / 11-04-2011 / 20:19:42 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
349 |
"Modified: / 13-04-2011 / 14:07:46 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
350 |
! |
|
351 |
||
352 |
throwIllegalAccessError |
|
353 |
exceptionThrower throwIllegalAccessError. |
|
354 |
||
355 |
"Created: / 11-04-2011 / 19:39:16 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
356 |
"Modified: / 13-04-2011 / 23:06:58 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
357 |
! |
|
358 |
||
359 |
throwIncompatibleClassChangeError |
|
360 |
exceptionThrower throwIncompatibleClassChangeError. |
|
361 |
||
362 |
"Created: / 11-04-2011 / 20:02:01 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
363 |
"Modified: / 13-04-2011 / 14:07:54 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
364 |
! |
|
365 |
||
366 |
throwNoSuchFieldException |
|
367 |
exceptionThrower throwNoSuchFieldException. |
|
368 |
||
369 |
"Created: / 11-04-2011 / 21:35:02 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
370 |
"Modified: / 13-04-2011 / 14:07:57 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
371 |
! |
|
372 |
||
373 |
throwNoSuchMethodError |
|
374 |
exceptionThrower throwNoSuchMethodError. |
|
375 |
||
376 |
"Created: / 11-04-2011 / 20:19:41 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
377 |
"Modified: / 13-04-2011 / 14:08:01 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
378 |
! ! |
|
379 |
||
380 |
!JavaResolver methodsFor:'field resolving'! |
|
381 |
||
382 |
resolveFieldIndentifiedByRef: aJavaFieldRef |
|
383 |
| result class | |
|
384 |
||
385 |
self validateFieldRef: aJavaFieldRef. |
|
386 |
result := self lookupFieldIfAlreadyResolved: aJavaFieldRef. |
|
387 |
result ifNotNil: [ ^ result ]. |
|
388 |
class := aJavaFieldRef classRef resolve. |
|
389 |
class ifNil: [ self halt: 'should not happen - tell mh' ]. |
|
390 |
result := class lookupFieldByNameAndType: aJavaFieldRef nameAndType. |
|
391 |
result ifNil: [ self throwNoSuchFieldException ]. |
|
392 |
(self |
|
393 |
checkPermissionsForField: result |
|
394 |
from: aJavaFieldRef classRef owner |
|
395 |
to: class) ifFalse: [ self throwIllegalAccessError ]. |
|
396 |
^ result. |
|
397 |
||
398 |
" |
|
399 |
To resolve an unresolved symbolic reference from D to a field in a class |
|
400 |
or interface C, the symbolic reference to C given by the field reference |
|
401 |
must first be resolved (§5.4.3.1). Therefore, any exception that can be |
|
402 |
thrown as a result of failure of resolution of a class or interface reference |
|
403 |
can be thrown as a result of failure of field resolution. If the reference |
|
404 |
to C can be successfully resolved, an exception relating to the failure of |
|
405 |
resolution of the field reference itself can be thrown. |
|
406 |
When resolving a field reference, field resolution first attempts to look |
|
407 |
up the referenced field in C and its superclasses: |
|
408 |
||
409 |
If C declares a field with the name and descriptor specified by the field |
|
410 |
reference, field lookup succeeds. The declared field is the result of the |
|
411 |
field lookup. |
|
412 |
||
413 |
Otherwise, field lookup is applied recursively to the direct superinterfaces |
|
414 |
of the specified class or interface C. |
|
415 |
||
416 |
Otherwise, if C has a superclass S, field lookup is applied recursively to S. |
|
417 |
||
418 |
Otherwise, field lookup fails. |
|
419 |
If field lookup fails, field resolution throws a NoSuchFieldError. Otherwise, |
|
420 |
if field lookup succeeds but the referenced field is not accessible (§5.4.4) |
|
421 |
to D, field resolution throws an IllegalAccessError. |
|
422 |
Otherwise, let <E, L1> be the class or interface in which the referenced |
|
423 |
field is actually declared and let L2 be the defining loader of D. Let T be |
|
424 |
the name of the type of the referenced field. The Java virtual machine must |
|
425 |
impose the loading constraint that TL1=TL2(§5.3.4)." |
|
426 |
||
427 |
"Created: / 11-04-2011 / 21:15:20 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
428 |
"Modified: / 13-04-2011 / 11:57:24 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
429 |
! |
|
430 |
||
431 |
resolveStaticFieldIndentifiedByRef: aJavaFieldRef |
|
432 |
| result class | |
|
433 |
||
434 |
self validateFieldRef: aJavaFieldRef. |
|
435 |
result := self lookupFieldIfAlreadyResolved: aJavaFieldRef. |
|
436 |
result ifNotNil: [ ^ result ]. |
|
437 |
class := aJavaFieldRef classRef resolve. |
|
438 |
class ifNil: [ self halt: 'should not happen - tell mh' ]. |
|
439 |
result := class lookupStaticFieldByNameAndType: aJavaFieldRef nameAndType. |
|
440 |
result ifNil: [ self throwNoSuchFieldException ]. |
|
441 |
(self |
|
442 |
checkPermissionsForField: result |
|
443 |
from: aJavaFieldRef classRef owner |
|
444 |
to: class) ifFalse: [ self throwIllegalAccessError ]. |
|
445 |
^ result. |
|
446 |
||
447 |
" |
|
448 |
To resolve an unresolved symbolic reference from D to a field in a class |
|
449 |
or interface C, the symbolic reference to C given by the field reference |
|
450 |
must first be resolved (§5.4.3.1). Therefore, any exception that can be |
|
451 |
thrown as a result of failure of resolution of a class or interface reference |
|
452 |
can be thrown as a result of failure of field resolution. If the reference |
|
453 |
to C can be successfully resolved, an exception relating to the failure of |
|
454 |
resolution of the field reference itself can be thrown. |
|
455 |
When resolving a field reference, field resolution first attempts to look |
|
456 |
up the referenced field in C and its superclasses: |
|
457 |
||
458 |
If C declares a field with the name and descriptor specified by the field |
|
459 |
reference, field lookup succeeds. The declared field is the result of the |
|
460 |
field lookup. |
|
461 |
||
462 |
Otherwise, field lookup is applied recursively to the direct superinterfaces |
|
463 |
of the specified class or interface C. |
|
464 |
||
465 |
Otherwise, if C has a superclass S, field lookup is applied recursively to S. |
|
466 |
||
467 |
Otherwise, field lookup fails. |
|
468 |
If field lookup fails, field resolution throws a NoSuchFieldError. Otherwise, |
|
469 |
if field lookup succeeds but the referenced field is not accessible (§5.4.4) |
|
470 |
to D, field resolution throws an IllegalAccessError. |
|
471 |
Otherwise, let <E, L1> be the class or interface in which the referenced |
|
472 |
field is actually declared and let L2 be the defining loader of D. Let T be |
|
473 |
the name of the type of the referenced field. The Java virtual machine must |
|
474 |
impose the loading constraint that TL1=TL2(§5.3.4)." |
|
475 |
||
476 |
"Created: / 28-04-2011 / 22:31:34 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
477 |
! ! |
|
478 |
||
479 |
!JavaResolver methodsFor:'field resolving helpers'! |
|
480 |
||
481 |
checkPermissionsForField: aJavaField from: accessingJavaClass to: resolvedJavaClass |
|
482 |
"A class or interface C is accessible to a class or interface D if and only |
|
483 |
if either of the following conditions are true: |
|
484 |
C is public. |
|
485 |
C and D are members of the same runtime package (5.3). |
|
486 |
A field or method R is accessible to a class or interface D if and only |
|
487 |
if any of the following conditions is true: |
|
488 |
R is public. |
|
489 |
R is protected and is declared in a class C, and D is either a subclass of |
|
490 |
C or C itself. |
|
491 |
R is either protected or package private (that is, neither public nor protected |
|
492 |
nor private), and is declared by a class in the same runtime package as D. |
|
493 |
R is private and is declared in D. |
|
494 |
This discussion of access control omits a related restriction on the target of |
|
495 |
a protected field access or method invocation (the target must be of class D or |
|
496 |
a subtype of D). That requirement is checked as part of the verification process |
|
497 |
(5.4.1); it is not part of link-time access control." |
|
498 |
||
499 |
^ self |
|
500 |
checkPermissionsForMethodOrField: aJavaField from: accessingJavaClass |
|
501 |
to: resolvedJavaClass. |
|
502 |
||
503 |
"Created: / 11-04-2011 / 21:46:29 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
504 |
"Modified: / 14-04-2011 / 14:21:05 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
505 |
! |
|
506 |
||
507 |
lookupFieldIfAlreadyResolved: aJavaFieldRef |
|
508 |
^ nil. |
|
509 |
||
510 |
"Created: / 11-04-2011 / 21:16:45 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
511 |
"Modified: / 13-04-2011 / 11:57:13 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
512 |
! |
|
513 |
||
514 |
validateFieldRef: aJavaFieldRef |
|
515 |
aJavaFieldRef isJavaRef ifFalse:[self halt:'I expected Java Ref']. |
|
516 |
aJavaFieldRef isJavaFieldRef ifFalse:[self halt:'I expected Java Field Ref']. |
|
517 |
||
518 |
"Created: / 11-04-2011 / 21:16:44 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
519 |
! ! |
|
520 |
||
521 |
!JavaResolver methodsFor:'interface method resolving'! |
|
522 |
||
523 |
resolveInterfaceMethodIdentifiedByRef: aJavaInterfaceMethodRef |
|
524 |
| result class | |
|
525 |
||
526 |
self validateInterfaceMethodRef: aJavaInterfaceMethodRef. |
|
527 |
result := self |
|
528 |
lookupInterfaceMethodIfAlreadyResolved: aJavaInterfaceMethodRef. |
|
529 |
result ifNotNil: [ ^ result ]. |
|
530 |
class := aJavaInterfaceMethodRef classRef resolve. |
|
531 |
class ifNil: [ self halt: 'should not happen - tell mh' ]. |
|
532 |
class isInterface ifFalse: [ self throwIncompatibleClassChangeError ]. |
|
533 |
result := class |
|
534 |
lookupMethodByNameAndType: aJavaInterfaceMethodRef nameAndType. |
|
535 |
result ifNil: [ self throwNoSuchMethodError ]. |
|
536 |
(self |
|
537 |
checkPermissionsForMethod: result |
|
538 |
from: aJavaInterfaceMethodRef classRef owner |
|
539 |
to: class) ifFalse: [ self throwIllegalAccessError ]. |
|
540 |
^ result. |
|
541 |
" |
|
542 |
To resolve an unresolved symbolic reference from D to an interface method in an |
|
543 |
interface C, the symbolic reference to C given by the interface method reference is |
|
544 |
first resolved (§5.4.3.1). Therefore, any exceptions that can be thrown as a result |
|
545 |
of failure of resolution of an interface reference can be thrown as a result of |
|
546 |
failure of interface method resolution. If the reference to C can be successfully |
|
547 |
resolved, exceptions relating to the resolution of the interface method reference |
|
548 |
itself can be thrown. |
|
549 |
When resolving an interface method reference: |
|
550 |
||
551 |
If C is not an interface, interface method resolution throws an IncompatibleClassChangeError.""Otherwise, if the referenced method does not have the same name and descriptor as |
|
552 |
a method in C or in one of the superinterfaces of C, or in class Object, interface |
|
553 |
method resolution throws a NoSuchMethodError. |
|
554 |
Otherwise, let <E, L1> be the interface in which the referenced interface method is |
|
555 |
actually declared and let L2 be the defining loader of D. Let T0 be the name of |
|
556 |
the type returned by the referenced method, and let T1, ..., Tn be the names of the |
|
557 |
argument types of the referenced method. The Java virtual machine must impose the |
|
558 |
loading constraints TiL1 = TiL2 for i = 0 to n (§5.3.4)." |
|
559 |
||
560 |
"Modified: / 23-05-2011 / 17:56:24 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
561 |
! ! |
|
562 |
||
563 |
!JavaResolver methodsFor:'interface method resolving helpers'! |
|
564 |
||
565 |
lookupInterfaceMethodIfAlreadyResolved: aJavaInterfaceMethodRef |
|
566 |
^ nil. |
|
567 |
||
568 |
"Created: / 13-04-2011 / 11:53:36 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
569 |
! |
|
570 |
||
571 |
validateInterfaceMethodRef: aJavaInterfaceMethodRef |
|
572 |
aJavaInterfaceMethodRef isJavaRef |
|
573 |
ifFalse: [ self halt: 'I expected JavaRef instance as an argument' ]. |
|
574 |
aJavaInterfaceMethodRef isJavaInterfaceMethodRef |
|
575 |
ifFalse: [ self halt: 'I expected JavaMethodRef instance as an argument' ]. |
|
576 |
||
577 |
"Created: / 13-04-2011 / 11:53:34 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
578 |
! ! |
|
579 |
||
580 |
!JavaResolver methodsFor:'method resolving'! |
|
581 |
||
582 |
resolveMethodIndentifiedByRef: aJavaMethodRef |
|
583 |
| result class | |
|
584 |
||
585 |
self validateMethodRef: aJavaMethodRef. |
|
586 |
result := self lookupMethodIfAlreadyResolved: aJavaMethodRef. |
|
587 |
result ifNotNil: [ ^ result ]. |
|
588 |
class := aJavaMethodRef classRef resolve. |
|
589 |
class ifNil: [ self error: 'should not happen - tell mh' ]. |
|
590 |
"Array types responds to all method of class java.lang.Object" |
|
591 |
class isJavaArrayClass ifTrue:[ |
|
592 |
class := Java classForName:'java.lang.Object'. |
|
593 |
]. |
|
594 |
||
595 |
||
596 |
" |
|
597 |
To resolve an unresolved symbolic reference from D to a method in |
|
598 |
a class C, the symbolic reference to C given by the method reference |
|
599 |
is first resolved (§5.4.3.1). Therefore, any exceptions that can be |
|
600 |
thrown due to resolution of a class reference can be thrown as a result |
|
601 |
of method resolution. If the reference to C can be successfully resolved, |
|
602 |
exceptions relating to the resolution of the method reference itself |
|
603 |
can be thrown. |
|
604 |
When resolving a method reference: |
|
605 |
||
606 |
Method resolution checks whether C is a class or an interface. |
|
607 |
If C is an interface, method resolution throws an IncompatibleClassChangeError." |
|
608 |
class isInterface ifTrue: [ self throwIncompatibleClassChangeError ]. |
|
609 |
"Method resolution attempts to look up the referenced method in C and its |
|
610 |
superclasses: |
|
611 |
If C declares a method with the name and descriptor specified by the method |
|
612 |
reference, method lookup succeeds. |
|
613 |
Otherwise, if C has a superclass, step 2 of method lookup is recursively |
|
614 |
invoked on the direct superclass of C. |
|
615 |
||
616 |
Otherwise, method lookup attempts to locate the referenced method in any of |
|
617 |
the superinterfaces of the specified class C. |
|
618 |
If any superinterface of C declares a method with the name and descriptor |
|
619 |
specified by the method reference, method lookup succeeds. |
|
620 |
Otherwise, method lookup fails. |
|
621 |
If method lookup fails, method resolution throws a NoSuchMethodError. If method |
|
622 |
lookup succeeds and the method is abstract, but C is not abstract, method resolution |
|
623 |
throws an AbstractMethodError. Otherwise, if the referenced method is not accessible |
|
624 |
(§5.4.4) to D, method resolution throws an IllegalAccessError." |
|
625 |
result := class lookupMethodByNameAndType: aJavaMethodRef nameAndType. |
|
626 |
result ifNil: [ self throwNoSuchMethodError ]. |
|
627 |
(result isAbstract and:[result javaClass isInterface not and:[ class isAbstract not ]]) |
|
628 |
ifTrue: [ self throwAbstractMethodError ]. |
|
2156
a8c5720d1d05
changed: #resolveMethodIndentifiedByRef:
Claus Gittinger <cg@exept.de>
parents:
2152
diff
changeset
|
629 |
true "FIXME does not work with saxon demo - q&d hack t make esug demo work" "(self |
749 | 630 |
checkPermissionsForMethod: result |
631 |
from: aJavaMethodRef classRef owner |
|
2156
a8c5720d1d05
changed: #resolveMethodIndentifiedByRef:
Claus Gittinger <cg@exept.de>
parents:
2152
diff
changeset
|
632 |
to: class)" ifFalse: [ self throwIllegalAccessError ]. |
749 | 633 |
^ result. |
634 |
||
635 |
"Otherwise, let <E, L1> be the class or interface in which the referenced method is |
|
636 |
actually declared and let L2 be the defining loader of D. Let T0 be the name of |
|
637 |
the type returned by the referenced method, and let T1, ..., Tn be the names of |
|
638 |
the argument types of the referenced method. The Java virtual machine must impose |
|
639 |
the loading constraints TiL1=TiL2 for i = 0 to n (§5.3.4)." |
|
640 |
||
641 |
"Created: / 11-04-2011 / 19:45:34 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
642 |
"Modified: / 14-04-2011 / 00:01:34 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
643 |
"Modified: / 10-08-2011 / 22:44:14 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
2156
a8c5720d1d05
changed: #resolveMethodIndentifiedByRef:
Claus Gittinger <cg@exept.de>
parents:
2152
diff
changeset
|
644 |
"Modified: / 19-08-2011 / 14:33:56 / cg" |
749 | 645 |
! |
646 |
||
647 |
resolveStaticMethodIndentifiedByRef: aJavaMethodRef |
|
648 |
| result class | |
|
649 |
||
650 |
self validateMethodRef: aJavaMethodRef. |
|
651 |
result := self lookupMethodIfAlreadyResolved: aJavaMethodRef. |
|
652 |
result ifNotNil: [ ^ result ]. |
|
653 |
class := aJavaMethodRef classRef resolve. |
|
654 |
class ifNil: [ self halt: 'should not happen - tell mh' ]. |
|
655 |
" |
|
656 |
To resolve an unresolved symbolic reference from D to a method in |
|
657 |
a class C, the symbolic reference to C given by the method reference |
|
658 |
is first resolved (§5.4.3.1). Therefore, any exceptions that can be |
|
659 |
thrown due to resolution of a class reference can be thrown as a result |
|
660 |
of method resolution. If the reference to C can be successfully resolved, |
|
661 |
exceptions relating to the resolution of the method reference itself |
|
662 |
can be thrown. |
|
663 |
When resolving a method reference: |
|
664 |
||
665 |
Method resolution checks whether C is a class or an interface. |
|
666 |
If C is an interface, method resolution throws an IncompatibleClassChangeError." |
|
667 |
class isInterface ifTrue: [ self throwIncompatibleClassChangeError ]. |
|
668 |
"Method resolution attempts to look up the referenced method in C and its |
|
669 |
superclasses: |
|
670 |
If C declares a method with the name and descriptor specified by the method |
|
671 |
reference, method lookup succeeds. |
|
672 |
Otherwise, if C has a superclass, step 2 of method lookup is recursively |
|
673 |
invoked on the direct superclass of C. |
|
674 |
||
675 |
Otherwise, method lookup attempts to locate the referenced method in any of |
|
676 |
the superinterfaces of the specified class C. |
|
677 |
If any superinterface of C declares a method with the name and descriptor |
|
678 |
specified by the method reference, method lookup succeeds. |
|
679 |
Otherwise, method lookup fails. |
|
680 |
If method lookup fails, method resolution throws a NoSuchMethodError. If method |
|
681 |
lookup succeeds and the method is abstract, but C is not abstract, method resolution |
|
682 |
throws an AbstractMethodError. Otherwise, if the referenced method is not accessible |
|
683 |
(§5.4.4) to D, method resolution throws an IllegalAccessError." |
|
684 |
result := class lookupStaticMethodByNameAndType: aJavaMethodRef nameAndType. |
|
685 |
result ifNil: [ self throwNoSuchMethodError ]. |
|
686 |
(result isAbstract and: [ class isAbstract not ]) |
|
687 |
ifTrue: [ self throwAbstractMethodError ]. |
|
688 |
(self |
|
689 |
checkPermissionsForMethod: result |
|
690 |
from: aJavaMethodRef classRef owner |
|
691 |
to: class) ifFalse: [ self throwIllegalAccessError ]. |
|
692 |
^ result. |
|
693 |
||
694 |
"Otherwise, let <E, L1> be the class or interface in which the referenced method is |
|
695 |
actually declared and let L2 be the defining loader of D. Let T0 be the name of |
|
696 |
the type returned by the referenced method, and let T1, ..., Tn be the names of |
|
697 |
the argument types of the referenced method. The Java virtual machine must impose |
|
698 |
the loading constraints TiL1=TiL2 for i = 0 to n (§5.3.4)." |
|
699 |
||
700 |
"Created: / 28-04-2011 / 22:50:04 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
701 |
! ! |
|
702 |
||
703 |
!JavaResolver methodsFor:'method resolving helpers'! |
|
704 |
||
705 |
checkPermissionsForMethod: aJavaMethod from: accessingJavaClass to: resolvedJavaClass |
|
706 |
"A class or interface C is accessible to a class or interface D if |
|
707 |
and only if either of the following conditions are true: |
|
708 |
C is public. |
|
709 |
C and D are members of the same runtime package (§5.3). |
|
710 |
A field or method R is accessible to a class or interface D if and only |
|
711 |
if any of the following conditions is true: |
|
712 |
R is public. |
|
713 |
R is protected and is declared in a class C, and D is either a subclass |
|
714 |
of C or C itself. |
|
715 |
R is either protected or package private (that is, neither public nor |
|
716 |
protected nor private), and is declared by a class in the same runtime |
|
717 |
package as D. |
|
718 |
R is private and is declared in D. |
|
719 |
This discussion of access control omits a related restriction on the target |
|
720 |
of a protected field access or method invocation (the target must be of class |
|
721 |
D or a subtype of D). That requirement is checked as part of the verification |
|
722 |
process (§5.4.1); it is not part of link-time access control." |
|
723 |
||
724 |
"JV@2011-05-24: Temporary hack, since access checking |
|
725 |
is somewhat broken. Marcle should fix it :-)" |
|
726 |
OperatingSystem getLoginName = 'jv' ifTrue:[^true]. |
|
727 |
||
728 |
^ self checkPermissionsForMethodOrField: aJavaMethod from: accessingJavaClass to: resolvedJavaClass. |
|
729 |
||
730 |
"Created: / 11-04-2011 / 20:20:12 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
731 |
"Modified: / 14-04-2011 / 14:20:27 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
732 |
"Modified: / 24-05-2011 / 14:06:09 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
733 |
! |
|
734 |
||
735 |
lookupMethodIfAlreadyResolved: aJavaMethodRef |
|
736 |
^ nil. |
|
737 |
||
738 |
"Created: / 11-04-2011 / 19:50:38 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
739 |
"Modified: / 13-04-2011 / 11:57:00 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
740 |
! |
|
741 |
||
742 |
validateMethodRef: aJavaMethodRef |
|
743 |
aJavaMethodRef isJavaRef ifFalse:[self halt: 'I expected JavaRef instance as an argument']. |
|
744 |
aJavaMethodRef isJavaMethodRef ifFalse: [self halt: 'I expected JavaMethodRef instance as an argument']. |
|
745 |
||
746 |
"Created: / 11-04-2011 / 19:47:25 / Marcel Hlopko <hlopkmar@fel.cvut.cz>" |
|
747 |
! ! |
|
748 |
||
749 |
!JavaResolver class methodsFor:'documentation'! |
|
750 |
||
2156
a8c5720d1d05
changed: #resolveMethodIndentifiedByRef:
Claus Gittinger <cg@exept.de>
parents:
2152
diff
changeset
|
751 |
version |
a8c5720d1d05
changed: #resolveMethodIndentifiedByRef:
Claus Gittinger <cg@exept.de>
parents:
2152
diff
changeset
|
752 |
^ '$Header: /cvs/stx/stx/libjava/JavaResolver.st,v 1.2 2011-08-19 12:34:39 cg Exp $' |
a8c5720d1d05
changed: #resolveMethodIndentifiedByRef:
Claus Gittinger <cg@exept.de>
parents:
2152
diff
changeset
|
753 |
! |
a8c5720d1d05
changed: #resolveMethodIndentifiedByRef:
Claus Gittinger <cg@exept.de>
parents:
2152
diff
changeset
|
754 |
|
a8c5720d1d05
changed: #resolveMethodIndentifiedByRef:
Claus Gittinger <cg@exept.de>
parents:
2152
diff
changeset
|
755 |
version_CVS |
a8c5720d1d05
changed: #resolveMethodIndentifiedByRef:
Claus Gittinger <cg@exept.de>
parents:
2152
diff
changeset
|
756 |
^ '$Header: /cvs/stx/stx/libjava/JavaResolver.st,v 1.2 2011-08-19 12:34:39 cg Exp $' |
a8c5720d1d05
changed: #resolveMethodIndentifiedByRef:
Claus Gittinger <cg@exept.de>
parents:
2152
diff
changeset
|
757 |
! |
a8c5720d1d05
changed: #resolveMethodIndentifiedByRef:
Claus Gittinger <cg@exept.de>
parents:
2152
diff
changeset
|
758 |
|
749 | 759 |
version_SVN |
2156
a8c5720d1d05
changed: #resolveMethodIndentifiedByRef:
Claus Gittinger <cg@exept.de>
parents:
2152
diff
changeset
|
760 |
^ '§Id: JavaResolver.st,v 1.1 2011/08/18 19:06:53 vrany Exp §' |
749 | 761 |
! ! |
762 |
||
2152 | 763 |
JavaResolver initialize! |