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: */
017: package org.apache.harmony.pack200.bytecode;
018:
019: import java.io.DataOutputStream;
020: import java.io.IOException;
021:
022: public abstract class Attribute extends ClassFileEntry {
023: protected final CPUTF8 attributeName;
024:
025: private int attributeNameIndex;
026:
027: public Attribute(String attributeName) {
028: this .attributeName = new CPUTF8(attributeName,
029: ClassConstantPool.DOMAIN_ATTRIBUTEASCIIZ);
030: }
031:
032: protected void doWrite(DataOutputStream dos) throws IOException {
033: dos.writeShort(attributeNameIndex);
034: dos.writeInt(getLength());
035: writeBody(dos);
036: }
037:
038: public boolean equals(Object obj) {
039: if (this == obj)
040: return true;
041: if (obj == null)
042: return false;
043: if (this .getClass() != obj.getClass())
044: return false;
045: final Attribute other = (Attribute) obj;
046: if (attributeName == null) {
047: if (other.attributeName != null)
048: return false;
049: } else if (!attributeName.equals(other.attributeName))
050: return false;
051: return true;
052: }
053:
054: protected CPUTF8 getAttributeName() {
055: return attributeName;
056: }
057:
058: protected abstract int getLength();
059:
060: /**
061: * Answer the length of the receiver including its header (the u2 for the
062: * attribute name and the u4 for the attribute length). This is relevant
063: * when attributes are nested within other attributes - the outer attribute
064: * needs to take the inner attribute headers into account when calculating
065: * its length.
066: *
067: * @return int adjusted length
068: */
069: protected int getLengthIncludingHeader() {
070: return getLength() + 2 + 4;
071: }
072:
073: protected ClassFileEntry[] getNestedClassFileEntries() {
074: return new ClassFileEntry[] { getAttributeName() };
075: }
076:
077: /**
078: * Answer true if the receiver needs to have BCI renumbering applied to it;
079: * otherwise answer false.
080: *
081: * @return boolean BCI renumbering required
082: */
083: public boolean hasBCIRenumbering() {
084: return false;
085: }
086:
087: public int hashCode() {
088: final int PRIME = 31;
089: int result = 1;
090: result = PRIME
091: * result
092: + ((attributeName == null) ? 0 : attributeName
093: .hashCode());
094: return result;
095: }
096:
097: protected void resolve(ClassConstantPool pool) {
098: super .resolve(pool);
099: attributeNameIndex = pool.indexOf(attributeName);
100: }
101:
102: protected abstract void writeBody(DataOutputStream dos)
103: throws IOException;
104:
105: }
|