1 cananian 1.1.2.2 // ConstantString.java, created Mon Jan 18 22:44:37 1999 by cananian 2 cananian 1.1.2.1 // Copyright (C) 1998 C. Scott Ananian <cananian@alumni.princeton.edu> 3 cananian 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 cananian 1.1.2.1 package harpoon.IR.RawClass; 5 cananian 1.1.2.1 6 cananian 1.1.2.1 import harpoon.Util.Util; 7 cananian 1.1.2.1 /** 8 cananian 1.1.2.1 * The <code>CONSTANT_String_info</code> structure is used to 9 cananian 1.1.2.1 * represent constant objects of the type 10 cananian 1.1.2.1 * <code>java.lang.String</code>. 11 cananian 1.1.2.1 * 12 cananian 1.1.2.1 * @author C. Scott Ananian <cananian@alumni.princeton.edu> 13 cananian 1.2 * @version $Id: ConstantString.java,v 1.2 2002/02/25 21:05:27 cananian Exp $ 14 cananian 1.1.2.1 * @see "The Java Virtual Machine Specification, section 4.4.3" 15 cananian 1.1.2.1 * @see Constant 16 cananian 1.1.2.1 */ 17 cananian 1.1.2.1 public class ConstantString extends ConstantValue { 18 cananian 1.1.2.1 /** The value of the <code>string_index</code> item must be a valid 19 cananian 1.1.2.1 index into the <code>constant_pool</code> table. The 20 cananian 1.1.2.1 <code>constant_pool</code> entry at that point must be a 21 cananian 1.1.2.1 <code>CONSTANT_Utf8_info</code> representing the sequence of 22 cananian 1.1.2.1 characters to which the <code>java.lang.String</code> object is 23 cananian 1.1.2.1 to be initialized. */ 24 cananian 1.1.2.1 public int string_index; 25 cananian 1.1.2.1 26 cananian 1.1.2.1 /** Constructor. */ 27 cananian 1.1.2.1 ConstantString(ClassFile parent, ClassDataInputStream in) 28 cananian 1.1.2.1 throws java.io.IOException { 29 cananian 1.1.2.1 super(parent); 30 cananian 1.1.2.1 string_index = in.read_u2(); 31 cananian 1.1.2.1 } 32 cananian 1.1.2.1 /** Constructor. */ 33 cananian 1.1.2.1 public ConstantString(ClassFile parent, int string_index) { 34 cananian 1.1.2.1 super(parent); 35 cananian 1.1.2.1 this.string_index = string_index; 36 cananian 1.1.2.1 } 37 cananian 1.1.2.1 38 cananian 1.1.2.1 /** Write to a bytecode file. */ 39 cananian 1.1.2.1 public void write(ClassDataOutputStream out) throws java.io.IOException { 40 cananian 1.1.2.1 out.write_u1(CONSTANT_String); // tag 41 cananian 1.1.2.1 out.write_u2(string_index); 42 cananian 1.1.2.1 } 43 cananian 1.1.2.1 44 cananian 1.1.2.1 // convenience. 45 cananian 1.1.2.1 public ConstantUtf8 string_index() 46 cananian 1.1.2.1 { return (ConstantUtf8) parent.constant_pool[string_index]; } 47 cananian 1.1.2.1 public String string() { return string_index().val; } 48 cananian 1.1.2.1 /** Returns the value of this constant, wrapped as a 49 cananian 1.1.2.1 * <code>java.lang.String</code>. */ 50 cananian 1.1.2.1 public Object value() { return new String(string()); } 51 cananian 1.1.2.1 52 cananian 1.1.2.1 /** Create a human-readable representation of this constant. */ 53 cananian 1.1.2.1 public String toString() { 54 cananian 1.1.2.1 return 55 cananian 1.1.2.1 "CONSTANT_String: \"" + Util.escape(string()) + "\" " + 56 cananian 1.1.2.1 "{" + string_index + "}"; 57 cananian 1.1.2.1 } 58 cananian 1.2 }