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.Collections;
22 import java.util.List;
23 import java.util.Objects;
24
25
26
27
28 public class CPMember extends ClassFileEntry {
29
30 List<Attribute> attributes;
31 short flags;
32 CPUTF8 name;
33 transient int nameIndex;
34 protected final CPUTF8 descriptor;
35 transient int descriptorIndex;
36
37
38
39
40
41
42
43
44
45
46 public CPMember(final CPUTF8 name, final CPUTF8 descriptor, final long flags, final List<Attribute> attributes) {
47 this.name = Objects.requireNonNull(name, "name");
48 this.descriptor = Objects.requireNonNull(descriptor, "descriptor");
49 this.flags = (short) flags;
50 this.attributes = attributes == null ? Collections.EMPTY_LIST : attributes;
51 }
52
53 @Override
54 protected void doWrite(final DataOutputStream dos) throws IOException {
55 dos.writeShort(flags);
56 dos.writeShort(nameIndex);
57 dos.writeShort(descriptorIndex);
58 final int attributeCount = attributes.size();
59 dos.writeShort(attributeCount);
60 for (int i = 0; i < attributeCount; i++) {
61 final Attribute attribute = attributes.get(i);
62 attribute.doWrite(dos);
63 }
64 }
65
66 @Override
67 public boolean equals(final Object obj) {
68 if (this == obj) {
69 return true;
70 }
71 if (obj == null) {
72 return false;
73 }
74 if (getClass() != obj.getClass()) {
75 return false;
76 }
77 final CPMember other = (CPMember) obj;
78 if (!attributes.equals(other.attributes)) {
79 return false;
80 }
81 if (!descriptor.equals(other.descriptor)) {
82 return false;
83 }
84 if (flags != other.flags) {
85 return false;
86 }
87 if (!name.equals(other.name)) {
88 return false;
89 }
90 return true;
91 }
92
93 @Override
94 protected ClassFileEntry[] getNestedClassFileEntries() {
95 final int attributeCount = attributes.size();
96 final ClassFileEntry[] entries = new ClassFileEntry[attributeCount + 2];
97 entries[0] = name;
98 entries[1] = descriptor;
99 for (int i = 0; i < attributeCount; i++) {
100 entries[i + 2] = attributes.get(i);
101 }
102 return entries;
103 }
104
105 @Override
106 public int hashCode() {
107 final int PRIME = 31;
108 int result = 1;
109 result = PRIME * result + attributes.hashCode();
110 result = PRIME * result + descriptor.hashCode();
111 result = PRIME * result + flags;
112 result = PRIME * result + name.hashCode();
113 return result;
114 }
115
116 @Override
117 protected void resolve(final ClassConstantPool pool) {
118 super.resolve(pool);
119 nameIndex = pool.indexOf(name);
120 descriptorIndex = pool.indexOf(descriptor);
121 attributes.forEach(attribute -> attribute.resolve(pool));
122 }
123
124 @Override
125 public String toString() {
126 return "CPMember: " + name + "(" + descriptor + ")";
127 }
128
129 }