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