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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.classfmt;
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;
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;
24 private char[] signature;
25 private int attributeBytes;
26 static private final char[][] noException = CharOperation.NO_CHAR_CHAR;
28 * @param classFileBytes byte[]
29 * @param offsets int[]
32 public MethodInfo (byte classFileBytes[], int offsets[], int offset) {
33 super(classFileBytes, offset);
34 constantPoolOffsets = offsets;
36 int attributesCount = u2At(6);
38 for (int i = 0; i < attributesCount; i++) {
39 readOffset += (6 + u4At(readOffset + 2));
41 attributeBytes = readOffset;
44 * @see org.eclipse.jdt.internal.compiler.env.IGenericMethod#getArgumentNames()
46 public char[][] getArgumentNames() {
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.
54 * For example, java.lang.String is java/lang/String.
57 public char[][] getExceptionTypeNames() {
58 if (exceptionNames == null) {
59 readExceptionAttributes();
61 return exceptionNames;
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.
68 * - int foo(String) is (Ljava/lang/String;)I
69 * - void foo(Object[]) is (I)[Ljava/lang/Object;
72 public char[] getMethodDescriptor() {
73 if (signature == null) {
75 int utf8Offset = constantPoolOffsets[u2At(4)] - structOffset;
76 signature = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
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
86 public int getModifiers() {
87 if (accessFlags == -1) {
88 // compute the accessflag. Don't forget the deprecated attribute
89 accessFlags = u2At(0);
90 readDeprecatedAndSyntheticAttributes();
92 accessFlags |= AccDeprecated;
95 accessFlags |= AccSynthetic;
101 * Answer the name of the method.
103 * For a constructor, answer <init> & <clinit> for a clinit method.
106 public char[] getSelector() {
109 int utf8Offset = constantPoolOffsets[u2At(2)] - structOffset;
110 name = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
115 * Answer true if the method is a class initializer, false otherwise.
118 public boolean isClinit() {
119 char[] selector = getSelector();
120 return selector[0] == '<' && selector.length == 8; // Can only match <clinit>
123 * Answer true if the method is a constructor, false otherwise.
126 public boolean isConstructor() {
127 char[] selector = getSelector();
128 return selector[0] == '<' && selector.length == 6; // Can only match <init>
131 * Return true if the field is a synthetic method, false otherwise.
134 public boolean isSynthetic() {
135 return (getModifiers() & AccSynthetic) != 0;
137 private void readDeprecatedAndSyntheticAttributes() {
138 int attributesCount = u2At(6);
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)) {
145 } else if (CharOperation.equals(attributeName, SyntheticName)) {
148 readOffset += (6 + u4At(readOffset + 2));
151 private void readExceptionAttributes() {
152 int attributesCount = u2At(6);
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
162 if (entriesNumber == 0) {
163 exceptionNames = noException;
165 exceptionNames = new char[entriesNumber][];
166 for (int j = 0; j < entriesNumber; j++) {
168 constantPoolOffsets[u2At(
169 constantPoolOffsets[u2At(readOffset)] - structOffset + 1)]
171 exceptionNames[j] = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
176 readOffset += (6 + u4At(readOffset + 2));
179 if (exceptionNames == null) {
180 exceptionNames = noException;
184 * Answer the size of the receiver in bytes.
188 public int sizeInBytes() {
189 return attributeBytes;
191 public String toString() {
192 int modifiers = getModifiers();
193 StringBuffer buffer = new StringBuffer(this.getClass().getName());
195 .append("{") //$NON-NLS-1$
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$
210 public int compareTo(Object o) {
211 if (!(o instanceof MethodInfo)) {
212 throw new ClassCastException();
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()));
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.
228 getMethodDescriptor();
229 getExceptionTypeNames();
232 protected void reset() {
233 this.constantPoolOffsets = null;