开发者

Method Inspection Squeak/Smalltalk

I am trying to do some method inspection (in Squeak - Smalltalk).

I wanted to ask what is the way to check if a method is an abstract method? Meaning I want to write, A method which gets a class and a symbol and will check if there is such a symbol in the list of methods in an object which is of this class type and if found will return true if abstrac开发者_StackOverflow中文版t (else not). How can I check if a method is an abstract method or not?

Thanks in advance.


A method is abstract (in the sense Java or C++ people mean) if it looks like this:

myMethod
  self subclassResponsibility.

So all you need to do to answer "is MyObject>>#myMethod abstract?" is to answer "is MyObject>>#myMethod a sender of #subclassResponsibility?"

You can answer that question by adding this method to Object:

isMethodAbstract: aSelector on: aClass
    ^ (self systemNavigation allCallsOn: #subclassResponsibility)
        anySatisfy: [:each | each selector == aSelector
            and: [each classSymbol == aClass name]]

or simply evaluating this in a Workspace (with suitable replacements for #samplesPerFrame and SoundCodec of course):

(SystemNavigation default allCallsOn: #subclassResponsibility)
    anySatisfy: [:each | each selector == #samplesPerFrame
        and: [each classSymbol == SoundCodec name]]


You can use

(aClass>>aMethod) isAbstract

but it only works if aClass actually contains the method aMethod, and does not work for superclasses.

So you'll have to check it recursively, similarly to how canUnderstand: works.


While I don't know what your ultimate goal is, the Pharo code critics will identify methods where subclass responsibility is not defined. This may already be what you want to do. On the other hand, it's also worth checking out how that test is implemented to see whether you can use some or all of the existing code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜