removed Makefile; lifted repo/org.ibex.tool/src/ to src/
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / classfmt / MethodInfo.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.classfmt;
12
13 import org.eclipse.jdt.core.compiler.CharOperation;
14 import org.eclipse.jdt.internal.compiler.codegen.AttributeNamesConstants;
15 import org.eclipse.jdt.internal.compiler.env.IBinaryMethod;
16
17 public class MethodInfo extends ClassFileStruct implements IBinaryMethod, AttributeNamesConstants, Comparable {
18         private char[][] exceptionNames;
19         private int[] constantPoolOffsets;
20         private boolean isDeprecated;
21         private boolean isSynthetic;
22         private int accessFlags;
23         private char[] name;
24         private char[] signature;
25         private int attributeBytes;
26         static private final char[][] noException = CharOperation.NO_CHAR_CHAR;
27 /**
28  * @param classFileBytes byte[]
29  * @param offsets int[]
30  * @param offset int
31  */
32 public MethodInfo (byte classFileBytes[], int offsets[], int offset) {
33         super(classFileBytes, offset);
34         constantPoolOffsets = offsets;
35         accessFlags = -1;
36         int attributesCount = u2At(6);
37         int readOffset = 8;
38         for (int i = 0; i < attributesCount; i++) {
39                 readOffset += (6 + u4At(readOffset + 2));
40         }
41         attributeBytes = readOffset;
42 }
43 /**
44  * @see org.eclipse.jdt.internal.compiler.env.IGenericMethod#getArgumentNames()
45  */
46 public char[][] getArgumentNames() {
47         return null;
48 }
49 /**
50  * Answer the resolved names of the exception types in the
51  * class file format as specified in section 4.2 of the Java 2 VM spec
52  * or null if the array is empty.
53  *
54  * For example, java.lang.String is java/lang/String.
55  * @return char[][]
56  */
57 public char[][] getExceptionTypeNames() {
58         if (exceptionNames == null) {
59                 readExceptionAttributes();
60         }
61         return exceptionNames;
62 }
63 /**
64  * Answer the receiver's method descriptor which describes the parameter &
65  * return types as specified in section 4.3.3 of the Java 2 VM spec.
66  *
67  * For example:
68  *   - int foo(String) is (Ljava/lang/String;)I
69  *   - void foo(Object[]) is (I)[Ljava/lang/Object;
70  * @return char[]
71  */
72 public char[] getMethodDescriptor() {
73         if (signature == null) {
74                 // read the name
75                 int utf8Offset = constantPoolOffsets[u2At(4)] - structOffset;
76                 signature = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
77         }
78         return signature;
79 }
80 /**
81  * Answer an int whose bits are set according the access constants
82  * defined by the VM spec.
83  * Set the AccDeprecated and AccSynthetic bits if necessary
84  * @return int
85  */
86 public int getModifiers() {
87         if (accessFlags == -1) {
88                 // compute the accessflag. Don't forget the deprecated attribute
89                 accessFlags = u2At(0);
90                 readDeprecatedAndSyntheticAttributes();
91                 if (isDeprecated) {
92                         accessFlags |= AccDeprecated;
93                 }
94                 if (isSynthetic) {
95                         accessFlags |= AccSynthetic;
96                 }
97         }
98         return accessFlags;
99 }
100 /**
101  * Answer the name of the method.
102  *
103  * For a constructor, answer <init> & <clinit> for a clinit method.
104  * @return char[]
105  */
106 public char[] getSelector() {
107         if (name == null) {
108                 // read the name
109                 int utf8Offset = constantPoolOffsets[u2At(2)] - structOffset;
110                 name = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
111         }
112         return name;
113 }
114 /**
115  * Answer true if the method is a class initializer, false otherwise.
116  * @return boolean
117  */
118 public boolean isClinit() {
119         char[] selector = getSelector();
120         return selector[0] == '<' && selector.length == 8; // Can only match <clinit>
121 }
122 /**
123  * Answer true if the method is a constructor, false otherwise.
124  * @return boolean
125  */
126 public boolean isConstructor() {
127         char[] selector = getSelector();
128         return selector[0] == '<' && selector.length == 6; // Can only match <init>
129 }
130 /**
131  * Return true if the field is a synthetic method, false otherwise.
132  * @return boolean
133  */
134 public boolean isSynthetic() {
135         return (getModifiers() & AccSynthetic) != 0;
136 }
137 private void readDeprecatedAndSyntheticAttributes() {
138         int attributesCount = u2At(6);
139         int readOffset = 8;
140         for (int i = 0; i < attributesCount; i++) {
141                 int utf8Offset = constantPoolOffsets[u2At(readOffset)] - structOffset;
142                 char[] attributeName = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
143                 if (CharOperation.equals(attributeName, DeprecatedName)) {
144                         isDeprecated = true;
145                 } else if (CharOperation.equals(attributeName, SyntheticName)) {
146                         isSynthetic = true;
147                 }
148                 readOffset += (6 + u4At(readOffset + 2));
149         }
150 }
151 private void readExceptionAttributes() {
152         int attributesCount = u2At(6);
153         int readOffset = 8;
154         for (int i = 0; i < attributesCount; i++) {
155                 int utf8Offset = constantPoolOffsets[u2At(readOffset)] - structOffset;
156                 char[] attributeName = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
157                 if (CharOperation.equals(attributeName, ExceptionsName)) {
158                         // read the number of exception entries
159                         int entriesNumber = u2At(readOffset + 6);
160                         // place the readOffset at the beginning of the exceptions table
161                         readOffset += 8;
162                         if (entriesNumber == 0) {
163                                 exceptionNames = noException;
164                         } else {
165                                 exceptionNames = new char[entriesNumber][];
166                                 for (int j = 0; j < entriesNumber; j++) {
167                                         utf8Offset = 
168                                                 constantPoolOffsets[u2At(
169                                                         constantPoolOffsets[u2At(readOffset)] - structOffset + 1)]
170                                                         - structOffset; 
171                                         exceptionNames[j] = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
172                                         readOffset += 2;
173                                 }
174                         }
175                 } else {
176                         readOffset += (6 + u4At(readOffset + 2));
177                 }
178         }
179         if (exceptionNames == null) {
180                 exceptionNames = noException;
181         }
182 }
183 /**
184  * Answer the size of the receiver in bytes.
185  * 
186  * @return int
187  */
188 public int sizeInBytes() {
189         return attributeBytes;
190 }
191 public String toString() {
192         int modifiers = getModifiers();
193         StringBuffer buffer = new StringBuffer(this.getClass().getName());
194         return buffer
195                 .append("{") //$NON-NLS-1$
196                 .append(
197                         ((modifiers & AccDeprecated) != 0 ? "deprecated " : "") //$NON-NLS-1$ //$NON-NLS-2$
198                                 + ((modifiers & 0x0001) == 1 ? "public " : "") //$NON-NLS-1$ //$NON-NLS-2$
199                                 + ((modifiers & 0x0002) == 0x0002 ? "private " : "") //$NON-NLS-1$ //$NON-NLS-2$
200                                 + ((modifiers & 0x0004) == 0x0004 ? "protected " : "") //$NON-NLS-1$ //$NON-NLS-2$
201                                 + ((modifiers & 0x0008) == 0x000008 ? "static " : "") //$NON-NLS-1$ //$NON-NLS-2$
202                                 + ((modifiers & 0x0010) == 0x0010 ? "final " : "") //$NON-NLS-1$ //$NON-NLS-2$
203                                 + ((modifiers & 0x0040) == 0x0040 ? "volatile " : "") //$NON-NLS-1$ //$NON-NLS-2$
204                                 + ((modifiers & 0x0080) == 0x0080 ? "transient " : "")) //$NON-NLS-1$ //$NON-NLS-2$
205                 .append(getSelector())
206                 .append(getMethodDescriptor())
207                 .append("}") //$NON-NLS-1$
208                 .toString(); 
209 }
210 public int compareTo(Object o) {
211         if (!(o instanceof MethodInfo)) {
212                 throw new ClassCastException();
213         }
214
215         MethodInfo otherMethod = (MethodInfo) o;
216         int result = new String(this.getSelector()).compareTo(new String(otherMethod.getSelector()));
217         if (result != 0) return result;
218         return new String(this.getMethodDescriptor()).compareTo(new String(otherMethod.getMethodDescriptor()));
219 }
220
221 /**
222  * This method is used to fully initialize the contents of the receiver. All methodinfos, fields infos
223  * will be therefore fully initialized and we can get rid of the bytes.
224  */
225 void initialize() {
226         getModifiers();
227         getSelector();
228         getMethodDescriptor();
229         getExceptionTypeNames();
230         reset();
231 }
232 protected void reset() {
233         this.constantPoolOffsets = null;
234         super.reset();
235 }
236 }