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   * An {@link Attribute} representing a constant.
27   */
28  public class ConstantValueAttribute extends Attribute {
29  
30      private static CPUTF8 attributeName;
31  
32      public static void setAttributeName(final CPUTF8 cpUTF8Value) {
33          attributeName = cpUTF8Value;
34      }
35  
36      private int constantIndex;
37  
38      private final ClassFileEntry entry;
39  
40      /**
41       * Constructs a new instance.
42       *
43       * @param entry an entry.
44       */
45      public ConstantValueAttribute(final ClassFileEntry entry) {
46          super(attributeName);
47          this.entry = Objects.requireNonNull(entry, "entry");
48      }
49  
50      @Override
51      public boolean equals(final Object obj) {
52          if (this == obj) {
53              return true;
54          }
55          if (!super.equals(obj) || this.getClass() != obj.getClass()) {
56              return false;
57          }
58          final ConstantValueAttribute other = (ConstantValueAttribute) obj;
59          return Objects.equals(entry, other.entry);
60      }
61  
62      @Override
63      protected int getLength() {
64          return 2;
65      }
66  
67      @Override
68      protected ClassFileEntry[] getNestedClassFileEntries() {
69          return new ClassFileEntry[] { getAttributeName(), entry };
70      }
71  
72      @Override
73      public int hashCode() {
74          final int PRIME = 31;
75          int result = super.hashCode();
76          result = PRIME * result + (entry == null ? 0 : entry.hashCode());
77          return result;
78      }
79  
80      @Override
81      protected void resolve(final ClassConstantPool pool) {
82          super.resolve(pool);
83          entry.resolve(pool);
84          this.constantIndex = pool.indexOf(entry);
85      }
86  
87      @Override
88      public String toString() {
89          return "Constant:" + entry;
90      }
91  
92      @Override
93      protected void writeBody(final DataOutputStream dos) throws IOException {
94          dos.writeShort(constantIndex);
95      }
96  
97  }