[英]How to use reflection mechanisms to invoke a public method that resides in a base class with default visibility?
[英]Use Reflection to invoke a public method
我正在編寫一個API,但在使用反射時遇到了問題。 我想做的是從自定義對象類中調用方法,然后將值返回到調用的位置。 調用的開始發生在MainActivity.java中
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ByteMe b = new ByteMe();
ExampleObject object = new ExampleObject("Bob", 20, "indy", "male", "its bobby");
try {
b.examine(object);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
我的示例對象類旨在幫助確保此API正常工作
public class ExampleObject {
private String Name;
private int Age;
private String Location;
private String Sex;
private String Description;
/**
* Empty Constructor
*/
public ExampleObject() {}
/**
* Basic constructor with initializing data
*
* @param _Name String with the name of the user
* @param _Age Integer with the age of the user
* @param _Location String containing the curret city and state of the user
* @param _Sex String Male, Female, Transgender, or Other
* @param _Description String short blurb about the user
*/
public ExampleObject(String _Name, int _Age, String _Location, String _Sex, String _Description)
{
this.setName(_Name);
this.setAge(_Age);
this.setLocation(_Location);
this.setSex(_Sex);
this.setDescription(_Description);
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public String getLocation() {
return Location;
}
public void setLocation(String location) {
Location = location;
}
public String getSex() {
return Sex;
}
public void setSex(String sex) {
Sex = sex;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
}
現在,我遇到的主要問題是在另一個名為ByteMe.java的文件中進行此調用時:
public void examine(Object obj) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//Get the list of possible methods held in this object.
Method[] methods = obj.getClass().getMethods();
// iterate through them
for (Method method : methods) {
Log.d("" + this.getClass().getName(), "--------------------------");
Log.d("" + this.getClass().getName(), "Method: " + method.getName());
Log.d("" + this.getClass().getName(), "Return Type: " + method.getReturnType());
Log.d("" + this.getClass().getName(), "Class: " + method.getClass());
Log.d("" + this.getClass().getName(), "Declaring Class: " + method.getDeclaringClass());
if(method.getReturnType().getName().contains("int")) {
try {
Method m = method.getDeclaringClass().getMethod(method.getName(), Integer.TYPE);
int temp = (int) m.invoke(null, 0); //first argument is the object to invoke on, ignored if static method
Log.d("" + this.getClass().getName(),"temp value: " + temp);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
Log.d("" + this.getClass().getName(), "--------------------------");
}
}
這是我的logcat的摘錄:
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme D/Library.ByteMe: --------------------------
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme D/Library.ByteMe: Method: equals
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme D/Library.ByteMe: Return Type: boolean
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme D/Library.ByteMe: Class: class java.lang.reflect.Method
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme D/Library.ByteMe: Declaring Class: class java.lang.Object
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme D/Library.ByteMe: --------------------------
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme D/Library.ByteMe: --------------------------
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme D/Library.ByteMe: Method: getAge
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme D/Library.ByteMe: Return Type: int
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme D/Library.ByteMe: Class: class java.lang.reflect.Method
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme D/Library.ByteMe: Declaring Class: class productions.widowmaker110.byteme.ExampleObject
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: java.lang.NoSuchMethodException: getAge [int]
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at java.lang.Class.getMethod(Class.java:664)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at java.lang.Class.getMethod(Class.java:643)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at Library.ByteMe.examine(ByteMe.java:94)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at productions.widowmaker110.byteme.MainActivity.onCreate(MainActivity.java:21)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.app.Activity.performCreate(Activity.java:5958)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:144)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.os.Looper.loop(Looper.java:155)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5696)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at java.lang.reflect.Method.invoke(Native Method)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme D/Library.ByteMe: --------------------------
我查找了NoSuchMethodFound錯誤,並且大多數stackoverflow都說這是由於該方法是private
但是我的方法是public
。 inspect()中的invoke方法應該返回20,因為那是我在MainActivity中創建的對象的年齡。 它只是引發一個異常。 救命?
編輯 Alain O'Dea,感謝您的幫助。 這就是我必須進行更改才能使其正常工作的原因。
public void examine(Object obj) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//Get the list of possible methods held in this object.
Method[] methods = obj.getClass().getMethods();
// iterate through them
for (Method method : methods) {
Log.d("" + this.getClass().getName(), "--------------------------");
Log.d("" + this.getClass().getName(), "Method: " + method.getName());
Log.d("" + this.getClass().getName(), "Return Type: " + method.getReturnType());
Log.d("" + this.getClass().getName(), "Class: " + method.getClass());
Log.d("" + this.getClass().getName(), "Declaring Class: " + method.getDeclaringClass());
if(method.getReturnType().getName().contains("int")) {
try {
int temp = (int) method.invoke(obj);
Log.d("" + this.getClass().getName(),"temp value: " + temp);
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
Log.d("" + this.getClass().getName(), "--------------------------");
}
}
logcat的
11-21 20:48:15.459 26949-26949/productions.widowmaker110.byteme D/Library.ByteMe: --------------------------
11-21 20:48:15.469 26949-26949/productions.widowmaker110.byteme D/Library.ByteMe: Method: getAge
11-21 20:48:15.469 26949-26949/productions.widowmaker110.byteme D/Library.ByteMe: Return Type: int
11-21 20:48:15.469 26949-26949/productions.widowmaker110.byteme D/Library.ByteMe: Class: class java.lang.reflect.Method
11-21 20:48:15.469 26949-26949/productions.widowmaker110.byteme D/Library.ByteMe: Declaring Class: class productions.widowmaker110.byteme.ExampleObject
11-21 20:48:15.469 26949-26949/productions.widowmaker110.byteme D/Library.ByteMe: temp value: 20
11-21 20:48:15.469 26949-26949/productions.widowmaker110.byteme D/Library.ByteMe: --------------------------
您有以下代碼:
int temp = (int) m.invoke(null, 0); //first argument is the object to invoke on, ignored if static method
結果,您得到此堆棧跟蹤:
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: java.lang.NoSuchMethodException: getAge [int]
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at java.lang.Class.getMethod(Class.java:664)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at java.lang.Class.getMethod(Class.java:643)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at Library.ByteMe.examine(ByteMe.java:94)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at productions.widowmaker110.byteme.MainActivity.onCreate(MainActivity.java:21)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.app.Activity.performCreate(Activity.java:5958)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:144)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.os.Looper.loop(Looper.java:155)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5696)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at java.lang.reflect.Method.invoke(Native Method)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
11-21 20:08:11.704 18121-18121/productions.widowmaker110.byteme D/Library.ByteMe: --------------------------
值得注意的是, getAge(int)是一種非靜態(實例)方法:
public int getAge() {
return Age;
}
觸發該異常的問題源於以下代碼:
Method m = method.getDeclaringClass().getMethod(method.getName(), Integer.TYPE);
這將要求一個不存在的方法int methodName (例如int getAge(int) )觸發NoSuchMethodException 。 這是一個繁瑣的步驟,因為您可以只在方法上調用invoke 。
但是,由於調用也存在問題,因此這將無法單獨使用。 因此,您必須使用對象而不是null進行反射調用。 那也不是一個完整的問題。 該方法是int getAge()而不是int getAge(int) 。 它不需要int參數。 因此,您還需要從invoke刪除0參數。
這是更正的行:
int temp = (int) method.invoke(obj); //first argument is the object to invoke on, ignored if static method
Object Method.invoke(Object,Object ...)的文檔可用於參考:
public Object invoke(Object obj, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException
在具有指定參數的指定對象上調用此Method對象表示的基礎方法。 各個參數將自動解包以匹配原始形式參數,並且原始參數和引用參數都必須根據需要進行方法調用轉換。
如果基礎方法是靜態的,則忽略指定的obj參數。 它可以為空。
如果基礎方法所需的形式參數數量為0,則提供的args數組的長度可以為0或為null。
如果基礎方法是實例方法,則使用《 Java語言規范》第二版15.12.4.4節中所述的動態方法查找來調用該方法。 特別是,將發生基於目標對象的運行時類型的覆蓋。
如果基礎方法是靜態的,則聲明該方法的類將被初始化(如果尚未初始化)。
如果該方法正常完成,則它將返回的值返回給invoke的調用者; 如果值具有原始類型,則首先將其適當包裝在一個對象中。 但是,如果該值具有原始類型的數組的類型,則該數組的元素不會包裝在對象中;而是將其包裝在對象中。 換句話說,將返回原始類型的數組。 如果基礎方法的返回類型為void,則調用返回null。
參數:
- obj-調用基礎方法的對象
- args-用於方法調用的參數
返回:
- 使用args參數在obj上分派此對象表示的方法的結果
拋出:
- IllegalAccessException-如果此Method對象正在強制執行Java語言訪問控制,並且基礎方法不可訪問。
- IllegalArgumentException-如果該方法是一個實例方法,並且指定的對象參數不是聲明基礎方法(或其子類或實現者)的類或接口的實例; 如果實際參數和形式參數的數量不同; 如果原始參數的展開轉換失敗; 或者在可能的解包之后,無法通過方法調用轉換將參數值轉換為相應的形式參數類型。
- InvocationTargetException-如果基礎方法引發異常。
- NullPointerException-如果指定的對象為null,並且該方法是實例方法。 ExceptionInInitializerError-如果此方法引發的初始化失敗。
源: http : //docs.oracle.com/javase/7/docs/api/java/lang/reflect/Method.html#invoke(java.lang.Object,%20java.lang.Object ...)
如果查看Class類的文檔 ,則會發現getMethod(String name, Class...<?> parameterTypes)
帶有表示方法名稱(在您的情況下為“ getAge”)和參數類型(整數)。 您正在訪問的類沒有具有該簽名的方法( public int getAge(int age)
),因此發生了NoSuchMethodException。 這不是在騙你。
另外,由於您已經具有逐步執行的方法,為什么不正確地調用m.invoke(obj)
而不是再次查詢該方法的類呢? 那可能會把事情清理干凈。
最后,正如您的評論所言,當您調用invoke()
您需要傳遞調用方法的對象,除非它是靜態的(不是靜態的)。 因此,您需要將其更改為m.invoke(obj)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.