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.IBinaryField;
15 import net.sourceforge.phpdt.internal.compiler.impl.BooleanConstant;
16 import net.sourceforge.phpdt.internal.compiler.impl.ByteConstant;
17 import net.sourceforge.phpdt.internal.compiler.impl.CharConstant;
18 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
19 import net.sourceforge.phpdt.internal.compiler.impl.DoubleConstant;
20 import net.sourceforge.phpdt.internal.compiler.impl.FloatConstant;
21 import net.sourceforge.phpdt.internal.compiler.impl.IntConstant;
22 import net.sourceforge.phpdt.internal.compiler.impl.LongConstant;
23 import net.sourceforge.phpdt.internal.compiler.impl.ShortConstant;
24 import net.sourceforge.phpdt.internal.compiler.impl.StringConstant;
25 import net.sourceforge.phpdt.internal.compiler.lookup.TypeIds;
26 import net.sourceforge.phpdt.internal.compiler.util.CharOperation;
28 public class FieldInfo extends ClassFileStruct implements AttributeNamesConstants, IBinaryField, Comparable, TypeIds {
29 private Constant constant;
30 private boolean isDeprecated;
31 private boolean isSynthetic;
32 private int[] constantPoolOffsets;
33 private int accessFlags;
35 private char[] signature;
36 private int attributesCount;
37 private int attributeBytes;
38 private Object wrappedConstantValue;
40 * @param classFileBytes byte[]
41 * @param offsets int[]
44 public FieldInfo (byte classFileBytes[], int offsets[], int offset) throws ClassFormatException {
45 super(classFileBytes, offset);
46 constantPoolOffsets = offsets;
48 int attributesCount = u2At(6);
50 for (int i = 0; i < attributesCount; i++) {
51 readOffset += (6 + u4At(readOffset + 2));
53 attributeBytes = readOffset;
56 * Return the constant of the field.
57 * Return org.eclipse.jdt.internal.compiler.impl.Constant.NotAConstant if there is none.
58 * @return org.eclipse.jdt.internal.compiler.impl.Constant
60 public Constant getConstant() {
61 if (constant == null) {
63 readConstantAttribute();
68 * Answer an int whose bits are set according the access constants
69 * defined by the VM spec.
70 * Set the AccDeprecated and AccSynthetic bits if necessary
73 public int getModifiers() {
74 if (accessFlags == -1) {
75 // compute the accessflag. Don't forget the deprecated attribute
76 accessFlags = u2At(0);
77 readDeprecatedAndSyntheticAttributes();
79 accessFlags |= AccDeprecated;
82 accessFlags |= AccSynthetic;
88 * Answer the name of the field.
91 public char[] getName() {
94 int utf8Offset = constantPoolOffsets[u2At(2)] - structOffset;
95 name = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
100 * Answer the resolved name of the receiver's type in the
101 * class file format as specified in section 4.3.2 of the Java 2 VM spec.
104 * - java.lang.String is Ljava/lang/String;
106 * - a 2 dimensional array of strings is [[Ljava/lang/String;
107 * - an array of floats is [F
110 public char[] getTypeName() {
111 if (signature == null) {
112 // read the signature
113 int utf8Offset = constantPoolOffsets[u2At(4)] - structOffset;
114 signature = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
119 * Return a wrapper that contains the constant of the field.
120 * Throws a java.ibm.compiler.java.classfmt.ClassFormatException in case the signature is
121 * incompatible with the constant tag.
123 * @exception java.ibm.compiler.java.classfmt.ClassFormatException
124 * @return java.lang.Object
126 public Object getWrappedConstantValue() throws ClassFormatException {
128 if (this.wrappedConstantValue == null) {
130 Constant constant = getConstant();
131 switch (constant.typeID()) {
133 this.wrappedConstantValue = new Integer(constant.intValue());
136 this.wrappedConstantValue = new Byte(constant.byteValue());
139 this.wrappedConstantValue = new Short(constant.shortValue());
142 this.wrappedConstantValue = new Character(constant.charValue());
145 this.wrappedConstantValue = new Float(constant.floatValue());
148 this.wrappedConstantValue = new Double(constant.doubleValue());
151 this.wrappedConstantValue = new Boolean(constant.booleanValue());
154 this.wrappedConstantValue = new Long(constant.longValue());
157 this.wrappedConstantValue = constant.stringValue();
161 return this.wrappedConstantValue;
164 * Return true if the field has a constant value attribute, false otherwise.
167 public boolean hasConstant() {
168 return getConstant() != Constant.NotAConstant;
171 * Return true if the field is a synthetic field, false otherwise.
174 public boolean isSynthetic() {
175 return (getModifiers() & AccSynthetic) != 0;
178 private void readConstantAttribute() {
179 int attributesCount = u2At(6);
181 boolean isConstant = false;
182 for (int i = 0; i < attributesCount; i++) {
183 int utf8Offset = constantPoolOffsets[u2At(readOffset)] - structOffset;
184 char[] attributeName = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
186 .equals(attributeName, ConstantValueName)) {
188 // read the right constant
189 int relativeOffset = constantPoolOffsets[u2At(readOffset + 6)] - structOffset;
190 switch (u1At(relativeOffset)) {
192 char[] sign = getTypeName();
193 if (sign.length == 1) {
195 case 'Z' : // boolean constant
196 constant = new BooleanConstant(i4At(relativeOffset + 1) == 1);
198 case 'I' : // integer constant
199 constant = new IntConstant(i4At(relativeOffset + 1));
201 case 'C' : // char constant
202 constant = new CharConstant((char) i4At(relativeOffset + 1));
204 case 'B' : // byte constant
205 constant = new ByteConstant((byte) i4At(relativeOffset + 1));
207 case 'S' : // short constant
208 constant = new ShortConstant((short) i4At(relativeOffset + 1));
211 constant = Constant.NotAConstant;
214 constant = Constant.NotAConstant;
218 constant = new FloatConstant(floatAt(relativeOffset + 1));
221 constant = new DoubleConstant(doubleAt(relativeOffset + 1));
224 constant = new LongConstant(i8At(relativeOffset + 1));
227 utf8Offset = constantPoolOffsets[u2At(relativeOffset + 1)] - structOffset;
230 String.valueOf(utf8At(utf8Offset + 3, u2At(utf8Offset + 1))));
234 readOffset += (6 + u4At(readOffset + 2));
237 constant = Constant.NotAConstant;
240 private void readDeprecatedAndSyntheticAttributes() {
241 int attributesCount = u2At(6);
243 for (int i = 0; i < attributesCount; i++) {
244 int utf8Offset = constantPoolOffsets[u2At(readOffset)] - structOffset;
245 char[] attributeName = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
246 if (CharOperation.equals(attributeName, DeprecatedName)) {
248 } else if (CharOperation.equals(attributeName, SyntheticName)) {
251 readOffset += (6 + u4At(readOffset + 2));
255 * Answer the size of the receiver in bytes.
259 public int sizeInBytes() {
260 return attributeBytes;
262 public void throwFormatException() throws ClassFormatException {
263 throw new ClassFormatException(ClassFormatException.ErrBadFieldInfo);
265 public String toString() {
266 StringBuffer buffer = new StringBuffer(this.getClass().getName());
267 int modifiers = getModifiers();
269 .append("{") //$NON-NLS-1$
271 ((modifiers & AccDeprecated) != 0 ? "deprecated " : "") //$NON-NLS-1$ //$NON-NLS-2$
272 + ((modifiers & 0x0001) == 1 ? "public " : "") //$NON-NLS-1$ //$NON-NLS-2$
273 + ((modifiers & 0x0002) == 0x0002 ? "private " : "") //$NON-NLS-1$ //$NON-NLS-2$
274 + ((modifiers & 0x0004) == 0x0004 ? "protected " : "") //$NON-NLS-1$ //$NON-NLS-2$
275 + ((modifiers & 0x0008) == 0x000008 ? "static " : "") //$NON-NLS-1$ //$NON-NLS-2$
276 + ((modifiers & 0x0010) == 0x0010 ? "final " : "") //$NON-NLS-1$ //$NON-NLS-2$
277 + ((modifiers & 0x0040) == 0x0040 ? "volatile " : "") //$NON-NLS-1$ //$NON-NLS-2$
278 + ((modifiers & 0x0080) == 0x0080 ? "transient " : "")) //$NON-NLS-1$ //$NON-NLS-2$
279 .append(getTypeName())
280 .append(" ") //$NON-NLS-1$
282 .append(" ") //$NON-NLS-1$
283 .append(getConstant())
284 .append("}") //$NON-NLS-1$
288 public int compareTo(Object o) {
289 if (!(o instanceof FieldInfo)) {
290 throw new ClassCastException();
292 return new String(this.getName()).compareTo(new String(((FieldInfo) o).getName()));
295 * This method is used to fully initialize the contents of the receiver. All methodinfos, fields infos
296 * will be therefore fully initialized and we can get rid of the bytes.
305 protected void reset() {
306 this.constantPoolOffsets = null;