1/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
2 *
3 * This program and the accompanying materials are made available under
4 * the terms of the Common Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/cpl-v10.html
6 *
7 * $Id: CONSTANT_info.java,v 1.1.1.1 2004/05/09 16:57:48 vlad_r Exp $
8 */
9package com.vladium.jcd.cls.constant;
10
11import java.io.IOException;
12
13import com.vladium.jcd.compiler.IClassFormatOutput;
14import com.vladium.jcd.lib.UDataInputStream;
15import com.vladium.jcd.lib.UDataOutputStream;
16
17// ----------------------------------------------------------------------------
18/**
19 * An abstract base for all other CONSTANT_XXX_info structures. See $4.4 in VM
20 * spec 1.0 for all such structure definitions.
21 *
22 * @author (C) 2001, Vlad Roubtsov
23 */
24public
25abstract class CONSTANT_info implements Cloneable, IClassFormatOutput
26{
27    // public: ................................................................
28
29
30    /**
31     * Returns the tag byte for this CONSTANT type [this data is
32     * static class data].
33     */
34    public abstract byte tag ();
35
36    // Visitor:
37
38    public abstract Object accept (ICONSTANTVisitor visitor, Object ctx);
39
40    public abstract String toString ();
41
42    /**
43     * Returns the number of constant pool index slots occupied by this
44     * CONSTANT type. This implementation defaults to returning '1'.
45     *
46     * @see CONSTANT_Long_info
47     * @see CONSTANT_Long_info
48     *
49     * @return int
50     */
51    public int width ()
52    {
53        return 1;
54    }
55
56
57    /**
58     * Virtual constructor method for all CONSTANT_XXX_info structures.
59     */
60    public static CONSTANT_info new_CONSTANT_info (final UDataInputStream bytes)
61        throws IOException
62    {
63        byte tag = bytes.readByte ();
64
65        switch (tag)
66        {
67        case CONSTANT_Utf8_info.TAG:
68            return new CONSTANT_Utf8_info (bytes);
69
70        case CONSTANT_Integer_info.TAG:
71            return new CONSTANT_Integer_info (bytes);
72
73        case CONSTANT_Float_info.TAG:
74            return new CONSTANT_Float_info (bytes);
75
76        case CONSTANT_Long_info.TAG:
77            return new CONSTANT_Long_info (bytes);
78
79        case CONSTANT_Double_info.TAG:
80            return new CONSTANT_Double_info (bytes);
81
82
83        case CONSTANT_Class_info.TAG:
84            return new CONSTANT_Class_info (bytes);
85
86        case CONSTANT_String_info.TAG:
87            return new CONSTANT_String_info (bytes);
88
89
90        case CONSTANT_Fieldref_info.TAG:
91            return new CONSTANT_Fieldref_info (bytes);
92
93        case CONSTANT_Methodref_info.TAG:
94            return new CONSTANT_Methodref_info (bytes);
95
96        case CONSTANT_InterfaceMethodref_info.TAG:
97            return new CONSTANT_InterfaceMethodref_info (bytes);
98
99
100        case CONSTANT_NameAndType_info.TAG:
101            return new CONSTANT_NameAndType_info (bytes);
102
103        default: throw new IllegalStateException ("CONSTANT_info: invalid tag value [" + tag + ']');
104
105        } // end of switch
106    }
107
108    // Cloneable:
109
110    /**
111     * Chains to super.clone() and removes CloneNotSupportedException
112     * from the method signature.
113     */
114    public Object clone ()
115    {
116        try
117        {
118            return super.clone ();
119        }
120        catch (CloneNotSupportedException e)
121        {
122            throw new InternalError (e.toString ());
123        }
124    }
125
126    // IClassFormatOutput:
127
128    public void writeInClassFormat (final UDataOutputStream out) throws IOException
129    {
130        out.writeByte (tag ());
131    }
132
133    public static String tagToString (final CONSTANT_info constant)
134    {
135        switch (constant.tag ())
136        {
137        case CONSTANT_Utf8_info.TAG:
138            return "CONSTANT_Utf8";
139
140        case CONSTANT_Integer_info.TAG:
141            return "CONSTANT_Integer";
142
143        case CONSTANT_Float_info.TAG:
144            return "CONSTANT_Float";
145
146        case CONSTANT_Long_info.TAG:
147            return "CONSTANT_Long";
148
149        case CONSTANT_Double_info.TAG:
150            return "CONSTANT_Double";
151
152
153        case CONSTANT_Class_info.TAG:
154            return "CONSTANT_Class";
155
156        case CONSTANT_String_info.TAG:
157            return "CONSTANT_String";
158
159
160        case CONSTANT_Fieldref_info.TAG:
161            return "CONSTANT_Fieldref";
162
163        case CONSTANT_Methodref_info.TAG:
164            return "CONSTANT_Methodref";
165
166        case CONSTANT_InterfaceMethodref_info.TAG:
167            return "CONSTANT_InterfaceMethodref";
168
169
170        case CONSTANT_NameAndType_info.TAG:
171            return "CONSTANT_NameAndType";
172
173        default: throw new IllegalStateException ("CONSTANT_info: invalid tag value [" + constant.tag () + ']');
174
175        } // end of switch
176    }
177
178    // protected: .............................................................
179
180    /*
181    protected static final byte CONSTANT_Utf8                       = 1;
182    protected static final byte CONSTANT_Integer                    = 3;
183    protected static final byte CONSTANT_Float                      = 4;
184    protected static final byte CONSTANT_Long                       = 5;
185    protected static final byte CONSTANT_Double                     = 6;
186    protected static final byte CONSTANT_Class                      = 7;
187    protected static final byte CONSTANT_String                     = 8;
188    protected static final byte CONSTANT_Fieldref                   = 9;
189    protected static final byte CONSTANT_Methodref                  = 10;
190    protected static final byte CONSTANT_InterfaceMethodref         = 11;
191    protected static final byte CONSTANT_NameAndType                = 12;
192    */
193
194    protected CONSTANT_info ()
195    {
196    }
197
198    // package: ...............................................................
199
200    // private: ...............................................................
201
202} // end of class
203// ----------------------------------------------------------------------------
204