View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
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   * Exceptions class file attribute
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 + ExceptionsAttribute.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();
107         sb.append("Exceptions: ");
108         for (final CPClass exception : exceptions) {
109             sb.append(exception);
110             sb.append(' ');
111         }
112         return sb.toString();
113     }
114 
115     @Override
116     protected void writeBody(final DataOutputStream dos) throws IOException {
117         dos.writeShort(exceptionIndexes.length);
118         for (final int element : exceptionIndexes) {
119             dos.writeShort(element);
120         }
121     }
122 
123 }