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