CPRef.java

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

  21. /**
  22.  * Abstract superclass for reference constant pool entries, such as a method or field reference.
  23.  */
  24. public abstract class CPRef extends ConstantPoolEntry {

  25.     CPClass className;
  26.     transient int classNameIndex;

  27.     protected CPNameAndType nameAndType;
  28.     transient int nameAndTypeIndex;

  29.     protected String cachedToString;

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

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

  62.     @Override
  63.     protected ClassFileEntry[] getNestedClassFileEntries() {
  64.         final ClassFileEntry[] entries = new ClassFileEntry[2];
  65.         entries[0] = className;
  66.         entries[1] = nameAndType;
  67.         return entries;
  68.     }

  69.     @Override
  70.     protected void resolve(final ClassConstantPool pool) {
  71.         super.resolve(pool);
  72.         nameAndTypeIndex = pool.indexOf(nameAndType);
  73.         classNameIndex = pool.indexOf(className);
  74.     }

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

  92.     @Override
  93.     protected void writeBody(final DataOutputStream dos) throws IOException {
  94.         dos.writeShort(classNameIndex);
  95.         dos.writeShort(nameAndTypeIndex);
  96.     }

  97. }