Java 对象不是声明类的实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33476292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java object is not an instance of declaring class
提问by Peter Penzov
public class SendEmailImpl
{
private boolean isValidEmailAddress(String email)
{
boolean stricterFilter = true;
String stricterFilterString = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}";
String laxString = ".+@.+\.[A-Za-z]{2}[A-Za-z]*";
String emailRegex = stricterFilter ? stricterFilterString : laxString;
Pattern p = Pattern.compile(emailRegex);
Matcher m = p.matcher(email);
return m.matches();
}
}
I tried to call this code using reflection
我尝试使用反射调用此代码
@Test
public void testValidEmail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
Method method = SendEmailImpl.class.getDeclaredMethod("isValidEmailAddress", String.class);
method.setAccessible(true);
Boolean invoke = (Boolean) method.invoke("isValidEmailAddress", String.class);
assertTrue(invoke);
System.out.println("Testing E-mail validator - case [email protected]");
}
But I get error
但我得到错误
java.lang.IllegalArgumentException: object is not an instance of declaring class
java.lang.IllegalArgumentException:对象不是声明类的实例
Do you have any idea where is my code wrong?
你知道我的代码哪里错了吗?
I also tried this:
我也试过这个:
@Test
public void testValidEmail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
Method method1 = SendEmailImpl.class.getDeclaredMethod("isValidEmailAddress", String.class);
method1.setAccessible(true);
Boolean invoke = (Boolean)method1.invoke(String.class);
assertTrue(invoke);
System.out.println("Testing E-mail validator - case [email protected]");
}
But the result is the same.
但结果是一样的。
回答by Tunaki
You are invoking the isValidEmailAddress
method with a Class<String>
parameter (String.class
) instead of a String
. Also, the first argument should be an instance of the class you want to invoke the method on (since it is not a static method).
您正在isValidEmailAddress
使用Class<String>
参数 ( String.class
) 而不是String
. 此外,第一个参数应该是您要调用该方法的类的实例(因为它不是静态方法)。
Quoting Method.invoke
Javadoc:
引用Method.invoke
Javadoc:
Parameters:
- obj - the object the underlying method is invoked from
- args - the arguments used for the method call
参数:
- obj - 调用底层方法的对象
- args - 用于方法调用的参数
Corrected code:
更正的代码:
@Test
public void testValidEmail() throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
SendEmailImpl instance = new SendEmailImpl();
Method method = instance.getClass().getDeclaredMethod("isValidEmailAddress", String.class);
method.setAccessible(true);
Boolean invoke = (Boolean) method.invoke(instance, "myStringArgument");
// rest of code
}