1

I need to override a method of a hidden class (marked with @hide) which is only visible within its package. Is there any way to extend it?

More specifically, I need to override getNextCursorOffset() of android.widget.Editor because that method does not work very well with ReplacementSpan.

mljli
  • 593
  • 5
  • 23
  • why not use decorator of your own with all the methods of Editor and just getNextCursorOffset of yours? – SMA Jul 26 '16 at 10:18
  • @SMA You mean cloning Editor.java? That would be crazy since it has 5K+ lines of code. – mljli Jul 26 '16 at 10:20
  • Not cloning. You would compose editor in your new class and just delegate the call of other methods to Editor instance while for getNextCursorOffset, you would implement your own logic. – SMA Jul 26 '16 at 10:22
  • @SMA, That won't work if getNextCursorOffset() is package protected, it won't be visible to the code in a different package. Would've worked had that method been protected. – Egor Jul 26 '16 at 10:24
  • @SMA I see. But that won't be easy too. It's not a direct call and the whole logic is complex. Lots of private members need to be modified which themselves are not accessible either. – mljli Jul 26 '16 at 10:26
  • @Egor if OP can create the same package then that would work. Am assuming its protected.. – SMA Jul 26 '16 at 13:03
  • @Egor @SMA It seems that even I define the same package, `Editor` still can't be resolved. Is there anyway to modify the SDK source? I've googled a lot and didn't find anything useful. – mljli Jul 28 '16 at 13:54

2 Answers2

0

You can define the same package, android.widget within your project. Then you can extend it:

public class ProjectSpecificEditor extends Editor {
    // constructors et all
    public int getNextCursorOffset() {
        //...
    }

}

But note that this is discouraged. I.e. the next version of the framework might change some of the classes / method signatures. Then you can blame only yourself.

Tamas Rev
  • 7,008
  • 5
  • 32
  • 49
  • Sorry I didn't test it. Even I define the same package, Android Studio still can't resolve `Editor`. Is there anything that should be configured? – mljli Jul 28 '16 at 13:22
  • A good cross-check is whether you can compile it from command-line. – Tamas Rev Jul 28 '16 at 14:02
0

I got the idea from ryan gordon

Basically, anything marked with @hide is removed when compiling the framework, which Android Studio uses to compile your code. You can fine that in \AppData\Local\Android\Sdk\platforms\android-xx\android.jar. What ryan proposed, was to either compile android framework yourself without the @hide, or to take an already compiled framework from a rooted phone (because @hide doesn't affect code used in actual phones)

An simpler alternative in my opinion is to simply extract the .class file from android.jar, use any Java bytecode editor, and just add the missing method. Then replace the file in android.jar.

However, just as Tamas Rev mentioned, methods marked with @hide were never meant to be accessed, therefore there's no guarantee that it existed in older versions, or that the behavior is consistent across versions, or that it will exist in the future!

Guiorgy
  • 1,405
  • 9
  • 26