View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.jdo.impl.enhancer.classfile;
19  
20  import java.io.*;
21  import java.util.Stack;
22  
23  /***
24   * Class representing a class specification in the constant pool
25   *
26   * ConstString strictly speaking is not a ConstantValue in the
27   * Java VM sense.  However, the compiler generates ConstantValue attributes
28   * which refer to ConstString entries.  This occurs for initialized static
29   * final String fields.  I've changed ConstString to be a ConstValue for 
30   * now as a simplification.
31   */
32  public class ConstString extends ConstValue {
33      /* The tag associated with ConstClass entries */
34      public static final int MyTag = CONSTANTString;
35  
36      /* The name of the class being referred to */
37      private ConstUtf8 stringValue;
38  
39      /* The index of name of the class being referred to
40       *  - used while reading from a class file */
41      private int stringValueIndex;
42  
43      /* public accessors */
44  
45      /***
46       * Return the tag for this constant
47       */
48      public int tag() { return MyTag; }
49  
50      /***
51       * Return the utf8 string calue
52       */
53      public ConstUtf8 value() {
54          return stringValue;
55      }
56  
57      /***
58       * Return the descriptor string for the constant type.
59       */
60      public String descriptor() {
61          return "Ljava/lang/String;";
62      }
63  
64      /***
65       * A printable representation 
66       */
67      public String toString() {
68          return "CONSTANTString(" + indexAsString() + "): " + 
69              "string(" + stringValue.asString() + ")";
70      }
71  
72      /***
73       * Compares this instance with another for structural equality.
74       */
75      //@olsen: added method
76      public boolean isEqual(Stack msg, Object obj) {
77          if (!(obj instanceof ConstString)) {
78              msg.push("obj/obj.getClass() = "
79                       + (obj == null ? null : obj.getClass()));
80              msg.push("this.getClass() = "
81                       + this.getClass());
82              return false;
83          }
84          ConstString other = (ConstString)obj;
85  
86          if (!super.isEqual(msg, other)) {
87              return false;
88          }
89  
90          if (!this.stringValue.isEqual(msg, other.stringValue)) {
91              msg.push(String.valueOf("stringValue = "
92                                      + other.stringValue));
93              msg.push(String.valueOf("stringValue = "
94                                      + this.stringValue));
95              return false;
96          }
97          return true;
98      }
99  
100     /* package local methods *//package-summary/html">class="comment"> package local methods *//package-summary.html">/* package local methods *//package-summary.html">class="comment"> package local methods */
101 
102     ConstString(ConstUtf8 s) {
103         stringValue = s;
104     }
105 
106     ConstString(int sIndex) {
107         stringValueIndex = sIndex;
108     }
109 
110     void formatData(DataOutputStream b) throws IOException {
111         b.writeShort(stringValue.getIndex());
112     }
113     static ConstString read(DataInputStream input) throws IOException {
114         return new ConstString(input.readUnsignedShort());
115     }
116     void resolve(ConstantPool p) {
117         stringValue = (ConstUtf8) p.constantAt(stringValueIndex);
118     }
119 }