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