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