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 reference in the constant pool
25   */
26  public class ConstClass extends ConstBasic {
27      /* The tag associated with ConstClass entries */
28      public static final int MyTag = CONSTANTClass;
29  
30      /* The name of the class being referred to */
31      private ConstUtf8 theClassName;
32  
33      /* The index of name of the class being referred to
34       *  - used while reading from a class file */
35      private int theClassNameIndex;
36  
37      /* public accessors */
38  
39      /***
40       * Return the tag for this constant
41       */
42      public int tag() { return MyTag; }
43  
44      /***
45       * Return the class name
46       */
47      public ConstUtf8 className() {
48          return theClassName;
49      }
50  
51      /***
52       * Return the class name in simple string form
53       */
54      public String asString() {
55          return theClassName.asString();
56      }
57  
58      /***
59       * A printable representation 
60       */
61      public String toString() {
62          return "CONSTANTClass(" + indexAsString() + "): " + 
63              "className(" + theClassName.toString() + ")";
64      }
65  
66      /***
67       * Change the class reference (not to be done lightly)
68       */
69      public void changeClass(ConstUtf8 newName) {
70          theClassName = newName;
71          theClassNameIndex = newName.getIndex();
72      }
73  
74      /***
75       * Construct a ConstClass
76       */
77      public ConstClass(ConstUtf8 cname) {
78          theClassName = cname;
79      }
80  
81      /***
82       * Compares this instance with another for structural equality.
83       */
84      //@olsen: added method
85      public boolean isEqual(Stack msg, Object obj) {
86          if (!(obj instanceof ConstClass)) {
87              msg.push("obj/obj.getClass() = "
88                       + (obj == null ? null : obj.getClass()));
89              msg.push("this.getClass() = "
90                       + this.getClass());
91              return false;
92          }
93          ConstClass other = (ConstClass)obj;
94  
95          if (!super.isEqual(msg, other)) {
96              return false;
97          }
98  
99          if (!this.theClassName.isEqual(msg, other.theClassName)) {
100             msg.push(String.valueOf("theClassName = "
101                                     + other.theClassName));
102             msg.push(String.valueOf("theClassName = "
103                                     + this.theClassName));
104             return false;
105         }
106         return true;
107     }
108 
109     /* 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 */
110 
111     ConstClass(int cname) {
112         theClassNameIndex = cname;
113     }
114 
115     void formatData(DataOutputStream b) throws IOException {
116         b.writeShort(theClassName.getIndex());
117     }
118 
119     static ConstClass read(DataInputStream input) throws IOException {
120         return new ConstClass(input.readUnsignedShort());
121     }
122 
123     void resolve(ConstantPool p) {
124         theClassName = (ConstUtf8)p.constantAt(theClassNameIndex);
125     }
126 }