开发者

Java reflection in Android: interfaces

开发者 https://www.devze.com 2023-03-24 07:04 出处:网络
I\'ve tried some things described in tutorial here: investigating a class\'s methods. But when I tried to investigate the methods of android.content.SharedPreferences.Editor, I\'ve got a ClassNotFound

I've tried some things described in tutorial here: investigating a class's methods. But when I tried to investigate the methods of android.content.SharedPreferences.Editor, I've got a ClassNotFoundException, cause Editor is actually an interface, not a class. So, how can I access methods of an interface using Java reflection? Thanks in advance.

Here is code:

    String sClassName = "android.content.SharedPreferences.Editor";  
try {  
    Class classToIn开发者_Python百科vestigate = Class.forName(sClassName);   

    // Dynamically do stuff with this class  
    // List constructors, fields, methods, etc.  

} catch (ClassNotFoundException e) {  
    // Class not found!  
} catch (Exception e) {  
    // Unknown exception  
}  


You should be able to explore an interface in the same way as you would explore a class using reflection. The only difference beeing the fact that isInterface will return true.

Could you post some code and please precise if you are using JDK or Android SDK in your program as this could explain why you get a ClassNotFoundException.

Regards, Stéphane


Try following class name,

android.app.ContextImpl$SharedPreferencesImpl$EditorImpl

0

精彩评论

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