1/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved. 2 * 3 * This program and the accompanying materials are made available under 4 * the terms of the Common Public License v1.0 which accompanies this distribution, 5 * and is available at http://www.eclipse.org/legal/cpl-v10.html 6 * 7 * $Id: ConstantValueAttribute_info.java,v 1.1.1.1 2004/05/09 16:57:47 vlad_r Exp $ 8 */ 9package com.vladium.jcd.cls.attribute; 10 11import java.io.IOException; 12 13import com.vladium.jcd.cls.ClassDef; 14import com.vladium.jcd.cls.constant.CONSTANT_literal_info; 15import com.vladium.jcd.lib.UDataInputStream; 16import com.vladium.jcd.lib.UDataOutputStream; 17 18// ---------------------------------------------------------------------------- 19/** 20 * The ConstantValue attribute is a fixed-length attribute used in the attributes 21 * table of the {@link com.vladium.jcd.cls.Field_info} structures. A ConstantValue 22 * attribute represents the value of a constant field that must be (explicitly or 23 * implicitly) static; that is, the ACC_STATIC bit in the flags item of the 24 * Field_info structure must be set. The field is not required to be final. There 25 * can be no more than one ConstantValue attribute in the attributes table of a 26 * given Field_info structure. The constant field represented by the Field_info 27 * structure is assigned the value referenced by its ConstantValue attribute as 28 * part of its initialization.<P> 29 * 30 * The ConstantValue attribute has the format 31 * <PRE> 32 * ConstantValue_attribute { 33 * u2 attribute_name_index; 34 * u4 attribute_length; 35 * u2 constantvalue_index; 36 * } 37 * </PRE> 38 * 39 * The value of the constantvalue_index item must be a valid index into the constant 40 * pool table. The constant pool entry at that index must give the constant value 41 * represented by this attribute.<P> 42 * 43 * The constant pool entry must be of a type appropriate to the field, as shown below: 44 * 45 * <TABLE> 46 * <TR><TH>Field Type</TH> <TH>Entry Type</TH></TR> 47 * <TR><TD>long</TD> <TD>CONSTANT_Long</TD></TR> 48 * <TR><TD>float</TD> <TD>CONSTANT_Float</TD></TR> 49 * <TR><TD>double</TD> <TD>CONSTANT_Double</TD></TR> 50 * <TR><TD>int, short, char, byte, boolean</TD> <TD>CONSTANT_Integer</TD></TR> 51 * <TR><TD>java.lang.String</TD> <TD>CONSTANT_String</TD></TR> 52 * </TABLE> 53 * 54 * @author (C) 2001, Vlad Roubtsov 55 */ 56public 57final class ConstantValueAttribute_info extends Attribute_info 58{ 59 // public: ................................................................ 60 61 62 public int m_value_index; 63 64 65 public ConstantValueAttribute_info (final int attribute_name_index, final int value_index) 66 { 67 super (attribute_name_index, 2); 68 69 m_value_index = value_index; 70 } 71 72 public CONSTANT_literal_info getValue (final ClassDef cls) 73 { 74 return (CONSTANT_literal_info) cls.getConstants ().get (m_value_index); 75 } 76 77 public long length () 78 { 79 return 8; 80 } 81 82 // Visitor: 83 84 public void accept (final IAttributeVisitor visitor, final Object ctx) 85 { 86 visitor.visit (this, ctx); 87 } 88 89 public String toString () 90 { 91 // TODO: return more data here 92 return "ConstantValueAttribute_info: [attribute_name_index = " + m_name_index + ", attribute_length = " + m_attribute_length + ']'; 93 94 } 95 96 // Cloneable: 97 98 /** 99 * Performs a deep copy. 100 */ 101 public Object clone () 102 { 103 return super.clone (); 104 } 105 106 // IClassFormatOutput: 107 108 public void writeInClassFormat (final UDataOutputStream out) throws IOException 109 { 110 super.writeInClassFormat (out); 111 112 out.writeU2 (m_value_index); 113 } 114 115 // protected: ............................................................. 116 117 // package: ............................................................... 118 119 120 ConstantValueAttribute_info (final int attribute_name_index, final long attribute_length, 121 final UDataInputStream bytes) 122 throws IOException 123 { 124 super (attribute_name_index, attribute_length); 125 126 m_value_index = bytes.readU2 (); 127 if (DEBUG) System.out.println ("\tconstantvalue_index: " + m_value_index); 128 } 129 130 // private: ............................................................... 131 132 133 private static final boolean DEBUG = false; 134 135} // end of class 136// ---------------------------------------------------------------------------- 137