/
RGMethodDefinition.class.st
680 lines (508 loc) · 18.5 KB
/
RGMethodDefinition.class.st
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
"
RGMethodDefinition is a concrete representation of methods. It can be used to build browser for methods that are not in the image. It is polymorphic with CompiledMethod.
* We can ask a RGMethodDefinition for its selector using the selector message.
Example:
(Point>>#dist:) asRingDefinition selector
-> #dist
We can also ask the ring object representation of its class or the Smalltalk class actually implementing the corresponding compiledMethod.
* To access the ring class definition name, use parentName
aRGMethodDefinition parentName
Example:
(Point>>#dist:) asRingDefinition parentName
-> #Point
* If you have a complete model where classes and methods are ring definition, to access the ring class definition , use parent
aRGMethodDefinition parent
Example:
aRGMethodDefinition(Point>>#dist:) parent
-> aRGClassDefinition(Point)
* If you want to access the smalltalk class that contains the compiledMethod that is represented by a ringMethodDefinition, use realParent
aRGMethodDefinition realParent
Example:
(Point>>#dist:) asRingDefinition realParent
-> Point
Now a RingEntityDefinition offers two APIs: one that is generic and works for all the source code entities and this is the one we just
presented: parent, parentName and realParent. Having such interface is important to build generic tools that could manipulate
any entities in a polymorphic way (yes no isKindOf: everywhere).
In addition, a ring method definition offers a specific interface that should only be used when you know that you are solely manipulate
specific entity such as class element: method definition, class comment, and variables.
Here is the equivalence table
realParent realClass
parent ringClass
parentName className
* The message class returns the class of the object :).
Example:
(Point>>#dist:) asRingDefinition class
-> RingMethodDefinition
* The message className returns the name of the ring class defining the reingMethodDefinition.
Example:
(Point>>#dist:) asRingDefinition className
-> #Point
* If you have a complete model where classes and methods are ring definition, to access the ring class definition , use parent
aRGMethodDefinition ringClass
Example:
aRGMethodDefinition(Point>>#dist:) ringClass
-> aRGClassDefinition(Point)
* If you want to access the smalltalk class that contains the compiledMethod that is represented by a ringMethodDefinition, use realClass
aRGMethodDefinition realClass
Example:
(Point>>#dist:) asRingDefinition realClass
-> Point
"
Class {
#name : #RGMethodDefinition,
#superclass : #RGElementDefinition,
#instVars : [
'protocol',
'sourceCode',
'stamp',
'package'
],
#category : #'Ring-Definitions-Core-Base'
}
{ #category : #'instance creation' }
RGMethodDefinition class >> class: aRGBehaviorDefinition selector: aString [
"Create a ring method definition from a ring class and a selector"
^(self class: aRGBehaviorDefinition)
selector: aString asSymbol;
yourself
]
{ #category : #'instance creation' }
RGMethodDefinition class >> className: aString selector: aSelector isMetaSide: aBoolean [
^ (self class: (RGClassDefinition named: aString) selector: aSelector) isMetaSide: aBoolean; yourself
]
{ #category : #'elements-annotations' }
RGMethodDefinition class >> isExtensionKey [
^#isExtension
]
{ #category : #'instance creation' }
RGMethodDefinition class >> realClass: aClass selector: aString [
"Creates a ring method definition from a Smalltalk class and a selector <compiledMethod>"
^(aClass>>aString asSymbol) asActiveRingDefinition
]
{ #category : #categories }
RGMethodDefinition class >> unclassifiedProtocolName [
"Return the string labeling the unclassified protocol."
^ 'as yet unclassified'
]
{ #category : #comparing }
RGMethodDefinition >> <= aRGMethodDefinition [
"Sort method definitions according to: 1. class name - 2. class comment - 3. method selector - 4. timestamp"
self parentName < aRGMethodDefinition parentName ifTrue: [^true].
self parentName > aRGMethodDefinition parentName ifTrue: [^false].
self flag: 'This ugly test is needed right now because the old system is used to represent
class comment as method with Comment selector. And to mix comment and methods'.
aRGMethodDefinition isComment ifTrue: [^false].
^(self selector < aRGMethodDefinition selector)
or: [ (self selector == aRGMethodDefinition selector) and: [
self timeStamp <= aRGMethodDefinition timeStamp ]]
]
{ #category : #comparing }
RGMethodDefinition >> = aRGMethodDefinition [
"This method look for equality of the key properties of the receiver"
^(super = aRGMethodDefinition)
and: [ self selector == aRGMethodDefinition selector]
]
{ #category : #'managing container' }
RGMethodDefinition >> addInContainer: aRGContainer [
aRGContainer addMethod: self
]
{ #category : #traits }
RGMethodDefinition >> argumentNames [
"Return an array with the argument names of the method's selector"
self isActive
ifTrue: [ ^ self compiledMethod argumentNames ].
^ self ast ifNotNil:[:ast | ast arguments collect: [:each | each name] ]
]
{ #category : #'type of methods' }
RGMethodDefinition >> asActive [
"Sets the receiver as active object, which will allow itself to retrieve its data from the compiled method"
self annotationNamed: self class statusKey put: #active
]
{ #category : #'type of methods' }
RGMethodDefinition >> asHistorical [
"Sets the receiver as historical object, which will allow itself to retrieve its data using the sourcePointer"
self annotationNamed: self class statusKey put: #historical.
self sourcePointer ifNil:[ | pointer compiledMethod |
pointer := 0.
compiledMethod := self compiledMethod.
compiledMethod notNil ifTrue: [
pointer := compiledMethod sourcePointer ].
pointer isZero
ifFalse:[ self sourcePointer: pointer ] ]
]
{ #category : #'type of methods' }
RGMethodDefinition >> asPassive [
"Sets the receiver as passive object, which will allow itself to retrieve its data that was assigned in its creation"
self annotationNamed: self class statusKey put: #passive
]
{ #category : #accessing }
RGMethodDefinition >> ast [
self isActive
ifTrue: [ ^ self compiledMethod ast ].
^ self sourceCode ifNotNil: [ ^ self astFromSource ]
]
{ #category : #private }
RGMethodDefinition >> astFromSource [
^ RBParser parseMethod: self sourceCode
]
{ #category : #'stamp values' }
RGMethodDefinition >> author [
^self
annotationNamed: self class authorKey
ifAbsentPut: [ self class parseAuthorAliasFrom: self stamp ]
]
{ #category : #'stamp values' }
RGMethodDefinition >> author: aString [
self annotationNamed: self class authorKey put: aString
]
{ #category : #'backward compatibility' }
RGMethodDefinition >> category [
^ self protocol
]
{ #category : #'to remove as soon as possible' }
RGMethodDefinition >> classIsMeta [
^self isMetaSide
]
{ #category : #accessing }
RGMethodDefinition >> compiledMethod [
"Retrieves the compiled method of this definition if exists"
| rClass |
(rClass := self realClass) notNil
ifTrue: [ (rClass includesSelector: self selector)
ifTrue: [ ^rClass >> self selector ] ].
^nil
]
{ #category : #testing }
RGMethodDefinition >> containsHalt [
^ self method containsHalt
]
{ #category : #'type of methods' }
RGMethodDefinition >> fromActiveToHistorical [
"If the receiver was generated as an active method, it can be converted to a historical one by reading the data of the compiled method (if exists)"
self isActive ifTrue: [
self asHistorical ]
]
{ #category : #'type of methods' }
RGMethodDefinition >> fromActiveToPassive [
"If the receiver was generated as an active method, it can be converted to a passive one by reading the data of the compiled method (if exists)"
| compiledMethod |
self isActive
ifFalse: [ ^ self ].
compiledMethod := self compiledMethod.
compiledMethod notNil
ifTrue: [
self protocol: compiledMethod category.
self sourceCode: compiledMethod sourceCode.
self stamp: compiledMethod timeStamp ].
self asPassive
]
{ #category : #accessing }
RGMethodDefinition >> fullName [
"Keeps a unique description for the receiver. As annotation to avoid converting each time is invoked"
^self annotationNamed: self class fullNameKey
ifAbsentPut: [ (self parentName, '>>', self selector) asSymbol ]
]
{ #category : #'source pointers' }
RGMethodDefinition >> getPreambleFrom: aFileStream at: position [
^ SourceFiles getPreambleFrom: aFileStream at: position
]
{ #category : #testing }
RGMethodDefinition >> hasStamp [
^stamp isEmptyOrNil not
]
{ #category : #comparing }
RGMethodDefinition >> hash [
^super hash bitXor: self selector hash
]
{ #category : #testing }
RGMethodDefinition >> isAbstract [
self isActive
ifTrue: [ ^ self compiledMethod isAbstract ].
^ false
]
{ #category : #'type of methods' }
RGMethodDefinition >> isActive [
"A ring method isActive when it needs to access the compiledMethod for retrieving its data"
^(self annotationNamed: self class statusKey)
ifNil:[ false ]
ifNotNil:[ :status| status == #active ]
]
{ #category : #traits }
RGMethodDefinition >> isBinarySelector [
^self selector
allSatisfy: [:each | each isSpecial]
]
{ #category : #testing }
RGMethodDefinition >> isDefined [
"Answer whether the receiver exists in the environment"
| rClass |
self selector isDoIt ifTrue:[ ^false ].
rClass := self realClass.
^rClass notNil and: [ rClass includesSelector: self selector ]
]
{ #category : #'SUnit-support' }
RGMethodDefinition >> isErrorTest [
"Is the receiver a test method that raised an error?"
^ self methodClass isTestCase
and: [ self methodClass methodRaisedError: self selector ]
]
{ #category : #accessing }
RGMethodDefinition >> isExtension [
"The receiver is an extension when is defined in a different package to the one of its parent.
Ring allows to set this property. If not assigned tries to find its value "
^self
annotationNamed: self class isExtensionKey
ifAbsent: [
(self parent notNil and:[ self package notNil ])
ifTrue: [ | value |
value := self parent package ~= self package.
self annotationNamed: self class isExtensionKey put: value.
value ]
ifFalse: [ self protocol ifNil:[ false ] ifNotNil:[ :prot| prot beginsWith: '*' ] ] ]
]
{ #category : #accessing }
RGMethodDefinition >> isExtension: aBoolean [
"Explicitily set that the receiver is an extension"
self annotationNamed: self class isExtensionKey put: aBoolean
]
{ #category : #'SUnit-support' }
RGMethodDefinition >> isFailedTest [
"Is the receiver a test method that failed?"
^ self methodClass isTestCase
and: [ self methodClass methodFailed: self selector ]
]
{ #category : #testing }
RGMethodDefinition >> isFromTrait [
"Return true for methods that have been included from Traits"
^ self origin isTrait and: [ self origin ~= self methodClass ]
]
{ #category : #'type of methods' }
RGMethodDefinition >> isHistorical [
"A ring method can be used to point an old version of the receiver, in this case it will use the sourcePointer to retrieve its information"
^(self annotationNamed: self class statusKey)
ifNil:[ false ]
ifNotNil:[ :status| status == #historical ]
]
{ #category : #'managing container' }
RGMethodDefinition >> isIncludedInContainer: aRGContainer [
^aRGContainer includesMethod: self
]
{ #category : #testing }
RGMethodDefinition >> isLocalSelector [
"Answer whether the receiver exists in the environment as a local implementation"
| rClass |
rClass:= self realClass.
^rClass notNil and: [ rClass includesLocalSelector: self selector ]
]
{ #category : #testing }
RGMethodDefinition >> isMethod [
^true
]
{ #category : #testing }
RGMethodDefinition >> isOverridden [
self isActive
ifTrue: [ ^ self compiledMethod isOverridden ].
^ false
]
{ #category : #'SUnit-support' }
RGMethodDefinition >> isPassedTest [
"Is the receiver a test method that passed?"
^ self methodClass isTestCase
and: [ self methodClass methodPassed: self selector ]
]
{ #category : #'type of methods' }
RGMethodDefinition >> isPassive [
"A ring method isPassive when it retrieves the data that was assigned in its creation.
By default is passive"
^(self annotationNamed: self class statusKey)
ifNil:[ true ]
ifNotNil:[ :status| status == #passive ]
]
{ #category : #testing }
RGMethodDefinition >> isSameRevisionAs: aRGMethodDefinition [
"This method look for equality of the properties of the receiver"
"Stamp is ignored as in many cases a method is saved without containing any change. However it appears as changed due to a different stamp"
self flag: 'needs to be evaluated'.
^(super isSameRevisionAs: aRGMethodDefinition)
and: [ self selector = aRGMethodDefinition selector
and: [ self protocol = aRGMethodDefinition protocol
and: [ self sourceCode = aRGMethodDefinition sourceCode ] ] ]
]
{ #category : #'SUnit-support' }
RGMethodDefinition >> isTestMethod [
| selectorString |
self numArgs isZero
ifFalse: [ ^ false ].
selectorString := self selector asString.
"unary selectors starting with 'should' are supposed to be treated as test methods too"
((selectorString beginsWith: 'test') or: [ selectorString beginsWith: 'should' ])
ifFalse: [ ^ false ].
"Is the receiver a TestCase test method?"
self methodClass isTestCase
ifFalse: [ ^ false ].
^true
]
{ #category : #'to remove as soon as possible' }
RGMethodDefinition >> isValid [
"verifies that the receiver is locally defined in the class and that is not a DoIt"
^self isDefined
]
{ #category : #literals }
RGMethodDefinition >> literals [
"bad code but I do not know how to properly retrieve the good compiled method using a RGMethodDefinition"
^ self compiledMethod literals.
]
{ #category : #'backward compatibility' }
RGMethodDefinition >> messages [
self isActive
ifTrue: [ ^ self method messages ].
^ self ast ifNotNil: [ :ast | ast sentMessages ] ifNil: [ #() ]
]
{ #category : #'backward compatibility' }
RGMethodDefinition >> method [
^self compiledMethod
]
{ #category : #'backward compatibility' }
RGMethodDefinition >> methodClass [
"Return the class to which the receiver belongs to."
^ self realClass
]
{ #category : #accessing }
RGMethodDefinition >> name [
"name ivar is used for selector, but the name of a method is Class>>selector"
^self printString
]
{ #category : #accessing }
RGMethodDefinition >> numArgs [
^ self selector asString numArgs
]
{ #category : #metrics }
RGMethodDefinition >> numberOfLinesOfCode [
^ self annotationNamed: #numberOfLinesOfCode ifAbsentPut: [ self sourceCode lineCount ]
]
{ #category : #accessing }
RGMethodDefinition >> origin [
"Return the real oring of this method."
^ self compiledMethod
ifNil: [ self methodClass ]
ifNotNil: [ :compileMethod| compileMethod origin ]
]
{ #category : #accessing }
RGMethodDefinition >> package [
"Retrieves the package in which this class is contained, if exists"
^package
]
{ #category : #accessing }
RGMethodDefinition >> package: aRGPackage [
package:= aRGPackage
]
{ #category : #printing }
RGMethodDefinition >> printOn: aStream [
aStream print: self methodClass; nextPutAll: '>>'; print: self selector.
]
{ #category : #accessing }
RGMethodDefinition >> protocol [
self isActive
ifTrue: [ ^ self compiledMethod ifNil: [ protocol ] ifNotNil: [ self compiledMethod protocol ]].
self isHistorical
ifTrue: [ ^ self protocolAtPointer ifNil: [ self compiledMethod ifNil: [ protocol ] ifNotNil:[ :cm | cm protocol ] ] ].
^ protocol
]
{ #category : #accessing }
RGMethodDefinition >> protocol: anObject [
protocol := anObject
]
{ #category : #'source pointers' }
RGMethodDefinition >> protocolAtPointer [
"A RGMethodDefinition that was set as historical will retrieve the protocol using the sourcePointer"
^ self sourcePointer notNil
ifTrue: [ SourceFiles protocolAt: self sourcePointer ]
ifFalse:[ nil ]
]
{ #category : #operations }
RGMethodDefinition >> recompile [
self method recompile
]
{ #category : #'managing container' }
RGMethodDefinition >> removeFromContainer: aRGContainer [
aRGContainer removeMethod: self
]
{ #category : #accessing }
RGMethodDefinition >> selector [
"Retrieves the name of the method"
^name
]
{ #category : #accessing }
RGMethodDefinition >> selector: aSymbol [
"The name of a method is known as #selector"
name := aSymbol
]
{ #category : #accessing }
RGMethodDefinition >> sourceCode [
self isActive
ifTrue: [ ^ self compiledMethod ifNil:[ sourceCode ] ifNotNil: [ self compiledMethod sourceCode ]].
self isHistorical
ifTrue: [ ^ self sourceCodeAtPointer ifNil:[ self compiledMethod ifNil:[ sourceCode ] ifNotNil:[ :cm| cm sourceCode ] ] ].
^ sourceCode
]
{ #category : #accessing }
RGMethodDefinition >> sourceCode: anObject [
sourceCode := anObject
]
{ #category : #'source pointers' }
RGMethodDefinition >> sourceCodeAtPointer [
"A RGMethodDefinition that was set as historical will retrieve the sourceCode using the sourcePointer"
^ self sourcePointer notNil
ifTrue: [ SourceFiles sourceCodeAt: self sourcePointer ]
ifFalse:[ nil ]
]
{ #category : #'source pointers' }
RGMethodDefinition >> sourcePointer [
"Retrieves the sourcePointer for this definition if exists"
^ self isActive
ifTrue: [ self compiledMethod sourcePointer ]
ifFalse: [ self annotationNamed: self class sourcePointerKey ]
]
{ #category : #'source pointers' }
RGMethodDefinition >> sourcePointer: aNumber [
self annotationNamed: self class sourcePointerKey put: aNumber
]
{ #category : #accessing }
RGMethodDefinition >> stamp [
self isActive
ifTrue: [ ^ self compiledMethod timeStamp ].
self isHistorical
ifTrue: [ ^ self stampAtPointer ifNil:[ self compiledMethod ifNil:[ stamp ] ifNotNil:[ :cm| cm timeStamp ] ] ].
^ stamp
]
{ #category : #accessing }
RGMethodDefinition >> stamp: anObject [
"stores an author alias and a timestamp"
stamp := anObject
]
{ #category : #'source pointers' }
RGMethodDefinition >> stampAtPointer [
"A RGMethodDefinition that was set as historical will retrieve the stamp using the sourcePointer"
^ self sourcePointer notNil
ifTrue: [ SourceFiles timeStampAt: self sourcePointer ]
ifFalse:[ nil ]
]
{ #category : #'backward compatibility' }
RGMethodDefinition >> symbolic [
^ self method symbolic
]
{ #category : #accessing }
RGMethodDefinition >> symbolicBytecodes [
^self compiledMethod symbolicBytecodes
]
{ #category : #'stamp values' }
RGMethodDefinition >> timeStamp [
^self annotationNamed: self class timeStampKey
ifAbsentPut: [ self class
parseTimestampFrom: self stamp
default: (DateAndTime epoch) ]
]
{ #category : #'stamp values' }
RGMethodDefinition >> timeStamp: aTimestamp [
self annotationNamed: self class timeStampKey put: aTimestamp
]