简体   繁体   English

方法检查 吱吱声/杂音

[英]Method Inspection Squeak/Smalltalk

I am trying to do some method inspection (in Squeak - Smalltalk).我正在尝试进行一些方法检查(在 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 abstract (else not).意思是我想写一个方法,它获取一个class和一个符号,并将检查 object 中的方法列表中是否有这样的符号,它是这个 class 类型,如果没有找到抽象将返回. 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:一个方法是抽象的(在 Java 或 C++ 人们的意思上),如果它看起来像这样:

myMethod
  self subclassResponsibility.

So all you need to do to answer "is MyObject>>#myMethod abstract?"因此,您需要做的就是回答“ MyObject>>#myMethod是抽象的吗?” is to answer "is MyObject>>#myMethod a sender of #subclassResponsibility ?"是回答“ MyObject>>#myMethod#subclassResponsibility的发送者吗?”

You can answer that question by adding this method to Object:您可以通过将此方法添加到 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):或者简单地在工作区中评估它(当然有合适的替换#samplesPerFrameSoundCodec ):

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

You can use您可以使用

(aClass>>aMethod) isAbstract (aClass>>aMethod) isAbstract

but it only works if aClass actually contains the method aMethod, and does not work for superclasses.但它仅在 aClass 实际包含方法 aMethod 时才有效,并且不适用于超类。

So you'll have to check it recursively, similarly to how canUnderstand: works.所以你必须递归地检查它,类似于 canUnderstand: 的工作方式。

While I don't know what your ultimate goal is, the Pharo code critics will identify methods where subclass responsibility is not defined.虽然我不知道您的最终目标是什么,但 Pharo 代码批评者将识别未定义子类责任的方法。 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.另一方面,也值得检查该测试是如何实现的,看看您是否可以使用部分或全部现有代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM