summaryrefslogtreecommitdiffstats
path: root/awt/java/beans/PropertyDescriptor.java
blob: 93891525aa219d16e36ac2d1ff8ebc141fb6faf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package java.beans;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Vector;
import org.apache.harmony.beans.internal.nls.Messages;

public class PropertyDescriptor extends FeatureDescriptor {
    private Method getter;

    private Method setter;

    private Class<?> propertyEditorClass;

    private boolean constrained;

    private boolean bound;

    public PropertyDescriptor(String propertyName, Class<?> beanClass, String getterName,
            String setterName) throws IntrospectionException {
        super();
        if (beanClass == null) {
            throw new IntrospectionException(Messages.getString("beans.03")); //$NON-NLS-1$
        }
        if (propertyName == null || propertyName.length() == 0) {
            throw new IntrospectionException(Messages.getString("beans.04")); //$NON-NLS-1$
        }
        this.setName(propertyName);
        this.setDisplayName(propertyName);
        if (setterName != null) {
            if (hasMethod(beanClass, setterName)) {
                setWriteMethod(beanClass, setterName);
            } else {
                throw new IntrospectionException(Messages.getString("beans.20")); //$NON-NLS-1$
            }
        }
        if (getterName != null) {
            if (hasMethod(beanClass, getterName)) {
                setReadMethod(beanClass, getterName);
            } else {
                throw new IntrospectionException(Messages.getString("beans.1F")); //$NON-NLS-1$
            }
        }
    }

    public PropertyDescriptor(String propertyName, Method getter, Method setter)
            throws IntrospectionException {
        super();
        if (propertyName == null || propertyName.length() == 0) {
            throw new IntrospectionException(Messages.getString("beans.04")); //$NON-NLS-1$
        }
        this.setName(propertyName);
        this.setDisplayName(propertyName);
        setWriteMethod(setter);
        setReadMethod(getter);
    }

    public PropertyDescriptor(String propertyName, Class<?> beanClass)
            throws IntrospectionException {
        String getterName;
        String setterName;
        if (beanClass == null) {
            throw new IntrospectionException(Messages.getString("beans.03")); //$NON-NLS-1$
        }
        if (propertyName == null || propertyName.length() == 0) {
            throw new IntrospectionException(Messages.getString("beans.04")); //$NON-NLS-1$
        }
        this.setName(propertyName);
        this.setDisplayName(propertyName);
        getterName = createDefaultMethodName(propertyName, "is"); //$NON-NLS-1$
        if (hasMethod(beanClass, getterName)) {
            setReadMethod(beanClass, getterName);
        } else {
            getterName = createDefaultMethodName(propertyName, "get"); //$NON-NLS-1$
            if (hasMethod(beanClass, getterName)) {
                setReadMethod(beanClass, getterName);
            }
        }
        setterName = createDefaultMethodName(propertyName, "set"); //$NON-NLS-1$
        if (hasMethod(beanClass, setterName)) {
            setWriteMethod(beanClass, setterName);
        }
        if (getter == null && setter == null) {
            throw new IntrospectionException(Messages.getString("beans.01", propertyName)); //$NON-NLS-1$
        }
    }

    public void setWriteMethod(Method setter) throws IntrospectionException {
        if (setter != null) {
            int modifiers = setter.getModifiers();
            if (!Modifier.isPublic(modifiers)) {
                throw new IntrospectionException(Messages.getString("beans.05")); //$NON-NLS-1$
            }
            Class<?>[] parameterTypes = setter.getParameterTypes();
            if (parameterTypes.length != 1) {
                throw new IntrospectionException(Messages.getString("beans.06")); //$NON-NLS-1$
            }
            Class<?> parameterType = parameterTypes[0];
            Class<?> propertyType = getPropertyType();
            if (propertyType != null && !propertyType.equals(parameterType)) {
                throw new IntrospectionException(Messages.getString("beans.07")); //$NON-NLS-1$
            }
        }
        this.setter = setter;
    }

    public void setReadMethod(Method getter) throws IntrospectionException {
        if (getter != null) {
            int modifiers = getter.getModifiers();
            if (!Modifier.isPublic(modifiers)) {
                throw new IntrospectionException(Messages.getString("beans.0A")); //$NON-NLS-1$
            }
            Class<?>[] parameterTypes = getter.getParameterTypes();
            if (parameterTypes.length != 0) {
                throw new IntrospectionException(Messages.getString("beans.08")); //$NON-NLS-1$
            }
            Class<?> returnType = getter.getReturnType();
            if (returnType.equals(Void.TYPE)) {
                throw new IntrospectionException(Messages.getString("beans.33")); //$NON-NLS-1$
            }
            Class<?> propertyType = getPropertyType();
            if ((propertyType != null) && !returnType.equals(propertyType)) {
                throw new IntrospectionException(Messages.getString("beans.09")); //$NON-NLS-1$
            }
        }
        this.getter = getter;
    }

    public Method getWriteMethod() {
        return setter;
    }

    public Method getReadMethod() {
        return getter;
    }

    @Override
    public boolean equals(Object object) {
        boolean result = (object != null && object instanceof PropertyDescriptor);
        if (result) {
            PropertyDescriptor pd = (PropertyDescriptor) object;
            boolean gettersAreEqual = (this.getter == null) && (pd.getReadMethod() == null)
                    || (this.getter != null) && (this.getter.equals(pd.getReadMethod()));
            boolean settersAreEqual = (this.setter == null) && (pd.getWriteMethod() == null)
                    || (this.setter != null) && (this.setter.equals(pd.getWriteMethod()));
            boolean propertyTypesAreEqual = this.getPropertyType() == pd.getPropertyType();
            boolean propertyEditorClassesAreEqual = this.getPropertyEditorClass() == pd
                    .getPropertyEditorClass();
            boolean boundPropertyAreEqual = this.isBound() == pd.isBound();
            boolean constrainedPropertyAreEqual = this.isConstrained() == pd.isConstrained();
            result = gettersAreEqual && settersAreEqual && propertyTypesAreEqual
                    && propertyEditorClassesAreEqual && boundPropertyAreEqual
                    && constrainedPropertyAreEqual;
        }
        return result;
    }

    public void setPropertyEditorClass(Class<?> propertyEditorClass) {
        this.propertyEditorClass = propertyEditorClass;
    }

    public Class<?> getPropertyType() {
        Class<?> result = null;
        if (getter != null) {
            result = getter.getReturnType();
        } else if (setter != null) {
            Class<?>[] parameterTypes = setter.getParameterTypes();
            result = parameterTypes[0];
        }
        return result;
    }

    public Class<?> getPropertyEditorClass() {
        return propertyEditorClass;
    }

    public void setConstrained(boolean constrained) {
        this.constrained = constrained;
    }

    public void setBound(boolean bound) {
        this.bound = bound;
    }

    public boolean isConstrained() {
        return constrained;
    }

    public boolean isBound() {
        return bound;
    }

    boolean hasMethod(Class<?> beanClass, String methodName) {
        Method[] methods = findMethods(beanClass, methodName);
        return (methods.length > 0);
    }

    String createDefaultMethodName(String propertyName, String prefix) {
        String result = null;
        if (propertyName != null) {
            String bos = propertyName.substring(0, 1).toUpperCase();
            String eos = propertyName.substring(1, propertyName.length());
            result = prefix + bos + eos;
        }
        return result;
    }

    Method[] findMethods(Class<?> aClass, String methodName) {
        Method[] allMethods = aClass.getMethods();
        Vector<Method> matchedMethods = new Vector<Method>();
        Method[] result;
        for (Method method : allMethods) {
            if (method.getName().equals(methodName)) {
                matchedMethods.add(method);
            }
        }
        result = new Method[matchedMethods.size()];
        for (int j = 0; j < matchedMethods.size(); ++j) {
            result[j] = matchedMethods.elementAt(j);
        }
        return result;
    }

    void setReadMethod(Class<?> beanClass, String getterName) {
        boolean result = false;
        Method[] getters = findMethods(beanClass, getterName);
        for (Method element : getters) {
            try {
                setReadMethod(element);
                result = true;
            } catch (IntrospectionException ie) {
            }
            if (result) {
                break;
            }
        }
    }

    void setWriteMethod(Class<?> beanClass, String setterName) throws IntrospectionException {
        boolean result = false;
        Method[] setters = findMethods(beanClass, setterName);
        for (Method element : setters) {
            try {
                setWriteMethod(element);
                result = true;
            } catch (IntrospectionException ie) {
            }
            if (result) {
                break;
            }
        }
    }

    public PropertyEditor createPropertyEditor(Object bean) {
        PropertyEditor editor;
        if (propertyEditorClass == null) {
            return null;
        }
        if (!PropertyEditor.class.isAssignableFrom(propertyEditorClass)) {
            // beans.48=Property editor is not assignable from the
            // PropertyEditor interface
            throw new ClassCastException(Messages.getString("beans.48")); //$NON-NLS-1$
        }
        try {
            Constructor<?> constr;
            try {
                // try to look for the constructor with single Object argument
                constr = propertyEditorClass.getConstructor(Object.class);
                editor = (PropertyEditor) constr.newInstance(bean);
            } catch (NoSuchMethodException e) {
                // try no-argument constructor
                constr = propertyEditorClass.getConstructor();
                editor = (PropertyEditor) constr.newInstance();
            }
        } catch (Exception e) {
            // beans.47=Unable to instantiate property editor
            RuntimeException re = new RuntimeException(Messages.getString("beans.47"), e); //$NON-NLS-1$
            throw re;
        }
        return editor;
    }
}