001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.compress.harmony.unpack200.bytecode; 018 019import java.io.DataOutputStream; 020import java.io.IOException; 021import java.util.Objects; 022 023/** 024 * Field reference constant pool entry. 025 */ 026public class CPFieldRef extends ConstantPoolEntry { 027 028 CPClass className; 029 transient int classNameIndex; 030 private final CPNameAndType nameAndType; 031 transient int nameAndTypeIndex; 032 033 private boolean hashCodeComputed; 034 035 private int cachedHashCode; 036 037 public CPFieldRef(final CPClass className, final CPNameAndType descriptor, final int globalIndex) { 038 super(ConstantPoolEntry.CP_Fieldref, globalIndex); 039 this.className = className; 040 this.nameAndType = descriptor; 041 } 042 043 @Override 044 public boolean equals(final Object obj) { 045 if (this == obj) { 046 return true; 047 } 048 if (obj == null) { 049 return false; 050 } 051 if (getClass() != obj.getClass()) { 052 return false; 053 } 054 final CPFieldRef other = (CPFieldRef) obj; 055 if (!Objects.equals(className, other.className)) { 056 return false; 057 } 058 if (!Objects.equals(nameAndType, other.nameAndType)) { 059 return false; 060 } 061 return true; 062 } 063 064 private void generateHashCode() { 065 hashCodeComputed = true; 066 final int PRIME = 31; 067 int result = 1; 068 result = PRIME * result + (className == null ? 0 : className.hashCode()); 069 result = PRIME * result + (nameAndType == null ? 0 : nameAndType.hashCode()); 070 cachedHashCode = result; 071 } 072 073 @Override 074 protected ClassFileEntry[] getNestedClassFileEntries() { 075 return new ClassFileEntry[] { className, nameAndType }; 076 } 077 078 @Override 079 public int hashCode() { 080 if (!hashCodeComputed) { 081 generateHashCode(); 082 } 083 return cachedHashCode; 084 } 085 086 @Override 087 protected void resolve(final ClassConstantPool pool) { 088 super.resolve(pool); 089 nameAndTypeIndex = pool.indexOf(nameAndType); 090 classNameIndex = pool.indexOf(className); 091 } 092 093 @Override 094 public String toString() { 095 return "FieldRef: " + className + "#" + nameAndType; 096 } 097 098 @Override 099 protected void writeBody(final DataOutputStream dos) throws IOException { 100 dos.writeShort(classNameIndex); 101 dos.writeShort(nameAndTypeIndex); 102 } 103 104}