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