1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.compiler.classfmt;
13 import org.eclipse.jdt.core.compiler.CharOperation;
14 import org.eclipse.jdt.internal.compiler.codegen.AttributeNamesConstants;
15 import org.eclipse.jdt.internal.compiler.env.IBinaryField;
16 import org.eclipse.jdt.internal.compiler.impl.BooleanConstant;
17 import org.eclipse.jdt.internal.compiler.impl.ByteConstant;
18 import org.eclipse.jdt.internal.compiler.impl.CharConstant;
19 import org.eclipse.jdt.internal.compiler.impl.Constant;
20 import org.eclipse.jdt.internal.compiler.impl.DoubleConstant;
21 import org.eclipse.jdt.internal.compiler.impl.FloatConstant;
22 import org.eclipse.jdt.internal.compiler.impl.IntConstant;
23 import org.eclipse.jdt.internal.compiler.impl.LongConstant;
24 import org.eclipse.jdt.internal.compiler.impl.ShortConstant;
25 import org.eclipse.jdt.internal.compiler.impl.StringConstant;
26 import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
27 import org.eclipse.jdt.internal.compiler.util.Util;
29 public class FieldInfo extends ClassFileStruct implements AttributeNamesConstants, IBinaryField, Comparable, TypeIds {
30 private Constant constant;
31 private boolean isDeprecated;
32 private boolean isSynthetic;
33 private int[] constantPoolOffsets;
34 private int accessFlags;
36 private char[] signature;
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) {
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 * @return java.lang.Object
122 public Object getWrappedConstantValue() {
124 if (this.wrappedConstantValue == null) {
126 Constant fieldConstant = getConstant();
127 switch (fieldConstant.typeID()) {
129 this.wrappedConstantValue = new Integer(fieldConstant.intValue());
132 this.wrappedConstantValue = new Byte(fieldConstant.byteValue());
135 this.wrappedConstantValue = new Short(fieldConstant.shortValue());
138 this.wrappedConstantValue = new Character(fieldConstant.charValue());
141 this.wrappedConstantValue = new Float(fieldConstant.floatValue());
144 this.wrappedConstantValue = new Double(fieldConstant.doubleValue());
147 this.wrappedConstantValue = Util.toBoolean(fieldConstant.booleanValue());
150 this.wrappedConstantValue = new Long(fieldConstant.longValue());
153 this.wrappedConstantValue = fieldConstant.stringValue();
157 return this.wrappedConstantValue;
160 * Return true if the field has a constant value attribute, false otherwise.
163 public boolean hasConstant() {
164 return getConstant() != Constant.NotAConstant;
167 * Return true if the field is a synthetic field, false otherwise.
170 public boolean isSynthetic() {
171 return (getModifiers() & AccSynthetic) != 0;
174 private void readConstantAttribute() {
175 int attributesCount = u2At(6);
177 boolean isConstant = false;
178 for (int i = 0; i < attributesCount; i++) {
179 int utf8Offset = constantPoolOffsets[u2At(readOffset)] - structOffset;
180 char[] attributeName = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
182 .equals(attributeName, ConstantValueName)) {
184 // read the right constant
185 int relativeOffset = constantPoolOffsets[u2At(readOffset + 6)] - structOffset;
186 switch (u1At(relativeOffset)) {
188 char[] sign = getTypeName();
189 if (sign.length == 1) {
191 case 'Z' : // boolean constant
192 constant = new BooleanConstant(i4At(relativeOffset + 1) == 1);
194 case 'I' : // integer constant
195 constant = new IntConstant(i4At(relativeOffset + 1));
197 case 'C' : // char constant
198 constant = new CharConstant((char) i4At(relativeOffset + 1));
200 case 'B' : // byte constant
201 constant = new ByteConstant((byte) i4At(relativeOffset + 1));
203 case 'S' : // short constant
204 constant = new ShortConstant((short) i4At(relativeOffset + 1));
207 constant = Constant.NotAConstant;
210 constant = Constant.NotAConstant;
214 constant = new FloatConstant(floatAt(relativeOffset + 1));
217 constant = new DoubleConstant(doubleAt(relativeOffset + 1));
220 constant = new LongConstant(i8At(relativeOffset + 1));
223 utf8Offset = constantPoolOffsets[u2At(relativeOffset + 1)] - structOffset;
226 String.valueOf(utf8At(utf8Offset + 3, u2At(utf8Offset + 1))));
230 readOffset += (6 + u4At(readOffset + 2));
233 constant = Constant.NotAConstant;
236 private void readDeprecatedAndSyntheticAttributes() {
237 int attributesCount = u2At(6);
239 for (int i = 0; i < attributesCount; i++) {
240 int utf8Offset = constantPoolOffsets[u2At(readOffset)] - structOffset;
241 char[] attributeName = utf8At(utf8Offset + 3, u2At(utf8Offset + 1));
242 if (CharOperation.equals(attributeName, DeprecatedName)) {
244 } else if (CharOperation.equals(attributeName, SyntheticName)) {
247 readOffset += (6 + u4At(readOffset + 2));
251 * Answer the size of the receiver in bytes.
255 public int sizeInBytes() {
256 return attributeBytes;
258 public void throwFormatException() throws ClassFormatException {
259 throw new ClassFormatException(ClassFormatException.ErrBadFieldInfo);
261 public String toString() {
262 StringBuffer buffer = new StringBuffer(this.getClass().getName());
263 int modifiers = getModifiers();
265 .append("{") //$NON-NLS-1$
267 ((modifiers & AccDeprecated) != 0 ? "deprecated " : "") //$NON-NLS-1$ //$NON-NLS-2$
268 + ((modifiers & 0x0001) == 1 ? "public " : "") //$NON-NLS-1$ //$NON-NLS-2$
269 + ((modifiers & 0x0002) == 0x0002 ? "private " : "") //$NON-NLS-1$ //$NON-NLS-2$
270 + ((modifiers & 0x0004) == 0x0004 ? "protected " : "") //$NON-NLS-1$ //$NON-NLS-2$
271 + ((modifiers & 0x0008) == 0x000008 ? "static " : "") //$NON-NLS-1$ //$NON-NLS-2$
272 + ((modifiers & 0x0010) == 0x0010 ? "final " : "") //$NON-NLS-1$ //$NON-NLS-2$
273 + ((modifiers & 0x0040) == 0x0040 ? "volatile " : "") //$NON-NLS-1$ //$NON-NLS-2$
274 + ((modifiers & 0x0080) == 0x0080 ? "transient " : "")) //$NON-NLS-1$ //$NON-NLS-2$
275 .append(getTypeName())
276 .append(" ") //$NON-NLS-1$
278 .append(" ") //$NON-NLS-1$
279 .append(getConstant())
280 .append("}") //$NON-NLS-1$
284 public int compareTo(Object o) {
285 if (!(o instanceof FieldInfo)) {
286 throw new ClassCastException();
288 return new String(this.getName()).compareTo(new String(((FieldInfo) o).getName()));
291 * This method is used to fully initialize the contents of the receiver. All methodinfos, fields infos
292 * will be therefore fully initialized and we can get rid of the bytes.
301 protected void reset() {
302 this.constantPoolOffsets = null;