1 18 package org.apache.beehive.netui.util.internal.cache; 19 20 import java.lang.reflect.Field ; 21 import java.util.HashMap ; 22 23 import org.apache.beehive.netui.util.internal.concurrent.InternalConcurrentHashMap; 24 25 import org.apache.beehive.netui.util.logging.Logger; 26 27 30 public class FieldCache 31 { 32 private static final Logger _log = Logger.getInstance( FieldCache.class ); 33 34 private final InternalConcurrentHashMap _fieldCache; 35 private final InternalConcurrentHashMap _declaredFieldCache; 36 37 public FieldCache() 38 { 39 _fieldCache = new InternalConcurrentHashMap(); 40 _declaredFieldCache = new InternalConcurrentHashMap(); 41 } 42 43 public final Field getField( Class type, String fieldName ) 44 { 45 if ( _log.isDebugEnabled() ) _log.debug( "getFields for: " + type ); 46 47 HashMap fields = ( HashMap ) _fieldCache.get( type ); 48 49 if ( fields == null ) 50 { 51 Field [] fieldArray = type.getFields(); 52 fields = new HashMap (); 53 54 for ( int i = 0; i < fieldArray.length; i++ ) 55 { 56 Field field = fieldArray[i]; 57 fields.put( field.getName(), field ); 58 } 59 60 _fieldCache.put( type, fields ); 61 } 62 63 return ( Field ) fields.get( fieldName ); 64 } 65 66 public final Field getDeclaredField( Class type, String fieldName ) 67 { 68 if ( _log.isDebugEnabled() ) _log.debug( "getDeclaredFields for: " + type ); 69 70 HashMap fields = ( HashMap ) _declaredFieldCache.get( type ); 71 72 if ( fields == null ) 73 { 74 Field [] fieldArray = type.getDeclaredFields(); 75 fields = new HashMap (); 76 77 for ( int i = 0; i < fieldArray.length; i++ ) 78 { 79 Field field = fieldArray[i]; 80 field.setAccessible( true ); 81 fields.put( field.getName(), field ); 82 } 83 84 _declaredFieldCache.put( type, fields ); 85 } 86 87 return ( Field ) fields.get( fieldName ); 88 } 89 } 90 | Popular Tags |