Is it possible to call the Internal (no visibility operator) method from a class inherited from another package?

Asked

Viewed 120 times

5

Problem

I’m trying to create some extra behaviors in some native Android components.

For that I am creating a class above the Android component and rewriting some situations I intend to have a different behavior, and I came across a situation I need in my custom method, call a method of class inherited (in this case the Android component), but the method is not accessible to my class, since he is international and not protected, and my class is in another package.

Questions?

  • Although I know which attributes and methods internal are only accessible for members of the same package Java, I wonder if there are any how to access these methods from a class of a another package?
  • And optionally, if there is any other way to overwrite Android native components, who avoid this kind of problem?

Example of what I’m trying to do:

public class MeuAbsListView extends AdapterView<ListAdapter>{

    public MeuAbsListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void meuMetodoDisparadoPeloMeuEvento(){
        // faz seu comportamento custumizado ... 

        // e tem esse metodo 'rememberSyncState' do 'AdapterView' que preciso chamar
        // só que ele é internal e não tenho acesso a ele, 
        // já no AbsListView nativo é possivel pois ele é do mesmo package do AdapterView
        rememberSyncState();
    }   
}

In the code Android Adapterview source the method is declared as:

void rememberSyncState() {
    // codigo do rememberSyncState ...
}
  • 1

    I didn’t understand: "Is there any other way to overwrite native Android components, who avoid this kind of problem?" You want to create a subclass that makes the visibility of a class method less restrictive than in the superclass?

  • @Math, it’s out there, I believe that’s not possible, since it goes against everything, but I wanted to remove the doubt.

  • I spoke nonsense in my previous comment, you can not leave MORE restrictive, IE, if you have a method without modifier you can leave it less restrictive in a superscript of a subclass your.

1 answer

5


Two ways to achieve this through emergency technical adaptations (a.k.a Gambiarra).

Works in java but not on android

1.Create a third class heiress in the same restrictive class package

public class ClasseRestritiva
{
   protected void foo(){}
}

and

public MinhaClasseNaoRestritiva extends ClasseRestritiva
{

     public void chamaFoo()
     {
           foo();
     }

}

2. Reflections

There is a good example in that question SOEN. In addition I recommend that you see the official documentation

Solution with Reflection

Method rememberSyncState = AdapterView.class.getDeclaredMethod("rememberSyncState");
// Set accessible provide a way to access private methods too
rememberSyncState.setAccessible(true);
// this é instancia da class que herda de AdapterView
rememberSyncState.invoke(this);
  • It is possible to create a class in the same package of native Android components? In case android.widget?

  • Already tried to create the package android.widget and put something there in your project?

  • I had tried this before, and I tried it again, but it didn’t work. Some other idea?

  • @Fernando tried to use reflection? Note I thought it was possible because you can put classes in java.io. I’ve never actually worked with android.

  • 2

    I think Reflection works. I was able to access private attributes from a superclass that wasn’t mine. I’m not sure about methods, but it’s worth a try.

  • 1

    @Kyllopardiun, Reflection worked, I will edit your question and put as I did. (Grace to "emergency technical adaptations (a.k.a Gambiarra).").

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.