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   * Field reference constant pool entry.
27   */
28  public class CPFieldRef extends ConstantPoolEntry {
29  
30      CPClass className;
31      transient int classNameIndex;
32      private final CPNameAndType nameAndType;
33      transient int nameAndTypeIndex;
34  
35      private boolean hashCodeComputed;
36  
37      private int cachedHashCode;
38  
39      public CPFieldRef(final CPClass className, final CPNameAndType descriptor, final int globalIndex) {
40          super(CP_Fieldref, globalIndex);
41          this.className = className;
42          this.nameAndType = descriptor;
43      }
44  
45      @Override
46      public boolean equals(final Object obj) {
47          if (this == obj) {
48              return true;
49          }
50          if (obj == null || getClass() != obj.getClass()) {
51              return false;
52          }
53          final CPFieldRef other = (CPFieldRef) obj;
54          return Objects.equals(className, other.className)
55                  && Objects.equals(nameAndType, other.nameAndType);
56      }
57  
58      private void generateHashCode() {
59          hashCodeComputed = true;
60          final int PRIME = 31;
61          int result = 1;
62          result = PRIME * result + (className == null ? 0 : className.hashCode());
63          result = PRIME * result + (nameAndType == null ? 0 : nameAndType.hashCode());
64          cachedHashCode = result;
65      }
66  
67      @Override
68      protected ClassFileEntry[] getNestedClassFileEntries() {
69          return new ClassFileEntry[] { className, nameAndType };
70      }
71  
72      @Override
73      public int hashCode() {
74          if (!hashCodeComputed) {
75              generateHashCode();
76          }
77          return cachedHashCode;
78      }
79  
80      @Override
81      protected void resolve(final ClassConstantPool pool) {
82          super.resolve(pool);
83          nameAndTypeIndex = pool.indexOf(nameAndType);
84          classNameIndex = pool.indexOf(className);
85      }
86  
87      @Override
88      public String toString() {
89          return "FieldRef: " + className + "#" + nameAndType;
90      }
91  
92      @Override
93      protected void writeBody(final DataOutputStream dos) throws IOException {
94          dos.writeShort(classNameIndex);
95          dos.writeShort(nameAndTypeIndex);
96      }
97  
98  }