KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > bytecode > ConstantValueAttr


1 // Copyright (c) 2000 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.bytecode;
5 import java.io.*;
6
7
8 /* Represents the contents of a standard "ConstantValue" attribute.
9  * @author Per Bothner
10  */

11
12 public class ConstantValueAttr extends Attribute
13 {
14   Object JavaDoc value;
15   int value_index;
16
17   public Object JavaDoc getValue(ConstantPool cpool)
18   {
19     if (value != null)
20       return value;
21     CpoolEntry entry = cpool.getPoolEntry(value_index);
22     switch (entry.getTag())
23       {
24       case ConstantPool.STRING:
25     value = ((CpoolString) entry).getString().getString();
26     break;
27       case ConstantPool.INTEGER:
28     value = new Integer JavaDoc(((CpoolValue1) entry).value);
29     break;
30       case ConstantPool.LONG:
31     value = new Long JavaDoc(((CpoolValue2) entry).value);
32     break;
33       case ConstantPool.FLOAT:
34     float f = Float.intBitsToFloat(((CpoolValue1) entry).value);
35     value = new Float JavaDoc(f);
36     break;
37       case ConstantPool.DOUBLE:
38     double d = Double.longBitsToDouble(((CpoolValue2) entry).value);
39     value = new Double JavaDoc(d);
40     break;
41       }
42     return value;
43   }
44
45   public ConstantValueAttr (Object JavaDoc value)
46   {
47     super("ConstantValue");
48     this.value = value;
49   }
50
51   public ConstantValueAttr (int index)
52   {
53     super("ConstantValue");
54     this.value_index = index;
55   }
56
57   public void assignConstants (ClassType cl)
58   {
59     super.assignConstants(cl);
60     if (value_index == 0)
61       {
62     ConstantPool cpool = cl.getConstants();
63     CpoolEntry entry = null;
64     if (value instanceof String JavaDoc)
65       entry = cpool.addString((String JavaDoc) value);
66     else if (value instanceof Integer JavaDoc)
67       entry = cpool.addInt(((Integer JavaDoc) value).intValue());
68     else if (value instanceof Long JavaDoc)
69       entry = cpool.addLong(((Long JavaDoc) value).longValue());
70     else if (value instanceof Float JavaDoc)
71       entry = cpool.addFloat(((Float JavaDoc) value).floatValue());
72     else if (value instanceof Long JavaDoc)
73       entry = cpool.addDouble(((Double JavaDoc) value).doubleValue());
74     value_index = entry.getIndex();
75       }
76   }
77
78   public final int getLength() { return 2; }
79
80   public void write (DataOutputStream dstr) throws java.io.IOException JavaDoc
81   {
82     dstr.writeShort(value_index);
83   }
84
85   public void print (ClassTypeWriter dst)
86   {
87     dst.print("Attribute \"");
88     dst.print(getName());
89     dst.print("\", length:");
90     dst.print(getLength());
91     dst.print(", value: ");
92     if (value_index == 0)
93       {
94     Object JavaDoc value = getValue(dst.ctype.constants);
95     if (value instanceof String JavaDoc)
96       dst.printQuotedString((String JavaDoc) value);
97     else
98       dst.print(value);
99       }
100     else
101       {
102     if (dst.printConstants)
103       {
104         dst.print(value_index);
105         dst.print('=');
106       }
107     CpoolEntry entry = dst.ctype.constants.getPoolEntry(value_index);
108     entry.print(dst, 1);
109       }
110     dst.println();
111   }
112 }
113
Popular Tags