01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: * $Header:$
18: */
19: package org.apache.beehive.netui.util.internal.cache;
20:
21: import java.lang.reflect.Field;
22: import java.lang.reflect.Modifier;
23: import java.util.HashMap;
24:
25: import org.apache.beehive.netui.util.internal.concurrent.InternalConcurrentHashMap;
26:
27: import org.apache.beehive.netui.util.logging.Logger;
28:
29: /**
30: * @exclude
31: */
32: public class FieldCache {
33: private static final Logger _log = Logger
34: .getInstance(FieldCache.class);
35:
36: private final InternalConcurrentHashMap _fieldCache;
37: private final InternalConcurrentHashMap _declaredFieldCache;
38:
39: public FieldCache() {
40: _fieldCache = new InternalConcurrentHashMap();
41: _declaredFieldCache = new InternalConcurrentHashMap();
42: }
43:
44: public final Field getField(Class type, String fieldName) {
45: if (_log.isDebugEnabled())
46: _log.debug("getFields for: " + type);
47:
48: HashMap fields = (HashMap) _fieldCache.get(type);
49:
50: if (fields == null) {
51: Field[] fieldArray = type.getFields();
52: fields = new HashMap();
53:
54: for (int i = 0; i < fieldArray.length; i++) {
55: Field field = fieldArray[i];
56: fields.put(field.getName(), field);
57: }
58:
59: _fieldCache.put(type, fields);
60: }
61:
62: return (Field) fields.get(fieldName);
63: }
64:
65: public final Field getDeclaredField(Class type, String fieldName) {
66: if (_log.isDebugEnabled())
67: _log.debug("getDeclaredFields for: " + type);
68:
69: HashMap fields = (HashMap) _declaredFieldCache.get(type);
70:
71: if (fields == null) {
72: Field[] fieldArray = type.getDeclaredFields();
73: fields = new HashMap();
74:
75: for (int i = 0; i < fieldArray.length; i++) {
76: Field field = fieldArray[i];
77: if (!Modifier.isPublic(field.getModifiers()))
78: field.setAccessible(true);
79: fields.put(field.getName(), field);
80: }
81:
82: _declaredFieldCache.put(type, fields);
83: }
84:
85: return (Field) fields.get(fieldName);
86: }
87: }
|