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