ClassFileEntry.java

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

  20. import java.io.DataOutputStream;
  21. import java.io.IOException;

  22. /**
  23.  * The abstract superclass for all types of class file entries.
  24.  */
  25. public abstract class ClassFileEntry {

  26.     /**
  27.      * An empty ClassFileEntry array.
  28.      */
  29.     protected static final ClassFileEntry[] NONE = {};
  30.     private boolean resolved;

  31.     /**
  32.      * Writes this instance to the output stream.
  33.      *
  34.      * @param dos the output stream.
  35.      * @throws IOException if an I/O error occurs.
  36.      */
  37.     protected abstract void doWrite(DataOutputStream dos) throws IOException;

  38.     @Override
  39.     public abstract boolean equals(Object arg0);

  40.     /**
  41.      * Returns an empty array.
  42.      *
  43.      * @return an empty array.
  44.      */
  45.     protected ClassFileEntry[] getNestedClassFileEntries() {
  46.         return NONE;
  47.     }

  48.     @Override
  49.     public abstract int hashCode();

  50.     /**
  51.      * Delegates to super {@link #hashCode}.
  52.      *
  53.      * @return super {@link #hashCode}.
  54.      */
  55.     protected int objectHashCode() {
  56.         return super.hashCode();
  57.     }

  58.     /**
  59.      * Allows the constant pool entries to resolve their nested entries.
  60.      *
  61.      * @param pool The class constant pool.
  62.      */
  63.     protected void resolve(final ClassConstantPool pool) {
  64.         resolved = true;
  65.     }

  66.     @Override
  67.     public abstract String toString();

  68.     /**
  69.      * Writes this instance to the output stream.
  70.      *
  71.      * @param dos the output stream.
  72.      * @throws IOException if an I/O error occurs.
  73.      */
  74.     public final void write(final DataOutputStream dos) throws IOException {
  75.         if (!resolved) {
  76.             throw new IllegalStateException("Entry has not been resolved");
  77.         }
  78.         doWrite(dos);
  79.     }

  80. }