View Javadoc

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