KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > classfile > Method


1 /*
2  * Method.java
3  *
4  * The contents of this file are subject to the terms of the Common Development
5  * and Distribution License (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
9  * or http://www.netbeans.org/cddl.txt.
10  *
11  * When distributing Covered Code, include this CDDL Header Notice in each file
12  * and include the License file at http://www.netbeans.org/cddl.txt.
13  * If applicable, add the following below the CDDL Header, with the fields
14  * enclosed by brackets [] replaced by your own identifying information:
15  * "Portions Copyrighted [year] [name of copyright owner]"
16  *
17  * The Original Software is NetBeans. The Initial Developer of the Original
18  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
19  * Microsystems, Inc. All Rights Reserved.
20  *
21  * Contributor(s): Thomas Ball
22  *
23  * Version: $Revision: 1.17 $
24  */

25
26 package org.netbeans.modules.classfile;
27
28 import java.io.DataInputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Arrays JavaDoc;
32
33 /**
34  * A Java method object.
35  *
36  * @author Thomas Ball
37  */

38 public final class Method extends Field {
39
40     private Code code;
41     private CPClassInfo[] exceptions;
42     private Parameter[] parameters;
43     private ElementValue annotationDefault;
44     
45     /** Marker which indicates that the annotationDefault has not been loaded yet. */
46     private static final ElementValue notloadedAnnotationDefault = new ElementValue() {};
47     
48     static Method[] loadMethods(DataInputStream JavaDoc in, ConstantPool pool,
49                 ClassFile cls, boolean includeCode)
50       throws IOException JavaDoc {
51     int count = in.readUnsignedShort();
52     Method[] methods = new Method[count];
53     for (int i = 0; i < count; i++)
54         methods[i] = new Method(in, pool, cls, includeCode);
55     return methods;
56     }
57     
58     /** Creates new Method */
59     Method(DataInputStream JavaDoc in, ConstantPool pool, ClassFile cls,
60        boolean includeCode) throws IOException JavaDoc {
61         super(in, pool, cls, includeCode);
62         annotationDefault = notloadedAnnotationDefault;
63     }
64
65     /**
66      * Get the bytecodes of this method. This method returns null if
67      * the method is abstract, or if the ClassFile instance was created
68      * with a includeCode parameter of false.
69      *
70      * @return the Code object, or null.
71      */

72     public final Code getCode() {
73     if (code == null) {
74         DataInputStream JavaDoc in = attributes.getStream("Code"); // NOI18N
75
if (in != null) {
76         try {
77             code = new Code(in, classFile.constantPool);
78             in.close();
79         } catch (IOException JavaDoc e) {
80             throw new InvalidClassFileAttributeException("invalid Code attribute", e);
81         }
82         }
83     }
84         return code; // will be null for abstract methods
85
}
86     
87     public final CPClassInfo[] getExceptionClasses() {
88         if (exceptions == null) {
89         DataInputStream JavaDoc in = attributes.getStream("Exceptions"); // NOI18N
90
if (in != null) {
91         try {
92             exceptions =
93             ClassFile.getCPClassList(in, classFile.constantPool);
94             in.close();
95         } catch (IOException JavaDoc e) {
96             throw new InvalidClassFileAttributeException("invalid Exceptions attribute", e);
97         }
98         }
99         if (exceptions == null)
100         exceptions = new CPClassInfo[0];
101     }
102         return exceptions.clone();
103     }
104     
105     /**
106      * Returns true if this method is a generics bridge method defined
107      * by the compiler.
108      */

109     public final boolean isBridge() {
110     return (access & Access.BRIDGE) == Access.BRIDGE;
111     }
112             
113     /**
114      * Returns true if this method is declared with a variable number
115      * of arguments.
116      */

117     public final boolean isVarArgs() {
118     return (access & Access.VARARGS) == Access.VARARGS;
119     }
120
121     /**
122      * Returns true if this method is declared synchronized.
123      */

124     public final boolean isSynchronized() {
125     return (access & Access.SYNCHRONIZED) == Access.SYNCHRONIZED;
126     }
127
128     /**
129      * Returns true if this method is declared native.
130      */

131     public final boolean isNative() {
132     return (access & Access.NATIVE) == Access.NATIVE;
133     }
134
135     /**
136      * Returns true if this method is declared abstract.
137      */

138     public final boolean isAbstract() {
139     return (access & Access.ABSTRACT) == Access.ABSTRACT;
140     }
141
142     /**
143      * Returns the parameters for this method as a declaration-ordered list.
144      */

145     public final List JavaDoc<Parameter> getParameters() {
146     if (parameters == null)
147         parameters = Parameter.makeParams(this);
148     return Arrays.asList(parameters);
149     }
150
151     /**
152      * Returns the method's return type in the type format defined by
153      * the JVM Specification for Field Descriptors (section 4.3.2).
154      */

155     public final String JavaDoc getReturnType() {
156     String JavaDoc desc = getDescriptor();
157     int i = desc.indexOf(')') + 1;
158     return desc.substring(i);
159     }
160
161     /**
162      * Returns the method's return type as it would be defined in Java
163      * source code format.
164      */

165     public final String JavaDoc getReturnSignature() {
166     String JavaDoc type = getReturnType();
167     return CPFieldMethodInfo.getSignature(type, true);
168     }
169
170     /**
171      * Returns the default annotation value for the element
172      * defined by this method. Null is returned if no default
173      * is specified for this element, or if the class that contains
174      * this method does not define an annotation type.
175      */

176     public ElementValue getAnnotationDefault() {
177     if (annotationDefault == notloadedAnnotationDefault) {
178             annotationDefault = null;
179         DataInputStream JavaDoc in =
180         attributes.getStream("AnnotationDefault"); // NOI18N
181
if (in != null) {
182         try {
183             annotationDefault =
184             ElementValue.load(in, classFile.constantPool, false);
185             in.close();
186         } catch (IOException JavaDoc e) {
187             throw new InvalidClassFileAttributeException("invalid AnnotationDefault attribute", e);
188         }
189         }
190     }
191         return annotationDefault;
192     }
193
194     public String JavaDoc toString() {
195         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(super.toString());
196         sb.append(", params (");
197         getParameters();
198         for (int i = 0; i < parameters.length; i++) {
199             sb.append(parameters[i].toString());
200             if (i+1 < parameters.length)
201                 sb.append(", ");
202         }
203         sb.append("), returns ");
204     sb.append(getReturnSignature());
205         CPClassInfo[] ec = getExceptionClasses();
206         if (ec.length > 0) {
207             sb.append(", throws"); //NOI18N
208
for (int i = 0; i < ec.length; i++) {
209                 sb.append(' '); //NOI18N
210
sb.append(ec[i].getName());
211             }
212     }
213     if (getAnnotationDefault() != null) {
214         sb.append(", default \"");
215         sb.append(annotationDefault.toString());
216         sb.append("\" ");
217     }
218     Code code = getCode();
219     if (code != null) {
220             sb.append(' ');
221         sb.append(code.toString());
222         }
223         return sb.toString();
224     }
225
226     public final String JavaDoc getDeclaration() {
227         return CPMethodInfo.getFullMethodName(getName(), getDescriptor());
228     }
229 }
230
Popular Tags