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