ExceptionsAttribute.java

  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. import java.io.DataOutputStream;
  19. import java.io.IOException;
  20. import java.util.Arrays;

  21. /**
  22.  * Exceptions class file attribute
  23.  */
  24. public class ExceptionsAttribute extends Attribute {

  25.     private static CPUTF8 attributeName;

  26.     private static int hashCode(final Object[] array) {
  27.         final int prime = 31;
  28.         if (array == null) {
  29.             return 0;
  30.         }
  31.         int result = 1;
  32.         for (final Object element : array) {
  33.             result = prime * result + (element == null ? 0 : element.hashCode());
  34.         }
  35.         return result;
  36.     }

  37.     public static void setAttributeName(final CPUTF8 cpUTF8Value) {
  38.         attributeName = cpUTF8Value;
  39.     }

  40.     private transient int[] exceptionIndexes;

  41.     private final CPClass[] exceptions;

  42.     public ExceptionsAttribute(final CPClass[] exceptions) {
  43.         super(attributeName);
  44.         this.exceptions = exceptions;
  45.     }

  46.     @Override
  47.     public boolean equals(final Object obj) {
  48.         if (this == obj) {
  49.             return true;
  50.         }
  51.         if (!super.equals(obj)) {
  52.             return false;
  53.         }
  54.         if (getClass() != obj.getClass()) {
  55.             return false;
  56.         }
  57.         final ExceptionsAttribute other = (ExceptionsAttribute) obj;
  58.         if (!Arrays.equals(exceptions, other.exceptions)) {
  59.             return false;
  60.         }
  61.         return true;
  62.     }

  63.     @Override
  64.     protected int getLength() {
  65.         return 2 + 2 * exceptions.length;
  66.     }

  67.     @Override
  68.     protected ClassFileEntry[] getNestedClassFileEntries() {
  69.         final ClassFileEntry[] result = new ClassFileEntry[exceptions.length + 1];
  70.         System.arraycopy(exceptions, 0, result, 0, exceptions.length);
  71.         result[exceptions.length] = getAttributeName();
  72.         return result;
  73.     }

  74.     @Override
  75.     public int hashCode() {
  76.         final int prime = 31;
  77.         int result = super.hashCode();
  78.         result = prime * result + hashCode(exceptions);
  79.         return result;
  80.     }

  81.     @Override
  82.     protected void resolve(final ClassConstantPool pool) {
  83.         super.resolve(pool);
  84.         exceptionIndexes = new int[exceptions.length];
  85.         for (int i = 0; i < exceptions.length; i++) {
  86.             exceptions[i].resolve(pool);
  87.             exceptionIndexes[i] = pool.indexOf(exceptions[i]);
  88.         }
  89.     }

  90.     @Override
  91.     public String toString() {
  92.         final StringBuilder sb = new StringBuilder("Exceptions: ");
  93.         for (final CPClass exception : exceptions) {
  94.             sb.append(exception);
  95.             sb.append(' ');
  96.         }
  97.         return sb.toString();
  98.     }

  99.     @Override
  100.     protected void writeBody(final DataOutputStream dos) throws IOException {
  101.         dos.writeShort(exceptionIndexes.length);
  102.         for (final int element : exceptionIndexes) {
  103.             dos.writeShort(element);
  104.         }
  105.     }

  106. }