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