001 /* 002 * @(#)APoolInfo.java 1.5 95/08/16 Chuck McManis 003 * 004 * Copyright (c) 1996 Chuck McManis, All Rights Reserved. 005 * 006 * Permission to use, copy, modify, and distribute this software 007 * and its documentation for NON-COMMERCIAL purposes and without 008 * fee is hereby granted provided that this copyright notice 009 * appears in all copies. 010 * 011 * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY 012 * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 013 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 014 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS SHALL NOT BE 015 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, 016 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. 017 */ 018 019 package sysModel.classFile.constantPool; 020 021 import sysModel.classFile.constantPool.visitors.IPoolInfoVisitor; 022 023 import java.io.DataInputStream; 024 import java.io.DataOutputStream; 025 import java.io.IOException; 026 027 /** 028 * Represents an abstract constant pool object. 029 * 030 * @author Mathias Ricken 031 */ 032 public abstract class APoolInfo { 033 // Constant pool object types 034 public static final int CONSTANT_Class = 7; 035 public static final int CONSTANT_Fieldref = 9; 036 public static final int CONSTANT_Methodref = 10; 037 public static final int CONSTANT_InterfaceMethodref = 11; 038 public static final int CONSTANT_String = 8; 039 public static final int CONSTANT_Integer = 3; 040 public static final int CONSTANT_Float = 4; 041 public static final int CONSTANT_Long = 5; 042 public static final int CONSTANT_Double = 6; 043 public static final int CONSTANT_NameAndType = 12; 044 public static final int CONSTANT_Utf8_ASCII = 1; 045 public static final int CONSTANT_Utf8_Unicode = 2; 046 047 /** 048 * Object type. 049 */ 050 protected int _type; 051 052 /** 053 * Reference to the constant pool. 054 */ 055 protected ConstantPool _constantPool; 056 057 /** 058 * Constructor 059 * 060 * @param type object type 061 */ 062 public APoolInfo(int type, ConstantPool cp) { 063 _type = type; 064 _constantPool = cp; 065 } 066 067 /** 068 * Read from stream and return unresolved constant pool object. 069 * 070 * @param dis stream 071 * 072 * @return unresolved constant pool object. 073 * 074 * @throws IOException 075 * @throws ClassFormatError 076 */ 077 public static APoolInfo read(DataInputStream dis, ConstantPool cp) throws IOException, ClassFormatError { 078 int type = dis.readByte(); 079 APoolInfo item; 080 switch(type) { 081 case CONSTANT_Class: 082 item = new ClassPoolInfo(dis, cp); 083 break; 084 case CONSTANT_Fieldref: 085 item = new FieldPoolInfo(dis, cp); 086 break; 087 case CONSTANT_Methodref: 088 item = new MethodPoolInfo(dis, cp); 089 break; 090 case CONSTANT_InterfaceMethodref: 091 item = new InterfaceMethodPoolInfo(dis, cp); 092 break; 093 case CONSTANT_NameAndType: 094 item = new NameAndTypePoolInfo(dis, cp); 095 break; 096 case CONSTANT_String: 097 item = new StringPoolInfo(dis, cp); 098 break; 099 case CONSTANT_Integer: 100 item = new IntegerPoolInfo(dis, cp); 101 break; 102 case CONSTANT_Float: 103 item = new FloatPoolInfo(dis, cp); 104 break; 105 case CONSTANT_Long: 106 item = new LongPoolInfo(dis, cp); 107 break; 108 case CONSTANT_Double: 109 item = new DoublePoolInfo(dis, cp); 110 break; 111 case CONSTANT_Utf8_ASCII: 112 item = new ASCIIPoolInfo(dis, cp); 113 break; 114 case CONSTANT_Utf8_Unicode: 115 item = new UnicodePoolInfo(dis, cp); 116 break; 117 default: 118 throw new ClassFormatError("Unknown constant pool item type"); 119 } 120 return item; 121 } 122 123 /** 124 * Return the reference to the constant pool item that is already in pool, that matches this one. 125 * 126 * @param pool constant pool 127 * 128 * @return matching object or null if not found 129 */ 130 public APoolInfo inPool(ConstantPool pool) { 131 for(APoolInfo cpi : pool) { 132 if (equals(cpi)) { 133 return cpi; 134 } 135 } 136 return null; 137 } 138 139 /** 140 * Write this constant pool object into the stream, including the type byte. 141 * 142 * @param dos stream 143 * 144 * @throws IOException 145 */ 146 public abstract void write(DataOutputStream dos) throws IOException; 147 148 /** 149 * Resolve constant pool objects. This makes sure that the object links match the index links. 150 */ 151 public abstract void resolve(); 152 153 /** 154 * Reindex constant pool indices. This makes sure the index links match the object links. 155 */ 156 public abstract void reindex(); 157 158 /** 159 * Return a human-readable version of this constant pool object. 160 * 161 * @return string 162 */ 163 public abstract String toStringVerbose(); 164 165 /** 166 * Return a human-readable version of this constant pool object. 167 * 168 * @return string 169 */ 170 public abstract String toString(); 171 172 /** 173 * Return a hash code. 174 * 175 * @return hash code 176 */ 177 public abstract int hashCode(); 178 179 /** 180 * Compare this object and another one. 181 * 182 * @param obj other object 183 * 184 * @return true if the same 185 */ 186 public abstract boolean equals(Object obj); 187 188 /** 189 * Execute a visitor. 190 * 191 * @param visitor visitor 192 * @param data visitor-specific parameter 193 * 194 * @return visitor-specific return value 195 */ 196 public abstract <R, D> R execute(IPoolInfoVisitor<R, D> visitor, D data); 197 }