CPRef.java

  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. import java.io.DataOutputStream;
  21. import java.io.IOException;
  22. import java.util.Objects;

  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.     CPClass className;
  28.     transient int classNameIndex;

  29.     protected CPNameAndType nameAndType;
  30.     transient int nameAndTypeIndex;

  31.     protected String cachedToString;

  32.     /**
  33.      * Constructs a new CPRef.
  34.      *
  35.      * @param type        TODO
  36.      * @param className   TODO
  37.      * @param descriptor  TODO
  38.      * @param globalIndex index in CpBands
  39.      * @throws NullPointerException if descriptor or className is null
  40.      */
  41.     public CPRef(final byte type, final CPClass className, final CPNameAndType descriptor, final int globalIndex) {
  42.         super(type, globalIndex);
  43.         this.className = Objects.requireNonNull(className, "className");
  44.         this.nameAndType = Objects.requireNonNull(descriptor, "descriptor");
  45.     }

  46.     @Override
  47.     public boolean equals(final Object obj) {
  48.         if (this == obj) {
  49.             return true;
  50.         }
  51.         if (obj == null || getClass() != obj.getClass() || hashCode() != obj.hashCode()) {
  52.             return false;
  53.         }
  54.         final CPRef other = (CPRef) obj;
  55.         return Objects.equals(className, other.className)
  56.                 && Objects.equals(nameAndType, other.nameAndType);
  57.     }

  58.     @Override
  59.     protected ClassFileEntry[] getNestedClassFileEntries() {
  60.         final ClassFileEntry[] entries = new ClassFileEntry[2];
  61.         entries[0] = className;
  62.         entries[1] = nameAndType;
  63.         return entries;
  64.     }

  65.     @Override
  66.     protected void resolve(final ClassConstantPool pool) {
  67.         super.resolve(pool);
  68.         nameAndTypeIndex = pool.indexOf(nameAndType);
  69.         classNameIndex = pool.indexOf(className);
  70.     }

  71.     @Override
  72.     public String toString() {
  73.         if (cachedToString == null) {
  74.             final String type;
  75.             if (getTag() == CP_Fieldref) {
  76.                 type = "FieldRef"; //$NON-NLS-1$
  77.             } else if (getTag() == CP_Methodref) {
  78.                 type = "MethoddRef"; //$NON-NLS-1$
  79.             } else if (getTag() == CP_InterfaceMethodref) {
  80.                 type = "InterfaceMethodRef"; //$NON-NLS-1$
  81.             } else {
  82.                 type = "unknown"; //$NON-NLS-1$
  83.             }
  84.             cachedToString = type + ": " + className + "#" + nameAndType; //$NON-NLS-1$ //$NON-NLS-2$
  85.         }
  86.         return cachedToString;
  87.     }

  88.     @Override
  89.     protected void writeBody(final DataOutputStream dos) throws IOException {
  90.         dos.writeShort(classNameIndex);
  91.         dos.writeShort(nameAndTypeIndex);
  92.     }

  93. }