开发者

Locations of super() calls in Android Eclipse Plugin generated code reliable?

开发者 https://www.devze.com 2023-02-10 15:52 出处:网络
In many of Android methods, especially constructors and overridden methods, you should or even must call the parent class method using super(). When you use the EclipseSource > Override/Implement Meth

In many of Android methods, especially constructors and overridden methods, you should or even must call the parent class method using super(). When you use the Eclipse Source > Override/Implement Methods... you get code from a template with TODO tags like this:

public MyCanvas(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}


@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub      
    su开发者_如何学Cper.onDraw(canvas);
} 

I do not understand exacly what the superclass does in each case so I always insert my code at the exact location of the //TODO tags. In the example, I would call super() before my code in the constructor and after my code in onDraw().

Can I always rely on these code insertions locations in the generated code? Is there a simple rule/explanation when to call super()?


This is a good question. Unfortunately, there is no simple rule for this. You need to know what the superclass implementation does. Sometimes (as in View.onDraw()), the superclass implementation does nothing; calling super() is both harmless and unnecessary. In other cases (such as Activity.onCreate()) the superclass implementation performs critical operations that must be executed at some point in the subclass's processing. Sometimes what happens when you call super() should come before any processing in the subclass, sometimes at other points. Sometimes you want to completely replace the superclass processing with your own, in which case you don't call super() at all. You have complete freedom to call the superclass version at any point (or even at multiple points) in your subclass's logic.

In constructors, the call to a superclass constructor (if present) must be the first thing in the method. If you don't have one, the compiler automatically inserts a call to the no-argument constructor in the superclass. (If the superclass does not have a no-argument constructor, or if it is not accessible to the subclass, the compiler generates an error.)

If the documentation doesn't provide enough information, then you have to look at the source code. The Android code is available here (Gingerbread release). The API code is under core.

EDIT The code is no longer available at git.kernel.org. Here are two other places where you can browse the code:

  • github.com
  • omapzoom.org

The main code is in the repository Platform > Frameworks > Base


Can I always rely on these code insertions locations in the generated code?

No, sometimes you don't want to call the super.method. Sometimes you want to call it first, sometimes at the last place, etc. It depends. But, I'm talking about methods, no constructors.

Is there a simple rule/explanation when to call super()?

You will always have to all super as the previous answer points. The only case where you don't call super is when the constructor of the super class has no parameters; in that case the compiler will put the super for you.

I do not understand exacly what the superclass does in each case so I always insert my code at the exact location of the //TODO tags

If you are in doubt (I'm talking about super methods), you can always take a look at the source code. Google Code Search is a good resource to do so. Then you can decide whether to put your code before or after the super method; or even, don't put the super method at all.

Keep in mind that not putting the super method is valid at compile time. But, some methods on android won't work unless you invoke the super method (for instance, the onResume method of the Activity class).

Also, sometimes you will decide whether to run the super method or not in runtime. Consider this classic example:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if( KeyEvent.KEYCODE_BACK == event.getKeyCode() ){
        return true;
    }
    return super.onKeyUp(keyCode, event);
}

If the user pressed the back key, you won't call the super method. If the user didn't, you delegate the work to the super method.


"If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem."

Check the documentation for non-constructor overrides. Some times you want to call the super, sometimes not.

0

精彩评论

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

关注公众号