KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.Modifier JavaDoc;
29
30 import java.io.IOException JavaDoc;
31 import java.io.DataOutputStream JavaDoc;
32
33 // XXX annoying big modularity violation.
34
import org.aspectj.compiler.base.ast.NameType;
35
36 //import org.aspectj.compiler.base.bcg.ConstantPool.Constant;
37

38 /** Lifecycle: creation, addition of code (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 class MethodBuilder {
43
44     // ------------------------------
45
// state
46

47     ConstantPool pool;
48     ClassfileBuilder cfb;
49     Attributes attributes;
50
51     MethodBuilder(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 "(method " +
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("(method "); 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 addToExceptionsAttribute(NameType exn) {
122         attributes.addToExceptionsAttribute(exn);
123     }
124
125     public CodeBuilder getCodeBuilder() {
126         return new CodeBuilder(cfb);
127     }
128
129     CodeBuilder cb = null;
130     public void setCode(CodeBuilder cb) {
131         this.cb = cb;
132         attributes.addCodeAttribute(cb);
133     }
134
135     void resolve() {
136         if (cb != null) cb.resolve();
137     }
138
139     // ------------------------------
140
// disassembly methods
141

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