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