Attribute.java

  1. /*
  2.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.commons.compress.harmony.unpack200.bytecode;

  18. import java.io.DataOutputStream;
  19. import java.io.IOException;
  20. import java.util.Objects;

  21. /**
  22.  * Abstract superclass for class file attributes
  23.  */
  24. public abstract class Attribute extends ClassFileEntry {

  25.     protected final CPUTF8 attributeName;

  26.     private int attributeNameIndex;

  27.     public Attribute(final CPUTF8 attributeName) {
  28.         this.attributeName = attributeName;
  29.     }

  30.     @Override
  31.     protected void doWrite(final DataOutputStream dos) throws IOException {
  32.         dos.writeShort(attributeNameIndex);
  33.         dos.writeInt(getLength());
  34.         writeBody(dos);
  35.     }

  36.     @Override
  37.     public boolean equals(final Object obj) {
  38.         if (this == obj) {
  39.             return true;
  40.         }
  41.         if (obj == null) {
  42.             return false;
  43.         }
  44.         if (this.getClass() != obj.getClass()) {
  45.             return false;
  46.         }
  47.         final Attribute other = (Attribute) obj;
  48.         return Objects.equals(attributeName, other.attributeName);
  49.     }

  50.     protected CPUTF8 getAttributeName() {
  51.         return attributeName;
  52.     }

  53.     protected abstract int getLength();

  54.     /**
  55.      * Answer the length of the receiver including its header (the u2 for the attribute name and the u4 for the attribute length). This is relevant when
  56.      * attributes are nested within other attributes - the outer attribute needs to take the inner attribute headers into account when calculating its length.
  57.      *
  58.      * @return int adjusted length
  59.      */
  60.     protected int getLengthIncludingHeader() {
  61.         return getLength() + 2 + 4;
  62.     }

  63.     @Override
  64.     protected ClassFileEntry[] getNestedClassFileEntries() {
  65.         return new ClassFileEntry[] { getAttributeName() };
  66.     }

  67.     /**
  68.      * Answer true if the receiver needs to have BCI renumbering applied to it; otherwise answer false.
  69.      *
  70.      * @return boolean BCI renumbering required
  71.      */
  72.     public boolean hasBCIRenumbering() {
  73.         return false;
  74.     }

  75.     @Override
  76.     public int hashCode() {
  77.         return Objects.hash(attributeName);
  78.     }

  79.     /**
  80.      * Answer true if the receiver is a source file attribute (which gets special handling when the class is built); otherwise answer false.
  81.      *
  82.      * @return boolean source file attribute
  83.      */
  84.     public boolean isSourceFileAttribute() {
  85.         return false;
  86.     }

  87.     @Override
  88.     protected void resolve(final ClassConstantPool pool) {
  89.         super.resolve(pool);
  90.         attributeNameIndex = pool.indexOf(attributeName);
  91.     }

  92.     protected abstract void writeBody(DataOutputStream dos) throws IOException;

  93. }