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