author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 02 Sep 2015 18:29:03 +0100 | |
changeset 5 | 5cc2caa88b23 |
parent 3 | ed5aae792d24 |
permissions | -rw-r--r-- |
0 | 1 |
"{ Package: 'stx:goodies/ring' }" |
2 |
||
3 |
"{ NameSpace: Smalltalk }" |
|
4 |
||
5 |
RGGlobalDefinition subclass:#RGBehaviorDefinition |
|
6 |
instanceVariableNames:'superclass methods protocols' |
|
7 |
classVariableNames:'' |
|
8 |
poolDictionaries:'' |
|
9 |
category:'Ring-Core-Kernel' |
|
10 |
! |
|
11 |
||
12 |
RGBehaviorDefinition comment:'An RGBehaviorDefinition is an abstract definition for class-alike entities (e.g. classes, traits) Instance Variables methods: <Collection> protocols: <Collection> superclass: <Object>' |
|
13 |
! |
|
14 |
||
15 |
!RGBehaviorDefinition class methodsFor:'class-annotations'! |
|
16 |
||
17 |
allSubclassesKey |
|
18 |
||
19 |
^#allSubclasses |
|
20 |
! |
|
21 |
||
22 |
allSuperclassesKey |
|
23 |
||
24 |
^#allSuperclasses |
|
25 |
! |
|
26 |
||
27 |
definitionSourceKey |
|
28 |
||
29 |
^#definitionSource |
|
30 |
! |
|
31 |
||
32 |
isPoolKey |
|
33 |
||
34 |
^#isPool |
|
35 |
! |
|
36 |
||
37 |
subclassesKey |
|
38 |
||
39 |
^#subclasses |
|
40 |
! |
|
41 |
||
42 |
superclassNameKey |
|
43 |
||
44 |
^#superclassName |
|
45 |
! |
|
46 |
||
47 |
superclassesKey |
|
48 |
||
49 |
^#superclasses |
|
50 |
! |
|
51 |
||
52 |
traitCompositionSourceKey |
|
53 |
||
54 |
^#traitCompositionSource |
|
55 |
! |
|
56 |
||
57 |
usersKey |
|
58 |
||
59 |
^#users |
|
60 |
! ! |
|
61 |
||
62 |
!RGBehaviorDefinition methodsFor:'accessing'! |
|
63 |
||
64 |
methods |
|
65 |
||
66 |
^methods |
|
67 |
! |
|
68 |
||
69 |
methods: aDictionary |
|
70 |
||
71 |
methods:= aDictionary |
|
72 |
! |
|
73 |
||
74 |
package |
|
75 |
||
76 |
self subclassResponsibility |
|
77 |
! |
|
78 |
||
79 |
protocols |
|
80 |
"retrieves the protocols of the class" |
|
81 |
||
82 |
^protocols |
|
83 |
! |
|
84 |
||
85 |
protocols: aCollection |
|
86 |
"set the protocols of the class" |
|
87 |
||
88 |
protocols:= aCollection |
|
89 |
! |
|
90 |
||
91 |
realClass |
|
92 |
"Retrieves the current class existing in the runtime environment" |
|
93 |
||
94 |
^self rootEnvironment classNamed: self name |
|
95 |
! |
|
96 |
||
97 |
superclass |
|
98 |
||
99 |
^superclass |
|
100 |
! |
|
101 |
||
102 |
superclass: aRGBehaviorDefinition |
|
103 |
"The superclass is assigned. |
|
104 |
If aRGBehaviorDefinition is not nil the receiver is added as a subclass and the superclass assignment also happens for theMetaClass" |
|
105 |
||
106 |
superclass := aRGBehaviorDefinition. |
|
107 |
superclass notNil |
|
108 |
ifFalse: [ ^ self ]. |
|
109 |
self superclassName: aRGBehaviorDefinition name. |
|
110 |
aRGBehaviorDefinition addSubclass: self. |
|
111 |
self hasMetaclass |
|
112 |
ifTrue: [ self theMetaClass superclass: aRGBehaviorDefinition theMetaClass ] |
|
113 |
! |
|
114 |
||
115 |
theMetaClass |
|
116 |
||
117 |
self subclassResponsibility |
|
118 |
! |
|
119 |
||
3
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
120 |
theMetaclass |
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
121 |
^ self theMetaClass |
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
122 |
|
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
123 |
"Created: / 29-08-2015 / 11:40:10 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
124 |
! |
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
125 |
|
0 | 126 |
theNonMetaClass |
127 |
||
128 |
self subclassResponsibility |
|
129 |
! |
|
130 |
||
3
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
131 |
theNonMetaclass |
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
132 |
^ self theNonMetaClass |
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
133 |
|
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
134 |
"Created: / 29-08-2015 / 11:40:54 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
135 |
! |
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
136 |
|
0 | 137 |
traitNames |
138 |
"Assuming that traits in a composition can be identified by |
|
139 |
testing for the first character being an uppercase character |
|
140 |
(and thus not a special character such as {, # etc.)" |
|
141 |
| tokens | |
|
142 |
||
143 |
tokens := self traitCompositionSource parseLiterals flattened. |
|
144 |
^tokens select: [:each | each first isUppercase]. |
|
145 |
! |
|
146 |
||
147 |
traits |
|
148 |
"Retrieves ring traits based on the names in the traitComposition and from the environment if it is a ring object" |
|
149 |
||
150 |
^ self environment isRingObject |
|
151 |
ifTrue: [ self traitNames collect:[ :each| self environment traitNamed: each ] ] |
|
152 |
ifFalse:[ #() ] |
|
153 |
! ! |
|
154 |
||
155 |
!RGBehaviorDefinition methodsFor:'accessing class hierarchy'! |
|
156 |
||
157 |
allSubclasses |
|
158 |
"Retrieves all the subclasses of the receiver in the chan hiearchy - value is kept as an annotation" |
|
159 |
"is a good idea storing this?" |
|
160 |
||
161 |
^self annotationNamed: self class allSubclassesKey |
|
162 |
ifAbsentPut:[ | collection index | |
|
163 |
index := 1. |
|
164 |
collection := OrderedCollection withAll: self subclasses. |
|
165 |
[index <= collection size] whileTrue: |
|
166 |
[collection addAll: (collection at: index) subclasses. |
|
167 |
index := index + 1]. |
|
168 |
collection ] |
|
169 |
! |
|
170 |
||
171 |
allSubclasses: aCollection |
|
172 |
"Stores all the subclasses (direct and indirect) as an annotation" |
|
173 |
||
174 |
self annotationNamed: self class allSubclassesKey |
|
175 |
ifAbsentPut:[ aCollection ] |
|
176 |
! |
|
177 |
||
178 |
allSuperclasses |
|
179 |
"Answer an OrderedCollection of the receiver's and the receiver's |
|
180 |
ancestor's superclasses" |
|
181 |
"Is implementation of Behavior more efficient?" |
|
182 |
||
183 |
^self annotationNamed: self class allSuperclassesKey |
|
184 |
ifAbsentPut:[ | supers sprClass | |
|
185 |
supers := OrderedCollection new. |
|
186 |
sprClass := self superclass. |
|
187 |
[sprClass notNil] whileTrue: |
|
188 |
[supers add: sprClass. |
|
189 |
sprClass := sprClass superclass]. |
|
190 |
supers ] |
|
191 |
! |
|
192 |
||
193 |
allSuperclasses: aCollection |
|
194 |
"Stores all the superclasses (direct and indirect) as an annotation" |
|
195 |
||
196 |
self annotationNamed: self class allSuperclassesKey |
|
197 |
ifAbsentPut:[ aCollection ] |
|
198 |
! |
|
199 |
||
200 |
allSuperclassesDo: aBlock |
|
201 |
"Evaluate the argument, aBlock, for each of the receiver's superclasses." |
|
202 |
||
203 |
self superclass isNil |
|
204 |
ifTrue: [ ^ self ]. |
|
205 |
aBlock value: superclass. |
|
206 |
superclass allSuperclassesDo: aBlock |
|
207 |
! |
|
208 |
||
209 |
methodDict |
|
210 |
^ methods |
|
211 |
! |
|
212 |
||
3
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
213 |
methodDictionary |
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
214 |
^ methods |
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
215 |
|
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
216 |
"Created: / 29-08-2015 / 18:07:37 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
217 |
! |
ed5aae792d24
More Smalltalk/X API methods. Better RGClassDefinition creation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
0
diff
changeset
|
218 |
|
0 | 219 |
withAllSubclasses |
220 |
"if allSubclasses is stored should not affect the collection" |
|
221 |
||
222 |
^self allSubclasses, {self} |
|
223 |
! |
|
224 |
||
225 |
withAllSuperclasses |
|
226 |
"if allSuperclasses is stored should not affect the collection" |
|
227 |
||
228 |
^self allSuperclasses, {self} |
|
229 |
! ! |
|
230 |
||
231 |
!RGBehaviorDefinition methodsFor:'accessing methods'! |
|
232 |
||
233 |
allSelectors |
|
234 |
"Retrieves all the selectos of the receiver in the chain hierarchy" |
|
235 |
| class selectors | |
|
236 |
||
237 |
class:= self. |
|
238 |
selectors := Set new. |
|
239 |
[class notNil] whileTrue: |
|
240 |
[selectors addAll: class selectors. |
|
241 |
class := class superclass ]. |
|
242 |
^selectors |
|
243 |
! |
|
244 |
||
245 |
compiledMethodNamed: selector |
|
246 |
"Retrieves the compiled method from aRGMethodDefinition" |
|
247 |
| method | |
|
248 |
||
249 |
^(method:= self methodNamed: selector) notNil |
|
250 |
ifTrue: [ method compiledMethod ] |
|
251 |
ifFalse:[ nil ] |
|
252 |
! |
|
253 |
||
254 |
extensionMethods |
|
255 |
||
256 |
^self methods select:[ :each | each isExtension ] |
|
257 |
! |
|
258 |
||
259 |
includesSelector: selector |
|
260 |
"Looks if selector is a key in the methods dictionary" |
|
261 |
||
262 |
^methods includesKey: selector asSymbol |
|
263 |
! |
|
264 |
||
265 |
methodNamed: selector |
|
266 |
"Retrieves aRGMethodDefinition that matches the selector given as argument" |
|
267 |
"RB defines methodFor:" |
|
268 |
||
269 |
^methods at: selector asSymbol ifAbsent:[ nil ] |
|
270 |
! |
|
271 |
||
272 |
methodsInProtocol: aString |
|
273 |
"Retrieves the methods classified in protocol named aString" |
|
274 |
||
275 |
^methods select: [ :each | each protocol = aString ] |
|
276 |
! |
|
277 |
||
278 |
selectors |
|
279 |
"Retrieves the method selectors" |
|
280 |
||
281 |
^methods keys |
|
282 |
! ! |
|
283 |
||
284 |
!RGBehaviorDefinition methodsFor:'adding/removing methods'! |
|
285 |
||
286 |
addMethod: aRGMethodDefinition |
|
287 |
"Adds aRGMethodDefinition in the methods dictionary. |
|
288 |
Adds the protocol of such method too" |
|
289 |
||
290 |
self addMethod: aRGMethodDefinition in: methods. |
|
291 |
self addProtocol: aRGMethodDefinition protocol |
|
292 |
! |
|
293 |
||
294 |
addSelector: selectorName classified: protocolName sourced: source |
|
295 |
||
296 |
self addMethod: ((RGMethodDefinition named: selectorName) |
|
297 |
parent: self; |
|
298 |
protocol: protocolName; |
|
299 |
sourceCode: source; |
|
300 |
yourself) |
|
301 |
! |
|
302 |
||
303 |
removeMethod: aRGMethodDefinition |
|
304 |
"Removes aRGMethodDefinition from the methods dictionary" |
|
305 |
||
306 |
self removeMethod: aRGMethodDefinition from: methods |
|
307 |
! |
|
308 |
||
309 |
removeSelector: selector |
|
310 |
"Removes a method named as selector" |
|
311 |
||
312 |
methods removeKey: selector ifAbsent:[] |
|
313 |
! ! |
|
314 |
||
315 |
!RGBehaviorDefinition methodsFor:'adding/removing protocols'! |
|
316 |
||
317 |
addProtocol: anObject |
|
318 |
"Adds a protocol named anObject. |
|
319 |
Protocols are not repeated" |
|
320 |
||
321 |
anObject ifNil:[ ^self ]. |
|
322 |
protocols add: anObject |
|
323 |
! |
|
324 |
||
325 |
removeProtocol: aString |
|
326 |
"Removes a protocol named aString (if exists)" |
|
327 |
||
328 |
protocols remove: aString ifAbsent:[] |
|
329 |
! ! |
|
330 |
||
331 |
!RGBehaviorDefinition methodsFor:'annotations'! |
|
332 |
||
333 |
definitionSource |
|
334 |
"Retrieves the definition template of the receiver -> aString. |
|
335 |
This value is kept as an annotation" |
|
336 |
||
337 |
^self annotationNamed: self class definitionSourceKey |
|
338 |
! |
|
339 |
||
340 |
definitionSource: aString |
|
341 |
"Sets the definition template of the receiver -> aString. |
|
342 |
It's stored as an annotation" |
|
343 |
||
344 |
self annotationNamed: self class definitionSourceKey |
|
345 |
put: aString |
|
346 |
! |
|
347 |
||
348 |
hasDefinitionSource |
|
349 |
"Tests whether the receiver has a source definition." |
|
350 |
||
351 |
^ self definitionSource notNil |
|
352 |
! |
|
353 |
||
354 |
subclasses |
|
355 |
"Retrieves the direct subclasses of the receiver. |
|
356 |
This value is kept as an annotation" |
|
357 |
||
358 |
^self annotationNamed: self class subclassesKey |
|
359 |
ifAbsentPut:[ OrderedCollection new ] |
|
360 |
! |
|
361 |
||
362 |
subclasses: aCollection |
|
363 |
"Stores direct subclasses of the receiver as an annotation" |
|
364 |
||
365 |
self annotationNamed: self class subclassesKey |
|
366 |
ifAbsentPut:[ aCollection ] |
|
367 |
! |
|
368 |
||
369 |
superclassName |
|
370 |
"Retrieves the name of the superclass if exists" |
|
371 |
||
372 |
^self annotationNamed: self class superclassNameKey |
|
373 |
! |
|
374 |
||
375 |
superclassName: aSymbol |
|
376 |
||
377 |
self annotationNamed: self class superclassNameKey |
|
378 |
put: aSymbol |
|
379 |
! |
|
380 |
||
381 |
traitCompositionSource |
|
382 |
"Retrieves aString representing the used traits" |
|
383 |
||
384 |
^self annotationNamed: self class traitCompositionSourceKey |
|
385 |
ifAbsentPut:[ '{}' ] |
|
386 |
! |
|
387 |
||
388 |
traitCompositionSource: anString |
|
389 |
"Stores aString representing the traits used by the receiver " |
|
390 |
||
391 |
self annotationNamed: self class traitCompositionSourceKey |
|
392 |
put: anString |
|
393 |
! ! |
|
394 |
||
395 |
!RGBehaviorDefinition methodsFor:'compatibility'! |
|
396 |
||
397 |
soleInstance |
|
398 |
"to be depracated in the future" |
|
399 |
||
400 |
^self theNonMetaClass |
|
401 |
! ! |
|
402 |
||
403 |
!RGBehaviorDefinition methodsFor:'initialization'! |
|
404 |
||
405 |
initialize |
|
406 |
||
407 |
super initialize. |
|
408 |
methods:= IdentityDictionary new. |
|
409 |
protocols:= Set new. |
|
410 |
! ! |
|
411 |
||
412 |
!RGBehaviorDefinition methodsFor:'managing container'! |
|
413 |
||
414 |
addInContainer: aRGContainer |
|
415 |
||
416 |
aRGContainer addClass: self |
|
417 |
! |
|
418 |
||
419 |
isIncludedInContainer: aRGContainer |
|
420 |
||
421 |
^aRGContainer includesClass: self |
|
422 |
! |
|
423 |
||
424 |
removeFromContainer: aRGContainer |
|
425 |
||
426 |
aRGContainer removeClass: self |
|
427 |
! ! |
|
428 |
||
429 |
!RGBehaviorDefinition methodsFor:'printing'! |
|
430 |
||
431 |
printOn: aStream |
|
432 |
aStream nextPutAll: self name |
|
433 |
! |
|
434 |
||
435 |
storeOn: aStream |
|
436 |
self name storeOn: aStream |
|
437 |
! ! |
|
438 |
||
439 |
!RGBehaviorDefinition methodsFor:'private'! |
|
440 |
||
441 |
addMethod: aRGMethodDefinition in: aCollection |
|
442 |
"Adds aRGMethodDefinition in the collection received" |
|
443 |
||
444 |
aRGMethodDefinition parent ifNil:[ aRGMethodDefinition parent: self ]. |
|
445 |
aCollection at: aRGMethodDefinition selector |
|
446 |
put: aRGMethodDefinition |
|
447 |
! |
|
448 |
||
449 |
removeMethod: aRGMethodDefinition from: aCollection |
|
450 |
"Removes aRGMethodDefinition from the collection received" |
|
451 |
||
452 |
aCollection removeKey: aRGMethodDefinition selector ifAbsent:[] |
|
453 |
! ! |
|
454 |
||
455 |
!RGBehaviorDefinition methodsFor:'subclassing'! |
|
456 |
||
457 |
addSubclass: aRGBehaviorDefinition |
|
458 |
"Adds a direct subclass of the receiver" |
|
459 |
||
460 |
(self subclasses includes: aRGBehaviorDefinition) |
|
461 |
ifFalse:[ self subclasses add: aRGBehaviorDefinition ] |
|
462 |
! |
|
463 |
||
464 |
removeSubclass: aRGBehaviorDefinition |
|
465 |
"Removes aRGAbstractClassDefinition from the direct subclasses - without failing if does not exist" |
|
466 |
||
467 |
self subclasses remove: aRGBehaviorDefinition ifAbsent: [] |
|
468 |
! ! |
|
469 |
||
470 |
!RGBehaviorDefinition methodsFor:'testing'! |
|
471 |
||
472 |
hasMetaclass |
|
473 |
||
474 |
^ false |
|
475 |
! |
|
476 |
||
477 |
hasMethods |
|
478 |
"validates the existance of methods" |
|
479 |
||
480 |
^methods notEmpty |
|
481 |
! |
|
482 |
||
483 |
hasProtocols |
|
484 |
"Validates the existance of protocols" |
|
485 |
||
486 |
^protocols notEmpty |
|
487 |
! |
|
488 |
||
489 |
hasSuperclass |
|
490 |
||
491 |
^superclass notNil |
|
492 |
! |
|
493 |
||
494 |
hasTraitComposition |
|
495 |
||
496 |
^self traitCompositionSource ~= '{}' |
|
497 |
! |
|
498 |
||
499 |
includesProtocol: aString |
|
500 |
"Looks for a protocols named = aString" |
|
501 |
||
502 |
^protocols includes: aString |
|
503 |
! |
|
504 |
||
505 |
isDefined |
|
506 |
"If the class exists in the environment" |
|
507 |
||
508 |
^self realClass notNil |
|
509 |
||
510 |
! |
|
511 |
||
512 |
isMeta |
|
513 |
"By default is considered a non-meta class" |
|
514 |
||
515 |
^false |
|
516 |
! |
|
517 |
||
518 |
isSameRevisionAs: aRGBehaviorDefinition |
|
519 |
"This method look for equality of the properties of the receiver" |
|
520 |
||
521 |
^self class = aRGBehaviorDefinition class |
|
522 |
and:[ self name == aRGBehaviorDefinition name ] |
|
523 |
! ! |
|
524 |
||
525 |
!RGBehaviorDefinition methodsFor:'testing class hierarchy'! |
|
526 |
||
527 |
includesBehavior: aClass |
|
528 |
^self == aClass or: [self inheritsFrom: aClass] |
|
529 |
! ! |
|
530 |