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   * Abstract superclass for reference constant pool entries, such as a method or field reference.
25   */
26  public abstract class CPRef extends ConstantPoolEntry {
27  
28      CPClass className;
29      transient int classNameIndex;
30  
31      protected CPNameAndType nameAndType;
32      transient int nameAndTypeIndex;
33  
34      protected String cachedToString;
35  
36      /**
37       * Constructs a new CPRef.
38       *
39       * @param type        TODO
40       * @param className   TODO
41       * @param descriptor  TODO
42       * @param globalIndex index in CpBands
43       * @throws NullPointerException if descriptor or className is null
44       */
45      public CPRef(final byte type, final CPClass className, final CPNameAndType descriptor, final int globalIndex) {
46          super(type, globalIndex);
47          this.className = Objects.requireNonNull(className, "className");
48          this.nameAndType = Objects.requireNonNull(descriptor, "descriptor");
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 (getClass() != obj.getClass()) {
60              return false;
61          }
62          if (this.hashCode() != obj.hashCode()) {
63              return false;
64          }
65          final CPRef other = (CPRef) obj;
66          if (!className.equals(other.className)) {
67              return false;
68          }
69          if (!nameAndType.equals(other.nameAndType)) {
70              return false;
71          }
72          return true;
73      }
74  
75      @Override
76      protected ClassFileEntry[] getNestedClassFileEntries() {
77          final ClassFileEntry[] entries = new ClassFileEntry[2];
78          entries[0] = className;
79          entries[1] = nameAndType;
80          return entries;
81      }
82  
83      @Override
84      protected void resolve(final ClassConstantPool pool) {
85          super.resolve(pool);
86          nameAndTypeIndex = pool.indexOf(nameAndType);
87          classNameIndex = pool.indexOf(className);
88      }
89  
90      @Override
91      public String toString() {
92          if (cachedToString == null) {
93              String type;
94              if (getTag() == ConstantPoolEntry.CP_Fieldref) {
95                  type = "FieldRef"; //$NON-NLS-1$
96              } else if (getTag() == ConstantPoolEntry.CP_Methodref) {
97                  type = "MethoddRef"; //$NON-NLS-1$
98              } else if (getTag() == ConstantPoolEntry.CP_InterfaceMethodref) {
99                  type = "InterfaceMethodRef"; //$NON-NLS-1$
100             } else {
101                 type = "unknown"; //$NON-NLS-1$
102             }
103             cachedToString = type + ": " + className + "#" + nameAndType; //$NON-NLS-1$ //$NON-NLS-2$
104         }
105         return cachedToString;
106     }
107 
108     @Override
109     protected void writeBody(final DataOutputStream dos) throws IOException {
110         dos.writeShort(classNameIndex);
111         dos.writeShort(nameAndTypeIndex);
112     }
113 
114 }