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: */
017: package org.apache.harmony.pack200.bytecode;
018:
019: import java.io.DataOutputStream;
020: import java.io.IOException;
021: import java.util.Arrays;
022:
023: public class ExceptionsAttribute extends Attribute {
024:
025: private static int hashCode(Object[] array) {
026: final int prime = 31;
027: if (array == null)
028: return 0;
029: int result = 1;
030: for (int index = 0; index < array.length; index++) {
031: result = prime
032: * result
033: + (array[index] == null ? 0 : array[index]
034: .hashCode());
035: }
036: return result;
037: }
038:
039: private transient int[] exceptionIndexes;
040:
041: private CPClass[] exceptions;
042:
043: public ExceptionsAttribute(CPClass[] exceptions) {
044: super ("Exceptions"); //$NON-NLS-1$
045: this .exceptions = exceptions;
046: }
047:
048: public boolean equals(Object obj) {
049: if (this == obj)
050: return true;
051: if (!super .equals(obj))
052: return false;
053: if (getClass() != obj.getClass())
054: return false;
055: final ExceptionsAttribute other = (ExceptionsAttribute) obj;
056: if (!Arrays.equals(exceptions, other.exceptions))
057: return false;
058: return true;
059: }
060:
061: protected int getLength() {
062: return 2 + 2 * exceptions.length;
063: }
064:
065: protected ClassFileEntry[] getNestedClassFileEntries() {
066: ClassFileEntry[] result = new ClassFileEntry[exceptions.length + 1];
067: for (int i = 0; i < exceptions.length; i++) {
068: result[i] = exceptions[i];
069: }
070: result[exceptions.length] = getAttributeName();
071: return result;
072: }
073:
074: public int hashCode() {
075: final int prime = 31;
076: int result = super .hashCode();
077: result = prime * result
078: + ExceptionsAttribute.hashCode(exceptions);
079: return result;
080: }
081:
082: protected void resolve(ClassConstantPool pool) {
083: super .resolve(pool);
084: exceptionIndexes = new int[exceptions.length];
085: for (int i = 0; i < exceptions.length; i++) {
086: exceptions[i].resolve(pool);
087: exceptionIndexes[i] = pool.indexOf(exceptions[i]);
088: }
089: }
090:
091: public String toString() {
092: return "Exceptions:" + exceptions;
093: }
094:
095: protected void writeBody(DataOutputStream dos) throws IOException {
096: dos.writeShort(exceptionIndexes.length);
097: for (int i = 0; i < exceptionIndexes.length; i++) {
098: dos.writeShort(exceptionIndexes[i]);
099: }
100: }
101:
102: }
|