1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.jdo.impl.enhancer.classfile;
20
21 import java.io.*;
22 import java.util.Stack;
23
24 /***
25 * ConstantValueAttribute represents a constant value attribute
26 * in a class file. These attributes are used as initialization
27 * values for static fields.
28 */
29 public class ConstantValueAttribute extends ClassAttribute {
30
31 public static final String expectedAttrName = "ConstantValue";
32
33
34 private ConstValue constantValue;
35
36
37
38 public ConstValue value() {
39 return constantValue;
40 }
41
42 /***
43 * Construct a constant value attribute
44 */
45 public ConstantValueAttribute(ConstUtf8 attrName, ConstValue value) {
46 super(attrName);
47 constantValue = value;
48 }
49
50 /***
51 * Compares this instance with another for structural equality.
52 */
53
54 public boolean isEqual(Stack msg, Object obj) {
55 if (!(obj instanceof ConstantValueAttribute)) {
56 msg.push("obj/obj.getClass() = "
57 + (obj == null ? null : obj.getClass()));
58 msg.push("this.getClass() = "
59 + this.getClass());
60 return false;
61 }
62 ConstantValueAttribute other = (ConstantValueAttribute)obj;
63
64 if (!super.isEqual(msg, other)) {
65 return false;
66 }
67
68 if (!this.constantValue.isEqual(msg, other.constantValue)) {
69 msg.push(String.valueOf("constantValue = "
70 + other.constantValue));
71 msg.push(String.valueOf("constantValue = "
72 + this.constantValue));
73 return false;
74 }
75 return true;
76 }
77
78 /* package local methods *//package-summary/html">class="comment"> package local methods *//package-summary.html">
79
80 static ConstantValueAttribute read(ConstUtf8 attrName,
81 DataInputStream data, ConstantPool pool)
82 throws IOException {
83 int index = 0;
84 index = data.readUnsignedShort();
85
86 return new ConstantValueAttribute(attrName,
87 (ConstValue) pool.constantAt(index));
88 }
89
90 void write(DataOutputStream out) throws IOException {
91 out.writeShort(attrName().getIndex());
92 out.writeInt(2);
93 out.writeShort(constantValue.getIndex());
94 }
95
96 void print(PrintStream out, int indent) {
97 ClassPrint.spaces(out, indent);
98 out.println("ConstantValue: " + constantValue.toString());
99 }
100 }
101