1 30 package org.objectweb.asm.optimizer; 31 32 import java.util.HashMap ; 33 34 import org.objectweb.asm.Type; 35 36 41 public class ConstantPool extends HashMap { 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 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 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 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 cst) { 122 if (cst instanceof Integer ) { 123 int val = ((Integer ) cst).intValue(); 124 return newInteger(val); 125 } else if (cst instanceof Float ) { 126 float val = ((Float ) cst).floatValue(); 127 return newFloat(val); 128 } else if (cst instanceof Long ) { 129 long val = ((Long ) cst).longValue(); 130 return newLong(val); 131 } else if (cst instanceof Double ) { 132 double val = ((Double ) cst).doubleValue(); 133 return newDouble(val); 134 } else if (cst instanceof String ) { 135 return newString((String ) 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 ("value " + cst); 143 } 144 } 145 146 public Constant newField( 147 final String owner, 148 final String name, 149 final String 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 owner, 164 final String name, 165 final String 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 name, final String 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 ) key); 193 } 194 195 private void put(final Constant cst) { 196 put(cst, cst); 197 } 198 } | Popular Tags |