001 /*
002 * Copyright 2008-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package griffon.core.editors;
017
018 import org.slf4j.Logger;
019 import org.slf4j.LoggerFactory;
020
021 import javax.annotation.Nonnull;
022 import javax.annotation.Nullable;
023 import javax.annotation.concurrent.GuardedBy;
024 import java.beans.PropertyEditor;
025 import java.beans.PropertyEditorManager;
026 import java.beans.PropertyEditorSupport;
027 import java.lang.ref.Reference;
028 import java.lang.ref.WeakReference;
029 import java.util.LinkedHashMap;
030 import java.util.Map;
031 import java.util.WeakHashMap;
032
033 import static java.util.Objects.requireNonNull;
034
035 /**
036 * The PropertyEditorResolver can be used to locate a property editor for
037 * any given type name. This property editor must support the
038 * java.beans.PropertyEditor interface for editing a given object.
039 * <p>
040 *
041 * @author Andres Almiray
042 * @since 2.0.0
043 */
044 public final class PropertyEditorResolver {
045 private static final Logger LOG = LoggerFactory.getLogger(PropertyEditorResolver.class);
046 private static final Object LOCK = new Object[0];
047 @GuardedBy("LOCK")
048 private static final WeakCache<String, Class<? extends PropertyEditor>> propertyEditorRegistry = new WeakCache<>();
049 @GuardedBy("LOCK")
050 private static final Map<String, PropertyEditorChain> propertyEditorChainRegistry = new LinkedHashMap<>();
051 private static final String ERROR_TARGET_TYPE_NULL = "Argument 'targetType' must not be null";
052
053 private PropertyEditorResolver() {
054
055 }
056
057 /**
058 * Removes all currently registered value editors.
059 *
060 * @since 2.4.0
061 */
062 public static void clear() {
063 synchronized (LOCK) {
064 propertyEditorRegistry.clear();
065 propertyEditorChainRegistry.clear();
066 }
067 }
068
069 /**
070 * Locate a value editor for a given target type.
071 * <p>
072 * If the input {@code type} is an Enum then an instance of {@code EnumPropertyEditor}
073 * is returned with the {@code type} set as {@code enumType}.
074 *
075 * @param type The Class object for the type to be edited
076 *
077 * @return An editor object for the given target class.
078 * The result is null if no suitable editor can be found.
079 *
080 * @see griffon.core.editors.EnumPropertyEditor
081 */
082 @Nonnull
083 @SuppressWarnings("unchecked")
084 public static PropertyEditor findEditor(@Nonnull Class<?> type) {
085 requireNonNull(type, "Argument 'type' must not be null");
086 LOG.trace("Searching PropertyEditor for {}", type.getName());
087
088 PropertyEditor editor;
089 if (Enum.class.isAssignableFrom(type)) {
090 editor = new EnumPropertyEditor();
091 ((EnumPropertyEditor) editor).setEnumType((Class<? extends Enum<?>>) type);
092 } else {
093 editor = doFindEditor(type);
094 }
095
096 if (editor == null) {
097 // fallback
098 editor = PropertyEditorManager.findEditor(type);
099 }
100
101 if (editor == null) {
102 editor = new NoopPropertyEditor();
103 }
104
105 LOG.trace("PropertyEditor for {} is {}", type.getName(), editor.getClass().getName());
106 return editor;
107 }
108
109 /**
110 * Unregisters an editor class to edit values of the given target class.
111 *
112 * @param targetType the class object of the type to be edited
113 *
114 * @since 2.4.0
115 */
116 public static void unregisterEditor(@Nonnull Class<?> targetType) {
117 requireNonNull(targetType, ERROR_TARGET_TYPE_NULL);
118 synchronized (LOCK) {
119 String targetTypeName = targetType.getName();
120 propertyEditorChainRegistry.remove(targetTypeName);
121 propertyEditorRegistry.remove(targetTypeName);
122 }
123 }
124
125 /**
126 * Registers an editor class to edit values of the given target class.
127 * If the editor class is {@code null},
128 * then any existing definition will be removed.
129 * Thus this method can be used to cancel the registration.
130 * The registration is canceled automatically
131 * if either the target or editor class is unloaded.
132 * <p>
133 *
134 * @param targetType the class object of the type to be edited
135 * @param editorClass the class object of the editor class
136 *
137 * @since 2.4.0
138 */
139 @SuppressWarnings("unchecked")
140 public static void registerEditor(@Nonnull Class<?> targetType, @Nullable Class<? extends PropertyEditor> editorClass) {
141 requireNonNull(targetType, ERROR_TARGET_TYPE_NULL);
142 synchronized (LOCK) {
143 String targetTypeName = targetType.getName();
144 if (editorClass == null) {
145 propertyEditorChainRegistry.remove(targetTypeName);
146 propertyEditorRegistry.remove(targetTypeName);
147 return;
148 }
149
150 // is targetType handled by a chain?
151 PropertyEditorChain chain = propertyEditorChainRegistry.get(targetTypeName);
152 if (chain != null) {
153 PropertyEditorChain propertyEditorChain = chain.copyOf(editorClass);
154 if (propertyEditorChain.getSize() > 1) {
155 propertyEditorChainRegistry.put(targetTypeName, propertyEditorChain);
156 } else {
157 // standard registration
158 propertyEditorChainRegistry.remove(targetTypeName);
159 propertyEditorRegistry.put(targetTypeName, editorClass);
160 }
161 } else {
162 // is targetType handled by an editor ?
163 Class<? extends PropertyEditor> propertyEditorType = propertyEditorRegistry.get(targetTypeName);
164 if (propertyEditorType != null) {
165 propertyEditorRegistry.remove(targetTypeName);
166 Class<? extends PropertyEditor>[] propertyEditorClasses = new Class[2];
167 propertyEditorClasses[0] = propertyEditorType;
168 propertyEditorClasses[1] = editorClass;
169 PropertyEditorChain propertyEditorChain = new PropertyEditorChain(targetType, propertyEditorClasses);
170 if (propertyEditorChain.getSize() > 1) {
171 propertyEditorChainRegistry.put(targetTypeName, propertyEditorChain);
172 } else {
173 // standard registration
174 propertyEditorChainRegistry.remove(targetTypeName);
175 propertyEditorRegistry.put(targetTypeName, editorClass);
176 }
177 } else {
178 // standard registration
179 propertyEditorChainRegistry.remove(targetTypeName);
180 propertyEditorRegistry.put(targetTypeName, editorClass);
181 }
182 }
183 }
184 }
185
186 private static PropertyEditor doFindEditor(Class<?> targetType) {
187 synchronized (LOCK) {
188 String targetTypeName = targetType.getName();
189 if (propertyEditorChainRegistry.containsKey(targetTypeName)) {
190 PropertyEditorChain chain = propertyEditorChainRegistry.get(targetTypeName);
191 return chain.copyOf();
192 } else {
193 Class<?> propertyEditorType = propertyEditorRegistry.get(targetTypeName);
194 if (propertyEditorType != null) {
195 try {
196 return (PropertyEditor) propertyEditorType.newInstance();
197 } catch (InstantiationException | IllegalAccessException e) {
198 throw new IllegalStateException("Can't instantiate " + propertyEditorType, e);
199 }
200 }
201 }
202 }
203 return null;
204 }
205
206 public static final class NoopPropertyEditor extends PropertyEditorSupport {
207
208 }
209
210 private static final class WeakCache<K, V> {
211 private final Map<K, Reference<V>> map = new WeakHashMap<>();
212
213 private V get(K key) {
214 Reference<V> reference = this.map.get(key);
215 if (reference == null) {
216 return null;
217 } else {
218 V value = reference.get();
219 if (value == null) {
220 this.map.remove(key);
221 }
222
223 return value;
224 }
225 }
226
227 private void put(K key, V value) {
228 if (value != null) {
229 this.map.put(key, new WeakReference<>(value));
230 } else {
231 this.map.remove(key);
232 }
233 }
234
235 private void remove(K key) {
236 this.map.remove(key);
237 }
238
239 private void clear() {
240 this.map.clear();
241 }
242 }
243 }
|