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