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