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.Arrays;
22
23
24
25
26 public class ExceptionsAttribute extends Attribute {
27
28 private static CPUTF8 attributeName;
29
30 private static int hashCode(final Object[] array) {
31 final int prime = 31;
32 if (array == null) {
33 return 0;
34 }
35 int result = 1;
36 for (final Object element : array) {
37 result = prime * result + (element == null ? 0 : element.hashCode());
38 }
39 return result;
40 }
41
42 public static void setAttributeName(final CPUTF8 cpUTF8Value) {
43 attributeName = cpUTF8Value;
44 }
45
46 private transient int[] exceptionIndexes;
47
48 private final CPClass[] exceptions;
49
50 public ExceptionsAttribute(final CPClass[] exceptions) {
51 super(attributeName);
52 this.exceptions = exceptions;
53 }
54
55 @Override
56 public boolean equals(final Object obj) {
57 if (this == obj) {
58 return true;
59 }
60 if (!super.equals(obj)) {
61 return false;
62 }
63 if (getClass() != obj.getClass()) {
64 return false;
65 }
66 final ExceptionsAttribute other = (ExceptionsAttribute) obj;
67 if (!Arrays.equals(exceptions, other.exceptions)) {
68 return false;
69 }
70 return true;
71 }
72
73 @Override
74 protected int getLength() {
75 return 2 + 2 * exceptions.length;
76 }
77
78 @Override
79 protected ClassFileEntry[] getNestedClassFileEntries() {
80 final ClassFileEntry[] result = new ClassFileEntry[exceptions.length + 1];
81 System.arraycopy(exceptions, 0, result, 0, exceptions.length);
82 result[exceptions.length] = getAttributeName();
83 return result;
84 }
85
86 @Override
87 public int hashCode() {
88 final int prime = 31;
89 int result = super.hashCode();
90 result = prime * result + hashCode(exceptions);
91 return result;
92 }
93
94 @Override
95 protected void resolve(final ClassConstantPool pool) {
96 super.resolve(pool);
97 exceptionIndexes = new int[exceptions.length];
98 for (int i = 0; i < exceptions.length; i++) {
99 exceptions[i].resolve(pool);
100 exceptionIndexes[i] = pool.indexOf(exceptions[i]);
101 }
102 }
103
104 @Override
105 public String toString() {
106 final StringBuilder sb = new StringBuilder("Exceptions: ");
107 for (final CPClass exception : exceptions) {
108 sb.append(exception);
109 sb.append(' ');
110 }
111 return sb.toString();
112 }
113
114 @Override
115 protected void writeBody(final DataOutputStream dos) throws IOException {
116 dos.writeShort(exceptionIndexes.length);
117 for (final int element : exceptionIndexes) {
118 dos.writeShort(element);
119 }
120 }
121
122 }