View Javadoc

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