View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.jdo.impl.enhancer.classfile;
19  
20  import java.io.*;
21  
22  /***
23   * ClassField models the static and non-static fields of a class within
24   * a class file.
25   */
26  
27  final public class ClassField extends ClassMember {
28    /* access flag bit mask - see VMConstants */
29    private int accessFlags;
30  
31    /* The name of the field */
32    private ConstUtf8 fieldName;
33  
34    /* The type signature of the field */
35    private ConstUtf8 fieldSignature;
36  
37    /* The attributes associated with the field */
38    private AttributeVector fieldAttributes;
39    
40  
41    /* public accessors */
42  
43    /***
44     * Is the field transient?
45     */
46    public boolean isTransient() {
47      return (accessFlags & ACCTransient) != 0;
48    }
49  
50    /***
51     * Return the access flags for the field - see VMConstants
52     */
53    public int access() {
54      return accessFlags;
55    }
56  
57    /***
58     * Update the access flags for the field - see VMConstants
59     */
60    public void setAccess(int newFlags) {
61      accessFlags = newFlags;
62    }
63  
64    /***
65     * Return the name of the field
66     */
67    public ConstUtf8 name() {
68      return fieldName;
69    }
70  
71    /***
72     * Change the name of the field
73     */
74    public void changeName(ConstUtf8 name) {
75      fieldName = name;
76    }
77  
78    /***
79     * Return the type signature of the field
80     */
81    public ConstUtf8 signature() {
82      return fieldSignature;
83    }
84  
85    /***
86     * Change the type signature of the field
87     */
88    public void changeSignature(ConstUtf8 newSig) {
89      fieldSignature = newSig;
90    }
91  
92    /***
93     * Return the attributes associated with the field
94     */
95    public AttributeVector attributes() {
96      return fieldAttributes;
97    }
98  
99    /***
100    * Construct a class field object
101    */
102   public ClassField(int accFlags, ConstUtf8 name, ConstUtf8 sig,
103                     AttributeVector field_attrs) {
104     accessFlags = accFlags;
105     fieldName = name;
106     fieldSignature = sig;
107     fieldAttributes = field_attrs;
108   }
109 
110   </* package local methods *//package-summary/html">class="comment"> package local methods *//package-summary.html">em class="comment">/* package local methods *//package-summary.html">class="comment"> package local methods */
111 
112   static ClassField read(DataInputStream data, ConstantPool pool) 
113     throws IOException {
114     ClassField f = null;
115     int accessFlags = data.readUnsignedShort();
116     int name_index = data.readUnsignedShort();
117     int sig_index = data.readUnsignedShort();
118     AttributeVector fieldAttribs = AttributeVector.readAttributes(data, pool);
119     f = new ClassField(accessFlags, 
120 		       (ConstUtf8) pool.constantAt(name_index),
121 		       (ConstUtf8) pool.constantAt(sig_index),
122 		       fieldAttribs);
123     return f;
124   }
125 
126   void write (DataOutputStream data) throws IOException {
127     data.writeShort(accessFlags);
128     data.writeShort(fieldName.getIndex());
129     data.writeShort(fieldSignature.getIndex());
130     fieldAttributes.write(data);
131   }
132 
133   void print(PrintStream out, int indent) {
134     ClassPrint.spaces(out, indent);
135     out.print("'" + fieldName.asString() + "'");
136     out.print(" sig = " + fieldSignature.asString());
137     out.print(" access_flags = " + Integer.toString(accessFlags));
138     out.println(" attributes:");
139     fieldAttributes.print(out, indent+2);
140   }
141 }
142