Path: blob/master/src/java.desktop/share/classes/com/sun/beans/finder/BeanInfoFinder.java
41161 views
/*1* Copyright (c) 2009, 2012, 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;2526import java.beans.BeanDescriptor;27import java.beans.BeanInfo;28import java.beans.MethodDescriptor;29import java.beans.PropertyDescriptor;30import java.lang.reflect.Method;3132/**33* This is utility class that provides functionality34* to find a {@link BeanInfo} for a JavaBean specified by its type.35*36* @since 1.737*38* @author Sergey A. Malenkov39*/40public final class BeanInfoFinder41extends InstanceFinder<BeanInfo> {4243private static final String DEFAULT = "sun.beans.infos";44private static final String DEFAULT_NEW = "com.sun.beans.infos";4546public BeanInfoFinder() {47super(BeanInfo.class, true, "BeanInfo", DEFAULT);48}4950private static boolean isValid(Class<?> type, Method method) {51return (method != null) && method.getDeclaringClass().isAssignableFrom(type);52}5354@Override55protected BeanInfo instantiate(Class<?> type, String prefix, String name) {56if (DEFAULT.equals(prefix)) {57prefix = DEFAULT_NEW;58}59// this optimization will only use the BeanInfo search path60// if is has changed from the original61// or trying to get the ComponentBeanInfo62BeanInfo info = !DEFAULT_NEW.equals(prefix) || "ComponentBeanInfo".equals(name)63? super.instantiate(type, prefix, name)64: null;6566if (info != null) {67// make sure that the returned BeanInfo matches the class68BeanDescriptor bd = info.getBeanDescriptor();69if (bd != null) {70if (type.equals(bd.getBeanClass())) {71return info;72}73}74else {75PropertyDescriptor[] pds = info.getPropertyDescriptors();76if (pds != null) {77for (PropertyDescriptor pd : pds) {78Method method = pd.getReadMethod();79if (method == null) {80method = pd.getWriteMethod();81}82if (isValid(type, method)) {83return info;84}85}86}87else {88MethodDescriptor[] mds = info.getMethodDescriptors();89if (mds != null) {90for (MethodDescriptor md : mds) {91if (isValid(type, md.getMethod())) {92return info;93}94}95}96}97}98}99return null;100}101}102103104