KickJava   Java API By Example, From Geeks To Geeks.

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


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.IConstantPool;
17 import org.eclipse.jdt.core.util.IConstantPoolConstant;
18 import org.eclipse.jdt.core.util.IConstantPoolEntry;
19 import org.eclipse.jdt.core.util.IConstantValueAttribute;
20 import org.eclipse.jdt.core.util.IFieldInfo;
21 import org.eclipse.jdt.core.util.IModifierConstants;
22
23 /**
24  * Default implementation of IFieldInfo.
25  */

26 public class FieldInfo extends ClassFileStruct implements IFieldInfo {
27     private int accessFlags;
28     private int attributeBytes;
29     private IClassFileAttribute[] attributes;
30     private int attributesCount;
31     private IConstantValueAttribute constantValueAttribute;
32     private char[] descriptor;
33     private int descriptorIndex;
34     private boolean isDeprecated;
35     private boolean isSynthetic;
36     private char[] name;
37     private int nameIndex;
38     
39     /**
40      * @param classFileBytes byte[]
41      * @param constantPool IConstantPool
42      * @param offset int
43      */

44     public FieldInfo(byte classFileBytes[], IConstantPool constantPool, int offset)
45         throws ClassFormatException {
46         final int flags = u2At(classFileBytes, 0, offset);
47         this.accessFlags = flags;
48         if ((flags & IModifierConstants.ACC_SYNTHETIC) != 0) {
49             this.isSynthetic = true;
50         }
51         this.nameIndex = u2At(classFileBytes, 2, offset);
52         IConstantPoolEntry constantPoolEntry = constantPool.decodeEntry(this.nameIndex);
53         if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
54             throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
55         }
56         this.name = constantPoolEntry.getUtf8Value();
57     
58         this.descriptorIndex = u2At(classFileBytes, 4, offset);
59         constantPoolEntry = constantPool.decodeEntry(this.descriptorIndex);
60         if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
61             throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
62         }
63         this.descriptor = constantPoolEntry.getUtf8Value();
64     
65         this.attributesCount = u2At(classFileBytes, 6, offset);
66         this.attributes = ClassFileAttribute.NO_ATTRIBUTES;
67         int readOffset = 8;
68         if (this.attributesCount != 0) {
69             this.attributes = new IClassFileAttribute[this.attributesCount];
70         }
71         int attributesIndex = 0;
72         for (int i = 0; i < this.attributesCount; i++) {
73             constantPoolEntry = constantPool.decodeEntry(u2At(classFileBytes, readOffset, offset));
74             if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
75                 throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
76             }
77             char[] attributeName = constantPoolEntry.getUtf8Value();
78             if (equals(attributeName, IAttributeNamesConstants.DEPRECATED)) {
79                 this.isDeprecated = true;
80                 this.attributes[attributesIndex++] = new ClassFileAttribute(classFileBytes, constantPool, offset + readOffset);
81             } else if (equals(attributeName, IAttributeNamesConstants.SYNTHETIC)) {
82                 this.isSynthetic = true;
83                 this.attributes[attributesIndex++] = new ClassFileAttribute(classFileBytes, constantPool, offset + readOffset);
84             } else if (equals(attributeName, IAttributeNamesConstants.CONSTANT_VALUE)) {
85                 this.constantValueAttribute = new ConstantValueAttribute(classFileBytes, constantPool, offset + readOffset);
86                 this.attributes[attributesIndex++] = this.constantValueAttribute;
87             } else if (equals(attributeName, IAttributeNamesConstants.SIGNATURE)) {
88                 this.attributes[attributesIndex++] = new SignatureAttribute(classFileBytes, constantPool, offset + readOffset);
89             } else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_VISIBLE_ANNOTATIONS)) {
90                 this.attributes[attributesIndex++] = new RuntimeVisibleAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset);
91             } else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_INVISIBLE_ANNOTATIONS)) {
92                 this.attributes[attributesIndex++] = new RuntimeInvisibleAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset);
93             } else {
94                 this.attributes[attributesIndex++] = new ClassFileAttribute(classFileBytes, constantPool, offset + readOffset);
95             }
96             readOffset += (6 + u4At(classFileBytes, readOffset + 2, offset));
97         }
98
99         this.attributeBytes = readOffset;
100     }
101     /**
102      * @see IFieldInfo#getAccessFlags()
103      */

104     public int getAccessFlags() {
105         return this.accessFlags;
106     }
107     /**
108      * @see IFieldInfo#getAttributeCount()
109      */

110     public int getAttributeCount() {
111         return this.attributesCount;
112     }
113
114     /**
115      * @see IFieldInfo#getAttributes()
116      */

117     public IClassFileAttribute[] getAttributes() {
118         return this.attributes;
119     }
120
121     /**
122      * @see IFieldInfo#getConstantValueAttribute()
123      */

124     public IConstantValueAttribute getConstantValueAttribute() {
125         return this.constantValueAttribute;
126     }
127
128     /**
129      * @see IFieldInfo#getDescriptor()
130      */

131     public char[] getDescriptor() {
132         return this.descriptor;
133     }
134
135     /**
136      * @see IFieldInfo#getDescriptorIndex()
137      */

138     public int getDescriptorIndex() {
139         return this.descriptorIndex;
140     }
141
142     /**
143      * @see IFieldInfo#getName()
144      */

145     public char[] getName() {
146         return this.name;
147     }
148
149     /**
150      * @see IFieldInfo#getNameIndex()
151      */

152     public int getNameIndex() {
153         return this.nameIndex;
154     }
155     /**
156      * @see IFieldInfo#hasConstantValueAttribute()
157      */

158     public boolean hasConstantValueAttribute() {
159         return this.constantValueAttribute != null;
160     }
161
162     /**
163      * @see IFieldInfo#isDeprecated()
164      */

165     public boolean isDeprecated() {
166         return this.isDeprecated;
167     }
168
169     /**
170      * @see IFieldInfo#isSynthetic()
171      */

172     public boolean isSynthetic() {
173         return this.isSynthetic;
174     }
175
176     int sizeInBytes() {
177         return this.attributeBytes;
178     }
179 }
180
Popular Tags