1/* 2 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25package com.sun.beans.finder; 26 27import com.sun.beans.WeakCache; 28 29import java.beans.PropertyEditor; 30 31import com.sun.beans.editors.BooleanEditor; 32import com.sun.beans.editors.ByteEditor; 33import com.sun.beans.editors.DoubleEditor; 34import com.sun.beans.editors.EnumEditor; 35import com.sun.beans.editors.FloatEditor; 36import com.sun.beans.editors.IntegerEditor; 37import com.sun.beans.editors.LongEditor; 38import com.sun.beans.editors.ShortEditor; 39 40/** 41 * This is utility class that provides functionality 42 * to find a {@link PropertyEditor} for a JavaBean specified by its type. 43 * 44 * @since 1.7 45 * 46 * @author Sergey A. Malenkov 47 */ 48public final class PropertyEditorFinder 49 extends InstanceFinder<PropertyEditor> { 50 51 private static final String DEFAULT = "sun.beans.editors"; 52 private static final String DEFAULT_NEW = "com.sun.beans.editors"; 53 54 private final WeakCache<Class<?>, Class<?>> registry; 55 56 public PropertyEditorFinder() { 57 super(PropertyEditor.class, false, "Editor", DEFAULT); 58 59 this.registry = new WeakCache<Class<?>, Class<?>>(); 60 this.registry.put(Byte.TYPE, ByteEditor.class); 61 this.registry.put(Short.TYPE, ShortEditor.class); 62 this.registry.put(Integer.TYPE, IntegerEditor.class); 63 this.registry.put(Long.TYPE, LongEditor.class); 64 this.registry.put(Boolean.TYPE, BooleanEditor.class); 65 this.registry.put(Float.TYPE, FloatEditor.class); 66 this.registry.put(Double.TYPE, DoubleEditor.class); 67 } 68 69 public void register(Class<?> type, Class<?> editor) { 70 synchronized (this.registry) { 71 this.registry.put(type, editor); 72 } 73 } 74 75 @Override 76 public PropertyEditor find(Class<?> type) { 77 Class<?> predefined; 78 synchronized (this.registry) { 79 predefined = this.registry.get(type); 80 } 81 PropertyEditor editor = instantiate(predefined, null); 82 if (editor == null) { 83 editor = super.find(type); 84 if ((editor == null) && (null != type.getEnumConstants())) { 85 editor = new EnumEditor(type); 86 } 87 } 88 return editor; 89 } 90 91 @Override 92 protected PropertyEditor instantiate(Class<?> type, String prefix, String name) { 93 return super.instantiate(type, DEFAULT.equals(prefix) ? DEFAULT_NEW : prefix, name); 94 } 95} 96