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        if (!Objects.equals(attributeName, other.attributeName)) {
056            return false;
057        }
058        return true;
059    }
060
061    protected CPUTF8 getAttributeName() {
062        return attributeName;
063    }
064
065    protected abstract int getLength();
066
067    /**
068     * 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
069     * attributes are nested within other attributes - the outer attribute needs to take the inner attribute headers into account when calculating its length.
070     *
071     * @return int adjusted length
072     */
073    protected int getLengthIncludingHeader() {
074        return getLength() + 2 + 4;
075    }
076
077    @Override
078    protected ClassFileEntry[] getNestedClassFileEntries() {
079        return new ClassFileEntry[] { getAttributeName() };
080    }
081
082    /**
083     * Answer true if the receiver needs to have BCI renumbering applied to it; otherwise answer false.
084     *
085     * @return boolean BCI renumbering required
086     */
087    public boolean hasBCIRenumbering() {
088        return false;
089    }
090
091    @Override
092    public int hashCode() {
093        return Objects.hash(attributeName);
094    }
095
096    /**
097     * Answer true if the receiver is a source file attribute (which gets special handling when the class is built); otherwise answer false.
098     *
099     * @return boolean source file attribute
100     */
101    public boolean isSourceFileAttribute() {
102        return false;
103    }
104
105    @Override
106    protected void resolve(final ClassConstantPool pool) {
107        super.resolve(pool);
108        attributeNameIndex = pool.indexOf(attributeName);
109    }
110
111    protected abstract void writeBody(DataOutputStream dos) throws IOException;
112
113}