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 (hashCode() != obj.hashCode()) {
63              return false;
64          }
65          final CPRef other = (CPRef) obj;
66          return Objects.equals(className, other.className)
67                  && Objects.equals(nameAndType, other.nameAndType);
68      }
69  
70      @Override
71      protected ClassFileEntry[] getNestedClassFileEntries() {
72          final ClassFileEntry[] entries = new ClassFileEntry[2];
73          entries[0] = className;
74          entries[1] = nameAndType;
75          return entries;
76      }
77  
78      @Override
79      protected void resolve(final ClassConstantPool pool) {
80          super.resolve(pool);
81          nameAndTypeIndex = pool.indexOf(nameAndType);
82          classNameIndex = pool.indexOf(className);
83      }
84  
85      @Override
86      public String toString() {
87          if (cachedToString == null) {
88              String type;
89              if (getTag() == CP_Fieldref) {
90                  type = "FieldRef"; //$NON-NLS-1$
91              } else if (getTag() == CP_Methodref) {
92                  type = "MethoddRef"; //$NON-NLS-1$
93              } else if (getTag() == CP_InterfaceMethodref) {
94                  type = "InterfaceMethodRef"; //$NON-NLS-1$
95              } else {
96                  type = "unknown"; //$NON-NLS-1$
97              }
98              cachedToString = type + ": " + className + "#" + nameAndType; //$NON-NLS-1$ //$NON-NLS-2$
99          }
100         return cachedToString;
101     }
102 
103     @Override
104     protected void writeBody(final DataOutputStream dos) throws IOException {
105         dos.writeShort(classNameIndex);
106         dos.writeShort(nameAndTypeIndex);
107     }
108 
109 }