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 return Objects.equals(attributes, other.attributes)
79 && Objects.equals(descriptor, other.descriptor)
80 && flags == other.flags
81 && Objects.equals(name, other.name);
82 }
83
84 @Override
85 protected ClassFileEntry[] getNestedClassFileEntries() {
86 final int attributeCount = attributes.size();
87 final ClassFileEntry[] entries = new ClassFileEntry[attributeCount + 2];
88 entries[0] = name;
89 entries[1] = descriptor;
90 for (int i = 0; i < attributeCount; i++) {
91 entries[i + 2] = attributes.get(i);
92 }
93 return entries;
94 }
95
96 @Override
97 public int hashCode() {
98 final int PRIME = 31;
99 int result = 1;
100 result = PRIME * result + attributes.hashCode();
101 result = PRIME * result + descriptor.hashCode();
102 result = PRIME * result + flags;
103 result = PRIME * result + name.hashCode();
104 return result;
105 }
106
107 @Override
108 protected void resolve(final ClassConstantPool pool) {
109 super.resolve(pool);
110 nameIndex = pool.indexOf(name);
111 descriptorIndex = pool.indexOf(descriptor);
112 attributes.forEach(attribute -> attribute.resolve(pool));
113 }
114
115 @Override
116 public String toString() {
117 return "CPMember: " + name + "(" + descriptor + ")";
118 }
119
120 }