| 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 | |
| 21 | import java.io.DataOutputStream; |
| 22 | import java.io.IOException; |
| 23 | |
| 24 | /** |
| 25 | * The abstract superclass for all types of class file entries. |
| 26 | */ |
| 27 | public abstract class ClassFileEntry { |
| 28 | |
| 29 | /** |
| 30 | * An empty ClassFileEntry array. |
| 31 | */ |
| 32 | protected static final ClassFileEntry[] NONE = {}; |
| 33 | private boolean resolved; |
| 34 | |
| 35 | /** |
| 36 | * Writes this instance to the output stream. |
| 37 | * |
| 38 | * @param dos the output stream. |
| 39 | * @throws IOException if an I/O error occurs. |
| 40 | */ |
| 41 | protected abstract void doWrite(DataOutputStream dos) throws IOException; |
| 42 | |
| 43 | @Override |
| 44 | public abstract boolean equals(Object arg0); |
| 45 | |
| 46 | /** |
| 47 | * Returns an empty array. |
| 48 | * |
| 49 | * @return an empty array. |
| 50 | */ |
| 51 | protected ClassFileEntry[] getNestedClassFileEntries() { |
| 52 | return NONE; |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public abstract int hashCode(); |
| 57 | |
| 58 | /** |
| 59 | * Delegates to super {@link #hashCode}. |
| 60 | * |
| 61 | * @return super {@link #hashCode}. |
| 62 | */ |
| 63 | protected int objectHashCode() { |
| 64 | return super.hashCode(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Allows the constant pool entries to resolve their nested entries. |
| 69 | * |
| 70 | * @param pool The class constant pool. |
| 71 | */ |
| 72 | protected void resolve(final ClassConstantPool pool) { |
| 73 | resolved = true; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public abstract String toString(); |
| 78 | |
| 79 | /** |
| 80 | * Writes this instance to the output stream. |
| 81 | * |
| 82 | * @param dos the output stream. |
| 83 | * @throws IOException if an I/O error occurs. |
| 84 | */ |
| 85 | public final void write(final DataOutputStream dos) throws IOException { |
| 86 | if (!resolved) { |
| 87 | throw new IllegalStateException("Entry has not been resolved"); |
| 88 | } |
| 89 | doWrite(dos); |
| 90 | } |
| 91 | |
| 92 | } |