Overriding a method in behavior -smalltalk- - smalltalk

I have an object A in Smalltalk and I want to override a method specifically the method compile in the behavior.
my aim is to customize compile method by adding asserts before compiling a code.
I know that compile is found in the A class (up until behavior) but how can I use it ?
I have tried a lot of ways non are working, the last of what I did is :
I defined a method :
compile:code
self class compile:code. "this is not working it tells me message not understood"
how can I do this ?

[Pharo] Try redefining the method #compile:classified:withStamp:notifying: on the class side of your class. In this new implementation combine the assertion code with the first parameter (which is the source code you are about to compile) and then delegate to super with the modified code.

Related

Identify the superclass or interface on which a method is being called in the Java editor of IntelliJ 2018?

Is there a quick easy way to have IntelliJ identify the superclass or interface defining the method being called in a class?
For example, in the following example, the fireEvent method is being called on the implicit this but this method is not defined within this Java class being edited in IntelliJ 2018.3. Is there some way to ask IntelliJ which of the several interfaces being implemented as well as any parent in the superclass hierarchy defines this particular method?
…
private void doIt() {
…
fireEvent( event ) ;
}
What I am asking is for the opposite of this Question, Does IntelliJ have the equivalent of the Eclipse “method view”?. That Question asks “for a given class, what are its methods offered?”. I am asking “for a given method being invoked, from what inherited superclass or interface is that method defined?”.
I generally just context-click (Ctrl-click on PC, Command-click on macOS) to navigate to the source for a method. The .java file of the superclass or interface opens in the editor.
In order to avoid navigating away, you can also context-hover. Another option is showing the javadoc via context-q when the cursor is somewhere in the method name.

Objective-C Passing Method Reference As Parameter

I've seen a lot of discussions NEAR this subject, but none that actually work in Xcode 5.x, especially using ARC. I have a simple problem:
I need to pass a method reference to a CreateButton method so that when the button is called it calls my custom function, and not some generic one.
I've tried using (SEL) type, but that doesn't work with ARC. I've tried using the &func method, but that claims I haven't declared the function yet.
So my need is:
Class A calls Class B and sends over the info to make a UIButton. Within that call, I want to send over the action:method in a reference. I'm sure this is done routinely, but I can't seem to find an iOS 7 / Xcode 5.x method of doing it. I've also reviewed the O'Reilly iOS 7 fundamentals and cookbook code and couldn't find this discussed anywhere.
Thanks for you help.
When I have to pass selectors around, I convert them to strings with NSStringFromSelector() and back to selectors with NSSelectorFromString().
Passing the strings around is a lot easier. You can store them in collections (arrays, dictionaries), serialize and unserialize them, and they will work naturally with ARC.
Example:
In your class A where you gather the information to create a button:
NSString *selectorString = NSStringFromSelector(#selector(yourActionMethodNameHere:));
// Gather more information needed by Class B here, then package
// it all up into a dictionary, for example
NSDictionary *buttonInfo = #{#"selectorString": selectorString, /* more stuff here */};
At this point, you can call your button-constructing method in Class B, passing along buttonInfo, which contains all the information that that helper method needs, including the selector. The method can convert the string back to a selector and use it like this:
SEL actionSelector = NSSelectorFromString(buttonInfo[#"selectorString"]);
// configure your button to use actionSelector here
You should be able to use SEL parameters? I know I have done before.
ARC might complain and give you a warning, but it won't fail to compile. It's simply a warning because it can't quite figure out what to do memory wise.
If you really can't get that to work though, another alternative would be to use a block, so you might call your method like
[objectA performMethodWithParam:paramA paramb:paramB completion:^{ ... do somethhing ... }];
Then in that method you can just call
completion();
Instead of actually calling a method.
Another alternative would be to use the delegate pattern. Create a #protocol defining a method such as classADidFinish then make class B implement that method. Then set the instance of classB as the delegate for your classA instance, and have it call that method when it's done.
Both of these approaches will stop ARC moaning at you.
But as I said, using SEL params should work fine. There is a way you can even get the compiler to stop showing you the warnings but it's a little ugly.

Easy way to tell if a method is overriding another method?

I'm just beginning with ObjC. I'm wondering how to find out when looking at code, written by me or from a template that comes when you use the wizard to create a new class, how you can tell if a method is overriding something.
In Java, you can mark a method with #Override, and then it's very easy to see if it's overriding something. That's not foolproof, because #Override is optional, but if I'm still unsure I can just type that in and see if it generates an error.
Is the only way to look up the source of the superclass, or in the case of a framework to read the documentation?
I don't know a way to see this immediately, but you could check if super responds
to the same selector. Example:
- (void)myMethod
{
// Temporarily add this line. If the compiler does NOT complain,
// "myMethod" overrides a method from some superclass.
[super myMethod];
// ...
}
You can use instancesRespondToSelector to see if your instance has an implementation of the method in its object hierarchy.
[MyClass instancesRespondToSelector:#selector(myMethod)];
or depending on what type of checking you need to do
[MyClassSuperClass instancesRespondToSelector:#selector(myMethod)];

Setting a breakpoint for when a Java class is loaded in IntelliJ IDEA

Is there a way to set a breakpoint for when a particular Java class is loaded in IntelliJ IDEA?
In 2016 version it works by setting the breakpoint to the class definition line (public class YourClass ...). You don't have to use explicit constructor, it stops when ... new YourClass(); is called.
Not that I know of. But if you are trying to determine from where a class is first being loaded, you could put a break point in the constructor (or static fields/block) of the class, and look at the stack trace. That should tell you where the first call to the class is being made.

Restrict usage/generate a compile error when someone uses a particular method in an Objective-C class/object

Suppose I have an AwesomeClass but I cannot change it's implementation (ie it is from a static library or framework). I like everything about AwesomeClass except for - (void)thatOneBadMethod. I can subclass AwesomeClass to be MyAwesomeClass and override that method with an empty implementation (not calling super) but the problem is that if my fellow developer is using MyAwesomeClass and is unaware of my actions he may try to use thatOneBadMethod thinking that it is doing something it is not.
Is there some preprocessor directive that I can put in the override for thatOneBadMethod so that he gets a compiler error or warning when he tries to use it?
You can deprecate the method in the header file:
-(void)thatOneBadMethod __attribute__ ((deprecated("Unsupported, please call 'thatOneGoodMethod' instead!")));
But I'd also suggest generating an error when people try to call it:
-(void)thatOneBadMethod {
NSAssert(NO, #"Unsupported, please call 'thatOneGoodMethod' instead!");
}