View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.Arrays;
24  
25  /**
26   * Exceptions class file attribute
27   */
28  public class ExceptionsAttribute extends Attribute {
29  
30      private static CPUTF8 attributeName;
31  
32      private static int hashCode(final Object[] array) {
33          final int prime = 31;
34          if (array == null) {
35              return 0;
36          }
37          int result = 1;
38          for (final Object element : array) {
39              result = prime * result + (element == null ? 0 : element.hashCode());
40          }
41          return result;
42      }
43  
44      public static void setAttributeName(final CPUTF8 cpUTF8Value) {
45          attributeName = cpUTF8Value;
46      }
47  
48      private transient int[] exceptionIndexes;
49  
50      private final CPClass[] exceptions;
51  
52      public ExceptionsAttribute(final CPClass[] exceptions) {
53          super(attributeName);
54          this.exceptions = exceptions;
55      }
56  
57      @Override
58      public boolean equals(final Object obj) {
59          if (this == obj) {
60              return true;
61          }
62          if (!super.equals(obj) || getClass() != obj.getClass()) {
63              return false;
64          }
65          final ExceptionsAttribute other = (ExceptionsAttribute) obj;
66          if (!Arrays.equals(exceptions, other.exceptions)) {
67              return false;
68          }
69          return true;
70      }
71  
72      @Override
73      protected int getLength() {
74          return 2 + 2 * exceptions.length;
75      }
76  
77      @Override
78      protected ClassFileEntry[] getNestedClassFileEntries() {
79          final ClassFileEntry[] result = new ClassFileEntry[exceptions.length + 1];
80          System.arraycopy(exceptions, 0, result, 0, exceptions.length);
81          result[exceptions.length] = getAttributeName();
82          return result;
83      }
84  
85      @Override
86      public int hashCode() {
87          final int prime = 31;
88          int result = super.hashCode();
89          result = prime * result + hashCode(exceptions);
90          return result;
91      }
92  
93      @Override
94      protected void resolve(final ClassConstantPool pool) {
95          super.resolve(pool);
96          exceptionIndexes = new int[exceptions.length];
97          for (int i = 0; i < exceptions.length; i++) {
98              exceptions[i].resolve(pool);
99              exceptionIndexes[i] = pool.indexOf(exceptions[i]);
100         }
101     }
102 
103     @Override
104     public String toString() {
105         final StringBuilder sb = new StringBuilder("Exceptions: ");
106         for (final CPClass exception : exceptions) {
107             sb.append(exception);
108             sb.append(' ');
109         }
110         return sb.toString();
111     }
112 
113     @Override
114     protected void writeBody(final DataOutputStream dos) throws IOException {
115         dos.writeShort(exceptionIndexes.length);
116         for (final int element : exceptionIndexes) {
117             dos.writeShort(element);
118         }
119     }
120 
121 }