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