jdk/src/java.desktop/share/classes/java/beans/PropertyDescriptor.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 25566 jdk/src/share/classes/java/beans/PropertyDescriptor.java@ba387c302edd
child 30476 dd563be4f10f
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25123
1f70b30da563 8042860: Fix raw and unchecked warnings in java.beans
darcy
parents: 21278
diff changeset
     2
 * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4960
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4960
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4960
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4960
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4960
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.beans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.lang.ref.Reference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Constructor;
25566
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
    30
import java.util.Map.Entry;
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
    31
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
    32
import com.sun.beans.introspect.PropertyInfo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A PropertyDescriptor describes one property that a Java Bean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * exports via a pair of accessor methods.
25130
adfaa02ea516 8044855: Add missing @since tag under java.beans.*
henryjen
parents: 25123
diff changeset
    37
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class PropertyDescriptor extends FeatureDescriptor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
    41
    private Reference<? extends Class<?>> propertyTypeRef;
20110
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents: 19213
diff changeset
    42
    private final MethodRef readMethodRef = new MethodRef();
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents: 19213
diff changeset
    43
    private final MethodRef writeMethodRef = new MethodRef();
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
    44
    private Reference<? extends Class<?>> propertyEditorClassRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private boolean bound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private boolean constrained;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // The base name of the method name which will be prefixed with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // read and write method. If name == "foo" then the baseName is "Foo"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private String baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private String writeMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private String readMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Constructs a PropertyDescriptor for a property that follows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * the standard Java convention by having getFoo and setFoo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * accessor methods.  Thus if the argument name is "fred", it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * assume that the writer method is "setFred" and the reader method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * is "getFred" (or "isFred" for a boolean property).  Note that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * property name should start with a lower case character, which will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * be capitalized in the method names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param propertyName The programmatic name of the property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param beanClass The Class object for the target bean.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *          example sun.beans.OurButton.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @exception IntrospectionException if an exception occurs during
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *              introspection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public PropertyDescriptor(String propertyName, Class<?> beanClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                throws IntrospectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this(propertyName, beanClass,
20110
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents: 19213
diff changeset
    74
                Introspector.IS_PREFIX + NameGenerator.capitalize(propertyName),
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents: 19213
diff changeset
    75
                Introspector.SET_PREFIX + NameGenerator.capitalize(propertyName));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * This constructor takes the name of a simple property, and method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * names for reading and writing the property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param propertyName The programmatic name of the property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param beanClass The Class object for the target bean.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *          example sun.beans.OurButton.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param readMethodName The name of the method used for reading the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *           value.  May be null if the property is write-only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param writeMethodName The name of the method used for writing the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *           value.  May be null if the property is read-only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @exception IntrospectionException if an exception occurs during
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *              introspection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public PropertyDescriptor(String propertyName, Class<?> beanClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                String readMethodName, String writeMethodName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                throws IntrospectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (beanClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new IntrospectionException("Target Bean class is null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (propertyName == null || propertyName.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new IntrospectionException("bad property name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if ("".equals(readMethodName) || "".equals(writeMethodName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new IntrospectionException("read or write method name should not be the empty string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        setName(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        setClass0(beanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        this.readMethodName = readMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (readMethodName != null && getReadMethod() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new IntrospectionException("Method not found: " + readMethodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.writeMethodName = writeMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (writeMethodName != null && getWriteMethod() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            throw new IntrospectionException("Method not found: " + writeMethodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // If this class or one of its base classes allow PropertyChangeListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // then we assume that any properties we discover are "bound".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // See Introspector.getTargetPropertyInfo() method.
25123
1f70b30da563 8042860: Fix raw and unchecked warnings in java.beans
darcy
parents: 21278
diff changeset
   118
        Class<?>[] args = { PropertyChangeListener.class };
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5947
diff changeset
   119
        this.bound = null != Introspector.findMethod(beanClass, "addPropertyChangeListener", args.length, args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * This constructor takes the name of a simple property, and Method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * objects for reading and writing the property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param propertyName The programmatic name of the property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param readMethod The method used for reading the property value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *          May be null if the property is write-only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param writeMethod The method used for writing the property value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *          May be null if the property is read-only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @exception IntrospectionException if an exception occurs during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *              introspection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public PropertyDescriptor(String propertyName, Method readMethod, Method writeMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                throws IntrospectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (propertyName == null || propertyName.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new IntrospectionException("bad property name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        setName(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        setReadMethod(readMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        setWriteMethod(writeMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
25566
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   145
     * Creates {@code PropertyDescriptor} from the specified property info.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
25566
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   147
     * @param entry  the pair of values,
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   148
     *               where the {@code key} is the base name of the property (the rest of the method name)
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   149
     *               and the {@code value} is the automatically generated property info
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   150
     * @param bound  the flag indicating whether it is possible to treat this property as a bound property
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
25566
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   152
     * @since 1.9
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
25566
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   154
    PropertyDescriptor(Entry<String,PropertyInfo> entry, boolean bound) {
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   155
        String base = entry.getKey();
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   156
        PropertyInfo info = entry.getValue();
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   157
        setName(Introspector.decapitalize(base));
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   158
        setReadMethod0(info.getReadMethod());
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   159
        setWriteMethod0(info.getWriteMethod());
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   160
        setPropertyType(info.getPropertyType());
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   161
        setConstrained(info.isConstrained());
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   162
        setBound(bound && info.is(PropertyInfo.Name.bound));
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   163
        if (info.is(PropertyInfo.Name.expert)) {
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   164
            setValue(PropertyInfo.Name.expert.name(), Boolean.TRUE); // compatibility
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   165
            setExpert(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
25566
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   167
        if (info.is(PropertyInfo.Name.hidden)) {
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   168
            setValue(PropertyInfo.Name.hidden.name(), Boolean.TRUE); // compatibility
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   169
            setHidden(true);
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   170
        }
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   171
        if (info.is(PropertyInfo.Name.preferred)) {
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   172
            setPreferred(true);
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   173
        }
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   174
        Object visual = info.get(PropertyInfo.Name.visualUpdate);
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   175
        if (visual != null) {
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   176
            setValue(PropertyInfo.Name.visualUpdate.name(), visual);
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   177
        }
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   178
        Object description = info.get(PropertyInfo.Name.description);
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   179
        if (description != null) {
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   180
            setShortDescription(description.toString());
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   181
        }
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   182
        Object values = info.get(PropertyInfo.Name.enumerationValues);
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   183
        if (values != null) {
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   184
            setValue(PropertyInfo.Name.enumerationValues.name(), values);
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   185
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        this.baseName = base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
4392
4a92100685fa 4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents: 3241
diff changeset
   190
     * Returns the Java type info for the property.
4a92100685fa 4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents: 3241
diff changeset
   191
     * Note that the {@code Class} object may describe
4a92100685fa 4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents: 3241
diff changeset
   192
     * primitive Java types such as {@code int}.
4a92100685fa 4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents: 3241
diff changeset
   193
     * This type is returned by the read method
4a92100685fa 4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents: 3241
diff changeset
   194
     * or is used as the parameter type of the write method.
4a92100685fa 4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents: 3241
diff changeset
   195
     * Returns {@code null} if the type is an indexed property
4a92100685fa 4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents: 3241
diff changeset
   196
     * that does not support non-indexed access.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
4392
4a92100685fa 4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents: 3241
diff changeset
   198
     * @return the {@code Class} object that represents the Java type info,
4a92100685fa 4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents: 3241
diff changeset
   199
     *         or {@code null} if the type cannot be determined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public synchronized Class<?> getPropertyType() {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   202
        Class<?> type = getPropertyType0();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (type  == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                type = findPropertyType(getReadMethod(), getWriteMethod());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                setPropertyType(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            } catch (IntrospectionException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                // Fall
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   214
    private void setPropertyType(Class<?> type) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this.propertyTypeRef = getWeakReference(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   218
    private Class<?> getPropertyType0() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        return (this.propertyTypeRef != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                ? this.propertyTypeRef.get()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Gets the method that should be used to read the property value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @return The method that should be used to read the property value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * May return null if the property can't be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public synchronized Method getReadMethod() {
20110
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents: 19213
diff changeset
   231
        Method readMethod = this.readMethodRef.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (readMethod == null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   233
            Class<?> cls = getClass0();
20110
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents: 19213
diff changeset
   234
            if (cls == null || (readMethodName == null && !this.readMethodRef.isSet())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                // The read method was explicitly set to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
13356
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   238
            String nextMethodName = Introspector.GET_PREFIX + getBaseName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (readMethodName == null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   240
                Class<?> type = getPropertyType0();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                if (type == boolean.class || type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    readMethodName = Introspector.IS_PREFIX + getBaseName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                } else {
13356
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   244
                    readMethodName = nextMethodName;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            // Since there can be multiple write methods but only one getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            // method, find the getter method first so that you know what the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            // property type is.  For booleans, there can be "is" and "get"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // methods.  If an "is" method exists, this is the official
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            // reader method so look for this one first.
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5947
diff changeset
   253
            readMethod = Introspector.findMethod(cls, readMethodName, 0);
13356
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   254
            if ((readMethod == null) && !readMethodName.equals(nextMethodName)) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   255
                readMethodName = nextMethodName;
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5947
diff changeset
   256
                readMethod = Introspector.findMethod(cls, readMethodName, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                setReadMethod(readMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            } catch (IntrospectionException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                // fall
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return readMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Sets the method that should be used to read the property value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param readMethod The new read method.
19213
c360667a0da2 8022406: Fix doclint issues in java.beans
darcy
parents: 15990
diff changeset
   271
     * @throws IntrospectionException if the read method is invalid
25130
adfaa02ea516 8044855: Add missing @since tag under java.beans.*
henryjen
parents: 25123
diff changeset
   272
     * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public synchronized void setReadMethod(Method readMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                                throws IntrospectionException {
25566
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   276
        // The property type is determined by the read method.
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   277
        setPropertyType(findPropertyType(readMethod, this.writeMethodRef.get()));
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   278
        setReadMethod0(readMethod);
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   279
    }
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   280
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   281
    private void setReadMethod0(Method readMethod) {
20110
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents: 19213
diff changeset
   282
        this.readMethodRef.set(readMethod);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (readMethod == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            readMethodName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        setClass0(readMethod.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        readMethodName = readMethod.getName();
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   290
        setTransient(readMethod.getAnnotation(Transient.class));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Gets the method that should be used to write the property value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return The method that should be used to write the property value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * May return null if the property can't be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public synchronized Method getWriteMethod() {
20110
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents: 19213
diff changeset
   300
        Method writeMethod = this.writeMethodRef.get();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (writeMethod == null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   302
            Class<?> cls = getClass0();
20110
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents: 19213
diff changeset
   303
            if (cls == null || (writeMethodName == null && !this.writeMethodRef.isSet())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                // The write method was explicitly set to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            // We need the type to fetch the correct method.
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   309
            Class<?> type = getPropertyType0();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    // Can't use getPropertyType since it will lead to recursive loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    type = findPropertyType(getReadMethod(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    setPropertyType(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                } catch (IntrospectionException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    // Without the correct property type we can't be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    // to find the correct method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (writeMethodName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                writeMethodName = Introspector.SET_PREFIX + getBaseName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   326
            Class<?>[] args = (type == null) ? null : new Class<?>[] { type };
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5947
diff changeset
   327
            writeMethod = Introspector.findMethod(cls, writeMethodName, 1, args);
3241
6fd229e009e7 6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents: 466
diff changeset
   328
            if (writeMethod != null) {
6fd229e009e7 6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents: 466
diff changeset
   329
                if (!writeMethod.getReturnType().equals(void.class)) {
6fd229e009e7 6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents: 466
diff changeset
   330
                    writeMethod = null;
6fd229e009e7 6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents: 466
diff changeset
   331
                }
6fd229e009e7 6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents: 466
diff changeset
   332
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                setWriteMethod(writeMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            } catch (IntrospectionException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                // fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return writeMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Sets the method that should be used to write the property value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param writeMethod The new write method.
19213
c360667a0da2 8022406: Fix doclint issues in java.beans
darcy
parents: 15990
diff changeset
   346
     * @throws IntrospectionException if the write method is invalid
25130
adfaa02ea516 8044855: Add missing @since tag under java.beans.*
henryjen
parents: 25123
diff changeset
   347
     * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public synchronized void setWriteMethod(Method writeMethod)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                throws IntrospectionException {
25566
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   351
        // Set the property type - which validates the method
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   352
        setPropertyType(findPropertyType(getReadMethod(), writeMethod));
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   353
        setWriteMethod0(writeMethod);
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   354
    }
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   355
ba387c302edd 4058433: RFE: tool for creating BeanInfo template
malenkov
parents: 25130
diff changeset
   356
    private void setWriteMethod0(Method writeMethod) {
20110
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents: 19213
diff changeset
   357
        this.writeMethodRef.set(writeMethod);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (writeMethod == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            writeMethodName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        setClass0(writeMethod.getDeclaringClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        writeMethodName = writeMethod.getName();
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   365
        setTransient(writeMethod.getAnnotation(Transient.class));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Overridden to ensure that a super class doesn't take precedent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   371
    void setClass0(Class<?> clz) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (getClass0() != null && clz.isAssignableFrom(getClass0())) {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20110
diff changeset
   373
            // don't replace a subclass with a superclass
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        super.setClass0(clz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Updates to "bound" properties will cause a "PropertyChange" event to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * get fired when the property is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @return True if this is a bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public boolean isBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return bound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Updates to "bound" properties will cause a "PropertyChange" event to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * get fired when the property is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @param bound True if this is a bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public void setBound(boolean bound) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        this.bound = bound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Attempted updates to "Constrained" properties will cause a "VetoableChange"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * event to get fired when the property is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @return True if this is a constrained property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public boolean isConstrained() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        return constrained;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Attempted updates to "Constrained" properties will cause a "VetoableChange"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * event to get fired when the property is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @param constrained True if this is a constrained property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public void setConstrained(boolean constrained) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        this.constrained = constrained;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Normally PropertyEditors will be found using the PropertyEditorManager.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * However if for some reason you want to associate a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * PropertyEditor with a given property, then you can do it with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @param propertyEditorClass  The Class for the desired PropertyEditor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public void setPropertyEditorClass(Class<?> propertyEditorClass) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   429
        this.propertyEditorClassRef = getWeakReference(propertyEditorClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * Gets any explicit PropertyEditor Class that has been registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * for this property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @return Any explicit PropertyEditor Class that has been registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *          for this property.  Normally this will return "null",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *          indicating that no special editor has been registered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *          so the PropertyEditorManager should be used to locate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *          a suitable PropertyEditor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    public Class<?> getPropertyEditorClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return (this.propertyEditorClassRef != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                ? this.propertyEditorClassRef.get()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * Constructs an instance of a property editor using the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * property editor class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * If the property editor class has a public constructor that takes an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * Object argument then it will be invoked using the bean parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * as the argument. Otherwise, the default constructor will be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @param bean the source object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @return a property editor instance or null if a property editor has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *         not been defined or cannot be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    public PropertyEditor createPropertyEditor(Object bean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        Object editor = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   464
        Class<?> cls = getPropertyEditorClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (cls != null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   466
            Constructor<?> ctor = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            if (bean != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                try {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   469
                    ctor = cls.getConstructor(new Class<?>[] { Object.class });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    // Fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                if (ctor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    editor = cls.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    editor = ctor.newInstance(new Object[] { bean });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            } catch (Exception ex) {
11087
3217e8ad1d4a 7087876: java/beans/PropertyDescriptor.html#createPropertyEditor() throws RE if editor cannot be created
malenkov
parents: 6657
diff changeset
   481
                // Fall through
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return (PropertyEditor)editor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Compares this <code>PropertyDescriptor</code> against the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * Returns true if the objects are the same. Two <code>PropertyDescriptor</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * are the same if the read, write, property types, property editor and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * flags  are equivalent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (obj != null && obj instanceof PropertyDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            PropertyDescriptor other = (PropertyDescriptor)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            Method otherReadMethod = other.getReadMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            Method otherWriteMethod = other.getWriteMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            if (!compareMethods(getReadMethod(), otherReadMethod)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            if (!compareMethods(getWriteMethod(), otherWriteMethod)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            if (getPropertyType() == other.getPropertyType() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                getPropertyEditorClass() == other.getPropertyEditorClass() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                bound == other.isBound() && constrained == other.isConstrained() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                writeMethodName == other.writeMethodName &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                readMethodName == other.readMethodName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Package private helper method for Descriptor .equals methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @param a first method to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @param b second method to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @return boolean to indicate that the methods are equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    boolean compareMethods(Method a, Method b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        // Note: perhaps this should be a protected method in FeatureDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        if ((a == null) != (b == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (a != null && b != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (!a.equals(b)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * Package-private constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * Merge two property descriptors.  Where they conflict, give the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * second argument (y) priority over the first argument (x).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @param x  The first (lower priority) PropertyDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @param y  The second (higher priority) PropertyDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    PropertyDescriptor(PropertyDescriptor x, PropertyDescriptor y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        super(x,y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (y.baseName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            baseName = y.baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            baseName = x.baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (y.readMethodName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            readMethodName = y.readMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            readMethodName = x.readMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if (y.writeMethodName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            writeMethodName = y.writeMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            writeMethodName = x.writeMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if (y.propertyTypeRef != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            propertyTypeRef = y.propertyTypeRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            propertyTypeRef = x.propertyTypeRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        // Figure out the merged read method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        Method xr = x.getReadMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        Method yr = y.getReadMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        // Normally give priority to y's readMethod.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        try {
11668
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   586
            if (isAssignable(xr, yr)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                setReadMethod(yr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                setReadMethod(xr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        } catch (IntrospectionException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            // fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        // However, if both x and y reference read methods in the same class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        // give priority to a boolean "is" method over a boolean "get" method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        if (xr != null && yr != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                   xr.getDeclaringClass() == yr.getDeclaringClass() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                   getReturnType(getClass0(), xr) == boolean.class &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                   getReturnType(getClass0(), yr) == boolean.class &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                   xr.getName().indexOf(Introspector.IS_PREFIX) == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                   yr.getName().indexOf(Introspector.GET_PREFIX) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                setReadMethod(xr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            } catch (IntrospectionException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                // fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        Method xw = x.getWriteMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        Method yw = y.getWriteMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        try {
13651
cf84d3383a29 7186794: Setter not found. PropertyDescriptor(PropertyDescriptor,PropertyDescriptor)
malenkov
parents: 13650
diff changeset
   614
            if (yw != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                setWriteMethod(yw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                setWriteMethod(xw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        } catch (IntrospectionException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            // Fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (y.getPropertyEditorClass() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            setPropertyEditorClass(y.getPropertyEditorClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            setPropertyEditorClass(x.getPropertyEditorClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        bound = x.bound | y.bound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        constrained = x.constrained | y.constrained;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * Package-private dup constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * This must isolate the new object from any changes to the old object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    PropertyDescriptor(PropertyDescriptor old) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        super(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        propertyTypeRef = old.propertyTypeRef;
20110
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents: 19213
diff changeset
   641
        this.readMethodRef.set(old.readMethodRef.get());
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents: 19213
diff changeset
   642
        this.writeMethodRef.set(old.writeMethodRef.get());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        propertyEditorClassRef = old.propertyEditorClassRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        writeMethodName = old.writeMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        readMethodName = old.readMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        baseName = old.baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        bound = old.bound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        constrained = old.constrained;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
13773
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13651
diff changeset
   653
    void updateGenericsFor(Class<?> type) {
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13651
diff changeset
   654
        setClass0(type);
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13651
diff changeset
   655
        try {
20110
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents: 19213
diff changeset
   656
            setPropertyType(findPropertyType(this.readMethodRef.get(), this.writeMethodRef.get()));
13773
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13651
diff changeset
   657
        }
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13651
diff changeset
   658
        catch (IntrospectionException exception) {
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13651
diff changeset
   659
            setPropertyType(null);
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13651
diff changeset
   660
        }
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13651
diff changeset
   661
    }
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13651
diff changeset
   662
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * Returns the property type that corresponds to the read and write method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * The type precedence is given to the readMethod.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @return the type of the property descriptor or null if both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *         read and write methods are null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @throws IntrospectionException if the read or write method is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     */
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   671
    private Class<?> findPropertyType(Method readMethod, Method writeMethod)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        throws IntrospectionException {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   673
        Class<?> propertyType = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            if (readMethod != null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   676
                Class<?>[] params = getParameterTypes(getClass0(), readMethod);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                if (params.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    throw new IntrospectionException("bad read method arg count: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                                                     + readMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                propertyType = getReturnType(getClass0(), readMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                if (propertyType == Void.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    throw new IntrospectionException("read method " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                                        readMethod.getName() + " returns void");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            if (writeMethod != null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 6657
diff changeset
   688
                Class<?>[] params = getParameterTypes(getClass0(), writeMethod);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                if (params.length != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                    throw new IntrospectionException("bad write method arg count: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                                                     + writeMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                }
13547
508a6b13f804 7189112: java.beans.Introspector misses write methods
malenkov
parents: 13356
diff changeset
   693
                if (propertyType != null && !params[0].isAssignableFrom(propertyType)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                    throw new IntrospectionException("type mismatch between read and write methods");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                propertyType = params[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        } catch (IntrospectionException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        return propertyType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * Returns a hash code value for the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * See {@link java.lang.Object#hashCode} for a complete description.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        int result = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        result = 37 * result + ((getPropertyType() == null) ? 0 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                                getPropertyType().hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        result = 37 * result + ((getReadMethod() == null) ? 0 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                                getReadMethod().hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        result = 37 * result + ((getWriteMethod() == null) ? 0 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                                getWriteMethod().hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        result = 37 * result + ((getPropertyEditorClass() == null) ? 0 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                                getPropertyEditorClass().hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        result = 37 * result + ((writeMethodName == null) ? 0 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                                writeMethodName.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        result = 37 * result + ((readMethodName == null) ? 0 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                                readMethodName.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        result = 37 * result + getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        result = 37 * result + ((bound == false) ? 0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        result = 37 * result + ((constrained == false) ? 0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    // Calculate once since capitalize() is expensive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    String getBaseName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        if (baseName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            baseName = NameGenerator.capitalize(getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        return baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
4960
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 4392
diff changeset
   742
    void appendTo(StringBuilder sb) {
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 4392
diff changeset
   743
        appendTo(sb, "bound", this.bound);
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 4392
diff changeset
   744
        appendTo(sb, "constrained", this.constrained);
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 4392
diff changeset
   745
        appendTo(sb, "propertyEditorClass", this.propertyEditorClassRef);
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 4392
diff changeset
   746
        appendTo(sb, "propertyType", this.propertyTypeRef);
20110
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents: 19213
diff changeset
   747
        appendTo(sb, "readMethod", this.readMethodRef.get());
85b98c3054f6 7172865: PropertyDescriptor fails to work with setter method name if setter is non-void
malenkov
parents: 19213
diff changeset
   748
        appendTo(sb, "writeMethod", this.writeMethodRef.get());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
11668
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   750
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   751
    private boolean isAssignable(Method m1, Method m2) {
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   752
        if (m1 == null) {
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   753
            return true; // choose second method
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   754
        }
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   755
        if (m2 == null) {
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   756
            return false; // choose first method
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   757
        }
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   758
        if (!m1.getName().equals(m2.getName())) {
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   759
            return true; // choose second method by default
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   760
        }
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   761
        Class<?> type1 = m1.getDeclaringClass();
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   762
        Class<?> type2 = m2.getDeclaringClass();
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   763
        if (!type1.isAssignableFrom(type2)) {
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   764
            return false; // choose first method: it declared later
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   765
        }
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   766
        type1 = getReturnType(getClass0(), m1);
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   767
        type2 = getReturnType(getClass0(), m2);
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   768
        if (!type1.isAssignableFrom(type2)) {
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   769
            return false; // choose first method: it overrides return type
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   770
        }
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   771
        Class<?>[] args1 = getParameterTypes(getClass0(), m1);
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   772
        Class<?>[] args2 = getParameterTypes(getClass0(), m2);
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   773
        if (args1.length != args2.length) {
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   774
            return true; // choose second method by default
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   775
        }
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   776
        for (int i = 0; i < args1.length; i++) {
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   777
            if (!args1[i].isAssignableFrom(args2[i])) {
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   778
                return false; // choose first method: it overrides parameter
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   779
            }
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   780
        }
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   781
        return true; // choose second method
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11269
diff changeset
   782
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
}