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 (this.hashCode() != obj.hashCode()) {
63 return false;
64 }
65 final CPRef other = (CPRef) obj;
66 if (!className.equals(other.className)) {
67 return false;
68 }
69 if (!nameAndType.equals(other.nameAndType)) {
70 return false;
71 }
72 return true;
73 }
74
75 @Override
76 protected ClassFileEntry[] getNestedClassFileEntries() {
77 final ClassFileEntry[] entries = new ClassFileEntry[2];
78 entries[0] = className;
79 entries[1] = nameAndType;
80 return entries;
81 }
82
83 @Override
84 protected void resolve(final ClassConstantPool pool) {
85 super.resolve(pool);
86 nameAndTypeIndex = pool.indexOf(nameAndType);
87 classNameIndex = pool.indexOf(className);
88 }
89
90 @Override
91 public String toString() {
92 if (cachedToString == null) {
93 String type;
94 if (getTag() == ConstantPoolEntry.CP_Fieldref) {
95 type = "FieldRef";
96 } else if (getTag() == ConstantPoolEntry.CP_Methodref) {
97 type = "MethoddRef";
98 } else if (getTag() == ConstantPoolEntry.CP_InterfaceMethodref) {
99 type = "InterfaceMethodRef";
100 } else {
101 type = "unknown";
102 }
103 cachedToString = type + ": " + className + "#" + nameAndType;
104 }
105 return cachedToString;
106 }
107
108 @Override
109 protected void writeBody(final DataOutputStream dos) throws IOException {
110 dos.writeShort(classNameIndex);
111 dos.writeShort(nameAndTypeIndex);
112 }
113
114 }