All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.metawidget.inspector.impl.actionstyle.BaseActionStyle Maven / Gradle / Ivy

There is a newer version: 4.2
Show newest version
// Metawidget
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package org.metawidget.inspector.impl.actionstyle;

import java.util.Collections;
import java.util.Map;

import org.metawidget.util.CollectionUtils;

/**
 * Convenience implementation for ActionStyles.
 * 

* Handles caching, and unwrapping proxies. * * @author Richard Kennard */ public abstract class BaseActionStyle implements ActionStyle { // // Private members // /** * Cache of Action lookups. *

* Action lookups are potentially expensive, so we cache them. The cache itself is a member * variable, not a static, because we rely on BaseObjectInspector to only create * one instance of ActionStyle for all Inspectors. *

* This also stops problems with subclasses of BaseActionStyle sharing the same * static cache. *

* Note: the cache is unbounded, because the number of Classes in the system is fixed. This even * applies to hot deployment products such as FakeReplace, because new Classes are replaced such * that they .equal() their originals. */ private final Map, Map> mActionCache = CollectionUtils.newWeakHashMap(); // // Public methods // public Map getActions( Class clazz ) { synchronized ( mActionCache ) { Map actions = getCachedActions( clazz ); if ( actions == null ) { // If the class is not a proxy... if ( !isProxy( clazz ) ) { // ...inspect it normally actions = inspectActions( clazz ); } else { // ...otherwise, if the superclass is not just java.lang.Object... Class superclass = clazz.getSuperclass(); if ( !superclass.equals( Object.class ) ) { // ...inspect the superclass actions = getCachedActions( superclass ); if ( actions == null ) { actions = inspectActions( superclass ); cacheActions( superclass, actions ); } } else { // ...otherwise, inspect each interface and merge actions = inspectActions( clazz.getInterfaces() ); } } // Cache the result cacheActions( clazz, actions ); } return actions; } } /** * SPI for tools such as FakeReplace that * need to clear the action cache. *

* This does not affect the immutability of the PropertyStyle, as its external behaviour is * unchanged (it will just be a little slower the next time it is called, while it re-caches). */ public void clearCache() { synchronized ( mActionCache ) { mActionCache.clear(); } } // // Protected methods // /** * Returns true if the given class is a 'proxy' of its original self. *

* Proxied classes generally don't carry annotations, so it is important to traverse away from * the proxied class back to the original class before inspection. *

* By default, returns true for classes with _$$_javassist_ or * ByCGLIB$$ in their name. */ protected boolean isProxy( Class clazz ) { // (don't use .getSimpleName or .contains, for J2SE 1.4 compatibility) String name = clazz.getName(); if ( name.indexOf( "_$$_javassist_" ) != -1 ) { return true; } if ( name.indexOf( "ByCGLIB$$" ) != -1 ) { return true; } return false; } /** * Inspect the given Classes and merge their results. *

* This version of inspectActions is used when inspecting the interfaces of a * proxied class. */ protected Map inspectActions( Class[] classes ) { Map actionsToReturn = CollectionUtils.newTreeMap(); for ( Class clazz : classes ) { Map actions = getCachedActions( clazz ); if ( actions == null ) { actions = inspectActions( clazz ); cacheActions( clazz, actions ); } actionsToReturn.putAll( actions ); } return actionsToReturn; } /** * @return the properties of the given class. Never null. */ protected abstract Map inspectActions( Class clazz ); protected Map getCachedActions( Class clazz ) { return mActionCache.get( clazz ); } protected void cacheActions( Class clazz, Map actions ) { mActionCache.put( clazz, Collections.unmodifiableMap( actions ) ); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy