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 name and an associated type in the constant pool
25   * of a class file
26   */
27  public class ConstNameAndType extends ConstBasic {
28      /* The tag value associated with ConstDouble */
29      public static final int MyTag = CONSTANTNameAndType;
30  
31      /* The name of interest */
32      private ConstUtf8 theName;
33  
34      /* The index of the name to be resolved
35       *   - used during class file reading */
36      private int theNameIndex;
37  
38      /* The type signature associated with the name */
39      private ConstUtf8 typeSignature;
40  
41      /* The index of the signature to be resolved
42       *   - used during class file reading */
43      private int typeSignatureIndex;
44  
45      /* public accessors */
46  
47      /***
48       * The tag of this constant entry
49       */
50      public int tag () { return MyTag; }
51  
52      /***
53       * Return the name
54       */
55      public ConstUtf8 name() {
56          return theName;
57      }
58  
59      /***
60       * Return the type signature associated with the name
61       */
62      public ConstUtf8 signature() {
63          return typeSignature;
64      }
65  
66      /***
67       * Modify the signature
68       */
69      public void changeSignature(ConstUtf8 newSig) {
70          typeSignature = newSig;
71      }
72  
73      /***
74       * A printable representation
75       */
76      public String toString () {
77          return "CONSTANTNameAndType(" + indexAsString() + "): " + 
78              "name(" + theName.toString() + ") " +
79              " type(" + typeSignature.toString() + ")";
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 ConstNameAndType)) {
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          ConstNameAndType other = (ConstNameAndType)obj;
95  
96          if (!super.isEqual(msg, other)) {
97              return false;
98          }
99  
100         if (!this.theName.isEqual(msg, other.theName)) {
101             msg.push(String.valueOf("theName = "
102                                     + other.theName));
103             msg.push(String.valueOf("theName = "
104                                     + this.theName));
105             return false;
106         }
107         if (!this.typeSignature.isEqual(msg, other.typeSignature)) {
108             msg.push(String.valueOf("typeSignature = "
109                                     + other.typeSignature));
110             msg.push(String.valueOf("typeSignature = "
111                                     + this.typeSignature));
112             return false;
113         }
114         return true;
115     }
116 
117     /* 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 */
118 
119     ConstNameAndType (ConstUtf8 n, ConstUtf8 sig) {
120         theName = n; typeSignature = sig;
121     }
122 
123     ConstNameAndType (int n, int sig) {
124         theNameIndex = n; typeSignatureIndex = sig;
125     }
126 
127     void formatData (DataOutputStream b) throws IOException {
128         b.writeShort(theName.getIndex());
129         b.writeShort(typeSignature.getIndex());
130     }
131 
132     static ConstNameAndType read (DataInputStream input) throws IOException {
133         int cname = input.readUnsignedShort();
134         int sig = input.readUnsignedShort();
135 
136         return new ConstNameAndType (cname, sig);
137     }
138 
139     void resolve (ConstantPool p) {
140         theName = (ConstUtf8) p.constantAt(theNameIndex);
141         typeSignature = (ConstUtf8) p.constantAt(typeSignatureIndex);
142     }
143 }