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   * Source file class file attribute
25   */
26  public class SourceFileAttribute extends Attribute {
27  
28      private static CPUTF8 attributeName;
29  
30      public static void setAttributeName(final CPUTF8 cpUTF8Value) {
31          attributeName = cpUTF8Value;
32      }
33  
34      private final CPUTF8 name;
35  
36      private int nameIndex;
37  
38      public SourceFileAttribute(final CPUTF8 name) {
39          super(attributeName);
40          this.name = name;
41      }
42  
43      @Override
44      public boolean equals(final Object obj) {
45          if (this == obj) {
46              return true;
47          }
48          if (!super.equals(obj)) {
49              return false;
50          }
51          if (this.getClass() != obj.getClass()) {
52              return false;
53          }
54          final SourceFileAttribute other = (SourceFileAttribute) obj;
55          if (!Objects.equals(name, other.name)) {
56              return false;
57          }
58          return true;
59      }
60  
61      @Override
62      protected int getLength() {
63          return 2;
64      }
65  
66      @Override
67      protected ClassFileEntry[] getNestedClassFileEntries() {
68          return new ClassFileEntry[] { getAttributeName(), name };
69      }
70  
71      @Override
72      public int hashCode() {
73          final int PRIME = 31;
74          int result = super.hashCode();
75          result = PRIME * result + (name == null ? 0 : name.hashCode());
76          return result;
77      }
78  
79      /*
80       * (non-Javadoc)
81       *
82       * @see org.apache.commons.compress.harmony.unpack200.bytecode.Attribute#isSourceFileAttribute()
83       */
84      @Override
85      public boolean isSourceFileAttribute() {
86          return true;
87      }
88  
89      @Override
90      protected void resolve(final ClassConstantPool pool) {
91          super.resolve(pool);
92          nameIndex = pool.indexOf(name);
93      }
94  
95      @Override
96      public String toString() {
97          return "SourceFile: " + name;
98      }
99  
100     @Override
101     protected void writeBody(final DataOutputStream dos) throws IOException {
102         dos.writeShort(nameIndex);
103     }
104 }