4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
Summary: Add the Transient annotation and support it (JSR-273)
Reviewed-by: peterz, loneid
diff --git a/test/java/beans/Introspector/BeanUtils.java b/test/java/beans/Introspector/BeanUtils.java
index 4b1e763..63c5819 100644
--- a/test/java/beans/Introspector/BeanUtils.java
+++ b/test/java/beans/Introspector/BeanUtils.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
+ * Copyright 2003-2008 Sun Microsystems, Inc.  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
@@ -21,6 +21,7 @@
  * have any questions.
  */
 
+import java.beans.BeanDescriptor;
 import java.beans.EventSetDescriptor;
 import java.beans.IndexedPropertyDescriptor;
 import java.beans.IntrospectionException;
@@ -39,13 +40,26 @@
     }
 
     /**
+     * Returns a bean descriptor for specified class.
+     *
+     * @param type  the class to introspect
+     * @return a bean descriptor
+     */
+    public static BeanDescriptor getBeanDescriptor(Class type) {
+        try {
+            return Introspector.getBeanInfo(type).getBeanDescriptor();
+        } catch (IntrospectionException exception) {
+            throw new Error("unexpected exception", exception);
+        }
+    }
+
+    /**
      * Returns an array of property descriptors for specified class.
      *
      * @param type  the class to introspect
      * @return an array of property descriptors
      */
     public static PropertyDescriptor[] getPropertyDescriptors(Class type) {
-        System.out.println(type);
         try {
             return Introspector.getBeanInfo(type).getPropertyDescriptors();
         } catch (IntrospectionException exception) {
@@ -54,6 +68,38 @@
     }
 
     /**
+     * Returns an array of event set descriptors for specified class.
+     *
+     * @param type  the class to introspect
+     * @return an array of event set descriptors
+     */
+    public static EventSetDescriptor[] getEventSetDescriptors(Class type) {
+        try {
+            return Introspector.getBeanInfo(type).getEventSetDescriptors();
+        } catch (IntrospectionException exception) {
+            throw new Error("unexpected exception", exception);
+        }
+    }
+
+    /**
+     * Finds an event set descriptor for the class
+     * that matches the event set name.
+     *
+     * @param type  the class to introspect
+     * @param name  the name of the event set to search
+     * @return the {@code EventSetDescriptor} or {@code null}
+     */
+    public static EventSetDescriptor findEventSetDescriptor(Class type, String name) {
+        EventSetDescriptor[] esds = getEventSetDescriptors(type);
+        for (EventSetDescriptor esd : esds) {
+            if (esd.getName().equals(name)) {
+                return esd;
+            }
+        }
+        return null;
+    }
+
+    /**
      * Finds a property descriptor for the class
      * that matches the property name.
      *
@@ -72,6 +118,22 @@
     }
 
     /**
+     * Returns a event set descriptor for the class
+     * that matches the property name.
+     *
+     * @param type the class to introspect
+     * @param name the name of the event set to search
+     * @return the {@code EventSetDescriptor}
+     */
+    public static EventSetDescriptor getEventSetDescriptor(Class type, String name) {
+        EventSetDescriptor esd = findEventSetDescriptor(type, name);
+        if (esd != null) {
+            return esd;
+        }
+        throw new Error("could not find event set '" + name + "' in " + type);
+    }
+
+    /**
      * Returns a property descriptor for the class
      * that matches the property name.
      *
diff --git a/test/java/beans/Introspector/Test4896879.java b/test/java/beans/Introspector/Test4896879.java
index 8ccab4d..2423841 100644
--- a/test/java/beans/Introspector/Test4896879.java
+++ b/test/java/beans/Introspector/Test4896879.java
@@ -28,22 +28,16 @@
  * @author Mark Davidson
  */
 
-import java.beans.BeanInfo;
-import java.beans.EventSetDescriptor;
-import java.beans.IntrospectionException;
-import java.beans.Introspector;
 import java.util.EventListener;
 
 public class Test4896879 {
-    public static void main(String[] args) throws IntrospectionException {
+    public static void main(String[] args) {
         test(A.class);
         test(B.class);
     }
 
-    private static void test(Class type) throws IntrospectionException {
-        BeanInfo info = Introspector.getBeanInfo(type);
-        EventSetDescriptor[] descriptors = info.getEventSetDescriptors();
-        if (descriptors.length != 0) {
+    private static void test(Class type) {
+        if (BeanUtils.getEventSetDescriptors(type).length != 0) {
             throw new Error("Should not have any EventSetDescriptors");
         }
     }
diff --git a/test/java/beans/Introspector/Test4935607.java b/test/java/beans/Introspector/Test4935607.java
new file mode 100644
index 0000000..7ce48c9
--- /dev/null
+++ b/test/java/beans/Introspector/Test4935607.java
@@ -0,0 +1,404 @@
+/*
+ * Copyright 2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test %I% %G%
+ * @bug 4935607
+ * @summary Tests transient properties
+ * @author Sergey Malenkov
+ */
+
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.Point;
+import java.awt.Rectangle;
+import java.awt.ScrollPane;
+import java.awt.geom.RectangularShape;
+import java.awt.im.InputContext;
+
+import java.beans.FeatureDescriptor;
+import java.beans.Transient;
+
+import java.util.EventListener;
+
+import javax.swing.AbstractButton;
+import javax.swing.DefaultListSelectionModel;
+import javax.swing.ImageIcon;
+import javax.swing.JComboBox;
+import javax.swing.JComponent;
+import javax.swing.JLabel;
+import javax.swing.JList;
+import javax.swing.JMenuBar;
+import javax.swing.JScrollPane;
+import javax.swing.JTabbedPane;
+import javax.swing.JViewport;
+import javax.swing.table.JTableHeader;
+import javax.swing.text.JTextComponent;
+
+public class Test4935607 {
+    public static void main(String[] args) {
+        // test all possible cases
+        test(Null.class);
+        test(True.class);
+        test(False.class);
+        test(NullNull.class);
+        test(TrueNull.class);
+        test(FalseNull.class);
+        test(NullTrue.class);
+        test(TrueTrue.class);
+        test(FalseTrue.class);
+        test(NullFalse.class);
+        test(TrueFalse.class);
+        test(FalseFalse.class);
+        // test transient properties in AWT
+        test(RectangularShape.class, "frame"); // NON-NLS: getFrame()
+        test(Rectangle.class, "bounds"); // NON-NLS: getBounds()
+        test(Dimension.class, "size"); // NON-NLS: getSize()
+        test(Point.class, "location"); // NON-NLS: getLocation()
+        test(Component.class, "foreground"); // NON-NLS: getForeground()
+        test(Component.class, "background"); // NON-NLS: getBackground()
+        test(Component.class, "font"); // NON-NLS: getFont()
+        test(Component.class, "visible"); // NON-NLS: getVisible()
+        test(ScrollPane.class, "scrollPosition"); // NON-NLS: getScrollPosition()
+        test(InputContext.class, "compositionEnabled"); // NON-NLS: getCompositionEnabled()
+        // test transient properties in Swing
+        test(JComponent.class, "minimumSize"); // NON-NLS: getMinimumSize()
+        test(JComponent.class, "preferredSize"); // NON-NLS: getPreferredSize()
+        test(JComponent.class, "maximumSize"); // NON-NLS: getMaximumSize()
+        test(ImageIcon.class, "image"); // NON-NLS: getImage()
+        test(ImageIcon.class, "imageObserver"); // NON-NLS: getImageObserver()
+        test(JMenuBar.class, "helpMenu"); // NON-NLS: getHelpMenu()
+        test(JScrollPane.class, "verticalScrollBar"); // NON-NLS: getVerticalScrollBar()
+        test(JScrollPane.class, "horizontalScrollBar"); // NON-NLS: getHorizontalScrollBar()
+        test(JScrollPane.class, "rowHeader"); // NON-NLS: getRowHeader()
+        test(JScrollPane.class, "columnHeader"); // NON-NLS: getColumnHeader()
+        test(JViewport.class, "extentSize"); // NON-NLS: getExtentSize()
+        test(JTableHeader.class, "defaultRenderer"); // NON-NLS: getDefaultRenderer()
+        test(JList.class, "cellRenderer"); // NON-NLS: getCellRenderer()
+        test(JList.class, "selectedIndices"); // NON-NLS: getSelectedIndices()
+        test(DefaultListSelectionModel.class, "leadSelectionIndex"); // NON-NLS: getLeadSelectionIndex()
+        test(DefaultListSelectionModel.class, "anchorSelectionIndex"); // NON-NLS: getAnchorSelectionIndex()
+        test(JComboBox.class, "selectedIndex"); // NON-NLS: getSelectedIndex()
+        test(JTabbedPane.class, "selectedIndex"); // NON-NLS: getSelectedIndex()
+        test(JTabbedPane.class, "selectedComponent"); // NON-NLS: getSelectedComponent()
+        test(AbstractButton.class, "disabledIcon"); // NON-NLS: getDisabledIcon()
+        test(JLabel.class, "disabledIcon"); // NON-NLS: getDisabledIcon()
+        test(JTextComponent.class, "caret"); // NON-NLS: getCaret()
+        test(JTextComponent.class, "caretPosition"); // NON-NLS: getCaretPosition()
+        test(JTextComponent.class, "selectionStart"); // NON-NLS: getSelectionStart()
+        test(JTextComponent.class, "selectionEnd"); // NON-NLS: getSelectionEnd()
+    }
+
+    private static void test(Class type) {
+        Object value = getExpectedValue(type);
+        test(value, BeanUtils.getPropertyDescriptor(type, "property")); // NON-NLS: the property to check
+        test(value, BeanUtils.getEventSetDescriptor(type, "eventSet")); // NON-NLS: the event set to check
+        System.out.println();
+    }
+
+    private static void test(Class type, String property) {
+        System.out.print(type.getName() + ": ");
+        test(Boolean.TRUE, BeanUtils.getPropertyDescriptor(type, property));
+    }
+
+    private static void test(Object expected, FeatureDescriptor fd) {
+        System.out.println(fd.getName());
+        Object actual = fd.getValue("transient"); // NON-NLS: the attribute name
+        if ((actual == null) ? (expected != null) : !actual.equals(expected))
+            throw new Error("expected " + expected + " value, but actual value is " + actual);
+    }
+
+    private static Object getExpectedValue(Class type) {
+        try {
+            return type.getField("VALUE").get(type); // NON-NLS: the field name with expected value
+        } catch (NoSuchFieldException exception) {
+            return null;
+        } catch (IllegalAccessException exception) {
+            throw new Error("unexpected error", exception);
+        }
+    }
+
+
+    public static class Null {
+        public Object getProperty() {
+            return this;
+        }
+
+        public void setProperty(Object object) {
+        }
+
+        public void addEventSetListener(EventSetListener listener) {
+        }
+
+        public void removeEventSetListener(EventSetListener listener) {
+        }
+    }
+
+    public static class True {
+        public static final Boolean VALUE = Boolean.TRUE;
+
+        @Transient
+        public Object getProperty() {
+            return this;
+        }
+
+        @Transient
+        public void setProperty(Object object) {
+        }
+
+        @Transient
+        public void addEventSetListener(EventSetListener listener) {
+        }
+
+        @Transient
+        public void removeEventSetListener(EventSetListener listener) {
+        }
+    }
+
+    public static class False {
+        public static final Boolean VALUE = Boolean.FALSE;
+
+        @Transient(false)
+        public Object getProperty() {
+            return this;
+        }
+
+        @Transient(false)
+        public void setProperty(Object object) {
+        }
+
+        @Transient(false)
+        public void addEventSetListener(EventSetListener listener) {
+        }
+
+        @Transient(false)
+        public void removeEventSetListener(EventSetListener listener) {
+        }
+    }
+
+    public static class NullNull extends Null {
+        @Override
+        public Object getProperty() {
+            return this;
+        }
+
+        @Override
+        public void setProperty(Object object) {
+        }
+
+        @Override
+        public void addEventSetListener(EventSetListener listener) {
+        }
+
+        @Override
+        public void removeEventSetListener(EventSetListener listener) {
+        }
+    }
+
+    public static class TrueNull extends Null {
+        public static final Boolean VALUE = Boolean.TRUE;
+
+        @Override
+        @Transient
+        public Object getProperty() {
+            return this;
+        }
+
+        @Override
+        @Transient
+        public void setProperty(Object object) {
+        }
+
+        @Override
+        @Transient
+        public void addEventSetListener(EventSetListener listener) {
+        }
+
+        @Override
+        @Transient
+        public void removeEventSetListener(EventSetListener listener) {
+        }
+    }
+
+    public static class FalseNull extends Null {
+        public static final Boolean VALUE = Boolean.FALSE;
+
+        @Override
+        @Transient(false)
+        public Object getProperty() {
+            return this;
+        }
+
+        @Override
+        @Transient(false)
+        public void setProperty(Object object) {
+        }
+
+        @Override
+        @Transient(false)
+        public void addEventSetListener(EventSetListener listener) {
+        }
+
+        @Override
+        @Transient(false)
+        public void removeEventSetListener(EventSetListener listener) {
+        }
+    }
+
+    public static class NullTrue extends True {
+        @Override
+        public Object getProperty() {
+            return this;
+        }
+
+        @Override
+        public void setProperty(Object object) {
+        }
+
+        @Override
+        public void addEventSetListener(EventSetListener listener) {
+        }
+
+        @Override
+        public void removeEventSetListener(EventSetListener listener) {
+        }
+    }
+
+    public static class TrueTrue extends True {
+        @Override
+        @Transient
+        public Object getProperty() {
+            return this;
+        }
+
+        @Override
+        @Transient
+        public void setProperty(Object object) {
+        }
+
+        @Override
+        @Transient
+        public void addEventSetListener(EventSetListener listener) {
+        }
+
+        @Override
+        @Transient
+        public void removeEventSetListener(EventSetListener listener) {
+        }
+    }
+
+    public static class FalseTrue extends True {
+        public static final Boolean VALUE = Boolean.FALSE;
+
+        @Override
+        @Transient(false)
+        public Object getProperty() {
+            return this;
+        }
+
+        @Override
+        @Transient(false)
+        public void setProperty(Object object) {
+        }
+
+        @Override
+        @Transient(false)
+        public void addEventSetListener(EventSetListener listener) {
+        }
+
+        @Override
+        @Transient(false)
+        public void removeEventSetListener(EventSetListener listener) {
+        }
+    }
+
+    public static class NullFalse extends False {
+        @Override
+        public Object getProperty() {
+            return this;
+        }
+
+        @Override
+        public void setProperty(Object object) {
+        }
+
+        @Override
+        public void addEventSetListener(EventSetListener listener) {
+        }
+
+        @Override
+        public void removeEventSetListener(EventSetListener listener) {
+        }
+    }
+
+    public static class TrueFalse extends False {
+        public static final Boolean VALUE = Boolean.TRUE;
+
+        @Override
+        @Transient
+        public Object getProperty() {
+            return this;
+        }
+
+        @Override
+        @Transient
+        public void setProperty(Object object) {
+        }
+
+        @Override
+        @Transient
+        public void addEventSetListener(EventSetListener listener) {
+        }
+
+        @Override
+        @Transient
+        public void removeEventSetListener(EventSetListener listener) {
+        }
+    }
+
+    public static class FalseFalse extends False {
+        @Override
+        @Transient(false)
+        public Object getProperty() {
+            return this;
+        }
+
+        @Override
+        @Transient(false)
+        public void setProperty(Object object) {
+        }
+
+        @Override
+        @Transient(false)
+        public void addEventSetListener(EventSetListener listener) {
+        }
+
+        @Override
+        @Transient(false)
+        public void removeEventSetListener(EventSetListener listener) {
+        }
+    }
+
+    public static final class EventSetListener implements EventListener {
+    }
+}
diff --git a/test/java/beans/XMLEncoder/Test4935607.java b/test/java/beans/XMLEncoder/Test4935607.java
new file mode 100644
index 0000000..3d5e70e
--- /dev/null
+++ b/test/java/beans/XMLEncoder/Test4935607.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test %I% %G%
+ * @bug 4935607
+ * @summary Tests transient properties
+ * @author Sergey Malenkov
+ */
+
+import java.beans.Transient;
+
+public class Test4935607 extends AbstractTest<Test4935607.TransientBean> {
+    public static void main(String[] args) {
+        new Test4935607().test(true);
+    }
+
+    @Override
+    protected TransientBean getObject() {
+        TransientBean bean = new TransientBean();
+        bean.setName("some string"); // NON-NLS: some string
+        return bean;
+    }
+
+    @Override
+    protected TransientBean getAnotherObject() {
+        TransientBean bean = new TransientBean();
+        bean.setName("another string"); // NON-NLS: another string
+        bean.setComment("some comment"); // NON-NLS: some comment
+        return bean;
+    }
+
+    @Override
+    protected void validate(TransientBean before, TransientBean after) {
+        if (!before.getName().equals(after.getName()))
+            throw new Error("the name property incorrectly encoded");
+
+        if (null != after.getComment())
+            throw new Error("the comment property should be encoded");
+    }
+
+    public static class TransientBean {
+        private String name;
+        private String comment;
+
+        public String getName() {
+            return this.name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+
+        @Transient
+        public String getComment() {
+            return this.comment;
+        }
+
+        public void setComment(String comment) {
+            this.comment = comment;
+        }
+    }
+}