/* * Copyright 1996-2009 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */ package java.beans; import com.sun.beans.finder.ClassFinder; import java.lang.ref.Reference; import java.lang.ref.SoftReference; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Collections; import java.util.Map; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.EventListener; import java.util.List; import java.util.WeakHashMap; import java.util.TreeMap; import sun.awt.AppContext; import sun.reflect.misc.ReflectUtil; /** * The Introspector class provides a standard way for tools to learn about * the properties, events, and methods supported by a target Java Bean. *
* For each of those three kinds of information, the Introspector will * separately analyze the bean's class and superclasses looking for * either explicit or implicit information and use that information to * build a BeanInfo object that comprehensively describes the target bean. *
* For each class "Foo", explicit information may be available if there exists * a corresponding "FooBeanInfo" class that provides a non-null value when * queried for the information. We first look for the BeanInfo class by * taking the full package-qualified name of the target bean class and * appending "BeanInfo" to form a new class name. If this fails, then * we take the final classname component of this name, and look for that * class in each of the packages specified in the BeanInfo package search * path. *
* Thus for a class such as "sun.xyz.OurButton" we would first look for a * BeanInfo class called "sun.xyz.OurButtonBeanInfo" and if that failed we'd * look in each package in the BeanInfo search path for an OurButtonBeanInfo * class. With the default search path, this would mean looking for * "sun.beans.infos.OurButtonBeanInfo". *
* If a class provides explicit BeanInfo about itself then we add that to * the BeanInfo information we obtained from analyzing any derived classes, * but we regard the explicit information as being definitive for the current * class and its base classes, and do not proceed any further up the superclass * chain. *
* If we don't find explicit BeanInfo on a class, we use low-level * reflection to study the methods of the class and apply standard design * patterns to identify property accessors, event sources, or public * methods. We then proceed to analyze the class's superclass and add * in the information from it (and possibly on up the superclass chain). * *
* Because the Introspector caches BeanInfo classes for better performance,
* take care if you use it in an application that uses
* multiple class loaders.
* In general, when you destroy a ClassLoader
* that has been used to introspect classes,
* you should use the
* {@link #flushCaches Introspector.flushCaches}
* or
* {@link #flushFromCaches Introspector.flushFromCaches} method
* to flush all of the introspected classes out of the cache.
*
*
* For more information about introspection and design patterns, please * consult the * JavaBeans specification. */ public class Introspector { // Flags that can be used to control getBeanInfo: public final static int USE_ALL_BEANINFO = 1; public final static int IGNORE_IMMEDIATE_BEANINFO = 2; public final static int IGNORE_ALL_BEANINFO = 3; // Static Caches to speed up introspection. private static Map declaredMethodCache = Collections.synchronizedMap(new WeakHashMap()); private static final Object BEANINFO_CACHE = new Object(); private Class beanClass; private BeanInfo explicitBeanInfo; private BeanInfo superBeanInfo; private BeanInfo additionalBeanInfo[]; private boolean propertyChangeSource = false; private static Class eventListenerType = EventListener.class; // These should be removed. private String defaultEventName; private String defaultPropertyName; private int defaultEventIndex = -1; private int defaultPropertyIndex = -1; // Methods maps from Method objects to MethodDescriptors private Map methods; // properties maps from String names to PropertyDescriptors private Map properties; // events maps from String names to EventSetDescriptors private Map events; private final static String DEFAULT_INFO_PATH = "sun.beans.infos"; private static String[] searchPath = { DEFAULT_INFO_PATH }; private final static EventSetDescriptor[] EMPTY_EVENTSETDESCRIPTORS = new EventSetDescriptor[0]; static final String ADD_PREFIX = "add"; static final String REMOVE_PREFIX = "remove"; static final String GET_PREFIX = "get"; static final String SET_PREFIX = "set"; static final String IS_PREFIX = "is"; private static final String BEANINFO_SUFFIX = "BeanInfo"; //====================================================================== // Public methods //====================================================================== /** * Introspect on a Java Bean and learn about all its properties, exposed * methods, and events. *
* If the BeanInfo class for a Java Bean has been previously Introspected
* then the BeanInfo class is retrieved from the BeanInfo cache.
*
* @param beanClass The bean class to be analyzed.
* @return A BeanInfo object describing the target bean.
* @exception IntrospectionException if an exception occurs during
* introspection.
* @see #flushCaches
* @see #flushFromCaches
*/
public static BeanInfo getBeanInfo(Class> beanClass)
throws IntrospectionException
{
if (!ReflectUtil.isPackageAccessible(beanClass)) {
return (new Introspector(beanClass, null, USE_ALL_BEANINFO)).getBeanInfo();
}
Map
* If the BeanInfo class for a Java Bean has been previously Introspected
* based on the same arguments then the BeanInfo class is retrieved
* from the BeanInfo cache.
*
* @param beanClass The bean class to be analyzed.
* @param flags Flags to control the introspection.
* If flags == USE_ALL_BEANINFO then we use all of the BeanInfo
* classes we can discover.
* If flags == IGNORE_IMMEDIATE_BEANINFO then we ignore any
* BeanInfo associated with the specified beanClass.
* If flags == IGNORE_ALL_BEANINFO then we ignore all BeanInfo
* associated with the specified beanClass or any of its
* parent classes.
* @return A BeanInfo object describing the target bean.
* @exception IntrospectionException if an exception occurs during
* introspection.
*/
public static BeanInfo getBeanInfo(Class> beanClass, int flags)
throws IntrospectionException {
return getBeanInfo(beanClass, null, flags);
}
/**
* Introspect on a Java bean and learn all about its properties, exposed
* methods, below a given "stop" point.
*
* If the BeanInfo class for a Java Bean has been previously Introspected
* based on the same arguments, then the BeanInfo class is retrieved
* from the BeanInfo cache.
*
* @param beanClass The bean class to be analyzed.
* @param stopClass The baseclass at which to stop the analysis. Any
* methods/properties/events in the stopClass or in its baseclasses
* will be ignored in the analysis.
* @exception IntrospectionException if an exception occurs during
* introspection.
*/
public static BeanInfo getBeanInfo(Class> beanClass, Class> stopClass)
throws IntrospectionException {
return getBeanInfo(beanClass, stopClass, USE_ALL_BEANINFO);
}
/**
* Introspect on a Java Bean and learn about all its properties,
* exposed methods and events, below a given {@code stopClass} point
* subject to some control {@code flags}.
*
* If the BeanInfo class for a Java Bean has been
* previously introspected based on the same arguments then
* the BeanInfo class is retrieved from the BeanInfo cache.
*
* @param beanClass the bean class to be analyzed
* @param stopClass the parent class at which to stop the analysis
* @param flags flags to control the introspection
* @return a BeanInfo object describing the target bean
* @exception IntrospectionException if an exception occurs during introspection
*
* @since 1.7
*/
public static BeanInfo getBeanInfo(Class> beanClass, Class> stopClass,
int flags) throws IntrospectionException {
BeanInfo bi;
if (stopClass == null && flags == USE_ALL_BEANINFO) {
// Same parameters to take advantage of caching.
bi = getBeanInfo(beanClass);
} else {
bi = (new Introspector(beanClass, stopClass, flags)).getBeanInfo();
}
return bi;
// Old behaviour: Make an independent copy of the BeanInfo.
//return new GenericBeanInfo(bi);
}
/**
* Utility method to take a string and convert it to normal Java variable
* name capitalization. This normally means converting the first
* character from upper case to lower case, but in the (unusual) special
* case when there is more than one character and both the first and
* second characters are upper case, we leave it alone.
*
* Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays
* as "URL".
*
* @param name The string to be decapitalized.
* @return The decapitalized version of the string.
*/
public static String decapitalize(String name) {
if (name == null || name.length() == 0) {
return name;
}
if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
Character.isUpperCase(name.charAt(0))){
return name;
}
char chars[] = name.toCharArray();
chars[0] = Character.toLowerCase(chars[0]);
return new String(chars);
}
/**
* Gets the list of package names that will be used for
* finding BeanInfo classes.
*
* @return The array of package names that will be searched in
* order to find BeanInfo classes. The default value
* for this array is implementation-dependent; e.g.
* Sun implementation initially sets to {"sun.beans.infos"}.
*/
public static synchronized String[] getBeanInfoSearchPath() {
// Return a copy of the searchPath.
String result[] = new String[searchPath.length];
for (int i = 0; i < searchPath.length; i++) {
result[i] = searchPath[i];
}
return result;
}
/**
* Change the list of package names that will be used for
* finding BeanInfo classes. The behaviour of
* this method is undefined if parameter path
* is null.
*
* First, if there is a security manager, its
* Used in the contructors of the EventSetDescriptor,
* PropertyDescriptor and the IndexedPropertyDescriptor.
*
* @param cls The Class object on which to retrieve the method.
* @param methodName Name of the method.
* @param argCount Number of arguments for the desired method.
* @param args Array of argument types for the method.
* @return the method or null if not found
*/
static Method findMethod(Class cls, String methodName, int argCount,
Class args[]) {
if (methodName == null) {
return null;
}
return internalFindMethod(cls, methodName, argCount, args);
}
/**
* Return true if class a is either equivalent to class b, or
* if class a is a subclass of class b, i.e. if a either "extends"
* or "implements" b.
* Note tht either or both "Class" objects may represent interfaces.
*/
static boolean isSubclass(Class a, Class b) {
// We rely on the fact that for any given java class or
// primtitive type there is a unqiue Class object, so
// we can use object equivalence in the comparisons.
if (a == b) {
return true;
}
if (a == null || b == null) {
return false;
}
for (Class x = a; x != null; x = x.getSuperclass()) {
if (x == b) {
return true;
}
if (b.isInterface()) {
Class interfaces[] = x.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
if (isSubclass(interfaces[i], b)) {
return true;
}
}
}
}
return false;
}
/**
* Return true iff the given method throws the given exception.
*/
private boolean throwsException(Method method, Class exception) {
Class exs[] = method.getExceptionTypes();
for (int i = 0; i < exs.length; i++) {
if (exs[i] == exception) {
return true;
}
}
return false;
}
/**
* Try to create an instance of a named class.
* First try the classloader of "sibling", then try the system
* classloader then the class loader of the current Thread.
*/
static Object instantiate(Class sibling, String className)
throws InstantiationException, IllegalAccessException,
ClassNotFoundException {
// First check with sibling's classloader (if any).
ClassLoader cl = sibling.getClassLoader();
Class cls = ClassFinder.findClass(className, cl);
return cls.newInstance();
}
} // end class Introspector
//===========================================================================
/**
* Package private implementation support class for Introspector's
* internal use.
*
* Mostly this is used as a placeholder for the descriptors.
*/
class GenericBeanInfo extends SimpleBeanInfo {
private BeanDescriptor beanDescriptor;
private EventSetDescriptor[] events;
private int defaultEvent;
private PropertyDescriptor[] properties;
private int defaultProperty;
private MethodDescriptor[] methods;
private BeanInfo targetBeanInfo;
public GenericBeanInfo(BeanDescriptor beanDescriptor,
EventSetDescriptor[] events, int defaultEvent,
PropertyDescriptor[] properties, int defaultProperty,
MethodDescriptor[] methods, BeanInfo targetBeanInfo) {
this.beanDescriptor = beanDescriptor;
this.events = events;
this.defaultEvent = defaultEvent;
this.properties = properties;
this.defaultProperty = defaultProperty;
this.methods = methods;
this.targetBeanInfo = targetBeanInfo;
}
/**
* Package-private dup constructor
* This must isolate the new object from any changes to the old object.
*/
GenericBeanInfo(GenericBeanInfo old) {
beanDescriptor = new BeanDescriptor(old.beanDescriptor);
if (old.events != null) {
int len = old.events.length;
events = new EventSetDescriptor[len];
for (int i = 0; i < len; i++) {
events[i] = new EventSetDescriptor(old.events[i]);
}
}
defaultEvent = old.defaultEvent;
if (old.properties != null) {
int len = old.properties.length;
properties = new PropertyDescriptor[len];
for (int i = 0; i < len; i++) {
PropertyDescriptor oldp = old.properties[i];
if (oldp instanceof IndexedPropertyDescriptor) {
properties[i] = new IndexedPropertyDescriptor(
(IndexedPropertyDescriptor) oldp);
} else {
properties[i] = new PropertyDescriptor(oldp);
}
}
}
defaultProperty = old.defaultProperty;
if (old.methods != null) {
int len = old.methods.length;
methods = new MethodDescriptor[len];
for (int i = 0; i < len; i++) {
methods[i] = new MethodDescriptor(old.methods[i]);
}
}
targetBeanInfo = old.targetBeanInfo;
}
public PropertyDescriptor[] getPropertyDescriptors() {
return properties;
}
public int getDefaultPropertyIndex() {
return defaultProperty;
}
public EventSetDescriptor[] getEventSetDescriptors() {
return events;
}
public int getDefaultEventIndex() {
return defaultEvent;
}
public MethodDescriptor[] getMethodDescriptors() {
return methods;
}
public BeanDescriptor getBeanDescriptor() {
return beanDescriptor;
}
public java.awt.Image getIcon(int iconKind) {
if (targetBeanInfo != null) {
return targetBeanInfo.getIcon(iconKind);
}
return super.getIcon(iconKind);
}
}
*
* Any methods/properties/events in the {@code stopClass}
* or in its parent classes will be ignored in the analysis.
* checkPropertiesAccess
* method is called. This could result in a SecurityException.
*
* @param path Array of package names.
* @exception SecurityException if a security manager exists and its
* checkPropertiesAccess method doesn't allow setting
* of system properties.
* @see SecurityManager#checkPropertiesAccess
*/
public static synchronized void setBeanInfoSearchPath(String path[]) {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPropertiesAccess();
}
searchPath = path;
}
/**
* Flush all of the Introspector's internal caches. This method is
* not normally required. It is normally only needed by advanced
* tools that update existing "Class" objects in-place and need
* to make the Introspector re-analyze existing Class objects.
*/
public static void flushCaches() {
Map map = (Map) AppContext.getAppContext().get(BEANINFO_CACHE);
if (map != null) {
map.clear();
}
declaredMethodCache.clear();
}
/**
* Flush the Introspector's internal cached information for a given class.
* This method is not normally required. It is normally only needed
* by advanced tools that update existing "Class" objects in-place
* and need to make the Introspector re-analyze an existing Class object.
*
* Note that only the direct state associated with the target Class
* object is flushed. We do not flush state for other Class objects
* with the same name, nor do we flush state for any related Class
* objects (such as subclasses), even though their state may include
* information indirectly obtained from the target Class object.
*
* @param clz Class object to be flushed.
* @throws NullPointerException If the Class object is null.
*/
public static void flushFromCaches(Class> clz) {
if (clz == null) {
throw new NullPointerException();
}
Map map = (Map) AppContext.getAppContext().get(BEANINFO_CACHE);
if (map != null) {
map.remove(clz);
}
declaredMethodCache.remove(clz);
}
//======================================================================
// Private implementation methods
//======================================================================
private Introspector(Class beanClass, Class stopClass, int flags)
throws IntrospectionException {
this.beanClass = beanClass;
// Check stopClass is a superClass of startClass.
if (stopClass != null) {
boolean isSuper = false;
for (Class c = beanClass.getSuperclass(); c != null; c = c.getSuperclass()) {
if (c == stopClass) {
isSuper = true;
}
}
if (!isSuper) {
throw new IntrospectionException(stopClass.getName() + " not superclass of " +
beanClass.getName());
}
}
if (flags == USE_ALL_BEANINFO) {
explicitBeanInfo = findExplicitBeanInfo(beanClass);
}
Class superClass = beanClass.getSuperclass();
if (superClass != stopClass) {
int newFlags = flags;
if (newFlags == IGNORE_IMMEDIATE_BEANINFO) {
newFlags = USE_ALL_BEANINFO;
}
superBeanInfo = getBeanInfo(superClass, stopClass, newFlags);
}
if (explicitBeanInfo != null) {
additionalBeanInfo = explicitBeanInfo.getAdditionalBeanInfo();
}
if (additionalBeanInfo == null) {
additionalBeanInfo = new BeanInfo[0];
}
}
/**
* Constructs a GenericBeanInfo class from the state of the Introspector
*/
private BeanInfo getBeanInfo() throws IntrospectionException {
// the evaluation order here is import, as we evaluate the
// event sets and locate PropertyChangeListeners before we
// look for properties.
BeanDescriptor bd = getTargetBeanDescriptor();
MethodDescriptor mds[] = getTargetMethodInfo();
EventSetDescriptor esds[] = getTargetEventInfo();
PropertyDescriptor pds[] = getTargetPropertyInfo();
int defaultEvent = getTargetDefaultEventIndex();
int defaultProperty = getTargetDefaultPropertyIndex();
return new GenericBeanInfo(bd, esds, defaultEvent, pds,
defaultProperty, mds, explicitBeanInfo);
}
/**
* Looks for an explicit BeanInfo class that corresponds to the Class.
* First it looks in the existing package that the Class is defined in,
* then it checks to see if the class is its own BeanInfo. Finally,
* the BeanInfo search path is prepended to the class and searched.
*
* @return Instance of an explicit BeanInfo class or null if one isn't found.
*/
private static synchronized BeanInfo findExplicitBeanInfo(Class beanClass) {
String name = beanClass.getName() + BEANINFO_SUFFIX;
try {
return (java.beans.BeanInfo)instantiate(beanClass, name);
} catch (Exception ex) {
// Just drop through
}
// Now try checking if the bean is its own BeanInfo.
try {
if (isSubclass(beanClass, java.beans.BeanInfo.class)) {
return (java.beans.BeanInfo)beanClass.newInstance();
}
} catch (Exception ex) {
// Just drop through
}
// Now try looking for