KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > asm > optimizer > ConstantPool


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2005 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package org.objectweb.asm.optimizer;
31
32 import java.util.HashMap JavaDoc;
33
34 import org.objectweb.asm.Type;
35
36 /**
37  * A constant pool.
38  *
39  * @author Eric Bruneton
40  */

41 public class ConstantPool extends HashMap JavaDoc {
42
43     private Constant key1 = new Constant();
44
45     private Constant key2 = new Constant();
46
47     private Constant key3 = new Constant();
48
49     public Constant newInteger(final int value) {
50         key1.set(value);
51         Constant result = get(key1);
52         if (result == null) {
53             result = new Constant(key1);
54             put(result);
55         }
56         return result;
57     }
58
59     public Constant newFloat(final float value) {
60         key1.set(value);
61         Constant result = get(key1);
62         if (result == null) {
63             result = new Constant(key1);
64             put(result);
65         }
66         return result;
67     }
68
69     public Constant newLong(final long value) {
70         key1.set(value);
71         Constant result = get(key1);
72         if (result == null) {
73             result = new Constant(key1);
74             put(result);
75         }
76         return result;
77     }
78
79     public Constant newDouble(final double value) {
80         key1.set(value);
81         Constant result = get(key1);
82         if (result == null) {
83             result = new Constant(key1);
84             put(result);
85         }
86         return result;
87     }
88
89     public Constant newUTF8(final String JavaDoc value) {
90         key1.set('s', value, null, null);
91         Constant result = get(key1);
92         if (result == null) {
93             result = new Constant(key1);
94             put(result);
95         }
96         return result;
97     }
98
99     private Constant newString(final String JavaDoc value) {
100         key2.set('S', value, null, null);
101         Constant result = get(key2);
102         if (result == null) {
103             newUTF8(value);
104             result = new Constant(key2);
105             put(result);
106         }
107         return result;
108     }
109
110     public Constant newClass(final String JavaDoc value) {
111         key2.set('C', value, null, null);
112         Constant result = get(key2);
113         if (result == null) {
114             newUTF8(value);
115             result = new Constant(key2);
116             put(result);
117         }
118         return result;
119     }
120
121     public Constant newConst(final Object JavaDoc cst) {
122         if (cst instanceof Integer JavaDoc) {
123             int val = ((Integer JavaDoc) cst).intValue();
124             return newInteger(val);
125         } else if (cst instanceof Float JavaDoc) {
126             float val = ((Float JavaDoc) cst).floatValue();
127             return newFloat(val);
128         } else if (cst instanceof Long JavaDoc) {
129             long val = ((Long JavaDoc) cst).longValue();
130             return newLong(val);
131         } else if (cst instanceof Double JavaDoc) {
132             double val = ((Double JavaDoc) cst).doubleValue();
133             return newDouble(val);
134         } else if (cst instanceof String JavaDoc) {
135             return newString((String JavaDoc) cst);
136         } else if (cst instanceof Type) {
137             Type t = (Type) cst;
138             return newClass(t.getSort() == Type.OBJECT
139                     ? t.getInternalName()
140                     : t.getDescriptor());
141         } else {
142             throw new IllegalArgumentException JavaDoc("value " + cst);
143         }
144     }
145
146     public Constant newField(
147         final String JavaDoc owner,
148         final String JavaDoc name,
149         final String JavaDoc desc)
150     {
151         key3.set('G', owner, name, desc);
152         Constant result = get(key3);
153         if (result == null) {
154             newClass(owner);
155             newNameType(name, desc);
156             result = new Constant(key3);
157             put(result);
158         }
159         return result;
160     }
161
162     public Constant newMethod(
163         final String JavaDoc owner,
164         final String JavaDoc name,
165         final String JavaDoc desc,
166         final boolean itf)
167     {
168         key3.set(itf ? 'N' : 'M', owner, name, desc);
169         Constant result = get(key3);
170         if (result == null) {
171             newClass(owner);
172             newNameType(name, desc);
173             result = new Constant(key3);
174             put(result);
175         }
176         return result;
177     }
178
179     public Constant newNameType(final String JavaDoc name, final String JavaDoc desc) {
180         key2.set('T', name, desc, null);
181         Constant result = get(key2);
182         if (result == null) {
183             newUTF8(name);
184             newUTF8(desc);
185             result = new Constant(key2);
186             put(result);
187         }
188         return result;
189     }
190
191     private Constant get(final Constant key) {
192         return (Constant) get((Object JavaDoc) key);
193     }
194
195     private void put(final Constant cst) {
196         put(cst, cst);
197     }
198 }
Popular Tags