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   * ConstantValueAttribute represents a constant value attribute 
25   * in a class file.  These attributes are used as initialization
26   * values for static fields.
27   */
28  public class ConstantValueAttribute extends ClassAttribute {
29      /* The expected name of this attribute */
30      public static final String expectedAttrName = "ConstantValue";
31  
32      /* The value */
33      private ConstValue constantValue;
34  
35      /* public accessors */
36  
37      public ConstValue value() {
38          return constantValue;
39      }
40  
41      /*** 
42       * Construct a constant value attribute
43       */
44      public ConstantValueAttribute(ConstUtf8 attrName, ConstValue value) {
45          super(attrName);
46          constantValue = value;
47      }
48  
49      /***
50       * Compares this instance with another for structural equality.
51       */
52      //@olsen: added method
53      public boolean isEqual(Stack msg, Object obj) {
54          if (!(obj instanceof ConstantValueAttribute)) {
55              msg.push("obj/obj.getClass() = "
56                       + (obj == null ? null : obj.getClass()));
57              msg.push("this.getClass() = "
58                       + this.getClass());
59              return false;
60          }
61          ConstantValueAttribute other = (ConstantValueAttribute)obj;
62  
63          if (!super.isEqual(msg, other)) {
64              return false;
65          }
66  
67          if (!this.constantValue.isEqual(msg, other.constantValue)) {
68              msg.push(String.valueOf("constantValue = "
69                                      + other.constantValue));
70              msg.push(String.valueOf("constantValue = "
71                                      + this.constantValue));
72              return false;
73          }
74          return true;
75      }
76  
77      /* 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 */
78  
79      static ConstantValueAttribute read(ConstUtf8 attrName,
80                                         DataInputStream data, ConstantPool pool)
81          throws IOException {
82          int index = 0;
83          index = data.readUnsignedShort();
84  
85          return new ConstantValueAttribute(attrName,
86                                            (ConstValue) pool.constantAt(index));
87      }
88  
89      void write(DataOutputStream out) throws IOException {
90          out.writeShort(attrName().getIndex());
91          out.writeInt(2);
92          out.writeShort(constantValue.getIndex());
93      }
94  
95      void print(PrintStream out, int indent) {
96          ClassPrint.spaces(out, indent);
97          out.println("ConstantValue: " + constantValue.toString());
98      }
99  }
100