123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- /*
- * Javassist, a Java-bytecode translator toolkit.
- * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
- *
- * The contents of this file are subject to the Mozilla Public License Version
- * 1.1 (the "License"); you may not use this file except in compliance with
- * the License. Alternatively, the contents of this file may be used under
- * the terms of the GNU Lesser General Public License Version 2.1 or later.
- *
- * Software distributed under the License is distributed on an "AS IS" basis,
- * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- * for the specific language governing rights and limitations under the
- * License.
- */
-
- package javassist.bytecode.annotation;
-
- import java.io.*;
-
- import javassist.bytecode.ByteArray;
- import javassist.bytecode.ConstPool;
-
- /**
- * A convenience class for constructing a
- * <code>..Annotations_attribute</code>.
- * See the source code of the <code>AnnotationsAttribute.Copier</code> class.
- *
- * <p>The following code snippet is an example of use of this class:
- *
- * <ul><pre>
- * ConstPool pool = ...;
- * output = new ByteArrayOutputStream();
- * writer = new AnnotationsWriter(output, pool);
- *
- * writer.numAnnotations(1);
- * writer.annotation("Author", 2);
- * writer.memberValuePair("name");
- * writer.constValueIndex("chiba");
- * writer.memberValuePair("address");
- * writer.constValueIndex("tokyo");
- *
- * writer.close();
- * byte[] attribute_info = output.toByteArray();
- * AnnotationsAttribute anno
- * = new AnnotationsAttribute(pool, AnnotationsAttribute.visibleTag,
- * attribute_info);
- * </pre></ul>
- *
- * <p>The code snippet above generates the annotation attribute
- * corresponding to this annotation:
- *
- * <ul><pre>
- * @Author(name = "chiba", address = "tokyo")
- * </pre></ul>
- *
- * @see javassist.bytecode.AnnotationsAttribute
- * @see javassist.bytecode.ParameterAnnotationsAttribute
- */
- public class AnnotationsWriter {
- private OutputStream output;
- private ConstPool pool;
-
- /**
- * Constructs with the given output stream.
- *
- * @param os the output stream.
- * @param cp the constant pool.
- */
- public AnnotationsWriter(OutputStream os, ConstPool cp) {
- output = os;
- pool = cp;
- }
-
- /**
- * Obtains the constant pool given to the constructor.
- */
- public ConstPool getConstPool() {
- return pool;
- }
-
- /**
- * Closes the output stream.
- *
- */
- public void close() throws IOException {
- output.close();
- }
-
- /**
- * Writes <code>num_parameters</code> in
- * <code>Runtime(In)VisibleParameterAnnotations_attribute</code>.
- * This method must be followed by <code>num</code> calls to
- * <code>numAnnotations()</code>.
- */
- public void numParameters(int num) throws IOException {
- output.write(num);
- }
-
- /**
- * Writes <code>num_annotations</code> in
- * <code>Runtime(In)VisibleAnnotations_attribute</code>.
- * This method must be followed by <code>num</code> calls to
- * <code>annotation()</code>.
- */
- public void numAnnotations(int num) throws IOException {
- write16bit(num);
- }
-
- /**
- * Writes <code>annotation</code>.
- * This method must be followed by <code>numMemberValuePairs</code>
- * calls to <code>memberValuePair()</code>.
- *
- * @param type the annotation interface name.
- * @param numMemberValuePairs <code>num_member_value_pairs</code>
- * in <code>annotation</code>.
- */
- public void annotation(String type, int numMemberValuePairs)
- throws IOException
- {
- annotation(pool.addUtf8Info(type), numMemberValuePairs);
- }
-
- /**
- * Writes <code>annotation</code>.
- * This method must be followed by <code>numMemberValuePairs</code>
- * calls to <code>memberValuePair()</code>.
- *
- * @param typeIndex <code>type_index</code> in <code>annotation</code>.
- * @param numMemberValuePairs <code>num_member_value_pairs</code>
- * in <code>annotation</code>.
- */
- public void annotation(int typeIndex, int numMemberValuePairs)
- throws IOException
- {
- write16bit(typeIndex);
- write16bit(numMemberValuePairs);
- }
-
- /**
- * Writes an element of a <code>member_value_pairs</code> array
- * in <code>annotation</code>.
- * This method must be followed by a
- * call to <code>constValueIndex()</code>, <code>enumConstValue()</code>,
- * etc.
- *
- * @param memberName the name of the annotation type member.
- */
- public void memberValuePair(String memberName) throws IOException {
- memberValuePair(pool.addUtf8Info(memberName));
- }
-
- /**
- * Writes an element of a <code>member_value_pairs</code> array
- * in <code>annotation</code>.
- * This method must be followed by a
- * call to <code>constValueIndex()</code>, <code>enumConstValue()</code>,
- * etc.
- *
- * @param memberNameIndex <code>member_name_index</code>
- * in <code>member_value_pairs</code> array.
- */
- public void memberValuePair(int memberNameIndex) throws IOException {
- write16bit(memberNameIndex);
- }
-
- /**
- * Writes <code>tag</code> and <code>const_value_index</code>
- * in <code>member_value</code>.
- *
- * @param value the constant value.
- */
- public void constValueIndex(boolean value) throws IOException {
- constValueIndex('Z', pool.addIntegerInfo(value ? 1 : 0));
- }
-
- /**
- * Writes <code>tag</code> and <code>const_value_index</code>
- * in <code>member_value</code>.
- *
- * @param value the constant value.
- */
- public void constValueIndex(byte value) throws IOException {
- constValueIndex('B', pool.addIntegerInfo(value));
- }
-
- /**
- * Writes <code>tag</code> and <code>const_value_index</code>
- * in <code>member_value</code>.
- *
- * @param value the constant value.
- */
- public void constValueIndex(char value) throws IOException {
- constValueIndex('C', pool.addIntegerInfo(value));
- }
-
- /**
- * Writes <code>tag</code> and <code>const_value_index</code>
- * in <code>member_value</code>.
- *
- * @param value the constant value.
- */
- public void constValueIndex(short value) throws IOException {
- constValueIndex('S', pool.addIntegerInfo(value));
- }
-
- /**
- * Writes <code>tag</code> and <code>const_value_index</code>
- * in <code>member_value</code>.
- *
- * @param value the constant value.
- */
- public void constValueIndex(int value) throws IOException {
- constValueIndex('I', pool.addIntegerInfo(value));
- }
-
- /**
- * Writes <code>tag</code> and <code>const_value_index</code>
- * in <code>member_value</code>.
- *
- * @param value the constant value.
- */
- public void constValueIndex(long value) throws IOException {
- constValueIndex('J', pool.addLongInfo(value));
- }
-
- /**
- * Writes <code>tag</code> and <code>const_value_index</code>
- * in <code>member_value</code>.
- *
- * @param value the constant value.
- */
- public void constValueIndex(float value) throws IOException {
- constValueIndex('F', pool.addFloatInfo(value));
- }
-
- /**
- * Writes <code>tag</code> and <code>const_value_index</code>
- * in <code>member_value</code>.
- *
- * @param value the constant value.
- */
- public void constValueIndex(double value) throws IOException {
- constValueIndex('D', pool.addDoubleInfo(value));
- }
-
- /**
- * Writes <code>tag</code> and <code>const_value_index</code>
- * in <code>member_value</code>.
- *
- * @param value the constant value.
- */
- public void constValueIndex(String value) throws IOException {
- constValueIndex('s', pool.addUtf8Info(value));
- }
-
- /**
- * Writes <code>tag</code> and <code>const_value_index</code>
- * in <code>member_value</code>.
- *
- * @param tag <code>tag</code> in <code>member_value</code>.
- * @param index <code>const_value_index</code>
- * in <code>member_value</code>.
- */
- public void constValueIndex(int tag, int index)
- throws IOException
- {
- output.write(tag);
- write16bit(index);
- }
-
- /**
- * Writes <code>tag</code> and <code>enum_const_value</code>
- * in <code>member_value</code>.
- *
- * @param typeName the type name of the enum constant.
- * @param constName the simple name of the enum constant.
- */
- public void enumConstValue(String typeName, String constName)
- throws IOException
- {
- enumConstValue(pool.addUtf8Info(typeName),
- pool.addUtf8Info(constName));
- }
-
- /**
- * Writes <code>tag</code> and <code>enum_const_value</code>
- * in <code>member_value</code>.
- *
- * @param typeNameIndex <code>type_name_index</code>
- * in <code>member_value</code>.
- * @param constNameIndex <code>const_name_index</code>
- * in <code>member_value</code>.
- */
- public void enumConstValue(int typeNameIndex, int constNameIndex)
- throws IOException
- {
- output.write('e');
- write16bit(typeNameIndex);
- write16bit(constNameIndex);
- }
-
- /**
- * Writes <code>tag</code> and <code>class_info_index</code>
- * in <code>member_value</code>.
- *
- * @param name the class name.
- */
- public void classInfoIndex(String name) throws IOException {
- classInfoIndex(pool.addUtf8Info(name));
- }
-
- /**
- * Writes <code>tag</code> and <code>class_info_index</code>
- * in <code>member_value</code>.
- *
- * @param index <code>class_info_index</code>
- */
- public void classInfoIndex(int index) throws IOException {
- output.write('c');
- write16bit(index);
- }
-
- /**
- * Writes <code>tag</code> and <code>annotation_value</code>
- * in <code>member_value</code>.
- * This method must be followed by a call to <code>annotation()</code>.
- */
- public void annotationValue() throws IOException {
- output.write('@');
- }
-
- /**
- * Writes <code>tag</code> and <code>array_value</code>
- * in <code>member_value</code>.
- * This method must be followed by <code>numValues</code> calls
- * to <code>constValueIndex()</code>, <code>enumConstValue()</code>,
- * etc.
- *
- * @param numValues <code>num_values</code>
- * in <code>array_value</code>.
- */
- public void arrayValue(int numValues) throws IOException {
- output.write('[');
- write16bit(numValues);
- }
-
- private void write16bit(int value) throws IOException {
- byte[] buf = new byte[2];
- ByteArray.write16bit(value, buf, 0);
- output.write(buf);
- }
- }
|