001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.core.util;
011:
012: import org.eclipse.jdt.core.util.ClassFormatException;
013: import org.eclipse.jdt.core.util.IAttributeNamesConstants;
014: import org.eclipse.jdt.core.util.IClassFileAttribute;
015: import org.eclipse.jdt.core.util.IConstantPool;
016: import org.eclipse.jdt.core.util.IConstantPoolConstant;
017: import org.eclipse.jdt.core.util.IConstantPoolEntry;
018: import org.eclipse.jdt.core.util.IConstantValueAttribute;
019: import org.eclipse.jdt.core.util.IFieldInfo;
020: import org.eclipse.jdt.core.util.IModifierConstants;
021:
022: /**
023: * Default implementation of IFieldInfo.
024: */
025: public class FieldInfo extends ClassFileStruct implements IFieldInfo {
026: private int accessFlags;
027: private int attributeBytes;
028: private IClassFileAttribute[] attributes;
029: private int attributesCount;
030: private IConstantValueAttribute constantValueAttribute;
031: private char[] descriptor;
032: private int descriptorIndex;
033: private boolean isDeprecated;
034: private boolean isSynthetic;
035: private char[] name;
036: private int nameIndex;
037:
038: /**
039: * @param classFileBytes byte[]
040: * @param constantPool IConstantPool
041: * @param offset int
042: */
043: public FieldInfo(byte classFileBytes[], IConstantPool constantPool,
044: int offset) throws ClassFormatException {
045: final int flags = u2At(classFileBytes, 0, offset);
046: this .accessFlags = flags;
047: if ((flags & IModifierConstants.ACC_SYNTHETIC) != 0) {
048: this .isSynthetic = true;
049: }
050: this .nameIndex = u2At(classFileBytes, 2, offset);
051: IConstantPoolEntry constantPoolEntry = constantPool
052: .decodeEntry(this .nameIndex);
053: if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
054: throw new ClassFormatException(
055: ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
056: }
057: this .name = constantPoolEntry.getUtf8Value();
058:
059: this .descriptorIndex = u2At(classFileBytes, 4, offset);
060: constantPoolEntry = constantPool
061: .decodeEntry(this .descriptorIndex);
062: if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
063: throw new ClassFormatException(
064: ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
065: }
066: this .descriptor = constantPoolEntry.getUtf8Value();
067:
068: this .attributesCount = u2At(classFileBytes, 6, offset);
069: this .attributes = ClassFileAttribute.NO_ATTRIBUTES;
070: int readOffset = 8;
071: if (this .attributesCount != 0) {
072: this .attributes = new IClassFileAttribute[this .attributesCount];
073: }
074: int attributesIndex = 0;
075: for (int i = 0; i < this .attributesCount; i++) {
076: constantPoolEntry = constantPool.decodeEntry(u2At(
077: classFileBytes, readOffset, offset));
078: if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) {
079: throw new ClassFormatException(
080: ClassFormatException.INVALID_CONSTANT_POOL_ENTRY);
081: }
082: char[] attributeName = constantPoolEntry.getUtf8Value();
083: if (equals(attributeName,
084: IAttributeNamesConstants.DEPRECATED)) {
085: this .isDeprecated = true;
086: this .attributes[attributesIndex++] = new ClassFileAttribute(
087: classFileBytes, constantPool, offset
088: + readOffset);
089: } else if (equals(attributeName,
090: IAttributeNamesConstants.SYNTHETIC)) {
091: this .isSynthetic = true;
092: this .attributes[attributesIndex++] = new ClassFileAttribute(
093: classFileBytes, constantPool, offset
094: + readOffset);
095: } else if (equals(attributeName,
096: IAttributeNamesConstants.CONSTANT_VALUE)) {
097: this .constantValueAttribute = new ConstantValueAttribute(
098: classFileBytes, constantPool, offset
099: + readOffset);
100: this .attributes[attributesIndex++] = this .constantValueAttribute;
101: } else if (equals(attributeName,
102: IAttributeNamesConstants.SIGNATURE)) {
103: this .attributes[attributesIndex++] = new SignatureAttribute(
104: classFileBytes, constantPool, offset
105: + readOffset);
106: } else if (equals(
107: attributeName,
108: IAttributeNamesConstants.RUNTIME_VISIBLE_ANNOTATIONS)) {
109: this .attributes[attributesIndex++] = new RuntimeVisibleAnnotationsAttribute(
110: classFileBytes, constantPool, offset
111: + readOffset);
112: } else if (equals(
113: attributeName,
114: IAttributeNamesConstants.RUNTIME_INVISIBLE_ANNOTATIONS)) {
115: this .attributes[attributesIndex++] = new RuntimeInvisibleAnnotationsAttribute(
116: classFileBytes, constantPool, offset
117: + readOffset);
118: } else {
119: this .attributes[attributesIndex++] = new ClassFileAttribute(
120: classFileBytes, constantPool, offset
121: + readOffset);
122: }
123: readOffset += (6 + u4At(classFileBytes, readOffset + 2,
124: offset));
125: }
126:
127: this .attributeBytes = readOffset;
128: }
129:
130: /**
131: * @see IFieldInfo#getAccessFlags()
132: */
133: public int getAccessFlags() {
134: return this .accessFlags;
135: }
136:
137: /**
138: * @see IFieldInfo#getAttributeCount()
139: */
140: public int getAttributeCount() {
141: return this .attributesCount;
142: }
143:
144: /**
145: * @see IFieldInfo#getAttributes()
146: */
147: public IClassFileAttribute[] getAttributes() {
148: return this .attributes;
149: }
150:
151: /**
152: * @see IFieldInfo#getConstantValueAttribute()
153: */
154: public IConstantValueAttribute getConstantValueAttribute() {
155: return this .constantValueAttribute;
156: }
157:
158: /**
159: * @see IFieldInfo#getDescriptor()
160: */
161: public char[] getDescriptor() {
162: return this .descriptor;
163: }
164:
165: /**
166: * @see IFieldInfo#getDescriptorIndex()
167: */
168: public int getDescriptorIndex() {
169: return this .descriptorIndex;
170: }
171:
172: /**
173: * @see IFieldInfo#getName()
174: */
175: public char[] getName() {
176: return this .name;
177: }
178:
179: /**
180: * @see IFieldInfo#getNameIndex()
181: */
182: public int getNameIndex() {
183: return this .nameIndex;
184: }
185:
186: /**
187: * @see IFieldInfo#hasConstantValueAttribute()
188: */
189: public boolean hasConstantValueAttribute() {
190: return this .constantValueAttribute != null;
191: }
192:
193: /**
194: * @see IFieldInfo#isDeprecated()
195: */
196: public boolean isDeprecated() {
197: return this .isDeprecated;
198: }
199:
200: /**
201: * @see IFieldInfo#isSynthetic()
202: */
203: public boolean isSynthetic() {
204: return this .isSynthetic;
205: }
206:
207: int sizeInBytes() {
208: return this.attributeBytes;
209: }
210: }
|