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 (Object element : array) {
037            result = prime * result + (element == null ? 0 : element.hashCode());
038        }
039        return result;
040    }
041
042    private transient int[] exceptionIndexes;
043
044    private final CPClass[] exceptions;
045
046    public ExceptionsAttribute(final CPClass[] exceptions) {
047        super(attributeName);
048        this.exceptions = exceptions;
049    }
050
051    @Override
052    public boolean equals(final Object obj) {
053        if (this == obj) {
054            return true;
055        }
056        if (!super.equals(obj)) {
057            return false;
058        }
059        if (getClass() != obj.getClass()) {
060            return false;
061        }
062        final ExceptionsAttribute other = (ExceptionsAttribute) obj;
063        if (!Arrays.equals(exceptions, other.exceptions)) {
064            return false;
065        }
066        return true;
067    }
068
069    @Override
070    protected int getLength() {
071        return 2 + 2 * exceptions.length;
072    }
073
074    @Override
075    protected ClassFileEntry[] getNestedClassFileEntries() {
076        final ClassFileEntry[] result = new ClassFileEntry[exceptions.length + 1];
077        System.arraycopy(exceptions, 0, result, 0, exceptions.length);
078        result[exceptions.length] = getAttributeName();
079        return result;
080    }
081
082    @Override
083    public int hashCode() {
084        final int prime = 31;
085        int result = super.hashCode();
086        result = prime * result + ExceptionsAttribute.hashCode(exceptions);
087        return result;
088    }
089
090    @Override
091    protected void resolve(final ClassConstantPool pool) {
092        super.resolve(pool);
093        exceptionIndexes = new int[exceptions.length];
094        for (int i = 0; i < exceptions.length; i++) {
095            exceptions[i].resolve(pool);
096            exceptionIndexes[i] = pool.indexOf(exceptions[i]);
097        }
098    }
099
100    @Override
101    public String toString() {
102        final StringBuilder sb = new StringBuilder();
103        sb.append("Exceptions: ");
104        for (CPClass exception : exceptions) {
105            sb.append(exception);
106            sb.append(' ');
107        }
108        return sb.toString();
109    }
110
111    @Override
112    protected void writeBody(final DataOutputStream dos) throws IOException {
113        dos.writeShort(exceptionIndexes.length);
114        for (int element : exceptionIndexes) {
115            dos.writeShort(element);
116        }
117    }
118
119    public static void setAttributeName(final CPUTF8 cpUTF8Value) {
120        attributeName = cpUTF8Value;
121    }
122
123}