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   * Constant pool entry for a class
27   */
28  public class CPClass extends ConstantPoolEntry {
29  
30      private int index;
31  
32      public String name;
33  
34      private final CPUTF8 utf8;
35  
36      private boolean hashCodeComputed;
37  
38      private int cachedHashCode;
39  
40      /**
41       * Creates a new CPClass
42       *
43       * @param name        TODO
44       * @param globalIndex index in CpBands
45       * @throws NullPointerException if name is null
46       */
47      public CPClass(final CPUTF8 name, final int globalIndex) {
48          super(CP_Class, globalIndex);
49          this.name = Objects.requireNonNull(name, "name").underlyingString();
50          this.utf8 = name;
51      }
52  
53      @Override
54      public boolean equals(final Object obj) {
55          if (this == obj) {
56              return true;
57          }
58          if (obj == null || this.getClass() != obj.getClass()) {
59              return false;
60          }
61          final CPClass other = (CPClass) obj;
62          return utf8.equals(other.utf8);
63      }
64  
65      private void generateHashCode() {
66          hashCodeComputed = true;
67          cachedHashCode = utf8.hashCode();
68      }
69  
70      public String getName() {
71          return name;
72      }
73  
74      @Override
75      protected ClassFileEntry[] getNestedClassFileEntries() {
76          return new ClassFileEntry[] { utf8, };
77      }
78  
79      @Override
80      public int hashCode() {
81          if (!hashCodeComputed) {
82              generateHashCode();
83          }
84          return cachedHashCode;
85      }
86  
87      @Override
88      protected void resolve(final ClassConstantPool pool) {
89          super.resolve(pool);
90          index = pool.indexOf(utf8);
91      }
92  
93      @Override
94      public String toString() {
95          return "Class: " + getName();
96      }
97  
98      @Override
99      protected void writeBody(final DataOutputStream dos) throws IOException {
100         dos.writeShort(index);
101     }
102 }