View Javadoc

1   // jq_Field.java, created Mon Feb  5 23:23:20 2001 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.Class;
5   
6   import java.util.Map;
7   import java.io.DataInput;
8   import java.io.IOException;
9   import joeq.ClassLib.ClassLibInterface;
10  import joeq.Main.jq;
11  
12  /*
13   * @author  John Whaley <jwhaley@alum.mit.edu>
14   * @version $Id: jq_Field.java 2465 2006-06-07 23:03:17Z joewhaley $
15   */
16  public abstract class jq_Field extends jq_Member {
17  
18      protected jq_Type type;
19      
20      // clazz, name, desc, access_flags are inherited
21      protected jq_Field(jq_Class clazz, jq_NameAndDesc nd) {
22          super(clazz, nd);
23          type = PrimordialClassLoader.getOrCreateType(clazz.getClassLoader(), nd.getDesc());
24      }
25      
26      public void load(char access_flags, DataInput in) 
27      throws IOException, ClassFormatError {
28          super.load(access_flags, in);
29          if (jq.RunningNative)
30              ClassLibInterface.DEFAULT.initNewField((java.lang.reflect.Field)this.getJavaLangReflectMemberObject(), this);
31      }
32  
33      public void load(char access_flags, Map attributes) {
34          super.load(access_flags, attributes);
35          if (jq.RunningNative)
36              ClassLibInterface.DEFAULT.initNewField((java.lang.reflect.Field)this.getJavaLangReflectMemberObject(), this);
37      }
38  
39      public final jq_Type getType() { return type; }
40      public boolean isVolatile() { chkState(STATE_LOADING2); return (access_flags & ACC_VOLATILE) != 0; }
41      public boolean isTransient() { chkState(STATE_LOADING2); return (access_flags & ACC_TRANSIENT) != 0; }
42      
43      public abstract int getWidth();
44  
45      public void accept(jq_FieldVisitor mv) {
46          mv.visitField(this);
47      }
48  
49      static interface Delegate {
50          boolean isCodeAddressType(jq_Field t);
51          boolean isHeapAddressType(jq_Field t);
52          boolean isStackAddressType(jq_Field t);
53      }
54      
55      private static Delegate _delegate;
56  
57      public final boolean isCodeAddressType() {
58          return _delegate.isCodeAddressType(this);
59      }
60      public final boolean isHeapAddressType() {
61          return _delegate.isHeapAddressType(this);
62      }
63      public final boolean isStackAddressType() {
64          return _delegate.isStackAddressType(this);
65      }
66      
67      public String toString() { return getDeclaringClass()+"."+getName(); }
68  
69      static {
70          /* Set up delegates. */
71          _delegate = null;
72          boolean nullVM = jq.nullVM;
73          if (!nullVM) {
74              _delegate = attemptDelegate("joeq.Class.Delegates$Field");
75          }
76          if (_delegate == null) {
77              _delegate = new NullDelegates.Field();
78          }
79      }
80  
81      private static Delegate attemptDelegate(String s) {
82          //String type = "field delegate";
83          try {
84              Class c = Class.forName(s);
85              return (Delegate)c.newInstance();
86          } catch (java.lang.ClassNotFoundException x) {
87              //System.err.println("Cannot find "+type+" "+s+": "+x);
88          } catch (java.lang.InstantiationException x) {
89              //System.err.println("Cannot instantiate "+type+" "+s+": "+x);
90          } catch (java.lang.IllegalAccessException x) {
91              //System.err.println("Cannot access "+type+" "+s+": "+x);
92          }
93          return null;
94      }
95  
96  }