1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.harmony.unpack200.bytecode;
20
21 import java.io.DataOutputStream;
22 import java.io.IOException;
23 import java.util.Objects;
24
25
26
27
28 public class ConstantValueAttribute extends Attribute {
29
30 private static CPUTF8 attributeName;
31
32 public static void setAttributeName(final CPUTF8 cpUTF8Value) {
33 attributeName = cpUTF8Value;
34 }
35
36 private int constantIndex;
37
38 private final ClassFileEntry entry;
39
40
41
42
43
44
45 public ConstantValueAttribute(final ClassFileEntry entry) {
46 super(attributeName);
47 this.entry = Objects.requireNonNull(entry, "entry");
48 }
49
50 @Override
51 public boolean equals(final Object obj) {
52 if (this == obj) {
53 return true;
54 }
55 if (!super.equals(obj) || this.getClass() != obj.getClass()) {
56 return false;
57 }
58 final ConstantValueAttribute other = (ConstantValueAttribute) obj;
59 return Objects.equals(entry, other.entry);
60 }
61
62 @Override
63 protected int getLength() {
64 return 2;
65 }
66
67 @Override
68 protected ClassFileEntry[] getNestedClassFileEntries() {
69 return new ClassFileEntry[] { getAttributeName(), entry };
70 }
71
72 @Override
73 public int hashCode() {
74 final int PRIME = 31;
75 int result = super.hashCode();
76 result = PRIME * result + (entry == null ? 0 : entry.hashCode());
77 return result;
78 }
79
80 @Override
81 protected void resolve(final ClassConstantPool pool) {
82 super.resolve(pool);
83 entry.resolve(pool);
84 this.constantIndex = pool.indexOf(entry);
85 }
86
87 @Override
88 public String toString() {
89 return "Constant:" + entry;
90 }
91
92 @Override
93 protected void writeBody(final DataOutputStream dos) throws IOException {
94 dos.writeShort(constantIndex);
95 }
96
97 }