forked from pharo-project/pharo
/
RBRemoveMethodRefactoring.class.st
182 lines (162 loc) · 5.83 KB
/
RBRemoveMethodRefactoring.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
"
I am a refactoring for removing a method.
My preconditions verify that this method is not referenced anywhere.
"
Class {
#name : #RBRemoveMethodRefactoring,
#superclass : #RBMethodRefactoring,
#instVars : [
'selectors'
],
#category : #'Refactoring-Core-Refactorings'
}
{ #category : #'instance creation' }
RBRemoveMethodRefactoring class >> model: aRBSmalltalk removeMethods: selectorCollection from: aClass [
^(self new)
model: aRBSmalltalk;
removeMethods: selectorCollection from: aClass;
yourself
]
{ #category : #'instance creation' }
RBRemoveMethodRefactoring class >> removeMethods: selectorCollection from: aClass [
^self new removeMethods: selectorCollection from: aClass
]
{ #category : #preconditions }
RBRemoveMethodRefactoring >> checkBrowseAllOccurrences: 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 }
RBRemoveMethodRefactoring >> checkBrowseOccurrenceOf: selector in: 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 }
RBRemoveMethodRefactoring >> checkBrowseOccurrences: anCollectionOfOccurrences [
^ anCollectionOfOccurrences size = 1
ifTrue: [ self checkBrowseOccurrenceOf: anCollectionOfOccurrences first key in: anCollectionOfOccurrences first value ]
ifFalse: [ self checkBrowseAllOccurrences: anCollectionOfOccurrences ]
]
{ #category : #preconditions }
RBRemoveMethodRefactoring >> checkReferencesToAnyOf: aSelectorCollection [
| occurrences |
occurrences := OrderedCollection new.
aSelectorCollection
do: [ :each |
self model
allReferencesTo: each
do: [ :aRBMethod |
(aSelectorCollection includes: aRBMethod selector)
ifFalse: [ occurrences add: each -> aRBMethod ] ] ].
occurrences ifEmpty: [ ^ self ].
^ self checkBrowseOccurrences: occurrences
]
{ #category : #preconditions }
RBRemoveMethodRefactoring >> checkReferencesToSuperSendsToAnyOf: superMessages [
[superMessages isEmpty] whileFalse:
[self
refactoringWarning: ('Although <1s> is equivalent to a superclass method,<n>it contains a super send so it might modify behavior.<n>Proceed anyway?'
expandMacrosWith: superMessages first).
superMessages remove: superMessages first]
]
{ #category : #preconditions }
RBRemoveMethodRefactoring >> checkSuperMethods [
| superMessages nonSupers |
nonSupers := OrderedCollection new.
superMessages := OrderedCollection new.
(selectors reject: [:each | self justSendsSuper: each]) do:
[:each |
(self superclassEquivalentlyDefines: each)
ifTrue:
[(class parseTreeFor: each) superMessages isEmpty
ifFalse: [superMessages add: each]]
ifFalse: [nonSupers add: each]].
nonSupers isEmpty & superMessages isEmpty ifTrue: [^self].
self checkReferencesToAnyOf: nonSupers.
self checkReferencesToSuperSendsToAnyOf: superMessages
]
{ #category : #preconditions }
RBRemoveMethodRefactoring >> justSendsSuper: aSelector [
| matcher parseTree superclass |
matcher := self parseTreeSearcherClass justSendsSuper.
parseTree := class parseTreeFor: aSelector.
( matcher executeTree: parseTree initialAnswer: false )
ifFalse: [ ^ false ].
parseTree lastIsReturn
ifTrue: [ ^ true ].
superclass := class superclass whichClassIncludesSelector: aSelector.
superclass ifNil: [ ^ true ]. "Since there isn't a superclass that implements the message, we can
delete it since it would be an error anyway."
parseTree := superclass parseTreeFor: aSelector.
matcher := self parseTreeSearcher.
matcher
matches: '^``@object'
do: [ :aNode :answer |
answer
add: aNode value;
yourself
].
matcher executeTree: parseTree initialAnswer: Set new.
^ ( matcher answer anySatisfy: [ :each | each isSelfVariable not ]) not
]
{ #category : #preconditions }
RBRemoveMethodRefactoring >> preconditions [
^ (selectors
inject: self emptyCondition
into: [ :cond :each |
cond & (RBCondition definesSelector: each in: class) ])
& (RBCondition withBlock: [
self checkSuperMethods.
true ])
]
{ #category : #initialization }
RBRemoveMethodRefactoring >> removeMethods: selectorCollection from: aClass [
class := self classObjectFor: aClass.
selectors := selectorCollection
]
{ #category : #printing }
RBRemoveMethodRefactoring >> storeOn: aStream [
aStream nextPut: $(.
self class storeOn: aStream.
aStream nextPutAll: ' removeMethods: '.
selectors asArray storeOn: aStream.
aStream nextPutAll: ' from: '.
class storeOn: aStream.
aStream nextPut: $)
]
{ #category : #preconditions }
RBRemoveMethodRefactoring >> superclassEquivalentlyDefines: aSelector [
| superTree myTree |
class superclass ifNil: [ ^ false ].
superTree := class superclass parseTreeFor: aSelector.
myTree := class parseTreeFor: aSelector.
( superTree isNil or: [ myTree isNil ] )
ifTrue: [ ^ false ].
^ superTree equalTo: myTree exceptForVariables: #()
]
{ #category : #transforming }
RBRemoveMethodRefactoring >> transform [
selectors do: [:each | class removeMethod: each]
]