    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 *   https://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.commons.compress.harmony.unpack200.bytecode;
    020
    021import java.io.DataOutputStream;
    022import java.io.IOException;
    023import java.util.Objects;
    024
    025/**
    026 * Source file class file attribute
    027 */
    028public class SourceFileAttribute extends Attribute {
    029
    030    private static CPUTF8 attributeName;
    031
    032    public static void setAttributeName(final CPUTF8 cpUTF8Value) {
    033        attributeName = cpUTF8Value;
    034    }
    035
    036    private final CPUTF8 name;
    037
    038    private int nameIndex;
    039
    040    public SourceFileAttribute(final CPUTF8 name) {
    041        super(attributeName);
    042        this.name = name;
    043    }
    044
    045    @Override
    046    public boolean equals(final Object obj) {
    047        if (this == obj) {
    048            return true;
    049        }
    050        if (!super.equals(obj) || this.getClass() != obj.getClass()) {
    051            return false;
    052        }
    053        final SourceFileAttribute other = (SourceFileAttribute) obj;
    054        return Objects.equals(name, other.name);
    055    }
    056
    057    @Override
    058    protected int getLength() {
    059        return 2;
    060    }
    061
    062    @Override
    063    protected ClassFileEntry[] getNestedClassFileEntries() {
    064        return new ClassFileEntry[] { getAttributeName(), name };
    065    }
    066
    067    @Override
    068    public int hashCode() {
    069        final int PRIME = 31;
    070        int result = super.hashCode();
    071        result = PRIME * result + (name == null ? 0 : name.hashCode());
    072        return result;
    073    }
    074
    075    /*
    076     * (non-Javadoc)
    077     *
    078     * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#isSourceFileAttribute()
    079     */
    080    @Override
    081    public boolean isSourceFileAttribute() {
    082        return true;
    083    }
    084
    085    @Override
    086    protected void resolve(final ClassConstantPool pool) {
    087        super.resolve(pool);
    088        nameIndex = pool.indexOf(name);
    089    }
    090
    091    @Override
    092    public String toString() {
    093        return "SourceFile: " + name;
    094    }
    095
    096    @Override
    097    protected void writeBody(final DataOutputStream dos) throws IOException {
    098        dos.writeShort(nameIndex);
    099    }
    100}