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