简体   繁体   English

如何在不同的程序包中调用类的私有方法

[英]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 有一个BookView.class,它具有如下定义的私有方法

  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. 我在执行上述工作的jar文件中找到了另一个公共方法。

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

If you want to do that you should make it public or make a public wrapper method it. 如果要这样做,则应将其public或使用public包装方法。

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. 看一看在调用层级中的private方法,看看你找到一个public ,做的方法调用的private之一,并且也做你所需要的。

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. 库的设计通常采用以下方式: public方法进行一些检查(给出所有参数,进行身份验证等),然后将调用传递给private方法以完成实际工作。 You almost never want to work around that process. 您几乎永远都不想解决该过程。

With reflection, you'll need an instance of BookView to invoke the method with (unless it's a static method). 通过反射,您将需要一个BookView实例来调用方法(除非它是静态方法)。

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...) 您正在寻找的方法是Method#invoke(Object, Object...)

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: 使用反射获取方法并将Accessible设置为true然后使用BookView Object实例和必需的参数(路径字符串)使用以下语句来调用该方法

     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);

Private methods cannot be accessed outside the class it is defined. 无法在定义的类之外访问私有方法。 Make it Public. 公开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM