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 * Abstract superclass for reference constant pool entries, such as a method or field reference. 025 */ 026public abstract class CPRef extends ConstantPoolEntry { 027 028 CPClass className; 029 transient int classNameIndex; 030 031 protected CPNameAndType nameAndType; 032 transient int nameAndTypeIndex; 033 034 /** 035 * Create a new CPRef 036 * 037 * @param type TODO 038 * @param className TODO 039 * @param descriptor TODO 040 * @param globalIndex index in CpBands 041 * @throws NullPointerException if descriptor or className is null 042 */ 043 public CPRef(final byte type, final CPClass className, final CPNameAndType descriptor, final int globalIndex) { 044 super(type, globalIndex); 045 this.className = Objects.requireNonNull(className, "className"); 046 this.nameAndType = Objects.requireNonNull(descriptor, "descriptor"); 047 } 048 049 @Override 050 public boolean equals(final Object obj) { 051 if (this == obj) { 052 return true; 053 } 054 if (obj == null) { 055 return false; 056 } 057 if (getClass() != obj.getClass()) { 058 return false; 059 } 060 if (this.hashCode() != obj.hashCode()) { 061 return false; 062 } 063 final CPRef other = (CPRef) obj; 064 if (!className.equals(other.className)) { 065 return false; 066 } 067 if (!nameAndType.equals(other.nameAndType)) { 068 return false; 069 } 070 return true; 071 } 072 073 @Override 074 protected ClassFileEntry[] getNestedClassFileEntries() { 075 final ClassFileEntry[] entries = new ClassFileEntry[2]; 076 entries[0] = className; 077 entries[1] = nameAndType; 078 return entries; 079 } 080 081 @Override 082 protected void resolve(final ClassConstantPool pool) { 083 super.resolve(pool); 084 nameAndTypeIndex = pool.indexOf(nameAndType); 085 classNameIndex = pool.indexOf(className); 086 } 087 088 protected String cachedToString; 089 090 @Override 091 public String toString() { 092 if (cachedToString == null) { 093 String type; 094 if (getTag() == ConstantPoolEntry.CP_Fieldref) { 095 type = "FieldRef"; //$NON-NLS-1$ 096 } else if (getTag() == ConstantPoolEntry.CP_Methodref) { 097 type = "MethoddRef"; //$NON-NLS-1$ 098 } else if (getTag() == ConstantPoolEntry.CP_InterfaceMethodref) { 099 type = "InterfaceMethodRef"; //$NON-NLS-1$ 100 } else { 101 type = "unknown"; //$NON-NLS-1$ 102 } 103 cachedToString = type + ": " + className + "#" + nameAndType; //$NON-NLS-1$ //$NON-NLS-2$ 104 } 105 return cachedToString; 106 } 107 108 @Override 109 protected void writeBody(final DataOutputStream dos) throws IOException { 110 dos.writeShort(classNameIndex); 111 dos.writeShort(nameAndTypeIndex); 112 } 113 114}