Path: blob/master/src/java.desktop/share/classes/com/sun/beans/finder/InstanceFinder.java
41161 views
/*1* Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.beans.finder;2526/**27* This is utility class that provides basic functionality28* to find an auxiliary class for a JavaBean specified by its type.29*30* @since 1.731*32* @author Sergey A. Malenkov33*/34class InstanceFinder<T> {3536private static final String[] EMPTY = { };3738private final Class<? extends T> type;39private final boolean allow;40private final String suffix;41private volatile String[] packages;4243InstanceFinder(Class<? extends T> type, boolean allow, String suffix, String... packages) {44this.type = type;45this.allow = allow;46this.suffix = suffix;47this.packages = packages.clone();48}4950public String[] getPackages() {51return this.packages.clone();52}5354public void setPackages(String... packages) {55this.packages = (packages != null) && (packages.length > 0)56? packages.clone()57: EMPTY;58}5960public T find(Class<?> type) {61if (type == null) {62return null;63}64String name = type.getName() + this.suffix;65T object = instantiate(type, name);66if (object != null) {67return object;68}69if (this.allow) {70object = instantiate(type, null);71if (object != null) {72return object;73}74}75int index = name.lastIndexOf('.') + 1;76if (index > 0) {77name = name.substring(index);78}79for (String prefix : this.packages) {80object = instantiate(type, prefix, name);81if (object != null) {82return object;83}84}85return null;86}8788@SuppressWarnings("deprecation")89protected T instantiate(Class<?> type, String name) {90if (type != null) {91try {92if (name != null) {93type = ClassFinder.findClass(name, type.getClassLoader());94}95if (this.type.isAssignableFrom(type)) {96@SuppressWarnings("unchecked")97T tmp = (T) type.newInstance();98return tmp;99}100}101catch (Exception exception) {102// ignore any exceptions103}104}105return null;106}107108protected T instantiate(Class<?> type, String prefix, String name) {109return instantiate(type, prefix + '.' + name);110}111}112113114