来自其他包的 class 时方法调用失败
Method invocation fails when class from other package
我正在尝试为我的程序编写基本代码,我想在返回一些信息之前进行一些身份验证检查。
这是它的示例代码,
//Man.java
package com.test;
import java.lang.reflect.Method;
public abstract class Man {
protected abstract String getPassword();
public final String getMailPassword(){
try{
Method method = getClass().getDeclaredMethod("getPassword");
System.out.println("this: " + this);
System.out.println("method: "+ method.toString());
//check something if all OK
return (String)method.invoke(this);
//else return null
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
}
//Boss.java
package com.test;
import com.test.Man;
public class Boss extends Man {
@Override
protected String getPassword() {
return "Boss'_password";
}
}
//Tester.java
package com.test;
import com.test.test2.Boss;
public class Tester {
public static void main(String arg[]){
//System.out.println(new Servent().getMailPassword());
System.out.println(new Boss().getMailPassword());
}
}
当我执行上面的代码时 (Tester.java) 它正确执行(所有文件都在同一个包中),我得到以下输出。
this: com.test.Boss@22509bfc
method: protected java.lang.String
com.test.Boss.getPassword() Boss'_password
但是如果我 Boss.java 移动到不同的包 "test2",我会得到异常。
java.lang.IllegalAccessException: Class com.test.Man can not access a member of class com.test.test2.Boss with modifiers "protected"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:109)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)
at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)
at java.lang.reflect.Method.invoke(Method.java:599)
at com.test.Man.getMailPassword(Man.java:14)
at com.test.Tester.main(Tester.java:8)
this: com.test.test2.Boss@5a30cefd
method: protected java.lang.String com.test.test2.Boss.getPassword()
null
我已经打印了 'this',它给出了 this: com.test.test2.Boss@5a30cefd
,但是在这种情况下异常检测到超级 class Class com.test.Man can not access a member of class com.test.test2.Boss with modifiers "protected"
。我理解异常,但我不明白为什么在第二种情况下它检测到 super class。如果有人可以帮我解决这个问题(我需要在不同的包中有子 classes,我不能把它们放在同一个包中)
先调用method.setAccessible(true)
再调用invoke()
我正在尝试为我的程序编写基本代码,我想在返回一些信息之前进行一些身份验证检查。
这是它的示例代码,
//Man.java
package com.test;
import java.lang.reflect.Method;
public abstract class Man {
protected abstract String getPassword();
public final String getMailPassword(){
try{
Method method = getClass().getDeclaredMethod("getPassword");
System.out.println("this: " + this);
System.out.println("method: "+ method.toString());
//check something if all OK
return (String)method.invoke(this);
//else return null
}catch(Exception ex){
ex.printStackTrace();
}
return null;
}
}
//Boss.java
package com.test;
import com.test.Man;
public class Boss extends Man {
@Override
protected String getPassword() {
return "Boss'_password";
}
}
//Tester.java
package com.test;
import com.test.test2.Boss;
public class Tester {
public static void main(String arg[]){
//System.out.println(new Servent().getMailPassword());
System.out.println(new Boss().getMailPassword());
}
}
当我执行上面的代码时 (Tester.java) 它正确执行(所有文件都在同一个包中),我得到以下输出。
this: com.test.Boss@22509bfc
method: protected java.lang.String
com.test.Boss.getPassword() Boss'_password
但是如果我 Boss.java 移动到不同的包 "test2",我会得到异常。
java.lang.IllegalAccessException: Class com.test.Man can not access a member of class com.test.test2.Boss with modifiers "protected"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:109)
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(AccessibleObject.java:261)
at java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:253)
at java.lang.reflect.Method.invoke(Method.java:599)
at com.test.Man.getMailPassword(Man.java:14)
at com.test.Tester.main(Tester.java:8)
this: com.test.test2.Boss@5a30cefd
method: protected java.lang.String com.test.test2.Boss.getPassword()
null
我已经打印了 'this',它给出了 this: com.test.test2.Boss@5a30cefd
,但是在这种情况下异常检测到超级 class Class com.test.Man can not access a member of class com.test.test2.Boss with modifiers "protected"
。我理解异常,但我不明白为什么在第二种情况下它检测到 super class。如果有人可以帮我解决这个问题(我需要在不同的包中有子 classes,我不能把它们放在同一个包中)
先调用method.setAccessible(true)
再调用invoke()