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.beans.PropertyDescriptor;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.Objects;
23 import java.util.Set;
24
25 /**
26 * <p>
27 * An implementation of the {@code IntrospectionContext} interface used by {@link PropertyUtilsBean} when doing introspection of a bean class.
28 * </p>
29 * <p>
30 * This class implements the methods required by the {@code IntrospectionContext} interface in a straight-forward manner based on a map. It is used internally
31 * only. It is not thread-safe.
32 * </p>
33 *
34 * @since 1.9
35 */
36 final class DefaultIntrospectionContext implements IntrospectionContext {
37 /** The current class for introspection. */
38 private final Class<?> currentClass;
39
40 /** A map for storing the already added property descriptors. */
41 private final Map<String, PropertyDescriptor> descriptors;
42
43 /**
44 *
45 * Creates a new instance of {@code DefaultIntrospectionContext} and sets the current class for introspection.
46 *
47 * @param cls the current class
48 */
49 public DefaultIntrospectionContext(final Class<?> cls) {
50 currentClass = cls;
51 descriptors = new HashMap<>();
52 }
53
54 @Override
55 public void addPropertyDescriptor(final PropertyDescriptor desc) {
56 Objects.requireNonNull(desc, "desc");
57 descriptors.put(desc.getName(), desc);
58 }
59
60 @Override
61 public void addPropertyDescriptors(final PropertyDescriptor[] descs) {
62 Objects.requireNonNull(descs, "descs");
63 for (final PropertyDescriptor desc : descs) {
64 addPropertyDescriptor(desc);
65 }
66 }
67
68 @Override
69 public PropertyDescriptor getPropertyDescriptor(final String name) {
70 return descriptors.get(name);
71 }
72
73 /**
74 * Returns an array with all descriptors added to this context. This method is used to obtain the results of introspection.
75 *
76 * @return an array with all known property descriptors
77 */
78 public PropertyDescriptor[] getPropertyDescriptors() {
79 return descriptors.values().toArray(PropertyDescriptors.EMPTY_ARRAY);
80 }
81
82 @Override
83 public Class<?> getTargetClass() {
84 return currentClass;
85 }
86
87 @Override
88 public boolean hasProperty(final String name) {
89 return descriptors.containsKey(name);
90 }
91
92 @Override
93 public Set<String> propertyNames() {
94 return descriptors.keySet();
95 }
96
97 @Override
98 public void removePropertyDescriptor(final String name) {
99 descriptors.remove(name);
100 }
101 }