KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > classfile > ClassAttribute


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.api.persistence.enhancer.classfile;
26
27 import java.util.Vector JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.io.*;
30
31 /**
32  * An abstract base class for the attributes within a class file
33  */

34
35 public abstract class ClassAttribute implements VMConstants {
36
37   /* The name of the attribute */
38   private ConstUtf8 attributeName;
39
40   /**
41    * Returns the name of the attribute
42    */

43   public ConstUtf8 attrName() {
44     return attributeName;
45   }
46
47   /**
48    * Constructor
49    */

50   ClassAttribute(ConstUtf8 theAttrName) {
51     attributeName = theAttrName;
52   }
53
54   /**
55    * General attribute reader
56    */

57   static ClassAttribute read(DataInputStream data, ConstantPool pool)
58     throws IOException {
59
60     ClassAttribute attr = null;
61     int attrNameIndex = data.readUnsignedShort();
62     ConstUtf8 attrName8 = (ConstUtf8) pool.constantAt(attrNameIndex);
63     String JavaDoc attrName = attrName8.asString();
64     int attrLength = data.readInt();
65
66     if (attrName.equals(CodeAttribute.expectedAttrName)) {
67       /* The old style code attribute reader uses more memory and
68      cpu when the instructions don't need to be examined than the
69      new deferred attribute reader. We may at some point decide that
70      we want to change the default based on the current situation
71      but for now we will just use the deferred reader in all cases. */

72       if (true) {
73     attr = CodeAttribute.read(attrName8, attrLength, data, pool);
74       } else {
75     attr = CodeAttribute.read(attrName8, data, pool);
76       }
77     }
78     else if (attrName.equals(SourceFileAttribute.expectedAttrName)) {
79       attr = SourceFileAttribute.read(attrName8, data, pool);
80     }
81     else if (attrName.equals(ConstantValueAttribute.expectedAttrName)) {
82       attr = ConstantValueAttribute.read(attrName8, data, pool);
83     }
84     else if (attrName.equals(ExceptionsAttribute.expectedAttrName)) {
85       attr = ExceptionsAttribute.read(attrName8, data, pool);
86     }
87     else if (attrName.equals(AnnotatedClassAttribute.expectedAttrName)) {
88       attr = AnnotatedClassAttribute.read(attrName8, data, pool);
89     }
90     else {
91       /* Unrecognized method attribute */
92       byte attrBytes[] = new byte[attrLength];
93       data.readFully(attrBytes);
94       attr = new GenericAttribute (attrName8, attrBytes);
95     }
96
97     return attr;
98   }
99
100   /*
101    * CodeAttribute attribute reader
102    */

103
104   static ClassAttribute read(DataInputStream data, CodeEnv env)
105     throws IOException {
106     ClassAttribute attr = null;
107     int attrNameIndex = data.readUnsignedShort();
108     ConstUtf8 attrName8 = (ConstUtf8) env.pool().constantAt(attrNameIndex);
109     String JavaDoc attrName = attrName8.asString();
110     int attrLength = data.readInt();
111
112     if (attrName.equals(LineNumberTableAttribute.expectedAttrName)) {
113       attr = LineNumberTableAttribute.read(attrName8, data, env);
114     }
115     else if (attrName.equals(LocalVariableTableAttribute.expectedAttrName)) {
116       attr = LocalVariableTableAttribute.read(attrName8, data, env);
117     }
118     else if (attrName.equals(AnnotatedMethodAttribute.expectedAttrName)) {
119       attr = AnnotatedMethodAttribute.read(attrName8, data, env);
120     }
121     //@olsen: fix 4467428, added support for synthetic code attribute
122
else if (attrName.equals(SyntheticAttribute.expectedAttrName)) {
123       attr = SyntheticAttribute.read(attrName8, data, env.pool());
124     }
125     else {
126       /* Unrecognized method attribute */
127       byte attrBytes[] = new byte[attrLength];
128       data.readFully(attrBytes);
129       attr = new GenericAttribute (attrName8, attrBytes);
130     }
131
132     return attr;
133   }
134
135   /**
136    * Write the attribute to the output stream
137    */

138   abstract void write(DataOutputStream out) throws IOException;
139
140   /**
141    * Print a description of the attribute to the print stream
142    */

143   abstract void print(PrintStream out, int indent);
144 }
145
146
Popular Tags