001 package edu.rice.cs.cunit.classFile.constantPool; 002 003 import edu.rice.cs.cunit.classFile.constantPool.visitors.CheckUTFVisitor; 004 import edu.rice.cs.cunit.classFile.constantPool.visitors.IPoolInfoVisitor; 005 006 import java.io.DataInputStream; 007 import java.io.DataOutputStream; 008 import java.io.IOException; 009 010 /** 011 * Represents a string in the constant pool. 012 * 013 * @author Mathias Ricken 014 */ 015 public class StringPoolInfo extends APoolInfo { 016 /** 017 * Utf information. 018 */ 019 private AUTFPoolInfo _utf; 020 021 /** 022 * Utf index. 023 */ 024 private short _utfIndex; 025 026 /** 027 * Constructor. 028 * 029 * @param utf utf information 030 */ 031 public StringPoolInfo(AUTFPoolInfo utf, ConstantPool cp) { 032 super(CONSTANT_String, cp); 033 _utf = utf; 034 reindex(); 035 } 036 037 /** 038 * Constructor reading from a stream. 039 * 040 * @param dis input stream 041 * @param cp constant pool 042 * 043 * @throws IOException 044 */ 045 public StringPoolInfo(DataInputStream dis, ConstantPool cp) throws IOException { 046 super(CONSTANT_String, cp); 047 _utfIndex = dis.readShort(); 048 } 049 050 /** 051 * Accessor for the utf information. 052 * 053 * @return utf information 054 */ 055 public AUTFPoolInfo getUtf() { 056 return _utf; 057 } 058 059 /** 060 * Mutator for the utf information. 061 * 062 * @param utf new utf information 063 */ 064 public void setUtf(AUTFPoolInfo utf) { 065 _utf = utf; 066 } 067 068 /** 069 * Write this constant pool object into the stream, including the type byte. 070 * 071 * @param dos stream 072 * 073 * @throws java.io.IOException 074 */ 075 public void write(DataOutputStream dos) throws IOException { 076 reindex(); 077 dos.writeByte(_type); 078 dos.writeShort(_utfIndex); 079 } 080 081 /** 082 * Resolve constant pool objects. This makes sure that the object links match the index links. 083 */ 084 public void resolve() { 085 _utf = _constantPool.get(_utfIndex).execute(CheckUTFVisitor.singleton(), null); 086 } 087 088 /** 089 * Reindex constant pool indices. This makes sure the index links match the object links. 090 */ 091 public void reindex() { 092 _utfIndex = _constantPool.indexOf(_utf); 093 } 094 095 /** 096 * Return a human-readable version of this constant pool object. 097 * 098 * @return string 099 */ 100 public String toStringVerbose() { 101 StringBuilder s = new StringBuilder(); 102 s.append("CONSTANT_String: Name = #"); 103 s.append(_utfIndex); 104 s.append(' '); 105 s.append(toString()); 106 107 return s.toString(); 108 } 109 110 /** 111 * Return a human-readable version of this constant pool object. 112 * 113 * @return string 114 */ 115 public String toString() { 116 return _utf.toString(); 117 } 118 119 /** 120 * Return a hash code. 121 * 122 * @return hash code 123 */ 124 public int hashCode() { 125 return _utfIndex; 126 } 127 128 /** 129 * Compare this object and another one. 130 * 131 * @param obj other object 132 * 133 * @return true if the same 134 */ 135 public boolean equals(Object obj) { 136 return (obj instanceof StringPoolInfo) && ( 137 (((StringPoolInfo)obj)._utfIndex == _utfIndex) || 138 (((StringPoolInfo)obj)._utf == _utf) 139 ); 140 } 141 142 /** 143 * Execute a visitor. 144 * 145 * @param visitor visitor 146 * @param data visitor-specific parameter 147 * 148 * @return visitor-specific return value 149 */ 150 public <R, D> R execute(IPoolInfoVisitor<R, D> visitor, D data) { 151 return visitor.stringCase(this, data); 152 } 153 }