CPFieldRef.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.  * Field reference constant pool entry.
  23.  */
  24. public class CPFieldRef extends ConstantPoolEntry {

  25.     CPClass className;
  26.     transient int classNameIndex;
  27.     private final CPNameAndType nameAndType;
  28.     transient int nameAndTypeIndex;

  29.     private boolean hashCodeComputed;

  30.     private int cachedHashCode;

  31.     public CPFieldRef(final CPClass className, final CPNameAndType descriptor, final int globalIndex) {
  32.         super(CP_Fieldref, globalIndex);
  33.         this.className = className;
  34.         this.nameAndType = descriptor;
  35.     }

  36.     @Override
  37.     public boolean equals(final Object obj) {
  38.         if (this == obj) {
  39.             return true;
  40.         }
  41.         if (obj == null) {
  42.             return false;
  43.         }
  44.         if (getClass() != obj.getClass()) {
  45.             return false;
  46.         }
  47.         final CPFieldRef other = (CPFieldRef) obj;
  48.         return Objects.equals(className, other.className)
  49.                 && Objects.equals(nameAndType, other.nameAndType);
  50.     }

  51.     private void generateHashCode() {
  52.         hashCodeComputed = true;
  53.         final int PRIME = 31;
  54.         int result = 1;
  55.         result = PRIME * result + (className == null ? 0 : className.hashCode());
  56.         result = PRIME * result + (nameAndType == null ? 0 : nameAndType.hashCode());
  57.         cachedHashCode = result;
  58.     }

  59.     @Override
  60.     protected ClassFileEntry[] getNestedClassFileEntries() {
  61.         return new ClassFileEntry[] { className, nameAndType };
  62.     }

  63.     @Override
  64.     public int hashCode() {
  65.         if (!hashCodeComputed) {
  66.             generateHashCode();
  67.         }
  68.         return cachedHashCode;
  69.     }

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

  76.     @Override
  77.     public String toString() {
  78.         return "FieldRef: " + className + "#" + nameAndType;
  79.     }

  80.     @Override
  81.     protected void writeBody(final DataOutputStream dos) throws IOException {
  82.         dos.writeShort(classNameIndex);
  83.         dos.writeShort(nameAndTypeIndex);
  84.     }

  85. }