alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Rich Unger. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.xml.tree.nodes;

import java.beans.*;
import java.lang.reflect.*;
import java.util.*;

/**
 * This class caches writer methods for particular classes. It has
 * been introduced to reduce the dependency on the Introspector's
 * internal cache of BeanInfo objects, as well as to allow for a more
 * efficient representation of the set of property
 * descriptors. Specifically, this eliminates the need to linearly
 * traverse the set of properties.

* * @author Paul Constantinides * @version $Id: WriterCache.java,v 1.5 2003/02/27 23:46:22 ffjre Exp $ **/ public class WriterCache { //////////////////// CONSTRUCTORS ///////////////////// /** This class should not be instantiated. */ private WriterCache() {} //////////////////// CLASS METHODS ///////////////////// /** * Looks up the specified writer method in the internal store. * * @return the writer method on class c for property sName; this * should never be null * @exception IntrospectionException thrown if there given property does * not exist on the given class **/ public static MethodAndType getWriterMethodAndType( Class c, String sName ) throws IntrospectionException { PropertyTree gb; // make sure we don't build the same PropertyTree twice and be // nice to multiple threads asking for the same thing at once synchronized( s_map ) { gb = (PropertyTree) s_map.get( c ); if ( gb == null ) { // we didn't have one, so populate a new one, and push // it in gb = new PropertyTree( c ); s_map.put( c, gb ); } } // do the return outside of synchronization; it's a search and // could take time, and there's no modification of the // PropertyTree going on MethodAndType mat = gb.getMethodAndType( sName ); if ( mat == null ) { if ( sName.equals("") ) { throw new IntrospectionException( "Unable to retrieve property information " + // NOI18N "because an empty property name was given for class " + c); // NOI18N } throw new IntrospectionException( "Unable to retrieve information for property " + // NOI18N sName + " on class " + c ); // NOI18N } else if ( mat.getMethod() == null ) { throw new IntrospectionException( "Could not find writer method for property " + // NOI18N sName + " on class " + c ); // NOI18N } return mat; } //////////////////// STATIC NESTED CLASSES ///////////////////// public static class MethodAndType { protected MethodAndType( Method m, Class t ) { m_method = m; m_type = t; } protected Method getMethod() { return m_method; } protected Class getType() { return m_type; } private Method m_method; private Class m_type; } /** * For a given class, this data structure gives access to the * mapping between the property names and types and their * respective writer methods for the given class. **/ private static class PropertyTree { /** * Creates a new PropertyTree for the given class. * * @param c The class for which to create the property tree * @exception IntrospectionException thrown if there are problems * introspecting or getting the bean info for the given class. **/ protected PropertyTree( Class c ) throws IntrospectionException { m_mapWriterTree = new TreeMap(); m_sDescriptor = "property tree for: " + c.getName(); // NOI18N BeanInfo bi = null; try { // Obtain the BeanInfo for the supplied class the // Introspector. The Introspector will cache this // information per class... but we'd like to use our // own, since it's is not sorted or optimized in any // way bi = Introspector.getBeanInfo( c ); } catch ( IntrospectionException ie ) { throw new IntrospectionException( "Unable to introspect " + c + " beacuse " + ie ); // NOI18N } if ( bi != null ) { // Get the PropertyDescriptors from the bean info PropertyDescriptor[] pdArr = bi.getPropertyDescriptors(); // Search for the PropertyDescriptor for this Param // and save its setter method for( int i = 0; i < pdArr.length; i++ ) { // get the name of the property, and the type and // map the writer method to it Class classPropertyType = ( pdArr[ i ] instanceof IndexedPropertyDescriptor ) ? ((IndexedPropertyDescriptor) pdArr[ i ]).getIndexedPropertyType() : pdArr[ i ].getPropertyType(); String sName = pdArr[ i ].getName(); Method methodWriter = pdArr[ i ].getWriteMethod(); // store this as propertyname, typeclassname saveMethodAndType( sName, classPropertyType, methodWriter ); } } else { throw new IntrospectionException( "Unable to retrieve BeanInfo for class " + c ); // NOI18N } } public String toString() { return m_sDescriptor; } /** Adds the given name, type -> method mapping into the map */ private void saveMethodAndType( String sName, Class type, Method method ) { // store this as propertyname:typeclassname in the // treemap m_mapWriterTree.put( sName, new WriterCache.MethodAndType( method, type ) ); } /** * Retrieves the writer method for the given property, if it * exists, or null, otherwise. A property will match if it has the * exact name and type as given * * @return the writer method for the given property, or null if it * does not exist **/ protected WriterCache.MethodAndType getMethodAndType( String sName ) { return (WriterCache.MethodAndType) m_mapWriterTree.get( sName ); } /** Stores the per class mapping of name, type tuple to method for all properties of this particular class. */ private Map m_mapWriterTree; /** Saved strictly for logging purposes (so that we know when this class gets cleaned up). */ private String m_sDescriptor; } //////////////////// CLASS VARIABLES ///////////////////// /** Stores the mapping of class to PropertyTree objects which provide access to the specific information for that class. */ protected static Map s_map = new WeakHashMap(); }

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.