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 * Constructs 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(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 attributes[attributes_count]; } 058 */ 059 060 @Override 061 public boolean equals(final Object obj) { 062 if (this == obj) { 063 return true; 064 } 065 if (obj == null) { 066 return false; 067 } 068 if (getClass() != obj.getClass()) { 069 return false; 070 } 071 final CPNameAndType other = (CPNameAndType) obj; 072 return Objects.equals(descriptor, other.descriptor) 073 && Objects.equals(name, other.name); 074 } 075 076 private void generateHashCode() { 077 hashCodeComputed = true; 078 final int PRIME = 31; 079 int result = 1; 080 result = PRIME * result + descriptor.hashCode(); 081 result = PRIME * result + name.hashCode(); 082 cachedHashCode = result; 083 } 084 085 @Override 086 protected ClassFileEntry[] getNestedClassFileEntries() { 087 return new ClassFileEntry[] { name, descriptor }; 088 } 089 090 @Override 091 public int hashCode() { 092 if (!hashCodeComputed) { 093 generateHashCode(); 094 } 095 return cachedHashCode; 096 } 097 098 /** 099 * Answers the invokeinterface count argument when the receiver is treated as an invokeinterface target. This value is not meaningful if the receiver is not 100 * an invokeinterface target. 101 * 102 * @return count 103 */ 104 public int invokeInterfaceCount() { 105 return 1 + SegmentUtils.countInvokeInterfaceArgs(descriptor.underlyingString()); 106 } 107 108 @Override 109 protected void resolve(final ClassConstantPool pool) { 110 super.resolve(pool); 111 descriptorIndex = pool.indexOf(descriptor); 112 nameIndex = pool.indexOf(name); 113 } 114 115 @Override 116 public String toString() { 117 return "NameAndType: " + name + "(" + descriptor + ")"; 118 } 119 120 @Override 121 protected void writeBody(final DataOutputStream dos) throws IOException { 122 dos.writeShort(nameIndex); 123 dos.writeShort(descriptorIndex); 124 } 125}