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