代码如下/**
* This method is for copying values from oldObject to newobject
*
* @param newObject
* @param oldObject
*/
public void copyObjects(Object newObject, Object oldObject) {
Method[] newObjectMethods = oldObject.getClass().getDeclaredMethods();
Method getterMethod = null;
Method setterMethod = null;
Object valueToSet = null;
String getterMethodName;
String setterMethodName;
Object values[] = new Object[1];
for (int i = 0; i < newObjectMethods.length; i++) {
getterMethod = newObjectMethods[i];
getterMethodName = getterMethod.getName();
if (getterMethodName.indexOf("get") != -1
&& !Modifier.isStatic(getterMethod.getModifiers())
&& !Modifier.isFinal(getterMethod.getModifiers())
&& !Modifier.isProtected(getterMethod.getModifiers())
&& !Modifier.isPrivate(getterMethod.getModifiers())) { getterMethod.setAccessible(true);
setterMethodName = getterMethodName.replaceFirst("get", "set");
setterMethod = getSetterMethodIfAvailable(newObject, setterMethodName);
if (setterMethod != null) {
try {
Class Parametertype[] = setterMethod.getParameterTypes();
valueToSet = getterMethod.invoke(oldObject, null); //这里的警告
if (valueToSet != null) {
valueToSet = convert(valueToSet, Parametertype[0]);
values[0] = valueToSet;
setterMethod.invoke(newObject, values);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
[javac] C:\Test.java:163: warning: non-varargs call of varargs method with inexact argument type for last parameter;
[javac] cast to java.lang.Object for a varargs call
[javac] cast to java.lang.Object[] for a non-varargs call and to suppress this warning
[javac] valueToSet = getterMethod.invoke(oldObject,null);
请问一下这种警告应该如何消除呢? 谢谢
* This method is for copying values from oldObject to newobject
*
* @param newObject
* @param oldObject
*/
public void copyObjects(Object newObject, Object oldObject) {
Method[] newObjectMethods = oldObject.getClass().getDeclaredMethods();
Method getterMethod = null;
Method setterMethod = null;
Object valueToSet = null;
String getterMethodName;
String setterMethodName;
Object values[] = new Object[1];
for (int i = 0; i < newObjectMethods.length; i++) {
getterMethod = newObjectMethods[i];
getterMethodName = getterMethod.getName();
if (getterMethodName.indexOf("get") != -1
&& !Modifier.isStatic(getterMethod.getModifiers())
&& !Modifier.isFinal(getterMethod.getModifiers())
&& !Modifier.isProtected(getterMethod.getModifiers())
&& !Modifier.isPrivate(getterMethod.getModifiers())) { getterMethod.setAccessible(true);
setterMethodName = getterMethodName.replaceFirst("get", "set");
setterMethod = getSetterMethodIfAvailable(newObject, setterMethodName);
if (setterMethod != null) {
try {
Class Parametertype[] = setterMethod.getParameterTypes();
valueToSet = getterMethod.invoke(oldObject, null); //这里的警告
if (valueToSet != null) {
valueToSet = convert(valueToSet, Parametertype[0]);
values[0] = valueToSet;
setterMethod.invoke(newObject, values);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
[javac] C:\Test.java:163: warning: non-varargs call of varargs method with inexact argument type for last parameter;
[javac] cast to java.lang.Object for a varargs call
[javac] cast to java.lang.Object[] for a non-varargs call and to suppress this warning
[javac] valueToSet = getterMethod.invoke(oldObject,null);
请问一下这种警告应该如何消除呢? 谢谢
common-beantuisl.jar这个包
invoke(oldObject,(Object[])null);