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   * The abstract base class used to represent the various type of
25   * references to members (fields/methods) within the constant pool. 
26   */
27  public abstract class ConstBasicMemberRef extends ConstBasic {
28      /* The name of the class on which the member is defined */
29      protected ConstClass theClassName;
30  
31      /* The index of the class on which the member is defined
32       *   - used temporarily while reading from a class file */
33      protected int theClassNameIndex;
34  
35      /* The name and type of the member */
36      protected ConstNameAndType theNameAndType;
37  
38      /* The index of the name and type of the member
39       *   - used temporarily while reading from a class file */
40      protected int theNameAndTypeIndex;
41  
42      /* public accessors */
43  
44      /***
45       * Return the name of the class defining the member 
46       */
47      public ConstClass className() {
48          return theClassName;
49      }
50  
51      /***
52       * Return the name and type of the member 
53       */
54      public ConstNameAndType nameAndType() {
55          return theNameAndType;
56      }
57  
58      public String toString () {
59          return "className(" + theClassName.toString() + ")" +
60              " nameAndType(" + theNameAndType.toString() + ")";
61      }
62  
63      /***
64       * Compares this instance with another for structural equality.
65       */
66      //@olsen: added method
67      public boolean isEqual(Stack msg, Object obj) {
68          if (!(obj instanceof ConstBasicMemberRef)) {
69              msg.push("obj/obj.getClass() = "
70                       + (obj == null ? null : obj.getClass()));
71              msg.push("this.getClass() = "
72                       + this.getClass());
73              return false;
74          }
75          ConstBasicMemberRef other = (ConstBasicMemberRef)obj;
76  
77          if (!super.isEqual(msg, other)) {
78              return false;
79          }
80  
81          if (!this.theClassName.isEqual(msg, other.theClassName)) {
82              msg.push(String.valueOf("theClassName = "
83                                      + other.theClassName));
84              msg.push(String.valueOf("theClassName = "
85                                      + this.theClassName));
86              return false;
87          }
88          if (!this.theNameAndType.isEqual(msg, other.theNameAndType)) {
89              msg.push(String.valueOf("theNameAndType = "
90                                      + other.theNameAndType));
91              msg.push(String.valueOf("theNameAndType = "
92                                      + this.theNameAndType));
93              return false;
94          }
95          return true;
96      }
97  
98      /* 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 */
99  
100     /***
101      * Constructor for "from scratch" creation
102      */
103     ConstBasicMemberRef (ConstClass cname, ConstNameAndType NT) {
104         theClassName = cname;
105         theNameAndType = NT;
106     }
107 
108     /***
109      * Constructor for reading from a class file
110      */
111     ConstBasicMemberRef (int cnameIndex, int NT_index) {
112         theClassNameIndex = cnameIndex;
113         theNameAndTypeIndex = NT_index;
114     }
115 
116     void formatData (DataOutputStream b) throws IOException {
117         b.writeShort(theClassName.getIndex());
118         b.writeShort(theNameAndType.getIndex());
119     }
120     void resolve (ConstantPool p) {
121         theClassName = (ConstClass) p.constantAt(theClassNameIndex);
122         theNameAndType = (ConstNameAndType) p.constantAt(theNameAndTypeIndex);
123     }
124 }