KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > bcg > FieldBuilder


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base.bcg;
26
27 import org.aspectj.compiler.base.bcg.pool.*;
28
29 import java.io.IOException JavaDoc;
30 import java.io.DataOutputStream JavaDoc;
31
32 //import org.aspectj.compiler.base.bcg.ConstantPool.Constant;
33
import java.lang.reflect.Modifier JavaDoc;
34
35 // XXX annoying big modularity violation.
36
import org.aspectj.compiler.base.ast.TypeDec;
37
38 /** Lifecycle: creation, addition entities (through the addX methods),
39     resolution of constant pool (done in the constant pool, when all
40     constant pool entries are set), output. It is an error for these
41     steps to occur out of order, but this is not currently checked. */

42 public final class FieldBuilder {
43
44     // ------------------------------
45
// state
46

47     ConstantPool pool;
48     //ClassfileBuilder cfb;
49
Attributes attributes;
50
51     FieldBuilder(ClassfileBuilder cfb) {
52         //this.cfb = cfb;
53
this.pool = cfb.pool;
54         attributes = new Attributes(pool);
55     }
56
57     int accessFlags = 0;
58     Constant name;
59     Constant descriptor;
60
61     // ------------------------------
62
// output methods
63

64     public void writeTo(DataOutputStream JavaDoc stream) throws IOException JavaDoc {
65         stream.writeShort((short) accessFlags);
66         name.writeIndex(stream);
67         descriptor.writeIndex(stream);
68         attributes.writeTo(stream);
69     }
70
71     public String JavaDoc toString() {
72         return "(field " +
73             Modifier.toString(accessFlags) + " " +
74             name + " " +
75             descriptor + " " + attributes + ")";
76     }
77
78     void display(int indent, boolean inline) {
79         indent += 2;
80         System.err.print("(field "); name.display(0, true);
81         System.err.print(" "); descriptor.display(0, true);
82         between(indent, inline);
83         System.err.print("(modifiers " + Modifier.toString(accessFlags) + ")");
84         between(indent, inline);
85         attributes.display(indent, inline);
86         System.err.print(")");
87     }
88
89     static void between(int indent, boolean inline) {
90         if (inline) {
91             System.err.print(" ");
92         } else {
93             System.err.println();
94             for (int s=indent; s >= 0; s--) System.err.print(" ");
95         }
96     }
97
98     // ------------------------------
99
// building methods
100

101     public void addAccessFlags(int newFlags) {
102         accessFlags = accessFlags | newFlags;
103     }
104     
105     public void setName(String JavaDoc name) {
106         this.name = pool.addUtf8(name);
107     }
108
109     public void setDescriptor(String JavaDoc descriptor) {
110         this.descriptor = pool.addUtf8(descriptor);
111     }
112
113     public void setDeprecated() {
114         attributes.addDeprecatedAttribute();
115     }
116
117     public void setSynthetic() {
118         attributes.addSyntheticAttribute();
119     }
120
121     public void setConstantValue(boolean b) {
122         attributes.addConstantValueAttribute(pool.addInt(b ? 1 : 0));
123     }
124     public void setConstantValue(String JavaDoc s) {
125         attributes.addConstantValueAttribute(pool.addString(s));
126     }
127     public void setConstantValue(int i) {
128         attributes.addConstantValueAttribute(pool.addInt(i));
129     }
130     public void setConstantValue(float f) {
131         attributes.addConstantValueAttribute(pool.addFloat(f));
132     }
133     public void setConstantValue(long l) {
134         attributes.addConstantValueAttribute(pool.addLong(l));
135     }
136     public void setConstantValue(double d) {
137         attributes.addConstantValueAttribute(pool.addDouble(d));
138     }
139
140     // ------------------------------
141
// disassembly methods
142

143     FieldBuilder readFrom(java.io.DataInputStream JavaDoc stream) throws java.io.IOException JavaDoc {
144         accessFlags = stream.readUnsignedShort();
145         name = pool.get(stream.readUnsignedShort());
146         descriptor = pool.get(stream.readUnsignedShort());
147         attributes.readFrom(stream);
148         return this;
149     }
150 }
151
Popular Tags