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   * GenericAttribute represents a class attribute in a class file which
25   * is not recognized as any supported attribute type.  These attributes
26   * are maintained, and are not modified in any way.
27   */
28  
29  public class GenericAttribute extends ClassAttribute {
30  
31      /* The bytes of the attribute following the name */
32      byte attributeBytes[];
33  
34      /* public accessors */
35  
36      /***
37       * constructor
38       */
39      public GenericAttribute(ConstUtf8 attrName, byte attrBytes[]) {
40          super(attrName);
41          attributeBytes = attrBytes;
42      }
43  
44      /***
45       * Compares this instance with another for structural equality.
46       */
47      //@olsen: added method
48      public boolean isEqual(Stack msg, Object obj) {
49          if (!(obj instanceof GenericAttribute)) {
50              msg.push("obj/obj.getClass() = "
51                       + (obj == null ? null : obj.getClass()));
52              msg.push("this.getClass() = "
53                       + this.getClass());
54              return false;
55          }
56          GenericAttribute other = (GenericAttribute)obj;
57  
58          if (!super.isEqual(msg, other)) {
59              return false;
60          }
61  
62          if (this.attributeBytes.length != other.attributeBytes.length) {
63              msg.push("attributeBytes.length "
64                       + String.valueOf(other.attributeBytes.length));
65              msg.push("attributeBytes.length "
66                       + String.valueOf(this.attributeBytes.length));
67              return false;
68          }
69  
70          for (int i = 0; i < attributeBytes.length; i++) {
71              byte b1 = this.attributeBytes[i];
72              byte b2 = other.attributeBytes[i];
73              if (b1 != b2) {
74                  msg.push("attributeBytes[" + i + "] = "
75                           + String.valueOf(b2));
76                  msg.push("attributeBytes[" + i + "] = "
77                           + String.valueOf(b1));
78                  return false;
79              }
80          }
81          return true;
82      }
83  
84      void write(DataOutputStream out) throws IOException {
85          out.writeShort(attrName().getIndex());
86          out.writeInt(attributeBytes.length);
87          out.write(attributeBytes, 0, attributeBytes.length);
88      }
89  
90      void print(PrintStream out, int indent) {
91          ClassPrint.spaces(out, indent);
92          out.println("Generic Attribute(" + attrName().asString() + "): " +
93                      Integer.toString(attributeBytes.length) +
94                      " in length");
95          for (int i=0; i<attributeBytes.length; i++) {
96              if ((i % 16) == 0) {
97                  if (i != 0) 
98                      out.println();
99                  out.print(i + " :");
100             }
101             out.print(" " + Integer.toString((attributeBytes[i] & 0xff), 16));
102         }
103         out.println();
104     }
105 }
106