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