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          if (!Objects.equals(attributeName, other.attributeName)) {
56              return false;
57          }
58          return true;
59      }
60  
61      protected CPUTF8 getAttributeName() {
62          return attributeName;
63      }
64  
65      protected abstract int getLength();
66  
67      /**
68       * 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
69       * attributes are nested within other attributes - the outer attribute needs to take the inner attribute headers into account when calculating its length.
70       *
71       * @return int adjusted length
72       */
73      protected int getLengthIncludingHeader() {
74          return getLength() + 2 + 4;
75      }
76  
77      @Override
78      protected ClassFileEntry[] getNestedClassFileEntries() {
79          return new ClassFileEntry[] { getAttributeName() };
80      }
81  
82      /**
83       * Answer true if the receiver needs to have BCI renumbering applied to it; otherwise answer false.
84       *
85       * @return boolean BCI renumbering required
86       */
87      public boolean hasBCIRenumbering() {
88          return false;
89      }
90  
91      @Override
92      public int hashCode() {
93          return Objects.hash(attributeName);
94      }
95  
96      /**
97       * Answer true if the receiver is a source file attribute (which gets special handling when the class is built); otherwise answer false.
98       *
99       * @return boolean source file attribute
100      */
101     public boolean isSourceFileAttribute() {
102         return false;
103     }
104 
105     @Override
106     protected void resolve(final ClassConstantPool pool) {
107         super.resolve(pool);
108         attributeNameIndex = pool.indexOf(attributeName);
109     }
110 
111     protected abstract void writeBody(DataOutputStream dos) throws IOException;
112 
113 }