Search code examples
squeak

Method isn't added to methodDict


As far as I've understood yet, that each time I do "Obj compile: foo", the function foo is added to "Obj methodDict", I tried to test that in a small code, and I've found that it's not always added...

Is there another way to find all the methods that the obj has compile for now? like all the methods that the object knows for now?


Solution

  • To get a more specific answer, you should provide the code you are using for your test. One code path that I could quickly find and that will not add a method is when the compilation of the source code fails. Also, an existing method with the same selector will be replaced by the new compiled method, since there can be no two methods for the same message selector in one class.


    To get all the message selectors understood by an object, you can use:

    anObject class allSelectors
    

    It will also include the selectors of methods implemented in superclasses.

    To get only the selectors implemented in this class, and not in the superclasses:

    aClass selectors
    

    To get the CompiledMethods, you can use:

    localMethods := OrderedCollection new.
    aClass methodsDo: [:each | localMethods add: each].
    
    allMethods := aClass allSelectors collect: [:each | aClass lookupSelector: each].
    

    Instead of using methodsDo:, you can also access the method dictionary directly, as you seem to do in your test code, which you didn't show us as of now.


    If you are looking for a practical overview that is not scripted, you can also use the protocol browser or Lexicon tool. Browse the class of which you want to see all the methods (with the system browser), then open the popup menu on the class and choose "browse protocol".

    System browser on the class "Class", popup menu to "browse protocol"

    The new tool that opens shows the methods of this class and all its superclasses. You can filter up to which superclass it goes via the button labelled with "Only through".

    Lexicon on the class "Class", showing methods through to the class "Behavior"