6179
|
1 |
"
|
|
2 |
COPYRIGHT (c) 2004 by eXept Software AG
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
5592
|
13 |
"{ Package: 'stx:libtool' }"
|
5591
|
14 |
|
|
15 |
"{ NameSpace: Tools }"
|
|
16 |
|
|
17 |
Object subclass:#ClassChecker
|
|
18 |
instanceVariableNames:'checkedClass badClasses badClassInfo badMethods badMethodInfo
|
|
19 |
obsoleteWarners allObsoleteMethods checksPerformed'
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Interface-Browsers-New'
|
|
23 |
!
|
|
24 |
|
|
25 |
!ClassChecker class methodsFor:'documentation'!
|
|
26 |
|
6179
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 2004 by eXept Software AG
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
5591
|
41 |
documentation
|
|
42 |
"
|
|
43 |
embeddable application displaying the class-categories.
|
|
44 |
Provides an outputGenerator, which enumerates the classes in
|
|
45 |
the selected categories.
|
|
46 |
|
|
47 |
[author:]
|
|
48 |
Claus Gittinger (cg@exept.de)
|
|
49 |
"
|
|
50 |
! !
|
|
51 |
|
|
52 |
!ClassChecker class methodsFor:'queries'!
|
|
53 |
|
|
54 |
individualChecks
|
|
55 |
"return a list of supported checks"
|
|
56 |
|
|
57 |
|checks|
|
|
58 |
|
|
59 |
checks := OrderedCollection new.
|
|
60 |
self methodDictionary keysAndValuesDo:[:sel :mthd |
|
|
61 |
(mthd category startsWith:'checks') ifTrue:[checks add:sel]
|
|
62 |
].
|
|
63 |
checks sort.
|
|
64 |
^ checks
|
|
65 |
|
|
66 |
"
|
|
67 |
self individualChecks
|
|
68 |
"
|
|
69 |
|
|
70 |
"Created: / 18.8.2000 / 22:30:46 / cg"
|
|
71 |
"Modified: / 18.8.2000 / 22:31:02 / cg"
|
|
72 |
! !
|
|
73 |
|
|
74 |
!ClassChecker methodsFor:'accessing'!
|
|
75 |
|
|
76 |
badClassInfo
|
|
77 |
^ badClassInfo
|
|
78 |
!
|
|
79 |
|
|
80 |
badMethodInfo
|
|
81 |
^ badMethodInfo
|
|
82 |
!
|
|
83 |
|
|
84 |
checkedClass
|
|
85 |
"return the value of the instance variable 'checkedClass' (automatically generated)"
|
|
86 |
|
|
87 |
^ checkedClass
|
|
88 |
!
|
|
89 |
|
|
90 |
checkedClass:something
|
|
91 |
"set the value of the instance variable 'checkedClass' (automatically generated)"
|
|
92 |
|
|
93 |
checkedClass := something.
|
|
94 |
checksPerformed := nil.
|
|
95 |
! !
|
|
96 |
|
|
97 |
!ClassChecker methodsFor:'checking'!
|
|
98 |
|
|
99 |
allChecks
|
|
100 |
self errorChecks.
|
|
101 |
self warningChecks.
|
|
102 |
self styleChecks.
|
|
103 |
!
|
|
104 |
|
|
105 |
doCheck:whichCheck
|
|
106 |
checksPerformed isNil ifTrue:[
|
|
107 |
checksPerformed := Set new.
|
|
108 |
].
|
|
109 |
(checksPerformed includes:whichCheck) ifFalse:[
|
|
110 |
checksPerformed add:whichCheck.
|
|
111 |
self perform:whichCheck
|
|
112 |
]
|
|
113 |
!
|
|
114 |
|
|
115 |
errorChecks
|
|
116 |
self doCheck:#subclassResponsibilityNotDefined.
|
|
117 |
self doCheck:#sentNotImplemented.
|
|
118 |
self doCheck:#definesEqualButNotHash.
|
|
119 |
self doCheck:#instanceVariablesNeverWritten.
|
|
120 |
!
|
|
121 |
|
|
122 |
styleChecks
|
|
123 |
self doCheck:#checkProtocols.
|
|
124 |
!
|
|
125 |
|
|
126 |
warningChecks
|
|
127 |
self doCheck:#messageNeverSentAndNotUsedAsSymbol.
|
|
128 |
self doCheck:#sendsObsoleteMessages.
|
|
129 |
self doCheck:#instanceVariablesNeverUsed.
|
|
130 |
self doCheck:#instanceVariablesNeverWritten.
|
|
131 |
self doCheck:#classVariablesNeverUsed.
|
|
132 |
self doCheck:#classVariablesNeverWritten.
|
|
133 |
self doCheck:#classInstanceVariablesNeverUsed.
|
|
134 |
self doCheck:#classInstanceVariablesNeverWritten.
|
|
135 |
! !
|
|
136 |
|
|
137 |
!ClassChecker methodsFor:'checks-individual'!
|
|
138 |
|
|
139 |
checkProtocols
|
|
140 |
checkedClass instAndClassSelectorsAndMethodsDo:[:mSelector :method |
|
|
141 |
self checkProtocolOf:method
|
|
142 |
]
|
|
143 |
!
|
|
144 |
|
|
145 |
checkVariableNameConventions
|
|
146 |
|badInstVars badClassVars|
|
|
147 |
|
|
148 |
badInstVars := checkedClass instanceVariableNames select:[:varName | varName first isUppercase].
|
|
149 |
badClassVars := checkedClass classVarNames select:[:varName | varName first isLowercase].
|
|
150 |
|
|
151 |
badInstVars notEmpty ifTrue:[
|
|
152 |
badInstVars := (badInstVars collect:[:v | '''' , v , '''']) asStringWith:$, .
|
|
153 |
self
|
|
154 |
rememberBadClass:checkedClass
|
|
155 |
info:('instVars ' , badInstVars , ' should be lowercase (#checkNameConventions)').
|
|
156 |
].
|
|
157 |
badClassVars notEmpty ifTrue:[
|
|
158 |
badClassVars := (badClassVars collect:[:v | '''' , v , '''']) asStringWith:$, .
|
|
159 |
self
|
|
160 |
rememberBadClass:checkedClass
|
|
161 |
info:('classVars ' , badClassVars , ' should be uppercase (#checkNameConventions)').
|
|
162 |
].
|
|
163 |
!
|
|
164 |
|
|
165 |
classInstanceVariablesNeverUsed
|
|
166 |
self instanceVariablesNeverUsedIn:checkedClass theMetaclass.
|
|
167 |
!
|
|
168 |
|
|
169 |
classInstanceVariablesNeverWritten
|
|
170 |
self instanceVariablesNeverWrittenIn:checkedClass theMetaclass.
|
|
171 |
!
|
|
172 |
|
|
173 |
classVariablesNeverUsed
|
|
174 |
|cls notUsedHere notUsedAnyWhere anySubclass|
|
|
175 |
|
|
176 |
cls := checkedClass theNonMetaclass.
|
|
177 |
notUsedHere := cls classVarNames asSet.
|
|
178 |
notUsedHere isEmpty ifTrue:[^ self].
|
|
179 |
|
|
180 |
self removeUsedClassVariablesIn:cls from:notUsedHere.
|
|
181 |
self removeUsedClassVariablesIn:cls class from:notUsedHere.
|
|
182 |
|
|
183 |
notUsedHere notEmpty ifTrue:[
|
|
184 |
notUsedAnyWhere := notUsedHere copy.
|
|
185 |
anySubclass := false.
|
|
186 |
cls allSubclassesDo:[:eachSubclass |
|
|
187 |
anySubclass := true.
|
|
188 |
notUsedAnyWhere notEmpty ifTrue:[
|
|
189 |
self removeUsedClassVariablesIn:eachSubclass from:notUsedAnyWhere.
|
|
190 |
self removeUsedClassVariablesIn:eachSubclass class from:notUsedAnyWhere.
|
|
191 |
]
|
|
192 |
].
|
|
193 |
|
|
194 |
notUsedHere do:[:eachVariable |
|
|
195 |
|className|
|
|
196 |
|
|
197 |
className := checkedClass name allBold.
|
|
198 |
self
|
|
199 |
rememberBadClass:cls
|
|
200 |
info:('classVar ' , eachVariable allBold , ' is unused in ' , className , ' (#classVariablesNeverUsed)').
|
|
201 |
(notUsedAnyWhere includes:eachVariable) ifTrue:[
|
|
202 |
self
|
|
203 |
rememberBadClass:cls
|
|
204 |
info:('classVar ' , eachVariable allBold , ' is not even used in subclasses of ' , className , ' (#classVariablesNeverUsed)')
|
|
205 |
]
|
|
206 |
].
|
|
207 |
]
|
|
208 |
!
|
|
209 |
|
|
210 |
classVariablesNeverWritten
|
|
211 |
|cls notWrittenHere notWrittenAnyWhere anySubclass|
|
|
212 |
|
|
213 |
cls := checkedClass theNonMetaclass.
|
|
214 |
notWrittenHere := cls classVarNames asSet.
|
|
215 |
notWrittenHere isEmpty ifTrue:[^ self].
|
|
216 |
|
|
217 |
self removeWrittenClassVariablesIn:cls from:notWrittenHere.
|
|
218 |
self removeWrittenClassVariablesIn:cls class from:notWrittenHere.
|
|
219 |
|
|
220 |
notWrittenHere notEmpty ifTrue:[
|
|
221 |
notWrittenAnyWhere := notWrittenHere copy.
|
|
222 |
anySubclass := false.
|
|
223 |
cls allSubclassesDo:[:eachSubclass |
|
|
224 |
anySubclass := true.
|
|
225 |
notWrittenAnyWhere notEmpty ifTrue:[
|
|
226 |
self removeWrittenClassVariablesIn:eachSubclass from:notWrittenAnyWhere.
|
|
227 |
self removeWrittenClassVariablesIn:eachSubclass class from:notWrittenAnyWhere.
|
|
228 |
]
|
|
229 |
].
|
|
230 |
|
|
231 |
notWrittenHere do:[:eachVariable |
|
|
232 |
|className|
|
|
233 |
|
|
234 |
className := cls name allBold.
|
|
235 |
self
|
|
236 |
rememberBadClass:cls
|
|
237 |
info:('classVar ' , eachVariable allBold , ' is not set in ' , className , ' (#classVariablesNeverWritten)').
|
|
238 |
(notWrittenAnyWhere includes:eachVariable) ifTrue:[
|
|
239 |
self
|
|
240 |
rememberBadClass:cls
|
|
241 |
info:('classVar ' , eachVariable allBold , ' is not even set in subclasses of ' , className , ' (#classVariablesNeverWritten)')
|
|
242 |
]
|
|
243 |
].
|
|
244 |
]
|
|
245 |
!
|
|
246 |
|
|
247 |
definesEqualButNotHash
|
|
248 |
(checkedClass includesSelector:#=) ifTrue:[
|
|
249 |
(checkedClass includesSelector:#hash) ifFalse:[
|
|
250 |
self
|
|
251 |
rememberBadClass:checkedClass
|
|
252 |
info:(checkedClass name allBold, ' redefines #=, but not #hash (#definesEqualButNotHash)').
|
|
253 |
]
|
|
254 |
]
|
|
255 |
!
|
|
256 |
|
|
257 |
instanceVariablesNeverUsed
|
|
258 |
self instanceVariablesNeverUsedIn:checkedClass theNonMetaclass.
|
|
259 |
!
|
|
260 |
|
|
261 |
instanceVariablesNeverWritten
|
|
262 |
self instanceVariablesNeverWrittenIn:checkedClass theNonMetaclass.
|
|
263 |
!
|
|
264 |
|
|
265 |
messageNeverSent
|
|
266 |
"/ old code: (slow)
|
|
267 |
"/ checkedClass instAndClassSelectorsAndMethodsDo:[:mSelector :method |
|
|
268 |
"/ (self anySendsOf:mSelector) ifFalse:[
|
|
269 |
"/ self
|
|
270 |
"/ rememberBadMethod:method
|
|
271 |
"/ info:('#' , mSelector , ' is nowhere sent (#messageNeverSent)')
|
|
272 |
"/ ].
|
|
273 |
"/ ].
|
|
274 |
|
|
275 |
|selectorsOfInterest|
|
|
276 |
|
|
277 |
selectorsOfInterest := IdentitySet new.
|
|
278 |
checkedClass instAndClassSelectorsAndMethodsDo:[:mSelector :method | selectorsOfInterest add:mSelector].
|
|
279 |
(self messagesNeverSentIn:selectorsOfInterest) do:[:eachNeverSent |
|
|
280 |
self
|
|
281 |
rememberBadMethod:(checkedClass compiledMethodAt:eachNeverSent)
|
|
282 |
key:#messageNeverSent
|
|
283 |
info:('#' , eachNeverSent allBold, ' is nowhere sent (#messageNeverSent)')
|
|
284 |
].
|
|
285 |
|
|
286 |
"Modified: / 18.8.2000 / 22:54:45 / cg"
|
|
287 |
!
|
|
288 |
|
|
289 |
messageNeverSentAndNotUsedAsSymbol
|
|
290 |
|selectorsOfInterest|
|
|
291 |
|
|
292 |
selectorsOfInterest := IdentitySet new.
|
|
293 |
checkedClass instAndClassSelectorsAndMethodsDo:[:mSelector :method | selectorsOfInterest add:mSelector].
|
|
294 |
(self messagesNeverSentAndNotUsedAsSymbolIn:selectorsOfInterest) do:[:eachNeverSent |
|
|
295 |
self
|
|
296 |
rememberBadMethod:(checkedClass compiledMethodAt:eachNeverSent)
|
|
297 |
key:#messageNeverSentAndNotUsedAsSymbol
|
|
298 |
info:('#' , eachNeverSent allBold, ' is nowhere sent and selector-symbol not used in any method (#messageNeverSentAndNotUsedAsSymbol)')
|
|
299 |
].
|
|
300 |
|
|
301 |
"Modified: / 18.8.2000 / 23:10:56 / cg"
|
|
302 |
!
|
|
303 |
|
|
304 |
sendsObsoleteMessages
|
|
305 |
obsoleteWarners isNil ifTrue:[
|
|
306 |
obsoleteWarners := Object selectors select:[:each | each startsWith:'obsoleteMethodWarning'].
|
|
307 |
].
|
|
308 |
allObsoleteMethods isNil ifTrue:[
|
|
309 |
allObsoleteMethods := IdentitySet new.
|
|
310 |
Method allSubInstancesDo:[:eachMethod | |lits|
|
|
311 |
((lits := eachMethod literals) notNil
|
|
312 |
and:[lits includesAny:obsoleteWarners]) ifTrue:[
|
|
313 |
(eachMethod messagesSent includesAny:obsoleteWarners) ifTrue:[
|
|
314 |
allObsoleteMethods add:(eachMethod selector).
|
|
315 |
].
|
|
316 |
].
|
|
317 |
].
|
|
318 |
|
|
319 |
"/ manually patchup; there are some which should not be considered as bad ...
|
|
320 |
allObsoleteMethods removeAllFoundIn:#( #',' #'at:' #'at:put:' #'raise').
|
|
321 |
allObsoleteMethods removeAllFoundIn:#( #'asText').
|
|
322 |
allObsoleteMethods removeAllFoundIn:obsoleteWarners.
|
|
323 |
].
|
|
324 |
|
|
325 |
checkedClass instAndClassSelectorsAndMethodsDo:[:sel :mthd |
|
|
326 |
|lits sentMessages whichOnes pT searcher nodes|
|
|
327 |
|
|
328 |
((lits := mthd literals) notNil
|
|
329 |
and:[lits includesAny:allObsoleteMethods]) ifTrue:[
|
|
330 |
sentMessages := mthd messagesSent.
|
|
331 |
(sentMessages includesAny:allObsoleteMethods) ifTrue:[
|
|
332 |
whichOnes := sentMessages select:[:each | allObsoleteMethods includes:each].
|
|
333 |
|
|
334 |
(RBParser notNil
|
|
335 |
and:[RBParser isLoaded]) ifTrue:[
|
|
336 |
"/ lets look at this a bit more detailed;
|
|
337 |
"/ parse it and see if we can filter out any messages
|
|
338 |
"/ (i.e. look if we can figure out the receiver type)
|
|
339 |
|
|
340 |
"/ pT := RBParser
|
|
341 |
"/ parseMethod: (mthd source)
|
|
342 |
"/ onError: [:aString :pos | nil].
|
|
343 |
"/ pT notNil ifTrue:[
|
|
344 |
"/ searcher := ParseTreeSearcher
|
|
345 |
"/ allMessageSendsMatchingAny:whichOnes ignoreCase:false.
|
|
346 |
"/ nodes := searcher executeTree:pT initialAnswer:(OrderedCollection new).
|
|
347 |
"/ nodes := nodes
|
|
348 |
"/ select:
|
|
349 |
"/ [:aSendNode |
|
|
350 |
"/ self halt.
|
|
351 |
"/ ].
|
|
352 |
"/ whichOnes := nodes collect:[:aSendNode | aSendNode selector].
|
|
353 |
"/ ].
|
|
354 |
].
|
|
355 |
|
|
356 |
whichOnes asOrderedCollection sort do:[:eachObsoleteMessage |
|
|
357 |
self
|
|
358 |
rememberBadMethod:mthd
|
|
359 |
key:#sendsObsoleteMessages
|
|
360 |
info:('possibly sends an obsolete message: ' , eachObsoleteMessage , ' (#sendsObsoleteMessages)')
|
|
361 |
].
|
|
362 |
].
|
|
363 |
]
|
|
364 |
]
|
|
365 |
!
|
|
366 |
|
|
367 |
sentNotImplemented
|
|
368 |
|alreadyChecked alreadyCheckedSelf alreadyCheckedSuper|
|
|
369 |
|
|
370 |
alreadyChecked := IdentitySet new.
|
|
371 |
alreadyCheckedSelf := IdentitySet new.
|
|
372 |
alreadyCheckedSuper := IdentitySet new.
|
|
373 |
|
|
374 |
checkedClass methodDictionary keysAndValuesDo:[:mSelector :method |
|
|
375 |
|sentSelectors selfSelectors superSelectors selectorsNoWhereImplemented|
|
|
376 |
|
|
377 |
selectorsNoWhereImplemented := IdentitySet new.
|
|
378 |
sentSelectors := method messagesSent.
|
|
379 |
sentSelectors do:[:eachSelector |
|
|
380 |
(alreadyChecked includes:eachSelector) ifFalse:[
|
|
381 |
(self anyImplementationOf:eachSelector) ifTrue:[
|
|
382 |
alreadyChecked add:eachSelector
|
|
383 |
] ifFalse:[
|
|
384 |
selectorsNoWhereImplemented add:eachSelector.
|
|
385 |
self
|
|
386 |
rememberBadMethod:method
|
|
387 |
key:#sentNotImplemented
|
|
388 |
info:('#' , eachSelector allBold, ' is nowhere implemented (#sentNotImplemented)')
|
|
389 |
]
|
|
390 |
]
|
|
391 |
].
|
|
392 |
|
|
393 |
selfSelectors := method messagesSentToSelf.
|
|
394 |
selfSelectors do:[:eachSelector |
|
|
395 |
(selectorsNoWhereImplemented includes:eachSelector) ifFalse:[
|
|
396 |
(alreadyCheckedSelf includes:eachSelector) ifFalse:[
|
|
397 |
(self anyImplementationOf:eachSelector inOrAbove:checkedClass) ifTrue:[
|
|
398 |
alreadyCheckedSelf add:eachSelector
|
|
399 |
] ifFalse:[
|
|
400 |
self
|
|
401 |
rememberBadMethod:method
|
|
402 |
key:#sentNotImplemented
|
|
403 |
info:('#' , eachSelector allBold, ' is not implemented in the class (#sentNotImplemented)')
|
|
404 |
]
|
|
405 |
]
|
|
406 |
]
|
|
407 |
].
|
|
408 |
|
|
409 |
superSelectors := method messagesSentToSuper.
|
|
410 |
superSelectors do:[:eachSelector |
|
|
411 |
(selectorsNoWhereImplemented includes:eachSelector) ifFalse:[
|
|
412 |
(alreadyCheckedSuper includes:eachSelector) ifFalse:[
|
|
413 |
(self anyImplementationOf:eachSelector inOrAbove:(checkedClass superclass)) ifTrue:[
|
|
414 |
alreadyCheckedSuper add:eachSelector
|
|
415 |
] ifFalse:[
|
|
416 |
self
|
|
417 |
rememberBadMethod:method
|
|
418 |
key:#sentNotImplemented
|
|
419 |
info:('#' , eachSelector allBold, ' is not implemented in any superclass (#sentNotImplemented)')
|
|
420 |
]
|
|
421 |
]
|
|
422 |
]
|
|
423 |
].
|
|
424 |
].
|
|
425 |
!
|
|
426 |
|
|
427 |
subclassResponsibilityNotDefined
|
|
428 |
|classesInBetween|
|
|
429 |
|
|
430 |
checkedClass allSuperclasses do:[:eachSuperClass |
|
|
431 |
eachSuperClass methodDictionary keysAndValuesDo:[:mSelector :method |
|
|
432 |
(method referencesLiteral:#subclassResponsibility) ifTrue:[
|
|
433 |
"/ parse it to see if it really does ...
|
|
434 |
(method sends:#subclassResponsibility) ifTrue:[
|
|
435 |
"/ ok, got one;
|
|
436 |
"/ now, see if it is defined below this superClass
|
|
437 |
classesInBetween := checkedClass withAllSuperclasses copy.
|
|
438 |
classesInBetween removeAll:(eachSuperClass withAllSuperclasses).
|
|
439 |
(self anyImplementationOf:mSelector in:classesInBetween) ifFalse:[
|
|
440 |
(self methodShouldBeIgnoredInSubclassResponsibilityNotDefined:method)
|
|
441 |
ifFalse:[
|
|
442 |
self
|
|
443 |
rememberBadMethod:method
|
|
444 |
key:#subclassResponsibilityNotDefined
|
|
445 |
info:(checkedClass name allBold , ' should redefine the #' , mSelector allBold , ' method (#subclassResponsibilityNotDefined)').
|
|
446 |
].
|
|
447 |
].
|
|
448 |
]
|
|
449 |
]
|
|
450 |
]
|
|
451 |
].
|
|
452 |
! !
|
|
453 |
|
|
454 |
!ClassChecker methodsFor:'helpers'!
|
|
455 |
|
|
456 |
anyImplementationOf:aSelector
|
|
457 |
Smalltalk allClassesDo:[:eachClass |
|
|
458 |
(eachClass theNonMetaclass includesSelector:aSelector) ifTrue:[^ true].
|
|
459 |
(eachClass theMetaclass includesSelector:aSelector) ifTrue:[^ true].
|
|
460 |
].
|
|
461 |
^ false
|
|
462 |
!
|
|
463 |
|
|
464 |
anyImplementationOf:aSelector in:aCollectionOfClasses
|
|
465 |
aCollectionOfClasses do:[:eachClass |
|
|
466 |
(eachClass includesSelector:aSelector) ifTrue:[^ true].
|
|
467 |
].
|
|
468 |
^ false
|
|
469 |
!
|
|
470 |
|
|
471 |
anyImplementationOf:aSelector inOrAbove:aClass
|
|
472 |
aClass withAllSuperclassesDo:[:eachClass |
|
|
473 |
(eachClass includesSelector:aSelector) ifTrue:[^ true].
|
|
474 |
].
|
|
475 |
^ false
|
|
476 |
!
|
|
477 |
|
|
478 |
anySendsOf:aSelector
|
|
479 |
Smalltalk allClassesDo:[:eachClass |
|
|
480 |
eachClass instAndClassSelectorsAndMethodsDo:[:mSelector :mthd |
|
|
481 |
(mthd sends:aSelector) ifTrue:[^ true].
|
|
482 |
].
|
|
483 |
].
|
|
484 |
^ false
|
|
485 |
!
|
|
486 |
|
|
487 |
checkProtocolOf:aMethod
|
|
488 |
|mClass mSelector protocol superClass implClass superProtocol|
|
|
489 |
|
|
490 |
mClass := aMethod mclass.
|
|
491 |
mSelector := aMethod selector.
|
|
492 |
|
|
493 |
protocol := aMethod category.
|
|
494 |
protocol isNil ifTrue: [^true].
|
|
495 |
|
|
496 |
superClass := mClass superclass.
|
|
497 |
superClass isNil ifTrue: [^true].
|
|
498 |
|
|
499 |
implClass := superClass whichClassIncludesSelector:mSelector.
|
|
500 |
implClass isNil ifTrue: [^true].
|
|
501 |
|
|
502 |
superProtocol := (implClass compiledMethodAt:mSelector) category.
|
|
503 |
superProtocol = protocol ifTrue: [^true].
|
|
504 |
superProtocol isNil ifTrue: [^true].
|
|
505 |
|
|
506 |
self
|
|
507 |
rememberBadMethod:aMethod
|
|
508 |
key:#checkProtocols
|
|
509 |
info:('#' , mSelector allBold , ' is classified under "' , protocol allBold , '" in '
|
|
510 |
, mClass name , ' and under "' , superProtocol allBold , '" in ' , implClass name
|
|
511 |
, ' (#checkProtocols)')
|
|
512 |
|
|
513 |
"Modified: / 18.8.2000 / 23:13:53 / cg"
|
|
514 |
!
|
|
515 |
|
|
516 |
checkUnusedVariables:aMethod rememberReadInstVarsIn:readInstVars writtenInstVarsIn:writtenInstVars readClassVarsIn:readClassVars writtenClassVarsIn:writtenClassVars
|
|
517 |
|p mClass sourceString|
|
|
518 |
|
|
519 |
sourceString := aMethod source.
|
|
520 |
|
|
521 |
mClass := aMethod mclass.
|
|
522 |
|
|
523 |
p := Parser parseMethodSilent:sourceString in:mClass.
|
|
524 |
readInstVars addAll:(p readInstVars).
|
|
525 |
writtenInstVars addAll:(p modifiedInstVars).
|
|
526 |
readClassVars addAll:(p readClassVars).
|
|
527 |
writtenClassVars addAll:(p modifiedClassVars).
|
|
528 |
!
|
|
529 |
|
|
530 |
instanceVariablesNeverUsedIn:aClass
|
|
531 |
|notUsedHere notUsedAnyWhere anySubclass|
|
|
532 |
|
|
533 |
notUsedHere := aClass instVarNames asSet.
|
|
534 |
notUsedHere isEmpty ifTrue:[^ self].
|
|
535 |
|
|
536 |
self removeUsedInstanceVariablesIn:aClass from:notUsedHere.
|
|
537 |
|
|
538 |
notUsedHere notEmpty ifTrue:[
|
|
539 |
notUsedAnyWhere := notUsedHere copy.
|
|
540 |
anySubclass := false.
|
|
541 |
aClass allSubclassesDo:[:eachSubclass |
|
|
542 |
anySubclass := true.
|
|
543 |
notUsedAnyWhere notEmpty ifTrue:[
|
|
544 |
self removeUsedInstanceVariablesIn:eachSubclass from:notUsedAnyWhere.
|
|
545 |
]
|
|
546 |
].
|
|
547 |
|
|
548 |
notUsedHere do:[:eachVariable |
|
|
549 |
|className|
|
|
550 |
|
|
551 |
className := aClass name allBold.
|
|
552 |
self
|
|
553 |
rememberBadClass:aClass
|
|
554 |
info:('instVar ' , eachVariable allBold , ' is unused in ' , className , ' (#instanceVariablesNeverUsed)').
|
|
555 |
|
|
556 |
anySubclass ifTrue:[
|
|
557 |
(notUsedAnyWhere includes:eachVariable) ifTrue:[
|
|
558 |
self
|
|
559 |
rememberBadClass:aClass
|
|
560 |
info:('instVar ' , eachVariable allBold , ' is not even used in subclasses of ' , className , ' (#instanceVariablesNeverUsed)')
|
|
561 |
]
|
|
562 |
]
|
|
563 |
].
|
|
564 |
]
|
|
565 |
!
|
|
566 |
|
|
567 |
instanceVariablesNeverWrittenIn:aClass
|
|
568 |
|notWrittenHere notWrittenAnyWhere anySubclass|
|
|
569 |
|
|
570 |
notWrittenHere := aClass instVarNames asSet.
|
|
571 |
notWrittenHere isEmpty ifTrue:[^ self].
|
|
572 |
|
|
573 |
self removeWrittenInstanceVariablesIn:aClass from:notWrittenHere.
|
|
574 |
|
|
575 |
notWrittenHere notEmpty ifTrue:[
|
|
576 |
notWrittenAnyWhere := notWrittenHere copy.
|
|
577 |
anySubclass := false.
|
|
578 |
aClass allSubclassesDo:[:eachSubclass |
|
|
579 |
anySubclass := true.
|
|
580 |
notWrittenAnyWhere notEmpty ifTrue:[
|
|
581 |
self removeWrittenInstanceVariablesIn:eachSubclass from:notWrittenAnyWhere.
|
|
582 |
]
|
|
583 |
].
|
|
584 |
|
|
585 |
notWrittenHere do:[:eachVariable |
|
|
586 |
|className|
|
|
587 |
|
|
588 |
className := aClass name allBold.
|
|
589 |
self
|
|
590 |
rememberBadClass:aClass
|
|
591 |
info:('instVar ' , eachVariable allBold , ' is nowhere set in ' , className , ' (#instanceVariablesNeverWritten)').
|
|
592 |
anySubclass ifTrue:[
|
|
593 |
(notWrittenAnyWhere includes:eachVariable) ifTrue:[
|
|
594 |
self
|
|
595 |
rememberBadClass:aClass
|
|
596 |
info:('instVar ' , eachVariable allBold , ' is not even set in subclasses of ' , className , ' (#instanceVariablesNeverWritten)')
|
|
597 |
]
|
|
598 |
]
|
|
599 |
].
|
|
600 |
]
|
|
601 |
!
|
|
602 |
|
|
603 |
messagesNeverSentAndNotUsedAsSymbolIn:selectorsOfInterest
|
|
604 |
|remaining toRemove checkBlock|
|
|
605 |
|
|
606 |
remaining := selectorsOfInterest copy asIdentitySet.
|
|
607 |
toRemove := IdentitySet new.
|
|
608 |
|
|
609 |
checkBlock := [:eachClass |
|
|
610 |
eachClass instAndClassSelectorsAndMethodsDo:[:mSel :mthd | |lits|
|
|
611 |
lits := mthd literals.
|
|
612 |
lits notNil ifTrue:[
|
|
613 |
lits traverse:[:eachLiteral |
|
|
614 |
eachLiteral isSymbol ifTrue:[
|
|
615 |
remaining remove:eachLiteral ifAbsent:nil.
|
|
616 |
]
|
|
617 |
].
|
|
618 |
remaining isEmpty ifTrue:[^ remaining].
|
|
619 |
]
|
|
620 |
].
|
|
621 |
].
|
|
622 |
|
|
623 |
"/ start searching in the checkedClass - chances are high, we find some here
|
|
624 |
checkBlock value:checkedClass.
|
|
625 |
checkedClass superclass notNil ifTrue:[checkBlock value:checkedClass superclass].
|
|
626 |
Smalltalk allClassesDo:checkBlock.
|
|
627 |
|
|
628 |
^ remaining
|
|
629 |
|
|
630 |
"Modified: / 18.8.2000 / 23:05:53 / cg"
|
|
631 |
"Created: / 18.8.2000 / 23:06:55 / cg"
|
|
632 |
!
|
|
633 |
|
|
634 |
messagesNeverSentIn:selectorsOfInterest
|
|
635 |
|remaining|
|
|
636 |
|
|
637 |
remaining := selectorsOfInterest copy.
|
|
638 |
Smalltalk allClassesDo:[:eachClass |
|
|
639 |
eachClass instAndClassSelectorsAndMethodsDo:[:mSelector :mthd | |lits|
|
|
640 |
lits := mthd literals.
|
|
641 |
lits notNil ifTrue:[
|
|
642 |
(lits includesAny:remaining) ifTrue:[
|
|
643 |
remaining removeAllFoundIn:(mthd messagesSent).
|
|
644 |
remaining isEmpty ifTrue:[^ remaining].
|
|
645 |
]
|
|
646 |
]
|
|
647 |
].
|
|
648 |
].
|
|
649 |
^ remaining
|
|
650 |
|
|
651 |
"Created: / 18.8.2000 / 22:53:38 / cg"
|
|
652 |
"Modified: / 18.8.2000 / 22:56:59 / cg"
|
|
653 |
!
|
|
654 |
|
|
655 |
methodShouldBeIgnoredInSubclassResponsibilityNotDefined:aMethod
|
|
656 |
"a kludge for now - would like to have a pragma, resource or other
|
|
657 |
way to mark such a method"
|
|
658 |
|
|
659 |
aMethod == (Object compiledMethodAt:#implementedBySubclass) ifTrue:[^ true].
|
|
660 |
aMethod == (Object compiledMethodAt:#readBinaryContentsFromData:manager:) ifTrue:[^ true].
|
6511
|
661 |
aMethod == (Object compiledMethodAt:#finalize) ifTrue:[^ true].
|
5591
|
662 |
^ false.
|
|
663 |
!
|
|
664 |
|
|
665 |
rememberBadClass:class info:whatIsWrong
|
|
666 |
|entry|
|
|
667 |
|
|
668 |
badClasses isNil ifTrue:[
|
|
669 |
badClasses := IdentitySet new.
|
|
670 |
].
|
|
671 |
badClasses add:class.
|
|
672 |
|
|
673 |
badClassInfo isNil ifTrue:[
|
|
674 |
badClassInfo := IdentityDictionary new.
|
|
675 |
].
|
|
676 |
entry := badClassInfo at:class ifAbsentPut:[ OrderedCollection new ].
|
|
677 |
entry add:whatIsWrong.
|
|
678 |
!
|
|
679 |
|
|
680 |
rememberBadMethod:method key:key info:whatIsWrong
|
|
681 |
|entry|
|
|
682 |
|
|
683 |
badMethods isNil ifTrue:[
|
|
684 |
badMethods := Set new.
|
|
685 |
].
|
|
686 |
(badMethods includes:(method -> key)) ifTrue:[^ self].
|
|
687 |
|
|
688 |
badMethods add:(method -> key).
|
|
689 |
|
|
690 |
badMethodInfo isNil ifTrue:[
|
|
691 |
badMethodInfo := IdentityDictionary new.
|
|
692 |
].
|
|
693 |
entry := badMethodInfo at:method ifAbsentPut:[ OrderedCollection new ].
|
|
694 |
entry add:whatIsWrong.
|
|
695 |
!
|
|
696 |
|
|
697 |
rememberBadMethods:methods key:key info:whatIsWrong
|
|
698 |
methods do:[:eachMethod |
|
|
699 |
self rememberBadMethod:eachMethod key:key info:whatIsWrong
|
|
700 |
].
|
|
701 |
!
|
|
702 |
|
|
703 |
removeUsedClassVariablesIn:aClass from:aCollectionOfVariablenames
|
|
704 |
aClass selectorsAndMethodsDo:[:mSelector :method |
|
|
705 |
|src usedVars parser|
|
|
706 |
|
|
707 |
src := method source.
|
|
708 |
src notNil ifTrue:[
|
|
709 |
parser := Parser
|
|
710 |
parseMethod:src
|
|
711 |
in:aClass
|
|
712 |
ignoreErrors:true
|
|
713 |
ignoreWarnings:true.
|
|
714 |
|
|
715 |
(parser notNil and:[parser ~~ #Error]) ifTrue:[
|
|
716 |
aCollectionOfVariablenames removeAllFoundIn:(parser usedClassVars).
|
|
717 |
aCollectionOfVariablenames isEmpty ifTrue:[^ self].
|
|
718 |
]
|
|
719 |
].
|
|
720 |
].
|
|
721 |
!
|
|
722 |
|
|
723 |
removeUsedInstanceVariablesIn:aClass from:aCollectionOfVariablenames
|
|
724 |
aClass selectorsAndMethodsDo:[:mSelector :method |
|
|
725 |
|src usedVars parser|
|
|
726 |
|
|
727 |
src := method source.
|
|
728 |
src notNil ifTrue:[
|
|
729 |
parser := Parser
|
|
730 |
parseMethod:src
|
|
731 |
in:aClass
|
|
732 |
ignoreErrors:true
|
|
733 |
ignoreWarnings:true.
|
|
734 |
|
|
735 |
(parser notNil and:[parser ~~ #Error]) ifTrue:[
|
|
736 |
aCollectionOfVariablenames removeAllFoundIn:(parser usedInstVars).
|
|
737 |
aCollectionOfVariablenames isEmpty ifTrue:[^ self].
|
|
738 |
]
|
|
739 |
].
|
|
740 |
].
|
|
741 |
!
|
|
742 |
|
|
743 |
removeWrittenClassVariablesIn:aClass from:aCollectionOfVariablenames
|
|
744 |
aClass selectorsAndMethodsDo:[:mSelector :method |
|
|
745 |
|src usedVars parser|
|
|
746 |
|
|
747 |
src := method source.
|
|
748 |
src notNil ifTrue:[
|
|
749 |
parser := Parser
|
|
750 |
parseMethod:src
|
|
751 |
in:aClass
|
|
752 |
ignoreErrors:true
|
|
753 |
ignoreWarnings:true.
|
|
754 |
|
|
755 |
(parser notNil and:[parser ~~ #Error]) ifTrue:[
|
|
756 |
aCollectionOfVariablenames removeAllFoundIn:(parser modifiedClassVars).
|
|
757 |
aCollectionOfVariablenames isEmpty ifTrue:[^ self].
|
|
758 |
]
|
|
759 |
].
|
|
760 |
].
|
|
761 |
!
|
|
762 |
|
|
763 |
removeWrittenInstanceVariablesIn:aClass from:aCollectionOfVariablenames
|
|
764 |
aClass selectorsAndMethodsDo:[:mSelector :method |
|
|
765 |
|src usedVars parser|
|
|
766 |
|
|
767 |
src := method source.
|
|
768 |
src notNil ifTrue:[
|
|
769 |
parser := Parser
|
|
770 |
parseMethod:src
|
|
771 |
in:aClass
|
|
772 |
ignoreErrors:true
|
|
773 |
ignoreWarnings:true.
|
|
774 |
|
|
775 |
(parser notNil and:[parser ~~ #Error]) ifTrue:[
|
|
776 |
aCollectionOfVariablenames removeAllFoundIn:(parser modifiedInstVars).
|
|
777 |
aCollectionOfVariablenames isEmpty ifTrue:[^ self].
|
|
778 |
]
|
|
779 |
].
|
|
780 |
].
|
|
781 |
! !
|
|
782 |
|
|
783 |
!ClassChecker class methodsFor:'documentation'!
|
|
784 |
|
|
785 |
version
|
6511
|
786 |
^ '$Header: /cvs/stx/stx/libtool/Tools_ClassChecker.st,v 1.4 2006-01-31 09:26:35 cg Exp $'
|
5591
|
787 |
! !
|