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