CPNameAndType.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. import org.apache.commons.compress.harmony.unpack200.SegmentUtils;

  22. /**
  23.  * Name and Type pair constant pool entry.
  24.  */
  25. public class CPNameAndType extends ConstantPoolEntry {

  26.     CPUTF8 descriptor;

  27.     transient int descriptorIndex;

  28.     CPUTF8 name;

  29.     transient int nameIndex;

  30.     private boolean hashCodeComputed;

  31.     private int cachedHashCode;

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

  45.     /*
  46.      * field_info { u2 access_flags; u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; }
  47.      */

  48.     @Override
  49.     public boolean equals(final Object obj) {
  50.         if (this == obj) {
  51.             return true;
  52.         }
  53.         if (obj == null) {
  54.             return false;
  55.         }
  56.         if (getClass() != obj.getClass()) {
  57.             return false;
  58.         }
  59.         final CPNameAndType other = (CPNameAndType) obj;
  60.         return Objects.equals(descriptor, other.descriptor)
  61.                 && Objects.equals(name, other.name);
  62.     }

  63.     private void generateHashCode() {
  64.         hashCodeComputed = true;
  65.         final int PRIME = 31;
  66.         int result = 1;
  67.         result = PRIME * result + descriptor.hashCode();
  68.         result = PRIME * result + name.hashCode();
  69.         cachedHashCode = result;
  70.     }

  71.     @Override
  72.     protected ClassFileEntry[] getNestedClassFileEntries() {
  73.         return new ClassFileEntry[] { name, descriptor };
  74.     }

  75.     @Override
  76.     public int hashCode() {
  77.         if (!hashCodeComputed) {
  78.             generateHashCode();
  79.         }
  80.         return cachedHashCode;
  81.     }

  82.     /**
  83.      * Answers the invokeinterface count argument when the receiver is treated as an invokeinterface target. This value is not meaningful if the receiver is not
  84.      * an invokeinterface target.
  85.      *
  86.      * @return count
  87.      */
  88.     public int invokeInterfaceCount() {
  89.         return 1 + SegmentUtils.countInvokeInterfaceArgs(descriptor.underlyingString());
  90.     }

  91.     @Override
  92.     protected void resolve(final ClassConstantPool pool) {
  93.         super.resolve(pool);
  94.         descriptorIndex = pool.indexOf(descriptor);
  95.         nameIndex = pool.indexOf(name);
  96.     }

  97.     @Override
  98.     public String toString() {
  99.         return "NameAndType: " + name + "(" + descriptor + ")";
  100.     }

  101.     @Override
  102.     protected void writeBody(final DataOutputStream dos) throws IOException {
  103.         dos.writeShort(nameIndex);
  104.         dos.writeShort(descriptorIndex);
  105.     }
  106. }