001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */package org.ejb3unit.asm.optimizer;
030:
031: import java.util.HashMap;
032:
033: import org.ejb3unit.asm.Type;
034:
035: /**
036: * A constant pool.
037: *
038: * @author Eric Bruneton
039: */
040: public class ConstantPool extends HashMap {
041:
042: private final Constant key1 = new Constant();
043:
044: private final Constant key2 = new Constant();
045:
046: private final Constant key3 = new Constant();
047:
048: public Constant newInteger(final int value) {
049: key1.set(value);
050: Constant result = get(key1);
051: if (result == null) {
052: result = new Constant(key1);
053: put(result);
054: }
055: return result;
056: }
057:
058: public Constant newFloat(final float value) {
059: key1.set(value);
060: Constant result = get(key1);
061: if (result == null) {
062: result = new Constant(key1);
063: put(result);
064: }
065: return result;
066: }
067:
068: public Constant newLong(final long value) {
069: key1.set(value);
070: Constant result = get(key1);
071: if (result == null) {
072: result = new Constant(key1);
073: put(result);
074: }
075: return result;
076: }
077:
078: public Constant newDouble(final double value) {
079: key1.set(value);
080: Constant result = get(key1);
081: if (result == null) {
082: result = new Constant(key1);
083: put(result);
084: }
085: return result;
086: }
087:
088: public Constant newUTF8(final String value) {
089: key1.set('s', value, null, null);
090: Constant result = get(key1);
091: if (result == null) {
092: result = new Constant(key1);
093: put(result);
094: }
095: return result;
096: }
097:
098: private Constant newString(final String value) {
099: key2.set('S', value, null, null);
100: Constant result = get(key2);
101: if (result == null) {
102: newUTF8(value);
103: result = new Constant(key2);
104: put(result);
105: }
106: return result;
107: }
108:
109: public Constant newClass(final String value) {
110: key2.set('C', value, null, null);
111: Constant result = get(key2);
112: if (result == null) {
113: newUTF8(value);
114: result = new Constant(key2);
115: put(result);
116: }
117: return result;
118: }
119:
120: public Constant newConst(final Object cst) {
121: if (cst instanceof Integer) {
122: int val = ((Integer) cst).intValue();
123: return newInteger(val);
124: } else if (cst instanceof Float) {
125: float val = ((Float) cst).floatValue();
126: return newFloat(val);
127: } else if (cst instanceof Long) {
128: long val = ((Long) cst).longValue();
129: return newLong(val);
130: } else if (cst instanceof Double) {
131: double val = ((Double) cst).doubleValue();
132: return newDouble(val);
133: } else if (cst instanceof String) {
134: return newString((String) cst);
135: } else if (cst instanceof Type) {
136: Type t = (Type) cst;
137: return newClass(t.getSort() == Type.OBJECT ? t
138: .getInternalName() : t.getDescriptor());
139: } else {
140: throw new IllegalArgumentException("value " + cst);
141: }
142: }
143:
144: public Constant newField(final String owner, final String name,
145: final String desc) {
146: key3.set('G', owner, name, desc);
147: Constant result = get(key3);
148: if (result == null) {
149: newClass(owner);
150: newNameType(name, desc);
151: result = new Constant(key3);
152: put(result);
153: }
154: return result;
155: }
156:
157: public Constant newMethod(final String owner, final String name,
158: final String desc, final boolean itf) {
159: key3.set(itf ? 'N' : 'M', owner, name, desc);
160: Constant result = get(key3);
161: if (result == null) {
162: newClass(owner);
163: newNameType(name, desc);
164: result = new Constant(key3);
165: put(result);
166: }
167: return result;
168: }
169:
170: public Constant newNameType(final String name, final String desc) {
171: key2.set('T', name, desc, null);
172: Constant result = get(key2);
173: if (result == null) {
174: newUTF8(name);
175: newUTF8(desc);
176: result = new Constant(key2);
177: put(result);
178: }
179: return result;
180: }
181:
182: private Constant get(final Constant key) {
183: return (Constant) get((Object) key);
184: }
185:
186: private void put(final Constant cst) {
187: put(cst, cst);
188: }
189: }
|