    001/*
    002 *  Licensed to the Apache Software Foundation (ASF) under one or more
    003 *  contributor license agreements.  See the NOTICE file distributed with
    004 *  this work for additional information regarding copyright ownership.
    005 *  The ASF licenses this file to You under the Apache License, Version 2.0
    006 *  (the "License"); you may not use this file except in compliance with
    007 *  the License.  You may obtain a copy of the License at
    008 *
    009 *     http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 */
    017package org.apache.commons.compress.harmony.unpack200.bytecode;
    018
    019import java.io.DataOutputStream;
    020import java.io.IOException;
    021import java.util.Arrays;
    022
    023/**
    024 * Exceptions class file attribute
    025 */
    026public class ExceptionsAttribute extends Attribute {
    027
    028    private static CPUTF8 attributeName;
    029
    030    private static int hashCode(final Object[] array) {
    031        final int prime = 31;
    032        if (array == null) {
    033            return 0;
    034        }
    035        int result = 1;
    036        for (final Object element : array) {
    037            result = prime * result + (element == null ? 0 : element.hashCode());
    038        }
    039        return result;
    040    }
    041
    042    public static void setAttributeName(final CPUTF8 cpUTF8Value) {
    043        attributeName = cpUTF8Value;
    044    }
    045
    046    private transient int[] exceptionIndexes;
    047
    048    private final CPClass[] exceptions;
    049
    050    public ExceptionsAttribute(final CPClass[] exceptions) {
    051        super(attributeName);
    052        this.exceptions = exceptions;
    053    }
    054
    055    @Override
    056    public boolean equals(final Object obj) {
    057        if (this == obj) {
    058            return true;
    059        }
    060        if (!super.equals(obj)) {
    061            return false;
    062        }
    063        if (getClass() != obj.getClass()) {
    064            return false;
    065        }
    066        final ExceptionsAttribute other = (ExceptionsAttribute) obj;
    067        if (!Arrays.equals(exceptions, other.exceptions)) {
    068            return false;
    069        }
    070        return true;
    071    }
    072
    073    @Override
    074    protected int getLength() {
    075        return 2 + 2 * exceptions.length;
    076    }
    077
    078    @Override
    079    protected ClassFileEntry[] getNestedClassFileEntries() {
    080        final ClassFileEntry[] result = new ClassFileEntry[exceptions.length + 1];
    081        System.arraycopy(exceptions, 0, result, 0, exceptions.length);
    082        result[exceptions.length] = getAttributeName();
    083        return result;
    084    }
    085
    086    @Override
    087    public int hashCode() {
    088        final int prime = 31;
    089        int result = super.hashCode();
    090        result = prime * result + ExceptionsAttribute.hashCode(exceptions);
    091        return result;
    092    }
    093
    094    @Override
    095    protected void resolve(final ClassConstantPool pool) {
    096        super.resolve(pool);
    097        exceptionIndexes = new int[exceptions.length];
    098        for (int i = 0; i < exceptions.length; i++) {
    099            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}