使用 JAVA 反射时出现 NoSuchMethodException
您好,我正在尝试使用反射来调用方法并更新该方法的 setter 值。但我在调用该方法时遇到了 NoSuchMethodException 。 我已经更新了代码。对于之前代码中的错误,我深感抱歉。我已经折射了代码。当类的 setMethod 接受原始类型参数时,会发生异常。
private static Object performMapping( Class voClass, Class[] clazz, Object voObject, Object data,String fieldType ){
voClass.getMethod( "set" + fieldType, clazz ).invoke( voObject, data );
return voObject;
}
private static Object mapField(ResultSet rs){
Class voClass=Class.forName( "com.test.Test" );
Object voObject = voClass.newInstance();
Class[] doubleArrayParamTypes = new Class[ 1 ];
doubleArrayParamTypes[ 0 ] = Double.class;
voObject = performMapping( voClass, doubleArrayParamTypes, voObject, rs.getDouble(fieldType.getColumn()), "Mark" );
}
/* This is my Class. I need to set the Mark. But it is primitive double. Is it possible to set the mark using the above code? */
public class Test{
private double mark;
public double getMark() {
return mark;
}
public void setMark(double mark) {
this.mark = mark;
Hi I'm trying to use reflection to invoke a method and update the setter value of that method. But I'm getting NoSuchMethodException while ivoking that method.
I've updated the code. I'm so sorry for the errors in the previous code. I've refractored the code. The exception occurs when the setMethod of the class accepts primitive type arguments.
private static Object performMapping( Class voClass, Class[] clazz, Object voObject, Object data,String fieldType ){
voClass.getMethod( "set" + fieldType, clazz ).invoke( voObject, data );
return voObject;
}
private static Object mapField(ResultSet rs){
Class voClass=Class.forName( "com.test.Test" );
Object voObject = voClass.newInstance();
Class[] doubleArrayParamTypes = new Class[ 1 ];
doubleArrayParamTypes[ 0 ] = Double.class;
voObject = performMapping( voClass, doubleArrayParamTypes, voObject, rs.getDouble(fieldType.getColumn()), "Mark" );
}
/* This is my Class. I need to set the Mark. But it is primitive double. Is it possible to set the mark using the above code? */
public class Test{
private double mark;
public double getMark() {
return mark;
}
public void setMark(double mark) {
this.mark = mark;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我看到的是,您传递了
setAddress1
并将其与set
连接起来,从而得到setsetAddress1
。要么传递属性名称并将其大写,要么从串联中删除set
。此外,您提供的代码将无法编译。你不能有一个名为
class
的变量What I see is that you pass
setAddress1
and concatenate it withset
, thus gettingsetsetAddress1
. Either pass the property name and capitalize it, or remove theset
from the concatenation.Also, the code you have provided won't compile. You can't have a variable called
class
一次大胆的尝试,但您不是想获取
setsetAddress1
方法吗?(
“设置”+方法名称
)A shot from the hip, but aren't you trying to get the method
setsetAddress1
?(
"set" + methodName
)下面的代码有效。您有两个错误(除了语法类名错误):
String data="TestData";
作为参数,即使您指定参数应为Double
类型:doubleArrayParamTypes[ 0 ] = Double.class;
The code below works. You had two bugs (except for the syntactical class-name error):
String
String data="TestData";
as argument, even though you specified that the argument should be of typeDouble
:doubleArrayParamTypes[ 0 ] = Double.class;