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