KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > util > MethodInfo


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.util;
12
13 import org.eclipse.jdt.core.util.ClassFormatException;
14 import org.eclipse.jdt.core.util.IAttributeNamesConstants;
15 import org.eclipse.jdt.core.util.IClassFileAttribute;
16 import org.eclipse.jdt.core.util.IClassFileReader;
17 import org.eclipse.jdt.core.util.ICodeAttribute;
18 import org.eclipse.jdt.core.util.IConstantPool;
19 import org.eclipse.jdt.core.util.IConstantPoolConstant;
20 import org.eclipse.jdt.core.util.IConstantPoolEntry;
21 import org.eclipse.jdt.core.util.IExceptionAttribute;
22 import org.eclipse.jdt.core.util.IMethodInfo;
23 import org.eclipse.jdt.core.util.IModifierConstants;
24
25 /**
26  * Default implementation of IMethodInfo.
27  */

28 public class MethodInfo extends ClassFileStruct implements IMethodInfo {
29     private int accessFlags;
30     private int attributeBytes;
31     private IClassFileAttribute[] attributes;
32     private int attributesCount;
33     private ICodeAttribute codeAttribute;
34     private char[] descriptor;
35     private int descriptorIndex;
36     private IExceptionAttribute exceptionAttribute;
37     private boolean isDeprecated;
38     private boolean isSynthetic;
39     private char[] name;
40     private int nameIndex;
41     
42     /**
43      * @param classFileBytes byte[]
44      * @param constantPool IConstantPool
45      * @param offset int
46      * @param decodingFlags int
47      */

48     public MethodInfo(byte classFileBytes[], IConstantPool constantPool, int offset, int decodingFlags)
49         throws ClassFormatException {
50             
51         boolean no_code_attribute = (decodingFlags & IClassFileReader.METHOD_BODIES) == 0;
52         final int flags = u2At(classFileBytes, 0, offset);
53         this.accessFlags = flags;
54         if ((flags & IModifierConstants.ACC_SYNTHETIC) != 0) {
55             this.isSynthetic = true;
56         }
57         
58         this.nameIndex = u2At(classFileBytes, 2, offset);
59         IConstantPoolEntry constantPoolEntry = constantPool.decodeEntry(this.nameIndex);
60         if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
61             throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
62         }
63         this.name = constantPoolEntry.getUtf8Value();
64         
65         this.descriptorIndex = u2At(classFileBytes, 4, offset);
66         constantPoolEntry = constantPool.decodeEntry(this.descriptorIndex);
67         if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
68             throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
69         }
70         this.descriptor = constantPoolEntry.getUtf8Value();
71         
72         this.attributesCount = u2At(classFileBytes, 6, offset);
73         this.attributes = ClassFileAttribute.NO_ATTRIBUTES;
74         if (this.attributesCount != 0) {
75             if (no_code_attribute && !isAbstract() && !isNative()) {
76                 if (this.attributesCount != 1) {
77                     this.attributes = new IClassFileAttribute[this.attributesCount - 1];
78                 }
79             } else {
80                 this.attributes = new IClassFileAttribute[this.attributesCount];
81             }
82         }
83         int attributesIndex = 0;
84         int readOffset = 8;
85         for (int i = 0; i < this.attributesCount; i++) {
86             constantPoolEntry = constantPool.decodeEntry(u2At(classFileBytes, readOffset, offset));
87             if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
88                 throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
89             }
90             char[] attributeName = constantPoolEntry.getUtf8Value();
91             if (equals(attributeName, IAttributeNamesConstants.DEPRECATED)) {
92                 this.isDeprecated = true;
93                 this.attributes[attributesIndex++] = new ClassFileAttribute(classFileBytes, constantPool, offset + readOffset);
94             } else if (equals(attributeName, IAttributeNamesConstants.SYNTHETIC)) {
95                 this.isSynthetic = true;
96                 this.attributes[attributesIndex++] = new ClassFileAttribute(classFileBytes, constantPool, offset + readOffset);
97             } else if (equals(attributeName, IAttributeNamesConstants.CODE)) {
98                 if (!no_code_attribute) {
99                     this.codeAttribute = new CodeAttribute(classFileBytes, constantPool, offset + readOffset);
100                     this.attributes[attributesIndex++] = this.codeAttribute;
101                 }
102             } else if (equals(attributeName, IAttributeNamesConstants.EXCEPTIONS)) {
103                 this.exceptionAttribute = new ExceptionAttribute(classFileBytes, constantPool, offset + readOffset);
104                 this.attributes[attributesIndex++] = this.exceptionAttribute;
105             } else if (equals(attributeName, IAttributeNamesConstants.SIGNATURE)) {
106                 this.attributes[attributesIndex++] = new SignatureAttribute(classFileBytes, constantPool, offset + readOffset);
107             } else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_VISIBLE_ANNOTATIONS)) {
108                 this.attributes[attributesIndex++] = new RuntimeVisibleAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset);
109             } else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_INVISIBLE_ANNOTATIONS)) {
110                 this.attributes[attributesIndex++] = new RuntimeInvisibleAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset);
111             } else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS)) {
112                 this.attributes[attributesIndex++] = new RuntimeVisibleParameterAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset);
113             } else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS)) {
114                 this.attributes[attributesIndex++] = new RuntimeInvisibleParameterAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset);
115             } else if (equals(attributeName, IAttributeNamesConstants.ANNOTATION_DEFAULT)) {
116                 this.attributes[attributesIndex++] = new AnnotationDefaultAttribute(classFileBytes, constantPool, offset + readOffset);
117             } else {
118                 this.attributes[attributesIndex++] = new ClassFileAttribute(classFileBytes, constantPool, offset + readOffset);
119             }
120             readOffset += (6 + u4At(classFileBytes, readOffset + 2, offset));
121         }
122         this.attributeBytes = readOffset;
123     }
124     /**
125      * @see IMethodInfo#getAccessFlags()
126      */

127     public int getAccessFlags() {
128         return this.accessFlags;
129     }
130
131     /**
132      * @see IMethodInfo#getAttributeCount()
133      */

134     public int getAttributeCount() {
135         return this.attributesCount;
136     }
137     /**
138      * @see IMethodInfo#getAttributes()
139      */

140     public IClassFileAttribute[] getAttributes() {
141         return this.attributes;
142     }
143
144     /**
145      * @see IMethodInfo#getCodeAttribute()
146      */

147     public ICodeAttribute getCodeAttribute() {
148         return this.codeAttribute;
149     }
150
151     /**
152      * @see IMethodInfo#getDescriptor()
153      */

154     public char[] getDescriptor() {
155         return this.descriptor;
156     }
157
158     /**
159      * @see IMethodInfo#getDescriptorIndex()
160      */

161     public int getDescriptorIndex() {
162         return this.descriptorIndex;
163     }
164
165     /**
166      * @see IMethodInfo#getExceptionAttribute()
167      */

168     public IExceptionAttribute getExceptionAttribute() {
169         return this.exceptionAttribute;
170     }
171
172     /**
173      * @see IMethodInfo#getName()
174      */

175     public char[] getName() {
176         return this.name;
177     }
178
179     /**
180      * @see IMethodInfo#getNameIndex()
181      */

182     public int getNameIndex() {
183         return this.nameIndex;
184     }
185
186     private boolean isAbstract() {
187         return (this.accessFlags & IModifierConstants.ACC_ABSTRACT) != 0;
188     }
189
190     /**
191      * @see IMethodInfo#isClinit()
192      */

193     public boolean isClinit() {
194         return this.name[0] == '<' && this.name.length == 8; // Can only match <clinit>
195
}
196
197     /**
198      * @see IMethodInfo#isConstructor()
199      */

200     public boolean isConstructor() {
201         return this.name[0] == '<' && this.name.length == 6; // Can only match <init>
202
}
203
204     /**
205      * @see IMethodInfo#isDeprecated()
206      */

207     public boolean isDeprecated() {
208         return this.isDeprecated;
209     }
210     
211     private boolean isNative() {
212         return (this.accessFlags & IModifierConstants.ACC_NATIVE) != 0;
213     }
214
215     /**
216      * @see IMethodInfo#isSynthetic()
217      */

218     public boolean isSynthetic() {
219         return this.isSynthetic;
220     }
221
222     int sizeInBytes() {
223         return this.attributeBytes;
224     }
225 }
226
Popular Tags