1 /*******************************************************************************
 
   2  * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
 
   3  * All rights reserved. This program and the accompanying materials 
 
   4  * are made available under the terms of the Common Public License v0.5 
 
   5  * which accompanies this distribution, and is available at
 
   6  * http://www.eclipse.org/legal/cpl-v05.html
 
   9  *     IBM Corporation - initial API and implementation
 
  10  ******************************************************************************/
 
  11 package net.sourceforge.phpdt.internal.compiler.classfmt;
 
  13 import net.sourceforge.phpdt.internal.compiler.codegen.AttributeNamesConstants;
 
  14 import net.sourceforge.phpdt.internal.compiler.env.IBinaryMethod;
 
  15 import net.sourceforge.phpdt.internal.compiler.util.CharOperation;
 
  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 attributesCount;
 
  26         private int attributeBytes;
 
  27         static private final char[][] noException = new char[0][0];
 
  28         private int decodeIndex;
 
  30  * @param classFileBytes byte[]
 
  31  * @param offsets int[]
 
  34 public MethodInfo (byte classFileBytes[], int offsets[], int offset) throws ClassFormatException {
 
  35         super(classFileBytes, offset);
 
  36         constantPoolOffsets = offsets;
 
  38         int attributesCount = u2At(6);
 
  40         for (int i = 0; i < attributesCount; i++) {
 
  41                 readOffset += (6 + u4At(readOffset + 2));
 
  43         attributeBytes = readOffset;
 
  46  * @see IGenericMethod#getArgumentNames()
 
  48 public char[][] getArgumentNames() {
 
  52  * Answer the resolved names of the exception types in the
 
  53  * class file format as specified in section 4.2 of the Java 2 VM spec
 
  54  * or null if the array is empty.
 
  56  * For example, java.lang.String is java/lang/String.
 
  59 public char[][] getExceptionTypeNames() {
 
  60         if (exceptionNames == null) {
 
  61                 readExceptionAttributes();
 
  63         return exceptionNames;
 
  66  * Answer the receiver's method descriptor which describes the parameter &
 
  67  * return types as specified in section 4.3.3 of the Java 2 VM spec.
 
  70  *   - int foo(String) is (Ljava/lang/String;)I
 
  71  *   - void foo(Object[]) is (I)[Ljava/lang/Object;
 
  74 public char[] getMethodDescriptor() {
 
  75         if (signature == null) {
 
  77                 int utf8Offset = constantPoolOffsets[u2At(4)] - structOffset;
 
  78                 signature = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
 
  83  * Answer an int whose bits are set according the access constants
 
  84  * defined by the VM spec.
 
  85  * Set the AccDeprecated and AccSynthetic bits if necessary
 
  88 public int getModifiers() {
 
  89         if (accessFlags == -1) {
 
  90                 // compute the accessflag. Don't forget the deprecated attribute
 
  91                 accessFlags = u2At(0);
 
  92                 readDeprecatedAndSyntheticAttributes();
 
  94                         accessFlags |= AccDeprecated;
 
  97                         accessFlags |= AccSynthetic;
 
 103  * Answer the name of the method.
 
 105  * For a constructor, answer <init> & <clinit> for a clinit method.
 
 108 public char[] getSelector() {
 
 111                 int utf8Offset = constantPoolOffsets[u2At(2)] - structOffset;
 
 112                 name = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
 
 117  * Answer true if the method is a class initializer, false otherwise.
 
 120 public boolean isClinit() {
 
 121         char[] selector = getSelector();
 
 122         return selector[0] == '<' && selector.length == 8; // Can only match <clinit>
 
 125  * Answer true if the method is a constructor, false otherwise.
 
 128 public boolean isConstructor() {
 
 129         char[] selector = getSelector();
 
 130         return selector[0] == '<' && selector.length == 6; // Can only match <init>
 
 133  * Return true if the field is a synthetic method, false otherwise.
 
 136 public boolean isSynthetic() {
 
 137         return (getModifiers() & AccSynthetic) != 0;
 
 139 private void readDeprecatedAndSyntheticAttributes() {
 
 140         int attributesCount = u2At(6);
 
 142         for (int i = 0; i < attributesCount; i++) {
 
 143                 int utf8Offset = constantPoolOffsets[u2At(readOffset)] - structOffset;
 
 144                 char[] attributeName = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
 
 145                 if (CharOperation.equals(attributeName, DeprecatedName)) {
 
 147                 } else if (CharOperation.equals(attributeName, SyntheticName)) {
 
 150                 readOffset += (6 + u4At(readOffset + 2));
 
 153 private void readExceptionAttributes() {
 
 154         int attributesCount = u2At(6);
 
 156         for (int i = 0; i < attributesCount; i++) {
 
 157                 int utf8Offset = constantPoolOffsets[u2At(readOffset)] - structOffset;
 
 158                 char[] attributeName = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
 
 159                 if (CharOperation.equals(attributeName, ExceptionsName)) {
 
 160                         // read the number of exception entries
 
 161                         int entriesNumber = u2At(readOffset + 6);
 
 162                         // place the readOffset at the beginning of the exceptions table
 
 164                         if (entriesNumber == 0) {
 
 165                                 exceptionNames = noException;
 
 167                                 exceptionNames = new char[entriesNumber][];
 
 168                                 for (int j = 0; j < entriesNumber; j++) {
 
 170                                                 constantPoolOffsets[u2At(
 
 171                                                         constantPoolOffsets[u2At(readOffset)] - structOffset + 1)]
 
 173                                         exceptionNames[j] = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
 
 178                         readOffset += (6 + u4At(readOffset + 2));
 
 181         if (exceptionNames == null) {
 
 182                 exceptionNames = noException;
 
 186  * Answer the size of the receiver in bytes.
 
 190 public int sizeInBytes() {
 
 191         return attributeBytes;
 
 193 public String toString() {
 
 194         int modifiers = getModifiers();
 
 195         StringBuffer buffer = new StringBuffer(this.getClass().getName());
 
 197                 .append("{") //$NON-NLS-1$
 
 199                         ((modifiers & AccDeprecated) != 0 ? "deprecated " : "") //$NON-NLS-1$ //$NON-NLS-2$
 
 200                                 + ((modifiers & 0x0001) == 1 ? "public " : "") //$NON-NLS-1$ //$NON-NLS-2$
 
 201                                 + ((modifiers & 0x0002) == 0x0002 ? "private " : "") //$NON-NLS-1$ //$NON-NLS-2$
 
 202                                 + ((modifiers & 0x0004) == 0x0004 ? "protected " : "") //$NON-NLS-1$ //$NON-NLS-2$
 
 203                                 + ((modifiers & 0x0008) == 0x000008 ? "static " : "") //$NON-NLS-1$ //$NON-NLS-2$
 
 204                                 + ((modifiers & 0x0010) == 0x0010 ? "final " : "") //$NON-NLS-1$ //$NON-NLS-2$
 
 205                                 + ((modifiers & 0x0040) == 0x0040 ? "volatile " : "") //$NON-NLS-1$ //$NON-NLS-2$
 
 206                                 + ((modifiers & 0x0080) == 0x0080 ? "transient " : "")) //$NON-NLS-1$ //$NON-NLS-2$
 
 207                 .append(getSelector())
 
 208                 .append(getMethodDescriptor())
 
 209                 .append("}") //$NON-NLS-1$
 
 212 public int compareTo(Object o) {
 
 213         if (!(o instanceof MethodInfo)) {
 
 214                 throw new ClassCastException();
 
 217         MethodInfo otherMethod = (MethodInfo) o;
 
 218         int result = new String(this.getSelector()).compareTo(new String(otherMethod.getSelector()));
 
 219         if (result != 0) return result;
 
 220         return new String(this.getMethodDescriptor()).compareTo(new String(otherMethod.getMethodDescriptor()));
 
 224  * This method is used to fully initialize the contents of the receiver. All methodinfos, fields infos
 
 225  * will be therefore fully initialized and we can get rid of the bytes.
 
 230         getMethodDescriptor();
 
 231         getExceptionTypeNames();
 
 234 protected void reset() {
 
 235         this.constantPoolOffsets = null;