View Javadoc
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  
19  import java.io.DataOutputStream;
20  import java.io.IOException;
21  import java.util.Objects;
22  
23  /**
24   * Abstract superclass for class file attributes
25   */
26  public abstract class Attribute extends ClassFileEntry {
27  
28      protected final CPUTF8 attributeName;
29  
30      private int attributeNameIndex;
31  
32      public Attribute(final CPUTF8 attributeName) {
33          this.attributeName = attributeName;
34      }
35  
36      @Override
37      protected void doWrite(final DataOutputStream dos) throws IOException {
38          dos.writeShort(attributeNameIndex);
39          dos.writeInt(getLength());
40          writeBody(dos);
41      }
42  
43      @Override
44      public boolean equals(final Object obj) {
45          if (this == obj) {
46              return true;
47          }
48          if (obj == null) {
49              return false;
50          }
51          if (this.getClass() != obj.getClass()) {
52              return false;
53          }
54          final Attribute other = (Attribute) obj;
55          return Objects.equals(attributeName, other.attributeName);
56      }
57  
58      protected CPUTF8 getAttributeName() {
59          return attributeName;
60      }
61  
62      protected abstract int getLength();
63  
64      /**
65       * 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
66       * attributes are nested within other attributes - the outer attribute needs to take the inner attribute headers into account when calculating its length.
67       *
68       * @return int adjusted length
69       */
70      protected int getLengthIncludingHeader() {
71          return getLength() + 2 + 4;
72      }
73  
74      @Override
75      protected ClassFileEntry[] getNestedClassFileEntries() {
76          return new ClassFileEntry[] { getAttributeName() };
77      }
78  
79      /**
80       * Answer true if the receiver needs to have BCI renumbering applied to it; otherwise answer false.
81       *
82       * @return boolean BCI renumbering required
83       */
84      public boolean hasBCIRenumbering() {
85          return false;
86      }
87  
88      @Override
89      public int hashCode() {
90          return Objects.hash(attributeName);
91      }
92  
93      /**
94       * Answer true if the receiver is a source file attribute (which gets special handling when the class is built); otherwise answer false.
95       *
96       * @return boolean source file attribute
97       */
98      public boolean isSourceFileAttribute() {
99          return false;
100     }
101 
102     @Override
103     protected void resolve(final ClassConstantPool pool) {
104         super.resolve(pool);
105         attributeNameIndex = pool.indexOf(attributeName);
106     }
107 
108     protected abstract void writeBody(DataOutputStream dos) throws IOException;
109 
110 }