1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.beanutils2;
18
19 import java.util.Arrays;
20 import java.util.Objects;
21
22 /**
23 * <p>
24 * DynaClass which implements the {@code MutableDynaClass} interface.
25 * </p>
26 *
27 * <p>
28 * A {@code MutableDynaClass</code> is a specialized extension to <code>DynaClass}
29 * that allows properties to be added or removed dynamically.</p>
30 *
31 * <p>This implementation has one slightly unusual default behavior - calling
32 * the {@code getDynaProperty(name)} method for a property which doesn't
33 * exist returns a {@code DynaProperty</code> rather than <code>null}. The reason for this is that {@code BeanUtils} calls this method to check if a property
34 * exists before trying to set the value. This would defeat the object of the {@code LazyDynaBean} which automatically adds missing properties when any of its
35 * {@code set()} methods are called. For this reason the {@code isDynaProperty(name)} method has been added to this implementation in order to determine if a
36 * property actually exists. If the more <em>normal</em> behavior of returning {@code null} is required, then this can be achieved by calling the
37 * {@code setReturnNull(true)}.
38 * </p>
39 *
40 * <p>
41 * The {@code add(name, type, readable, writable)} method is not implemented and always throws an {@code UnsupportedOperationException}. I believe this
42 * attributes need to be added to the {@code DynaProperty} class in order to control read/write facilities.
43 * </p>
44 *
45 * @see LazyDynaBean
46 */
47 public class LazyDynaClass extends BasicDynaClass implements MutableDynaClass {
48
49 private static final long serialVersionUID = 1L;
50
51 /**
52 * Controls whether changes to this DynaClass's properties are allowed.
53 */
54 protected boolean restricted;
55
56 /**
57 * <p>
58 * Controls whether the {@code getDynaProperty()} method returns null if a property doesn't exist - or creates a new one.
59 * </p>
60 *
61 * <p>
62 * Default is {@code false}.
63 */
64 protected boolean returnNull;
65
66 /**
67 * Constructs a new LazyDynaClass with default parameters.
68 */
69 public LazyDynaClass() {
70 this(null, (DynaProperty[]) null);
71 }
72
73 /**
74 * Constructs a new LazyDynaClass with the specified name.
75 *
76 * @param name Name of this DynaBean class
77 */
78 public LazyDynaClass(final String name) {
79 this(name, (DynaProperty[]) null);
80 }
81
82 /**
83 * Constructs a new LazyDynaClass with the specified name and DynaBean class.
84 *
85 * @param name Name of this DynaBean class
86 * @param dynaBeanClass The implementation class for new instances
87 */
88 public LazyDynaClass(final String name, final Class<?> dynaBeanClass) {
89 this(name, dynaBeanClass, null);
90 }
91
92 /**
93 * Constructs a new LazyDynaClass with the specified name, DynaBean class and properties.
94 *
95 * @param name Name of this DynaBean class
96 * @param dynaBeanClass The implementation class for new instances
97 * @param properties Property descriptors for the supported properties
98 */
99 public LazyDynaClass(final String name, final Class<?> dynaBeanClass, final DynaProperty[] properties) {
100 super(name, dynaBeanClass, properties);
101 }
102
103 /**
104 * Constructs a new LazyDynaClass with the specified name and properties.
105 *
106 * @param name Name of this DynaBean class
107 * @param properties Property descriptors for the supported properties
108 */
109 public LazyDynaClass(final String name, final DynaProperty[] properties) {
110 this(name, LazyDynaBean.class, properties);
111 }
112
113 /**
114 * Add a new dynamic property.
115 *
116 * @param property Property the new dynamic property to add.
117 * @throws IllegalArgumentException if name is null
118 * @throws IllegalStateException if this DynaClass is currently restricted, so no new properties can be added
119 */
120 protected void add(final DynaProperty property) {
121 Objects.requireNonNull(property, "property");
122 Objects.requireNonNull(property.getName(), "property.getName()");
123 if (isRestricted()) {
124 throw new IllegalStateException("DynaClass is currently restricted. No new properties can be added.");
125 }
126 // Check if property already exists
127 if (propertiesMap.get(property.getName()) != null) {
128 return;
129 }
130 // Create a new property array with the specified property
131 final DynaProperty[] oldProperties = getDynaProperties();
132 final DynaProperty[] newProperties = Arrays.copyOf(oldProperties, oldProperties.length + 1);
133 newProperties[oldProperties.length] = property;
134 // Update the properties
135 setProperties(newProperties);
136 }
137
138 /**
139 * Add a new dynamic property with no restrictions on data type, readability, or writeability.
140 *
141 * @param name Name of the new dynamic property
142 * @throws IllegalArgumentException if name is null
143 * @throws IllegalStateException if this DynaClass is currently restricted, so no new properties can be added
144 */
145 @Override
146 public void add(final String name) {
147 add(new DynaProperty(name));
148 }
149
150 /**
151 * Add a new dynamic property with the specified data type, but with no restrictions on readability or writeability.
152 *
153 * @param name Name of the new dynamic property
154 * @param type Data type of the new dynamic property (null for no restrictions)
155 * @throws IllegalArgumentException if name is null
156 * @throws IllegalStateException if this DynaClass is currently restricted, so no new properties can be added
157 */
158 @Override
159 public void add(final String name, final Class<?> type) {
160 if (type == null) {
161 add(name);
162 } else {
163 add(new DynaProperty(name, type));
164 }
165 }
166
167 /**
168 * <p>
169 * Add a new dynamic property with the specified data type, readability, and writeability.
170 * </p>
171 *
172 * <p>
173 * <strong>N.B.</strong>Support for readable/writable properties has not been implemented and this method always throws a
174 * {@code UnsupportedOperationException}.
175 * </p>
176 *
177 * <p>
178 * I'm not sure the intention of the original authors for this method, but it seems to me that readable/writable should be attributes of the
179 * {@code DynaProperty} class (which they are not) and is the reason this method has not been implemented.
180 * </p>
181 *
182 * @param name Name of the new dynamic property
183 * @param type Data type of the new dynamic property (null for no restrictions)
184 * @param readable Set to {@code true} if this property value should be readable
185 * @param writable Set to {@code true} if this property value should be writable
186 * @throws UnsupportedOperationException anytime this method is called
187 */
188 @Override
189 public void add(final String name, final Class<?> type, final boolean readable, final boolean writable) {
190 throw new java.lang.UnsupportedOperationException("readable/writable properties not supported");
191 }
192
193 /**
194 * <p>
195 * Return a property descriptor for the specified property.
196 * </p>
197 *
198 * <p>
199 * If the property is not found and the {@code returnNull} indicator is {@code true</code>, this method always returns <code>null}.
200 * </p>
201 *
202 * <p>
203 * If the property is not found and the {@code returnNull} indicator is {@code false} a new property descriptor is created and returned (although its not
204 * actually added to the DynaClass's properties). This is the default behavior.
205 * </p>
206 *
207 * <p>
208 * The reason for not returning a {@code null} property descriptor is that {@code BeanUtils} uses this method to check if a property exists before trying to
209 * set it - since these <em>Lazy</em> implementations automatically add any new properties when they are set, returning {@code null} from this method would
210 * defeat their purpose.
211 * </p>
212 *
213 * @param name Name of the dynamic property for which a descriptor is requested
214 * @return The dyna property for the specified name
215 * @throws IllegalArgumentException if no property name is specified
216 */
217 @Override
218 public DynaProperty getDynaProperty(final String name) {
219 Objects.requireNonNull(name, "name");
220 DynaProperty dynaProperty = propertiesMap.get(name);
221 // If it doesn't exist and returnNull is false
222 // create a new DynaProperty
223 if (dynaProperty == null && !isReturnNull() && !isRestricted()) {
224 dynaProperty = new DynaProperty(name);
225 }
226 return dynaProperty;
227 }
228
229 /**
230 * <p>
231 * Indicate whether a property actually exists.
232 * </p>
233 *
234 * <p>
235 * <strong>N.B.</strong> Using {@code getDynaProperty(name) == null} doesn't work in this implementation because that method might return a DynaProperty if
236 * it doesn't exist (depending on the {@code returnNull} indicator).
237 * </p>
238 *
239 * @param name The name of the property to check
240 * @return {@code true} if there is a property of the specified name, otherwise {@code false}
241 * @throws IllegalArgumentException if no property name is specified
242 */
243 public boolean isDynaProperty(final String name) {
244 return propertiesMap.get(Objects.requireNonNull(name, "name")) != null;
245 }
246
247 /**
248 * <p>
249 * Is this DynaClass currently restricted.
250 * </p>
251 * <p>
252 * If restricted, no changes to the existing registration of property names, data types, readability, or writeability are allowed.
253 * </p>
254 *
255 * @return {@code true} if this {@link MutableDynaClass} cannot be changed otherwise {@code false}
256 */
257 @Override
258 public boolean isRestricted() {
259 return restricted;
260 }
261
262 /**
263 * Should this DynaClass return a {@code null} from the {@code getDynaProperty(name)} method if the property doesn't exist.
264 *
265 * @return {@code true</code> if a <code>null} {@link DynaProperty} should be returned if the property doesn't exist, otherwise {@code false} if a new
266 * {@link DynaProperty} should be created.
267 */
268 public boolean isReturnNull() {
269 return returnNull;
270 }
271
272 /**
273 * Remove the specified dynamic property, and any associated data type, readability, and writeability, from this dynamic class. <strong>NOTE</strong> - This
274 * does <strong>NOT</strong> cause any corresponding property values to be removed from DynaBean instances associated with this DynaClass.
275 *
276 * @param name Name of the dynamic property to remove
277 * @throws IllegalArgumentException if name is null
278 * @throws IllegalStateException if this DynaClass is currently restricted, so no properties can be removed
279 */
280 @Override
281 public void remove(final String name) {
282 Objects.requireNonNull(name, "name");
283 if (isRestricted()) {
284 throw new IllegalStateException("DynaClass is currently restricted. No properties can be removed.");
285 }
286
287 // Ignore if property doesn't exist
288 if (propertiesMap.get(name) == null) {
289 return;
290 }
291
292 // Create a new property array of without the specified property
293 final DynaProperty[] oldProperties = getDynaProperties();
294 final DynaProperty[] newProperties = new DynaProperty[oldProperties.length - 1];
295 int j = 0;
296 for (final DynaProperty oldProperty : oldProperties) {
297 if (!name.equals(oldProperty.getName())) {
298 newProperties[j] = oldProperty;
299 j++;
300 }
301 }
302
303 // Update the properties
304 setProperties(newProperties);
305 }
306
307 /**
308 * <p>
309 * Set whether this DynaClass is currently restricted.
310 * </p>
311 * <p>
312 * If restricted, no changes to the existing registration of property names, data types, readability, or writeability are allowed.
313 * </p>
314 *
315 * @param restricted {@code true} if this {@link MutableDynaClass} cannot be changed otherwise {@code false}
316 */
317 @Override
318 public void setRestricted(final boolean restricted) {
319 this.restricted = restricted;
320 }
321
322 /**
323 * Sets whether this DynaClass should return a {@code null} from the {@code getDynaProperty(name)} method if the property doesn't exist.
324 *
325 * @param returnNull {@code true</code> if a <code>null} {@link DynaProperty} should be returned if the property doesn't exist, otherwise {@code false} if a
326 * new {@link DynaProperty} should be created.
327 */
328 public void setReturnNull(final boolean returnNull) {
329 this.returnNull = returnNull;
330 }
331
332 }