KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > structures > elementvalues > ElementValue


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7 package org.gjt.jclasslib.structures.elementvalues;
8
9 import org.gjt.jclasslib.structures.*;
10
11 import java.io.*;
12
13 /**
14  * Describes an <tt>ElementValue</tt> attribute structure.
15  *
16  * @author <a HREF="mailto:vitor.carreira@gmail.com">Vitor Carreira</a>
17  * @version $Revision: 1.1 $ $Date: 2004/12/28 13:04:32 $
18  */

19 public abstract class ElementValue extends AbstractStructure {
20
21     public final static char BYTE_TAG = 'B';
22     public final static char CHAR_TAG = 'C';
23     public final static char DOUBLE_TAG = 'D';
24     public final static char FLOAT_TAG = 'F';
25     public final static char INT_TAG = 'I';
26     public final static char LONG_TAG = 'J';
27     public final static char SHORT_TAG = 'S';
28     public final static char BOOL_TAG = 'Z';
29     public final static char STRING_TAG = 's';
30     public final static char ENUM_TAG = 'e';
31     public final static char CLASS_TAG = 'c';
32     public final static char ARRAY_TAG = '[';
33     public final static char ANNOTATION_TAG = '@';
34
35     public final static String JavaDoc BYTE_TAG_VERBOSE = "byte";
36     public final static String JavaDoc CHAR_TAG_VERBOSE = "String";
37     public final static String JavaDoc DOUBLE_TAG_VERBOSE = "double";
38     public final static String JavaDoc FLOAT_TAG_VERBOSE = "float";
39     public final static String JavaDoc INT_TAG_VERBOSE = "int";
40     public final static String JavaDoc LONG_TAG_VERBOSE = "long";
41     public final static String JavaDoc SHORT_TAG_VERBOSE = "short";
42     public final static String JavaDoc BOOL_TAG_VERBOSE = "boolean";
43     public final static String JavaDoc STRING_TAG_VERBOSE = "String";
44     public final static String JavaDoc ENUM_TAG_VERBOSE = "Enum";
45     public final static String JavaDoc CLASS_TAG_VERBOSE = "Class";
46     public final static String JavaDoc ARRAY_TAG_VERBOSE = "Array";
47     public final static String JavaDoc ANNOTATION_TAG_VERBOSE = "Annotation";
48
49     protected static final int INITIAL_LENGTH = 1;
50     private int tag;
51
52     public ElementValue(int tag) {
53         this.tag = tag;
54     }
55
56
57     /**
58      * Factory for creating <tt>ElementValue</tt> structures.
59      *
60      * @param in the <tt>DataInput</tt> from which to read the
61      * <tt>ElementValue</tt> structure
62      * @param classFile the parent class file of the structure to be created
63      * @return the new <tt>ElementValue</tt> structure
64      * @throws org.gjt.jclasslib.structures.InvalidByteCodeException
65      * if the byte code is invalid
66      * @throws java.io.IOException if an exception occurs with the <tt>DataInput</tt>
67      */

68     public static ElementValue create(DataInput in, ClassFile classFile)
69             throws InvalidByteCodeException, IOException {
70
71         int tagType = in.readUnsignedByte();
72
73         ElementValue entry = null;
74         switch (tagType) {
75             case BOOL_TAG:
76             case BYTE_TAG:
77             case CHAR_TAG:
78             case DOUBLE_TAG:
79             case FLOAT_TAG:
80             case SHORT_TAG:
81             case INT_TAG:
82             case LONG_TAG:
83             case STRING_TAG:
84                 entry = new ConstElementValue(tagType);
85                 break;
86             case ENUM_TAG:
87                 entry = new EnumElementValue();
88                 break;
89             case CLASS_TAG:
90                 entry = new ClassElementValue();
91                 break;
92             case ANNOTATION_TAG:
93                 entry = new AnnotationElementValue();
94                 break;
95             case ARRAY_TAG:
96                 entry = new ArrayElementValue();
97                 break;
98             default:
99                 throw new InvalidByteCodeException("Unknown tag " + (char)tagType);
100         }
101         entry.setClassFile(classFile);
102         entry.read(in);
103
104         return entry;
105     }
106
107
108     public void write(DataOutput out) throws InvalidByteCodeException, IOException {
109         super.write(out);
110
111         out.writeByte(tag);
112
113         if (debug) debug("wrote ");
114     }
115
116
117     protected abstract int getSpecificLength();
118
119     final public int getLength() {
120         return INITIAL_LENGTH + getSpecificLength();
121     }
122
123     public abstract String JavaDoc getEntryName();
124
125     protected String JavaDoc printAccessFlagsVerbose(int accessFlags) {
126         if (accessFlags != 0)
127             throw new RuntimeException JavaDoc("Access flags should be zero: " + Integer.toHexString(accessFlags));
128         return "";
129     }
130
131     public int getTag() {
132         return tag;
133     }
134
135     public static String JavaDoc getTagDescription(int tag) {
136         switch (tag) {
137             case BOOL_TAG:
138                 return BOOL_TAG_VERBOSE;
139             case BYTE_TAG:
140                 return BYTE_TAG_VERBOSE;
141             case CHAR_TAG:
142                 return CHAR_TAG_VERBOSE;
143             case DOUBLE_TAG:
144                 return DOUBLE_TAG_VERBOSE;
145             case FLOAT_TAG:
146                 return FLOAT_TAG_VERBOSE;
147             case SHORT_TAG:
148                 return SHORT_TAG_VERBOSE;
149             case INT_TAG:
150                 return INT_TAG_VERBOSE;
151             case LONG_TAG:
152                 return LONG_TAG_VERBOSE;
153             case STRING_TAG:
154                 return STRING_TAG_VERBOSE;
155             case ENUM_TAG:
156                 return ENUM_TAG_VERBOSE;
157             case CLASS_TAG:
158                 return CLASS_TAG_VERBOSE;
159             case ANNOTATION_TAG:
160                 return ANNOTATION_TAG_VERBOSE;
161             case ARRAY_TAG:
162                 return ARRAY_TAG_VERBOSE;
163             default:
164                 return "Unknown";
165         }
166     }
167
168
169 }
170
Popular Tags