01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.lib.contract.lang.cache;
28:
29: import java.util.*;
30: import java.lang.reflect.*;
31:
32: import org.cougaar.lib.contract.lang.*;
33:
34: /**
35: * Field lookup cache.
36: * <p>
37: * Currently no cache -- add MRU cache here.
38: **/
39: public final class FieldCache {
40:
41: /**
42: * <tt>IS_STATIC_EXPLICIT</tt> is used to prevent <tt>lookup</tt> from
43: * returning STATIC <code>Field</code>s when (<tt>isStatic</tt> == false).
44: * <p>
45: * @see MethodCache#IS_STATIC_EXPLICIT
46: */
47: public static final boolean IS_STATIC_EXPLICIT = true;
48:
49: /**
50: * Find <code>Field</code> "name" in <code>Class<code> "c".
51: * @param c Class instance
52: * @param name Field name
53: * @param isStatic Field static/nonstatic -- see IS_STATIC_EXPLICIT
54: **/
55: public static final Field lookup(final Class c, final String name,
56: final boolean isStatic) {
57: Field[] allFields = c.getFields();
58: for (int i = allFields.length - 1; i >= 0; i--) {
59: Field f = allFields[i];
60: if (name.equals(f.getName())) {
61: int fmods = f.getModifiers();
62: // must be PUBLIC, and must meet STATIC/NON_STATIC requirements
63: if (((fmods & Modifier.PUBLIC) != 0)
64: && (IS_STATIC_EXPLICIT ? (isStatic == ((fmods & Modifier.STATIC) != 0))
65: : (isStatic ? ((fmods & Modifier.STATIC) != 0)
66: : true))) {
67: // found field
68: return f;
69: }
70: }
71: }
72: // no match found
73: return null;
74: }
75:
76: }
|