/
RBRemoveMethodTransformation.class.st
201 lines (164 loc) · 5.86 KB
/
RBRemoveMethodTransformation.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
"
Removes a method from a class. Only the selector of the method is needed.
Usage:
| transformation |
transformation := (RBRemoveMethodTransformation
selector: #transform
from: RBRemoveMethodTransformation)
transform.
(ChangesBrowser changes: transformation model changes changes) open
Preconditions:
Checks whether there are any references to this method
"
Class {
#name : #RBRemoveMethodTransformation,
#superclass : #RBMethodTransformation,
#category : #'Refactoring2-Transformations-Model'
}
{ #category : #api }
RBRemoveMethodTransformation class >> model: aRBModel selector: aSelector from: aClassName [
^ self new
model: aRBModel;
selector: aSelector
from: aClassName;
yourself
]
{ #category : #api }
RBRemoveMethodTransformation class >> model: aRBModel selectors: aCollection from: aClassName [
^ RBCompositeTransformation new
transformations: (aCollection collect: [:e |
self model: aRBModel
selector: e
from: aClassName] )
]
{ #category : #api }
RBRemoveMethodTransformation class >> selector: aSelector from: aClassName [
^ self new
selector: aSelector
from: aClassName;
yourself
]
{ #category : #api }
RBRemoveMethodTransformation class >> selectors: aCollection from: aClassName [
^ self model: RBTransformation new model selectors: aCollection from: aClassName
]
{ #category : #preconditions }
RBRemoveMethodTransformation >> checkBrowseAllOccurences: anCollectionOfOccurrences [
| methods callers |
methods := anCollectionOfOccurrences collect: [ :c | c key ] as: Set.
callers := anCollectionOfOccurrences collect: [ :v | v value ].
methods size = 1
ifTrue: [ self refactoringError:
('Possible call to <2s> in <1p> methods.<n>Browse references?'
expandMacrosWith: anCollectionOfOccurrences size
with: methods anyOne)
with: [ self openBrowserOn: (RBBrowserEnvironment new referencesTo: methods anyOne) ] ]
ifFalse: [ self refactoringError:
('Possible call to the <2p> selectors in <1p> methods.<n>Browse references?'
expandMacrosWith: callers size
with: methods size)
with: [
| env |
env := RBSelectorEnvironment new.
callers do: [ :d | env addMethod: d method ].
self openBrowserOn: env ] ]
]
{ #category : #preconditions }
RBRemoveMethodTransformation >> checkBrowseOccurrenceIn: aRBMethod [
self refactoringError:
('Possible call to <2s> in <1p><n>Browse references?'
expandMacrosWith: aRBMethod modelClass
with: selector)
with: [ self openBrowserOn: (RBBrowserEnvironment new referencesTo: selector) ]
]
{ #category : #preconditions }
RBRemoveMethodTransformation >> checkBrowseOccurrences: anCollectionOfOccurrences [
anCollectionOfOccurrences size = 1
ifTrue: [ ^ self checkBrowseOccurrenceIn: anCollectionOfOccurrences first value ]
ifFalse: [ ^ self checkBrowseAllOccurences: anCollectionOfOccurrences ]
]
{ #category : #preconditions }
RBRemoveMethodTransformation >> checkReferences [
| occurrences |
occurrences := OrderedCollection new.
self model
allReferencesTo: selector
do: [ :aRBMethod |
(selector = aRBMethod selector)
ifFalse: [ occurrences add: selector -> aRBMethod ] ].
occurrences ifEmpty: [ ^ self ].
^ self checkBrowseOccurrences: occurrences
]
{ #category : #preconditions }
RBRemoveMethodTransformation >> checkSuperMethods [
(self justSendsSuper: selector) ifTrue: [ ^ self ]
ifFalse: [ (self superclassEquivalentlyDefines: selector)
ifTrue: [ (self definingClass parseTreeFor: selector) superMessages
ifNotEmpty: [ self
refactoringError: ('Although <1s> is equivalent to a superclass method,<n>
it contains a super send so it might modify behavior.'
expandMacrosWith: selector ) ] ]
ifFalse: [ self checkReferences ] ]
]
{ #category : #preconditions }
RBRemoveMethodTransformation >> justSendsSuper: aSelector [
| matcher parseTree superclass |
matcher := RBParseTreeSearcher justSendsSuper.
parseTree := self definingClass parseTreeFor: aSelector.
( matcher executeTree: parseTree initialAnswer: false )
ifFalse: [ ^ false ].
parseTree lastIsReturn
ifTrue: [ ^ true ]. "Since there isn't a superclass that implements the message, we can
delete it since it would be an error anyway."
superclass := self definingClass superclass whichClassIncludesSelector: aSelector.
superclass ifNil: [ ^ true ].
parseTree := superclass parseTreeFor: aSelector.
matcher := RBParseTreeSearcher new.
matcher
matches: '^``@object'
do: [ :aNode :answer |
answer
add: aNode value;
yourself
].
matcher executeTree: parseTree initialAnswer: Set new.
^ matcher answer
allSatisfy: [ :each | "^ (matcher answer
detect: [:each | (each isVariable and: [each name = 'self']) not]
ifNone: [nil]) isNil" each isVariable and: [ each name = 'self' ] ]
]
{ #category : #preconditions }
RBRemoveMethodTransformation >> preconditions [
^ (RBCondition definesSelector: selector in: self definingClass)
& (RBCondition withBlock: [ self checkSuperMethods. true ])
]
{ #category : #executing }
RBRemoveMethodTransformation >> privateTransform [
self definingClass removeMethod: selector
]
{ #category : #api }
RBRemoveMethodTransformation >> selector: aSelector from: aClassName [
self className: aClassName.
selector := aSelector
]
{ #category : #printing }
RBRemoveMethodTransformation >> storeOn: aStream [
aStream nextPut: $(.
self class storeOn: aStream.
aStream
nextPutAll: ' selector: ';
nextPutAll: selector;
nextPutAll: ' from: '.
class storeOn: aStream.
aStream nextPut: $)
]
{ #category : #preconditions }
RBRemoveMethodTransformation >> superclassEquivalentlyDefines: aSelector [
| superTree myTree |
self definingClass superclass ifNil: [ ^ false ].
superTree := self definingClass superclass parseTreeFor: aSelector.
myTree := self definingClass parseTreeFor: aSelector.
( superTree isNil or: [ myTree isNil ] )
ifTrue: [ ^ false ].
^ superTree equalTo: myTree exceptForVariables: #()
]