7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
Reviewed-by: rupashka
--- a/jdk/src/share/classes/java/beans/IndexedPropertyDescriptor.java Fri Sep 07 13:08:59 2012 +0400
+++ b/jdk/src/share/classes/java/beans/IndexedPropertyDescriptor.java Tue Sep 11 10:58:39 2012 +0400
@@ -495,6 +495,16 @@
indexedReadMethodName = old.indexedReadMethodName;
}
+ void updateGenericsFor(Class<?> type) {
+ super.updateGenericsFor(type);
+ try {
+ setIndexedPropertyType(findIndexedPropertyType(getIndexedReadMethod0(), getIndexedWriteMethod0()));
+ }
+ catch (IntrospectionException exception) {
+ setIndexedPropertyType(null);
+ }
+ }
+
/**
* Returns a hash code value for the object.
* See {@link java.lang.Object#hashCode} for a complete description.
--- a/jdk/src/share/classes/java/beans/Introspector.java Fri Sep 07 13:08:59 2012 +0400
+++ b/jdk/src/share/classes/java/beans/Introspector.java Tue Sep 11 10:58:39 2012 +0400
@@ -574,26 +574,25 @@
// replace existing property descriptor
// only if we have types to resolve
// in the context of this.beanClass
- try {
- String name = pd.getName();
- Method read = pd.getReadMethod();
- Method write = pd.getWriteMethod();
- boolean cls = true;
- if (read != null) cls = cls && read.getGenericReturnType() instanceof Class;
- if (write != null) cls = cls && write.getGenericParameterTypes()[0] instanceof Class;
- if (pd instanceof IndexedPropertyDescriptor) {
- IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor)pd;
- Method readI = ipd.getIndexedReadMethod();
- Method writeI = ipd.getIndexedWriteMethod();
- if (readI != null) cls = cls && readI.getGenericReturnType() instanceof Class;
- if (writeI != null) cls = cls && writeI.getGenericParameterTypes()[1] instanceof Class;
- if (!cls) {
- pd = new IndexedPropertyDescriptor(this.beanClass, name, read, write, readI, writeI);
- }
- } else if (!cls) {
- pd = new PropertyDescriptor(this.beanClass, name, read, write);
+ Method read = pd.getReadMethod();
+ Method write = pd.getWriteMethod();
+ boolean cls = true;
+ if (read != null) cls = cls && read.getGenericReturnType() instanceof Class;
+ if (write != null) cls = cls && write.getGenericParameterTypes()[0] instanceof Class;
+ if (pd instanceof IndexedPropertyDescriptor) {
+ IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
+ Method readI = ipd.getIndexedReadMethod();
+ Method writeI = ipd.getIndexedWriteMethod();
+ if (readI != null) cls = cls && readI.getGenericReturnType() instanceof Class;
+ if (writeI != null) cls = cls && writeI.getGenericParameterTypes()[1] instanceof Class;
+ if (!cls) {
+ pd = new IndexedPropertyDescriptor(ipd);
+ pd.updateGenericsFor(this.beanClass);
}
- } catch ( IntrospectionException e ) {
+ }
+ else if (!cls) {
+ pd = new PropertyDescriptor(pd);
+ pd.updateGenericsFor(this.beanClass);
}
}
list.add(pd);
--- a/jdk/src/share/classes/java/beans/PropertyDescriptor.java Fri Sep 07 13:08:59 2012 +0400
+++ b/jdk/src/share/classes/java/beans/PropertyDescriptor.java Tue Sep 11 10:58:39 2012 +0400
@@ -632,6 +632,16 @@
constrained = old.constrained;
}
+ void updateGenericsFor(Class<?> type) {
+ setClass0(type);
+ try {
+ setPropertyType(findPropertyType(getReadMethod0(), getWriteMethod0()));
+ }
+ catch (IntrospectionException exception) {
+ setPropertyType(null);
+ }
+ }
+
/**
* Returns the property type that corresponds to the read and write method.
* The type precedence is given to the readMethod.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/beans/Introspector/Test7193977.java Tue Sep 11 10:58:39 2012 +0400
@@ -0,0 +1,159 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 7193977
+ * @summary Tests that generified property descriptors do not loose additional info
+ * @author Sergey Malenkov
+ */
+
+import java.awt.Image;
+import java.beans.BeanDescriptor;
+import java.beans.BeanInfo;
+import java.beans.EventSetDescriptor;
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.MethodDescriptor;
+import java.beans.PropertyDescriptor;
+import java.util.Arrays;
+import java.util.List;
+
+public class Test7193977 {
+
+ private static final List<String> names = Arrays.asList("listType", "list", "value");
+
+ public static void main(String args[]) {
+ for (String name : names) {
+ test(Abstract.class, name);
+ test(Concrete.class, name);
+ }
+ }
+
+ private static void test(Class<?> type, String name) {
+ if (!Boolean.TRUE.equals(BeanUtils.getPropertyDescriptor(type, name).getValue("transient"))) {
+ throw new Error("property '" + name + "' is not transient");
+ }
+ }
+
+ public static final class Concrete extends Abstract<String> {
+ }
+
+ public static abstract class Abstract<T> {
+ private List<T> list;
+
+ public List<T> getList() {
+ return this.list;
+ }
+
+ public void setList(List<T> list) {
+ this.list = list;
+ }
+
+ public T getValue(int index) {
+ return (0 <= index) && (this.list != null) && (index < this.list.size())
+ ? this.list.get(index)
+ : null;
+ }
+
+ public void setValue(int index, T value) {
+ if ((0 <= index) && (this.list != null)) {
+ if (index == this.list.size()) {
+ this.list.add(value);
+ }
+ else if (index < this.list.size()) {
+ this.list.set(index, value);
+ }
+ }
+ }
+
+ public String getListType() {
+ return (this.list != null)
+ ? this.list.getClass().getName()
+ : null;
+ }
+
+ public void setListType(String type) throws Exception {
+ this.list = (type != null)
+ ? (List<T>) Class.forName(type).newInstance()
+ : null;
+ }
+ }
+
+ public static final class ConcreteBeanInfo extends Wrapper {
+ public ConcreteBeanInfo() throws IntrospectionException {
+ super(Concrete.class);
+ }
+ }
+
+ public static final class AbstractBeanInfo extends Wrapper {
+ public AbstractBeanInfo() throws IntrospectionException {
+ super(Abstract.class);
+ for (PropertyDescriptor pd : getPropertyDescriptors()) {
+ if (names.contains(pd.getName())) {
+ pd.setValue("transient", Boolean.TRUE);
+ }
+ }
+ }
+ }
+
+ private static class Wrapper implements BeanInfo {
+ private final BeanInfo info;
+
+ Wrapper(Class<?> type) throws IntrospectionException {
+ this.info = Introspector.getBeanInfo(type, Introspector.IGNORE_IMMEDIATE_BEANINFO);
+ }
+
+ public BeanDescriptor getBeanDescriptor() {
+ return this.info.getBeanDescriptor();
+ }
+
+ public EventSetDescriptor[] getEventSetDescriptors() {
+ return this.info.getEventSetDescriptors();
+ }
+
+ public int getDefaultEventIndex() {
+ return this.info.getDefaultEventIndex();
+ }
+
+ public PropertyDescriptor[] getPropertyDescriptors() {
+ return this.info.getPropertyDescriptors();
+ }
+
+ public int getDefaultPropertyIndex() {
+ return this.info.getDefaultPropertyIndex();
+ }
+
+ public MethodDescriptor[] getMethodDescriptors() {
+ return this.info.getMethodDescriptors();
+ }
+
+ public BeanInfo[] getAdditionalBeanInfo() {
+ return this.info.getAdditionalBeanInfo();
+ }
+
+ public Image getIcon(int kind) {
+ return this.info.getIcon(kind);
+ }
+ }
+}