1 cananian 1.1.2.2 // AttributeConstantValue.java, created Mon Jan 18 22:44:34 1999 by cananian
 2 cananian 1.1.2.1 // Copyright (C) 1998 C. Scott Ananian <cananian@alumni.princeton.edu>
 3 cananian 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details.
 4 cananian 1.1.2.1 package harpoon.IR.RawClass;
 5 cananian 1.1.2.1 
 6 cananian 1.1.2.1 /**
 7 cananian 1.1.2.1  * The <code>ConstantValue</code> attribute is a fixed-length
 8 cananian 1.1.2.1  * attribute used in the <code>attributes</code> table of the
 9 cananian 1.1.2.1  * <code>field_info</code> structures. A <code>ConstantValue</code>
10 cananian 1.1.2.1  * attribute represents the value of a constant field that must be
11 cananian 1.1.2.1  * (explicitly or implicitly) <code>static</code>; that is, the
12 cananian 1.1.2.1  * <code>ACC_STATIC</code> bit in the <code>flags</code> item of the
13 cananian 1.1.2.1  * <code>field_info</code> structure must be set.  The field is not
14 cananian 1.1.2.1  * required to be <code>final</code>.  There can be no more than one
15 cananian 1.1.2.1  * <code>ConstantValue</code> attribute in the <code>attributes</code>
16 cananian 1.1.2.1  * table of a given <code>field_info</code> structure.  The constant
17 cananian 1.1.2.1  * field represented by the <code>field_info</code> structure is
18 cananian 1.1.2.1  * assigned the value referenced by its <code>ConstantValue</code>
19 cananian 1.1.2.1  * attribute as part of its initialization.<p>
20 cananian 1.1.2.1  * <strong>CORRECTION FROM ERRATA:</strong>
21 cananian 1.1.2.1  * If a non-static field has a <code>ConstantValue</code> attribute, then
22 cananian 1.1.2.1  * the attribute is silently ignored.
23 cananian 1.1.2.1  *
24 cananian 1.1.2.1  * @author  C. Scott Ananian <cananian@alumni.princeton.edu>
25 cananian 1.3      * @version $Id: AttributeConstantValue.java,v 1.3 2003/09/05 21:45:15 cananian Exp $
26 cananian 1.1.2.1  * @see Attribute
27 cananian 1.1.2.1  * @see FieldInfo
28 cananian 1.1.2.1  * @see ClassFile
29 cananian 1.1.2.1  */
30 cananian 1.1.2.1 public class AttributeConstantValue extends Attribute {
31 cananian 1.3       /** The string naming this <code>Attribute</code> type. */
32 cananian 1.3       public static final String ATTRIBUTE_NAME = "ConstantValue";
33 cananian 1.1.2.1   /** The value of the <code>constantvalue_index</code> must be a
34 cananian 1.1.2.1       valid index into the <code>constant_pool</code> table.  The
35 cananian 1.1.2.1       <code>constant_pool</code> entry at that index must give the
36 cananian 1.1.2.1       constant value represented by this attribute. <p>  The
37 cananian 1.1.2.1       <code>constant_pool</code> entry must be of a type appropriate
38 cananian 1.1.2.1       to the field. */
39 cananian 1.1.2.1   public int constantvalue_index;
40 cananian 1.1.2.1 
41 cananian 1.1.2.1   /** Constructor. */
42 cananian 1.1.2.1   AttributeConstantValue(ClassFile parent, ClassDataInputStream in,
43 cananian 1.1.2.1                          int attribute_name_index) throws java.io.IOException {
44 cananian 1.1.2.1     super(parent, attribute_name_index);
45 cananian 1.1.2.1     long attribute_length = in.read_u4();
46 cananian 1.1.2.1     if (attribute_length != 2)
47 cananian 1.1.2.1       throw new ClassDataException("ConstantValue attribute with length " +
48 cananian 1.1.2.1                                    attribute_length);
49 cananian 1.1.2.1 
50 cananian 1.1.2.1     constantvalue_index = in.read_u2();
51 cananian 1.3         assert ATTRIBUTE_NAME.equals(attribute_name());
52 cananian 1.1.2.1   }
53 cananian 1.1.2.1   /** Constructor. */
54 cananian 1.1.2.1   public AttributeConstantValue(ClassFile parent, int attribute_name_index,
55 cananian 1.1.2.1                                 int constantvalue_index) {
56 cananian 1.1.2.1     super(parent, attribute_name_index);
57 cananian 1.1.2.1     this.constantvalue_index = constantvalue_index;
58 cananian 1.3         assert ATTRIBUTE_NAME.equals(attribute_name());
59 cananian 1.1.2.1   }
60 cananian 1.1.2.1 
61 cananian 1.1.2.1   public long attribute_length() { return 2; }
62 cananian 1.1.2.1   
63 cananian 1.1.2.1   // convenience.
64 cananian 1.1.2.1   public Constant constantvalue_index()
65 cananian 1.1.2.1   { return parent.constant_pool[constantvalue_index]; }
66 cananian 1.1.2.1 
67 cananian 1.1.2.1   /** Write to bytecode stream. */
68 cananian 1.1.2.1   public void write(ClassDataOutputStream out) throws java.io.IOException {
69 cananian 1.1.2.1     out.write_u2(attribute_name_index);
70 cananian 1.1.2.1     out.write_u4(attribute_length());
71 cananian 1.1.2.1     out.write_u2(constantvalue_index);
72 cananian 1.1.2.1   }
73 cananian 1.1.2.1 
74 cananian 1.1.2.1   /** Pretty-print the contents of this attribute. 
75 cananian 1.1.2.1    *  @param indent the indentation level to use.
76 cananian 1.1.2.1    */
77 cananian 1.1.2.1   public void print(java.io.PrintWriter pw, int indent) {
78 cananian 1.1.2.1     int in = indent;
79 cananian 1.1.2.1     indent(pw, in, "ConstantValue Attribute:");
80 cananian 1.1.2.1     indent(pw, in+1, "Value: " +
81 cananian 1.1.2.1            constantvalue_index().toString() + " {"+constantvalue_index+"}");
82 cananian 1.1.2.1   }
83 cananian 1.2     }