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(ConstantPoolEntry.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          if (!Objects.equals(className, other.className)) {
56              return false;
57          }
58          if (!Objects.equals(nameAndType, other.nameAndType)) {
59              return false;
60          }
61          return true;
62      }
63  
64      private void generateHashCode() {
65          hashCodeComputed = true;
66          final int PRIME = 31;
67          int result = 1;
68          result = PRIME * result + (className == null ? 0 : className.hashCode());
69          result = PRIME * result + (nameAndType == null ? 0 : nameAndType.hashCode());
70          cachedHashCode = result;
71      }
72  
73      @Override
74      protected ClassFileEntry[] getNestedClassFileEntries() {
75          return new ClassFileEntry[] { className, nameAndType };
76      }
77  
78      @Override
79      public int hashCode() {
80          if (!hashCodeComputed) {
81              generateHashCode();
82          }
83          return cachedHashCode;
84      }
85  
86      @Override
87      protected void resolve(final ClassConstantPool pool) {
88          super.resolve(pool);
89          nameAndTypeIndex = pool.indexOf(nameAndType);
90          classNameIndex = pool.indexOf(className);
91      }
92  
93      @Override
94      public String toString() {
95          return "FieldRef: " + className + "#" + nameAndType;
96      }
97  
98      @Override
99      protected void writeBody(final DataOutputStream dos) throws IOException {
100         dos.writeShort(classNameIndex);
101         dos.writeShort(nameAndTypeIndex);
102     }
103 
104 }