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