1 15 16 package javassist.bytecode.annotation; 17 18 import java.io.*; 19 20 import javassist.bytecode.ByteArray; 21 import javassist.bytecode.ConstPool; 22 23 59 public class AnnotationsWriter { 60 private OutputStream output; 61 private ConstPool pool; 62 63 69 public AnnotationsWriter(OutputStream os, ConstPool cp) { 70 output = os; 71 pool = cp; 72 } 73 74 77 public ConstPool getConstPool() { 78 return pool; 79 } 80 81 85 public void close() throws IOException { 86 output.close(); 87 } 88 89 95 public void numParameters(int num) throws IOException { 96 output.write(num); 97 } 98 99 105 public void numAnnotations(int num) throws IOException { 106 write16bit(num); 107 } 108 109 118 public void annotation(String type, int numMemberValuePairs) 119 throws IOException 120 { 121 annotation(pool.addUtf8Info(type), numMemberValuePairs); 122 } 123 124 133 public void annotation(int typeIndex, int numMemberValuePairs) 134 throws IOException 135 { 136 write16bit(typeIndex); 137 write16bit(numMemberValuePairs); 138 } 139 140 149 public void memberValuePair(String memberName) throws IOException { 150 memberValuePair(pool.addUtf8Info(memberName)); 151 } 152 153 163 public void memberValuePair(int memberNameIndex) throws IOException { 164 write16bit(memberNameIndex); 165 } 166 167 173 public void constValueIndex(boolean value) throws IOException { 174 constValueIndex('Z', pool.addIntegerInfo(value ? 1 : 0)); 175 } 176 177 183 public void constValueIndex(byte value) throws IOException { 184 constValueIndex('B', pool.addIntegerInfo(value)); 185 } 186 187 193 public void constValueIndex(char value) throws IOException { 194 constValueIndex('C', pool.addIntegerInfo(value)); 195 } 196 197 203 public void constValueIndex(short value) throws IOException { 204 constValueIndex('S', pool.addIntegerInfo(value)); 205 } 206 207 213 public void constValueIndex(int value) throws IOException { 214 constValueIndex('I', pool.addIntegerInfo(value)); 215 } 216 217 223 public void constValueIndex(long value) throws IOException { 224 constValueIndex('J', pool.addLongInfo(value)); 225 } 226 227 233 public void constValueIndex(float value) throws IOException { 234 constValueIndex('F', pool.addFloatInfo(value)); 235 } 236 237 243 public void constValueIndex(double value) throws IOException { 244 constValueIndex('D', pool.addDoubleInfo(value)); 245 } 246 247 253 public void constValueIndex(String value) throws IOException { 254 constValueIndex('s', pool.addUtf8Info(value)); 255 } 256 257 265 public void constValueIndex(int tag, int index) 266 throws IOException 267 { 268 output.write(tag); 269 write16bit(index); 270 } 271 272 279 public void enumConstValue(String typeName, String constName) 280 throws IOException 281 { 282 enumConstValue(pool.addUtf8Info(typeName), 283 pool.addUtf8Info(constName)); 284 } 285 286 295 public void enumConstValue(int typeNameIndex, int constNameIndex) 296 throws IOException 297 { 298 output.write('e'); 299 write16bit(typeNameIndex); 300 write16bit(constNameIndex); 301 } 302 303 309 public void classInfoIndex(String name) throws IOException { 310 classInfoIndex(pool.addUtf8Info(name)); 311 } 312 313 319 public void classInfoIndex(int index) throws IOException { 320 output.write('c'); 321 write16bit(index); 322 } 323 324 329 public void annotationValue() throws IOException { 330 output.write('@'); 331 } 332 333 343 public void arrayValue(int numValues) throws IOException { 344 output.write('['); 345 write16bit(numValues); 346 } 347 348 private void write16bit(int value) throws IOException { 349 byte[] buf = new byte[2]; 350 ByteArray.write16bit(value, buf, 0); 351 output.write(buf); 352 } 353 } 354 | Popular Tags |