    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;
    021
    022/**
    023 * Abstract superclass for class file attributes
    024 */
    025public abstract class Attribute extends ClassFileEntry {
    026
    027    protected final CPUTF8 attributeName;
    028
    029    private int attributeNameIndex;
    030
    031    public Attribute(final CPUTF8 attributeName) {
    032        this.attributeName = attributeName;
    033    }
    034
    035    @Override
    036    protected void doWrite(final DataOutputStream dos) throws IOException {
    037        dos.writeShort(attributeNameIndex);
    038        dos.writeInt(getLength());
    039        writeBody(dos);
    040    }
    041
    042    @Override
    043    public boolean equals(final Object obj) {
    044        if (this == obj) {
    045            return true;
    046        }
    047        if (obj == null) {
    048            return false;
    049        }
    050        if (this.getClass() != obj.getClass()) {
    051            return false;
    052        }
    053        final Attribute other = (Attribute) obj;
    054        if (attributeName == null) {
    055            if (other.attributeName != null) {
    056                return false;
    057            }
    058        } else if (!attributeName.equals(other.attributeName)) {
    059            return false;
    060        }
    061        return true;
    062    }
    063
    064    protected CPUTF8 getAttributeName() {
    065        return attributeName;
    066    }
    067
    068    protected abstract int getLength();
    069
    070    /**
    071     * Answer the length of the receiver including its header (the u2 for the attribute name and the u4 for the
    072     * attribute length). This is relevant when attributes are nested within other attributes - the outer attribute
    073     * needs to take the inner attribute headers into account when calculating its length.
    074     *
    075     * @return int adjusted length
    076     */
    077    protected int getLengthIncludingHeader() {
    078        return getLength() + 2 + 4;
    079    }
    080
    081    @Override
    082    protected ClassFileEntry[] getNestedClassFileEntries() {
    083        return new ClassFileEntry[] {getAttributeName()};
    084    }
    085
    086    /**
    087     * Answer true if the receiver needs to have BCI renumbering applied to it; otherwise answer false.
    088     *
    089     * @return boolean BCI renumbering required
    090     */
    091    public boolean hasBCIRenumbering() {
    092        return false;
    093    }
    094
    095    /**
    096     * Answer true if the receiver is a source file attribute (which gets special handling when the class is built);
    097     * otherwise answer false.
    098     *
    099     * @return boolean source file attribute
    100     */
    101    public boolean isSourceFileAttribute() {
    102        return false;
    103    }
    104
    105    @Override
    106    public int hashCode() {
    107        final int PRIME = 31;
    108        int result = 1;
    109        result = PRIME * result + ((attributeName == null) ? 0 : attributeName.hashCode());
    110        return result;
    111    }
    112
    113    @Override
    114    protected void resolve(final ClassConstantPool pool) {
    115        super.resolve(pool);
    116        attributeNameIndex = pool.indexOf(attributeName);
    117    }
    118
    119    protected abstract void writeBody(DataOutputStream dos) throws IOException;
    120
    121}