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(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 return Objects.equals(className, other.className)
56 && Objects.equals(nameAndType, other.nameAndType);
57 }
58
59 private void generateHashCode() {
60 hashCodeComputed = true;
61 final int PRIME = 31;
62 int result = 1;
63 result = PRIME * result + (className == null ? 0 : className.hashCode());
64 result = PRIME * result + (nameAndType == null ? 0 : nameAndType.hashCode());
65 cachedHashCode = result;
66 }
67
68 @Override
69 protected ClassFileEntry[] getNestedClassFileEntries() {
70 return new ClassFileEntry[] { className, nameAndType };
71 }
72
73 @Override
74 public int hashCode() {
75 if (!hashCodeComputed) {
76 generateHashCode();
77 }
78 return cachedHashCode;
79 }
80
81 @Override
82 protected void resolve(final ClassConstantPool pool) {
83 super.resolve(pool);
84 nameAndTypeIndex = pool.indexOf(nameAndType);
85 classNameIndex = pool.indexOf(className);
86 }
87
88 @Override
89 public String toString() {
90 return "FieldRef: " + className + "#" + nameAndType;
91 }
92
93 @Override
94 protected void writeBody(final DataOutputStream dos) throws IOException {
95 dos.writeShort(classNameIndex);
96 dos.writeShort(nameAndTypeIndex);
97 }
98
99 }