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