KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > classfile > ClassMethod


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.api.persistence.enhancer.classfile;
26
27 import java.io.*;
28 import java.util.Vector JavaDoc;
29 import java.util.Enumeration JavaDoc;
30
31 /**
32  * ClassMethod models the static and non-static methods of a class within
33  * a class file. This includes constructors and initializer code.
34  */

35
36 public class ClassMethod extends ClassMember {
37   /* The name of the constructor code */
38     public final static String JavaDoc intializerName = "<init>";//NOI18N
39

40   /* The name of the static initializer code */
41     public final static String JavaDoc staticIntializerName = "<clinit>";//NOI18N
42

43   /* access flag bit mask - see VMConstants */
44   private int accessFlags;
45
46   /* The name of the method */
47   private ConstUtf8 methodName;
48
49   /* The type signature of the method */
50   private ConstUtf8 methodSignature;
51
52   /* The attributes associated with the field */
53   private AttributeVector methodAttributes;
54   
55   
56   /* public accessors */
57
58   /**
59    * Return the access flags for the method - see VMConstants
60    */

61   public int access() {
62     return accessFlags;
63   }
64
65   /**
66    * Update the access flags for the field - see VMConstants
67    */

68   public void setAccess(int newFlags) {
69     accessFlags = newFlags;
70   }
71
72   /**
73    * Is the method abstract?
74    */

75   public boolean isAbstract() {
76     return (accessFlags & ACCAbstract) != 0;
77   }
78
79   /**
80    * Is the method native?
81    */

82   public boolean isNative() {
83     return (accessFlags & ACCNative) != 0;
84   }
85
86   /**
87    * Return the name of the method
88    */

89   public ConstUtf8 name() {
90     return methodName;
91   }
92
93   /**
94    * Change the name of the method
95    */

96   public void changeName(ConstUtf8 name) {
97     methodName = name;
98   }
99
100   /**
101    * Return the type signature of the method
102    */

103   public ConstUtf8 signature() {
104     return methodSignature;
105   }
106
107   /**
108    * Change the type signature of the method
109    */

110   public void changeSignature(ConstUtf8 newSig) {
111     methodSignature = newSig;
112   }
113
114   /**
115    * Return the attributes associated with the method
116    */

117   public AttributeVector attributes() {
118     return methodAttributes;
119   }
120
121   /**
122    * Construct a class method object
123    */

124   
125   public ClassMethod(int accFlags, ConstUtf8 name, ConstUtf8 sig,
126              AttributeVector methodAttrs) {
127     accessFlags = accFlags;
128     methodName = name;
129     methodSignature = sig;
130     methodAttributes = methodAttrs;
131   }
132
133   /**
134    * Returns the size of the method byteCode (if any)
135    */

136   int codeSize() {
137     CodeAttribute codeAttr = codeAttribute();
138     return (codeAttr == null) ? 0 : codeAttr.codeSize();
139   }
140
141   /**
142    * Returns the CodeAttribute associated with this method (if any)
143    */

144   public CodeAttribute codeAttribute() {
145     Enumeration JavaDoc e = methodAttributes.elements();
146     while (e.hasMoreElements()) {
147       ClassAttribute attr = (ClassAttribute) e.nextElement();
148       if (attr instanceof CodeAttribute)
149     return (CodeAttribute) attr;
150     }
151     return null;
152   }
153
154   /* package local methods */
155
156
157   static ClassMethod read(DataInputStream data, ConstantPool pool)
158     throws IOException {
159     int accessFlags = data.readUnsignedShort();
160     int nameIndex = data.readUnsignedShort();
161     int sigIndex = data.readUnsignedShort();
162     ClassMethod f =
163       new ClassMethod(accessFlags,
164               (ConstUtf8) pool.constantAt(nameIndex),
165               (ConstUtf8) pool.constantAt(sigIndex),
166               null);
167
168     f.methodAttributes = AttributeVector.readAttributes(data, pool);
169     return f;
170   }
171
172   void write(DataOutputStream data) throws IOException {
173     CodeAttribute codeAttr = codeAttribute();
174     data.writeShort(accessFlags);
175     data.writeShort(methodName.getIndex());
176     data.writeShort(methodSignature.getIndex());
177     methodAttributes.write(data);
178   }
179
180   void print(PrintStream out, int indent) {
181     ClassPrint.spaces(out, indent);
182     out.print("'" + methodName.asString() + "'");//NOI18N
183
out.print(" sig = " + methodSignature.asString());//NOI18N
184
out.print(" accessFlags = " + Integer.toString(accessFlags));//NOI18N
185
out.println(" attributes:");//NOI18N
186
methodAttributes.print(out, indent+2);
187   }
188
189 }
190
191
192
Popular Tags