1/*
2 * Copyright (c) 2004, 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25package sun.jvm.hotspot.utilities.soql;
26
27import java.util.*;
28import sun.jvm.hotspot.oops.*;
29import sun.jvm.hotspot.runtime.*;
30
31public class JSJavaField extends DefaultScriptObject {
32   private static final int FIELD_NAME               = 0;
33   private static final int FIELD_SIGNATURE          = 1;
34   private static final int FIELD_HOLDER             = 2;
35   private static final int FIELD_IS_PRIVATE         = 3;
36   private static final int FIELD_IS_PUBLIC          = 4;
37   private static final int FIELD_IS_PROTECTED       = 5;
38   private static final int FIELD_IS_PACKAGE_PRIVATE = 6;
39   private static final int FIELD_IS_STATIC          = 7;
40   private static final int FIELD_IS_FINAL           = 8;
41   private static final int FIELD_IS_VOLATILE        = 9;
42   private static final int FIELD_IS_TRANSIENT       = 10;
43   private static final int FIELD_IS_SYNTHETIC       = 11;
44   private static final int FIELD_UNDEFINED          = -1;
45
46   public JSJavaField(Field f, JSJavaFactory fac) {
47      this.field = f;
48      this.factory = fac;
49   }
50
51   public Object get(String name) {
52      int fieldID = getFieldID(name);
53      switch (fieldID) {
54      case FIELD_NAME:
55         return field.getID().getName();
56      case FIELD_SIGNATURE:
57         return field.getSignature().asString();
58      case FIELD_HOLDER:
59         return getFieldHolder();
60      case FIELD_IS_PRIVATE:
61         return Boolean.valueOf(field.isPrivate());
62      case FIELD_IS_PUBLIC:
63         return Boolean.valueOf(field.isPublic());
64      case FIELD_IS_PROTECTED:
65         return Boolean.valueOf(field.isProtected());
66      case FIELD_IS_PACKAGE_PRIVATE:
67         return Boolean.valueOf(field.isPackagePrivate());
68      case FIELD_IS_STATIC:
69         return Boolean.valueOf(field.isStatic());
70      case FIELD_IS_FINAL:
71         return Boolean.valueOf(field.isFinal());
72      case FIELD_IS_VOLATILE:
73         return Boolean.valueOf(field.isVolatile());
74      case FIELD_IS_TRANSIENT:
75         return Boolean.valueOf(field.isTransient());
76      case FIELD_IS_SYNTHETIC:
77         return Boolean.valueOf(field.isSynthetic());
78      case FIELD_UNDEFINED:
79      default:
80          return super.get(name);
81      }
82   }
83
84   public Object[] getIds() {
85      Object[] fieldNames = fields.keySet().toArray();
86      Object[] superFields = super.getIds();
87      Object[] res = new Object[fieldNames.length + superFields.length];
88      System.arraycopy(fieldNames, 0, res, 0, fieldNames.length);
89      System.arraycopy(superFields, 0, res, fieldNames.length, superFields.length);
90      return res;
91   }
92
93   public boolean has(String name) {
94      if (getFieldID(name) != FIELD_UNDEFINED) {
95          return true;
96      } else {
97          return super.has(name);
98      }
99   }
100
101   public void put(String name, Object value) {
102       if (getFieldID(name) == FIELD_UNDEFINED) {
103           super.put(name, value);
104       }
105   }
106
107   public boolean equals(Object o) {
108      if (o == null || !(o instanceof JSJavaField)) {
109         return false;
110      }
111
112      JSJavaField other = (JSJavaField) o;
113      return field.equals(other.field);
114   }
115
116   public int hashCode() {
117      return field.hashCode();
118   }
119
120   public String toString() {
121      StringBuffer buf = new StringBuffer();
122      buf.append("Field ");
123      buf.append(field.getFieldHolder().getName().asString().replace('/', '.'));
124      buf.append('.');
125      buf.append(field.getID().getName());
126      return buf.toString();
127   }
128
129   //-- Internals only below this point
130   private JSJavaObject getFieldHolder() {
131      return factory.newJSJavaKlass(field.getFieldHolder()).getJSJavaClass();
132   }
133
134   private static Map fields = new HashMap();
135   private static void addField(String name, int fieldId) {
136      fields.put(name, new Integer(fieldId));
137   }
138
139   private static int getFieldID(String name) {
140      Integer res = (Integer) fields.get(name);
141      return (res != null)? res.intValue() : FIELD_UNDEFINED;
142   }
143
144   static {
145      addField("name", FIELD_NAME);
146      addField("signature", FIELD_SIGNATURE);
147      addField("holder", FIELD_HOLDER);
148      addField("isPrivate", FIELD_IS_PRIVATE);
149      addField("isPublic", FIELD_IS_PUBLIC);
150      addField("isProtected", FIELD_IS_PROTECTED);
151      addField("isPackagePrivate", FIELD_IS_PACKAGE_PRIVATE);
152      addField("isStatic", FIELD_IS_STATIC);
153      addField("isFinal", FIELD_IS_FINAL);
154      addField("isVolatile", FIELD_IS_VOLATILE);
155      addField("isTransient", FIELD_IS_TRANSIENT);
156      addField("isSynthetic", FIELD_IS_SYNTHETIC);
157   }
158
159   private final Field field;
160   private final JSJavaFactory factory;
161}
162