    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 * Source file class file attribute
    025 */
    026public class SourceFileAttribute extends Attribute {
    027
    028    private static CPUTF8 attributeName;
    029    public static void setAttributeName(final CPUTF8 cpUTF8Value) {
    030        attributeName = cpUTF8Value;
    031    }
    032    private final CPUTF8 name;
    033
    034    private int nameIndex;
    035
    036    public SourceFileAttribute(final CPUTF8 name) {
    037        super(attributeName);
    038        this.name = name;
    039    }
    040
    041    @Override
    042    public boolean equals(final Object obj) {
    043        if (this == obj) {
    044            return true;
    045        }
    046        if (!super.equals(obj)) {
    047            return false;
    048        }
    049        if (this.getClass() != obj.getClass()) {
    050            return false;
    051        }
    052        final SourceFileAttribute other = (SourceFileAttribute) obj;
    053        if (!Objects.equals(name, other.name)) {
    054            return false;
    055        }
    056        return true;
    057    }
    058
    059    @Override
    060    protected int getLength() {
    061        return 2;
    062    }
    063
    064    @Override
    065    protected ClassFileEntry[] getNestedClassFileEntries() {
    066        return new ClassFileEntry[] {getAttributeName(), name};
    067    }
    068
    069    @Override
    070    public int hashCode() {
    071        final int PRIME = 31;
    072        int result = super.hashCode();
    073        result = PRIME * result + ((name == null) ? 0 : name.hashCode());
    074        return result;
    075    }
    076
    077    /*
    078     * (non-Javadoc)
    079     *
    080     * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#isSourceFileAttribute()
    081     */
    082    @Override
    083    public boolean isSourceFileAttribute() {
    084        return true;
    085    }
    086
    087    @Override
    088    protected void resolve(final ClassConstantPool pool) {
    089        super.resolve(pool);
    090        nameIndex = pool.indexOf(name);
    091    }
    092
    093    @Override
    094    public String toString() {
    095        return "SourceFile: " + name;
    096    }
    097
    098    @Override
    099    protected void writeBody(final DataOutputStream dos) throws IOException {
    100        dos.writeShort(nameIndex);
    101    }
    102}