All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javassist.bytecode.annotation.AnnotationsWriter Maven / Gradle / Ivy

There is a newer version: 2.22.2
Show newest version
/*
 * Javassist, a Java-bytecode translator toolkit.
 * Copyright (C) 1999- 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,
 * or the Apache License Version 2.0.
 *
 * 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
 * ..Annotations_attribute.
 * See the source code of the AnnotationsAttribute.Copier class.
 *
 * 

The following code snippet is an example of use of this class: * *

     * 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);
     * 
* *

The code snippet above generates the annotation attribute * corresponding to this annotation: * *

     *  @Author(name = "chiba", address = "tokyo")
     * 
* * @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 num_parameters in * Runtime(In)VisibleParameterAnnotations_attribute. * This method must be followed by num calls to * numAnnotations(). */ public void numParameters(int num) throws IOException { output.write(num); } /** * Writes num_annotations in * Runtime(In)VisibleAnnotations_attribute. * This method must be followed by num calls to * annotation(). */ public void numAnnotations(int num) throws IOException { write16bit(num); } /** * Writes annotation. * This method must be followed by numMemberValuePairs * calls to memberValuePair(). * * @param type the annotation interface name. * @param numMemberValuePairs num_member_value_pairs * in annotation. */ public void annotation(String type, int numMemberValuePairs) throws IOException { annotation(pool.addUtf8Info(type), numMemberValuePairs); } /** * Writes annotation. * This method must be followed by numMemberValuePairs * calls to memberValuePair(). * * @param typeIndex type_index in annotation. * @param numMemberValuePairs num_member_value_pairs * in annotation. */ public void annotation(int typeIndex, int numMemberValuePairs) throws IOException { write16bit(typeIndex); write16bit(numMemberValuePairs); } /** * Writes an element of a member_value_pairs array * in annotation. * This method must be followed by a * call to constValueIndex(), enumConstValue(), * 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 member_value_pairs array * in annotation. * This method must be followed by a * call to constValueIndex(), enumConstValue(), * etc. * * @param memberNameIndex member_name_index * in member_value_pairs array. */ public void memberValuePair(int memberNameIndex) throws IOException { write16bit(memberNameIndex); } /** * Writes tag and const_value_index * in member_value. * * @param value the constant value. */ public void constValueIndex(boolean value) throws IOException { constValueIndex('Z', pool.addIntegerInfo(value ? 1 : 0)); } /** * Writes tag and const_value_index * in member_value. * * @param value the constant value. */ public void constValueIndex(byte value) throws IOException { constValueIndex('B', pool.addIntegerInfo(value)); } /** * Writes tag and const_value_index * in member_value. * * @param value the constant value. */ public void constValueIndex(char value) throws IOException { constValueIndex('C', pool.addIntegerInfo(value)); } /** * Writes tag and const_value_index * in member_value. * * @param value the constant value. */ public void constValueIndex(short value) throws IOException { constValueIndex('S', pool.addIntegerInfo(value)); } /** * Writes tag and const_value_index * in member_value. * * @param value the constant value. */ public void constValueIndex(int value) throws IOException { constValueIndex('I', pool.addIntegerInfo(value)); } /** * Writes tag and const_value_index * in member_value. * * @param value the constant value. */ public void constValueIndex(long value) throws IOException { constValueIndex('J', pool.addLongInfo(value)); } /** * Writes tag and const_value_index * in member_value. * * @param value the constant value. */ public void constValueIndex(float value) throws IOException { constValueIndex('F', pool.addFloatInfo(value)); } /** * Writes tag and const_value_index * in member_value. * * @param value the constant value. */ public void constValueIndex(double value) throws IOException { constValueIndex('D', pool.addDoubleInfo(value)); } /** * Writes tag and const_value_index * in member_value. * * @param value the constant value. */ public void constValueIndex(String value) throws IOException { constValueIndex('s', pool.addUtf8Info(value)); } /** * Writes tag and const_value_index * in member_value. * * @param tag tag in member_value. * @param index const_value_index * in member_value. */ public void constValueIndex(int tag, int index) throws IOException { output.write(tag); write16bit(index); } /** * Writes tag and enum_const_value * in member_value. * * @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 tag and enum_const_value * in member_value. * * @param typeNameIndex type_name_index * in member_value. * @param constNameIndex const_name_index * in member_value. */ public void enumConstValue(int typeNameIndex, int constNameIndex) throws IOException { output.write('e'); write16bit(typeNameIndex); write16bit(constNameIndex); } /** * Writes tag and class_info_index * in member_value. * * @param name the class name. */ public void classInfoIndex(String name) throws IOException { classInfoIndex(pool.addUtf8Info(name)); } /** * Writes tag and class_info_index * in member_value. * * @param index class_info_index */ public void classInfoIndex(int index) throws IOException { output.write('c'); write16bit(index); } /** * Writes tag and annotation_value * in member_value. * This method must be followed by a call to annotation(). */ public void annotationValue() throws IOException { output.write('@'); } /** * Writes tag and array_value * in member_value. * This method must be followed by numValues calls * to constValueIndex(), enumConstValue(), * etc. * * @param numValues num_values * in array_value. */ 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); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy