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 023import org.apache.commons.compress.harmony.unpack200.SegmentUtils; 024 025/** 026 * Name and Type pair constant pool entry. 027 */ 028public class CPNameAndType extends ConstantPoolEntry { 029 030 CPUTF8 descriptor; 031 032 transient int descriptorIndex; 033 034 CPUTF8 name; 035 036 transient int nameIndex; 037 038 /** 039 * Create a new CPNameAndType 040 * 041 * @param name TODO 042 * @param descriptor TODO 043 * @param globalIndex - index in CpBands 044 * @throws NullPointerException if name or descriptor is null 045 */ 046 public CPNameAndType(final CPUTF8 name, final CPUTF8 descriptor, final int globalIndex) { 047 super(ConstantPoolEntry.CP_NameAndType, globalIndex); 048 this.name = Objects.requireNonNull(name, "name"); 049 this.descriptor = Objects.requireNonNull(descriptor, "descriptor"); 050 } 051 052 @Override 053 protected ClassFileEntry[] getNestedClassFileEntries() { 054 return new ClassFileEntry[] {name, descriptor}; 055 } 056 057 @Override 058 protected void resolve(final ClassConstantPool pool) { 059 super.resolve(pool); 060 descriptorIndex = pool.indexOf(descriptor); 061 nameIndex = pool.indexOf(name); 062 } 063 064 /* 065 * field_info { u2 access_flags; u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info 066 * attributes[attributes_count]; } 067 */ 068 069 @Override 070 protected void writeBody(final DataOutputStream dos) throws IOException { 071 dos.writeShort(nameIndex); 072 dos.writeShort(descriptorIndex); 073 } 074 075 @Override 076 public String toString() { 077 return "NameAndType: " + name + "(" + descriptor + ")"; 078 } 079 080 private boolean hashcodeComputed; 081 private int cachedHashCode; 082 083 private void generateHashCode() { 084 hashcodeComputed = true; 085 final int PRIME = 31; 086 int result = 1; 087 result = PRIME * result + descriptor.hashCode(); 088 result = PRIME * result + name.hashCode(); 089 cachedHashCode = result; 090 } 091 092 @Override 093 public int hashCode() { 094 if (!hashcodeComputed) { 095 generateHashCode(); 096 } 097 return cachedHashCode; 098 } 099 100 @Override 101 public boolean equals(final Object obj) { 102 if (this == obj) { 103 return true; 104 } 105 if (obj == null) { 106 return false; 107 } 108 if (getClass() != obj.getClass()) { 109 return false; 110 } 111 final CPNameAndType other = (CPNameAndType) obj; 112 if (!descriptor.equals(other.descriptor)) { 113 return false; 114 } 115 if (!name.equals(other.name)) { 116 return false; 117 } 118 return true; 119 } 120 121 /** 122 * Answers the invokeinterface count argument when the receiver is treated as an invokeinterface target. This value 123 * is not meaningful if the receiver is not an invokeinterface target. 124 * 125 * @return count 126 */ 127 public int invokeInterfaceCount() { 128 return 1 + SegmentUtils.countInvokeInterfaceArgs(descriptor.underlyingString()); 129 } 130}