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 (int index = 0; index < array.length; index++) { 037 result = prime * result + (array[index] == null ? 0 : array[index].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 for (int i = 0; i < exceptions.length; i++) { 078 result[i] = exceptions[i]; 079 } 080 result[exceptions.length] = getAttributeName(); 081 return result; 082 } 083 084 @Override 085 public int hashCode() { 086 final int prime = 31; 087 int result = super.hashCode(); 088 result = prime * result + ExceptionsAttribute.hashCode(exceptions); 089 return result; 090 } 091 092 @Override 093 protected void resolve(final ClassConstantPool pool) { 094 super.resolve(pool); 095 exceptionIndexes = new int[exceptions.length]; 096 for (int i = 0; i < exceptions.length; i++) { 097 exceptions[i].resolve(pool); 098 exceptionIndexes[i] = pool.indexOf(exceptions[i]); 099 } 100 } 101 102 @Override 103 public String toString() { 104 final StringBuffer sb = new StringBuffer(); 105 sb.append("Exceptions: "); 106 for (int i = 0; i < exceptions.length; i++) { 107 sb.append(exceptions[i]); 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 (int i = 0; i < exceptionIndexes.length; i++) { 117 dos.writeShort(exceptionIndexes[i]); 118 } 119 } 120 121 public static void setAttributeName(final CPUTF8 cpUTF8Value) { 122 attributeName = cpUTF8Value; 123 } 124 125}