Bhavyanshu
Bhavyanshu

Reputation: 536

How to call a private method of a class in different package

There is a BookView.class that has a private method defined as below

  public class BookView{
    private boolean importBook(String epubBookPath){
    //The function that adds books to database.
    }
  }

I am trying to call this function from a different package. My code is

    protected void onPostExecute(String file_url) {
        // dismiss the dialog after the file was downloaded
        dismissDialog(progress_bar_type);

        /*Now we add the book information to the sqlite file.*/
        TextView textView=(TextView)findViewById(R.id.textView1);
        String filename = textView.getText().toString();
        String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        String epubBookPath = baseDir+filename;
        Log.i("epubBookPath:",epubBookPath); //No errors till here!

        try {
            Method m=BookView.class.getDeclaredMethod("importBook");
            m.setAccessible(true);//Abracadabra 
            //I need help from here! How do i pass the epubBookPath to the private importBook method.
        } catch (NoSuchMethodException e) {

            e.printStackTrace();
        }
        Intent in = new Intent(getApplicationContext(),
                CallEPubUIActivity.class);        
        startActivity(in);
    }

EDIT:

I found another public method in the jar file which is doing the above work.

  public void jsImportBook(String epubBookPath) {
     if (!BookView.this.importBook(epubBookPath))
     return;
   BookView.this.createBookshelf();
  }

Upvotes: 0

Views: 4854

Answers (4)

Angelo Fuchs
Angelo Fuchs

Reputation: 9941

If you want to do that you should make it public or make a public wrapper method it.

If thats not possible, you can work your way around it, but thats ugly and bad and you should have really good reasons to do so.

public boolean importBook(String epubBookPath){
    //The function that adds books to database.
}

or

public boolean importBookPublic(String epubBookPath){
    return importBook(epubBookPath);
}
private boolean importBook(String epubBookPath){
    //The function that adds books to database.
}

Also note that if you CAN'T access the method directly in a third-party library than it is most likely INTENDED that way. Take a look at the call hierarchy of the private method and see if you find a public method that does the call to the private one and that also does what you need.

Libraries are often designed in a way that a public method does some checking (all Parameters given, authenticated etc.) and then pass the call to the private method to do the actual work. You almost never want to work around that process.

Upvotes: 11

Yogendra Singh
Yogendra Singh

Reputation: 34367

Use reflection to get you method and set Accessible as true, then invoke the method using BookView Object instance and required parameters(path string) using statement as below:

     Boolean result = (Boolean)method.invoke(bookObject, epubBookPath);

Sample code as below:

Method method = BookView.getDeclaredMethod("importBook");
method.setAccessible(true);
Boolean result = (Boolean)method.invoke(bookObject, epubBookPath);

Upvotes: 2

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279880

With reflection, you'll need an instance of BookView to invoke the method with (unless it's a static method).

BookView yourInstance = new BookView();
Method m = BookView.class.getDeclaredMethod("importBook");
m.setAccessible(true);//Abracadabra 
Boolean result = (Boolean) m.invoke(yourInstance, "A Path"); // pass your epubBookPath parameter (in this example it is "A Path"

The method you are looking for is Method#invoke(Object, Object...)

Upvotes: 9

Lahiru Rajeew Ananda
Lahiru Rajeew Ananda

Reputation: 310

Private methods cannot be accessed outside the class it is defined. Make it Public.

Upvotes: 0

Related Questions