我有一个简单的问题。我有一个在 Java JDK7 中运行的程序,但由于一些内省更改而无法在 JDK8 中运行。
这是重现该问题的测试程序:
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws IntrospectionException {
BeanInfo info = Introspector.getBeanInfo(MyListClass.class);
PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
for (int i = 0; i < descriptors.length; i++) {
System.out.println(descriptors[i].getClass().getName() + ":" + descriptors[i].getName());
}
System.out.println("\n");
BeanInfo info2 = Introspector.getBeanInfo(MyIndexedListClass.class);
PropertyDescriptor[] descriptors2 = info2.getPropertyDescriptors();
for (int i = 0; i < descriptors2.length; i++) {
System.out.println(descriptors2[i].getClass().getName() + ":" + descriptors2[i].getName());
}
System.out.println("\n");
BeanInfo info3 = Introspector.getBeanInfo(MyArrayClass.class);
PropertyDescriptor[] descriptors3 = info3.getPropertyDescriptors();
for (int i = 0; i < descriptors3.length; i++) {
System.out.println(descriptors3[i].getClass().getName() + ":" + descriptors3[i].getName());
}
System.out.println("\n");
BeanInfo info4 = Introspector.getBeanInfo(MyIndexedArrayClass.class);
PropertyDescriptor[] descriptors4 = info4.getPropertyDescriptors();
for (int i = 0; i < descriptors4.length; i++) {
System.out.println(descriptors4[i].getClass().getName() + ":" + descriptors4[i].getName());
}
}
public class MyListClass {
private List<String> myListClass = new ArrayList<String>();
public List<String> getMyListClass() {
return myListClass;
}
public void setMyListClass(List<String> myListClass) {
this.myListClass = myListClass;
}
}
public class MyIndexedListClass {
private List<String> myIndexedListClass = new ArrayList<String>();
public String getMyIndexedListClass(int index) {
return myIndexedListClass.get(index);
}
public void setMyIndexedListClass(int index, String element) {
this.myIndexedListClass.set(index, element);
}
public List<String> getMyIndexedListClass() {
return myIndexedListClass;
}
public void setMyIndexedListClass(List<String> myIndexedListClass) {
this.myIndexedListClass = myIndexedListClass;
}
}
public class MyArrayClass {
private String[] myArrayClass = new String[20];
public String[] getMyArrayClass() {
return myArrayClass;
}
public void setMyArrayClass(String[] myArrayClass) {
this.myArrayClass = myArrayClass;
}
}
public class MyIndexedArrayClass {
private String[] myIndexedArrayClass = new String[20];
public String getMyIndexedArrayClass(int index) {
return myIndexedArrayClass[index];
}
public void setMyIndexedArrayClass(int index, String myValue) {
this.myIndexedArrayClass[index] = myValue;
}
public String[] getMyIndexedArrayClass() {
return myIndexedArrayClass;
}
public void setMyIndexedArrayClass(String[] myIndexedArrayClass) {
this.myIndexedArrayClass = myIndexedArrayClass;
}
}
}
以下是 JDK 7 日志:
java.beans.PropertyDescriptor:class
java.beans.PropertyDescriptor:myListClass
java.beans.PropertyDescriptor:class
java.beans.IndexedPropertyDescriptor:myIndexedListClass
java.beans.PropertyDescriptor:class
java.beans.PropertyDescriptor:myArrayClass
java.beans.PropertyDescriptor:class
java.beans.IndexedPropertyDescriptor:myIndexedArrayClass
以下是 JDK8 的日志:
java.beans.PropertyDescriptor:class
java.beans.PropertyDescriptor:myListClass
java.beans.PropertyDescriptor:class
java.beans.PropertyDescriptor:myIndexedListClass -> Here is the change
java.beans.PropertyDescriptor:class
java.beans.PropertyDescriptor:myArrayClass
java.beans.PropertyDescriptor:class
java.beans.IndexedPropertyDescriptor:myIndexedArrayClass
我很快就必须使用 JDK8,但这是一个阻碍性的更改,因为我的应用程序不再工作了。我对所有扩展的类都存在这个问题Collection
界面(列表、地图……)
该代码已被图书馆使用commons-beanutils-1.8.0
来自阿帕奇共享资源。
我正在寻找一个解决方案,一个解决方法,使我的应用程序像以前一样工作,当使用JDK7时,有什么解决方案吗?我不能使用数组代替列表(因为数组没有改变)
以下是官方文档的链接:
JDK7: http://docs.oracle.com/javase/7/docs/api/java/beans/IndexedPropertyDescriptor.html http://docs.oracle.com/javase/7/docs/api/java/beans/IndexedPropertyDescriptor.html
JDK8: http://docs.oracle.com/javase/8/docs/api/java/beans/IndexedPropertyDescriptor.html http://docs.oracle.com/javase/8/docs/api/java/beans/IndexedPropertyDescriptor.html
编辑:我找到了我的解决方案,我的问题与struts 1.3有关。我必须在 ActionForm 中重命名索引 getter/setter :http://www.coderanch.com/t/55172/Struts/Indexed-Properties http://www.coderanch.com/t/55172/Struts/Indexed-Properties