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 abstract class CPRef extends ConstantPoolEntry {
29
30 CPClass className;
31 transient int classNameIndex;
32
33 protected CPNameAndType nameAndType;
34 transient int nameAndTypeIndex;
35
36 protected String cachedToString;
37
38
39
40
41
42
43
44
45
46
47 public CPRef(final byte type, final CPClass className, final CPNameAndType descriptor, final int globalIndex) {
48 super(type, globalIndex);
49 this.className = Objects.requireNonNull(className, "className");
50 this.nameAndType = Objects.requireNonNull(descriptor, "descriptor");
51 }
52
53 @Override
54 public boolean equals(final Object obj) {
55 if (this == obj) {
56 return true;
57 }
58 if (obj == null || getClass() != obj.getClass() || hashCode() != obj.hashCode()) {
59 return false;
60 }
61 final CPRef other = (CPRef) obj;
62 return Objects.equals(className, other.className)
63 && Objects.equals(nameAndType, other.nameAndType);
64 }
65
66 @Override
67 protected ClassFileEntry[] getNestedClassFileEntries() {
68 final ClassFileEntry[] entries = new ClassFileEntry[2];
69 entries[0] = className;
70 entries[1] = nameAndType;
71 return entries;
72 }
73
74 @Override
75 protected void resolve(final ClassConstantPool pool) {
76 super.resolve(pool);
77 nameAndTypeIndex = pool.indexOf(nameAndType);
78 classNameIndex = pool.indexOf(className);
79 }
80
81 @Override
82 public String toString() {
83 if (cachedToString == null) {
84 final String type;
85 if (getTag() == CP_Fieldref) {
86 type = "FieldRef";
87 } else if (getTag() == CP_Methodref) {
88 type = "MethoddRef";
89 } else if (getTag() == CP_InterfaceMethodref) {
90 type = "InterfaceMethodRef";
91 } else {
92 type = "unknown";
93 }
94 cachedToString = type + ": " + className + "#" + nameAndType;
95 }
96 return cachedToString;
97 }
98
99 @Override
100 protected void writeBody(final DataOutputStream dos) throws IOException {
101 dos.writeShort(classNameIndex);
102 dos.writeShort(nameAndTypeIndex);
103 }
104
105 }