KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > bytecode > ParameterAnnotationsAttribute


1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. Alternatively, the contents of this file may be used under
8  * the terms of the GNU Lesser General Public License Version 2.1 or later.
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  */

15
16 package javassist.bytecode;
17
18 import java.util.Map JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.DataInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22
23 import javassist.bytecode.AnnotationsAttribute.Copier;
24 import javassist.bytecode.AnnotationsAttribute.Parser;
25 import javassist.bytecode.annotation.*;
26
27 /**
28  * A class representing <code>RuntimeVisibleAnnotations_attribute</code> and
29  * <code>RuntimeInvisibleAnnotations_attribute</code>.
30  *
31  * <p>To obtain an ParameterAnnotationAttribute object, invoke
32  * <code>getAttribute(ParameterAnnotationsAttribute.invisibleTag)</code>
33  * in <code>MethodInfo</code>.
34  * The obtained attribute is a
35  * runtime invisible annotations attribute.
36  * If the parameter is
37  * <code>ParameterAnnotationAttribute.visibleTag</code>, then the obtained
38  * attribute is a runtime visible one.
39  */

40 public class ParameterAnnotationsAttribute extends AttributeInfo {
41     /**
42      * The name of the <code>RuntimeVisibleParameterAnnotations</code>
43      * attribute.
44      */

45     public static final String JavaDoc visibleTag
46         = "RuntimeVisibleParameterAnnotations";
47
48     /**
49      * The name of the <code>RuntimeInvisibleParameterAnnotations</code>
50      * attribute.
51      */

52     public static final String JavaDoc invisibleTag
53         = "RuntimeInvisibleParameterAnnotations";
54     /**
55      * Constructs
56      * a <code>Runtime(In)VisisbleParameterAnnotations_attribute</code>.
57      *
58      * @param cp constant pool
59      * @param attrname attribute name (<code>visibleTag</code> or
60      * <code>invisibleTag</code>).
61      * @param info the contents of this attribute. It does not
62      * include <code>attribute_name_index</code> or
63      * <code>attribute_length</code>.
64      */

65     public ParameterAnnotationsAttribute(ConstPool cp, String JavaDoc attrname,
66                                          byte[] info) {
67         super(cp, attrname, info);
68     }
69
70     /**
71      * Constructs an empty
72      * <code>Runtime(In)VisisbleParameterAnnotations_attribute</code>.
73      * A new annotation can be later added to the created attribute
74      * by <code>setAnnotations()</code>.
75      *
76      * @param cp constant pool
77      * @param attrname attribute name (<code>visibleTag</code> or
78      * <code>invisibleTag</code>).
79      * @see #setAnnotations(Annotation[][])
80      */

81     public ParameterAnnotationsAttribute(ConstPool cp, String JavaDoc attrname) {
82         this(cp, attrname, new byte[] { 0 });
83     }
84
85     /**
86      * @param n the attribute name.
87      */

88     ParameterAnnotationsAttribute(ConstPool cp, int n, DataInputStream JavaDoc in)
89         throws IOException JavaDoc
90     {
91         super(cp, n, in);
92     }
93
94     /**
95      * Returns <code>num_parameters</code>.
96      */

97     public int numParameters() {
98         return info[0] & 0xff;
99     }
100
101     /**
102      * Copies this attribute and returns a new copy.
103      */

104     public AttributeInfo copy(ConstPool newCp, Map JavaDoc classnames) {
105         Copier copier = new Copier(info, constPool, newCp, classnames);
106         try {
107             copier.parameters();
108             return new ParameterAnnotationsAttribute(newCp, getName(),
109                                                      copier.close());
110         }
111         catch (Exception JavaDoc e) {
112             throw new RuntimeException JavaDoc(e.toString());
113         }
114     }
115
116     /**
117      * Parses the annotations and returns a data structure representing
118      * that parsed annotations. Note that changes of the node values of the
119      * returned tree are not reflected on the annotations represented by
120      * this object unless the tree is copied back to this object by
121      * <code>setAnnotations()</code>.
122      *
123      * @return Each element of the returned array represents an array of
124      * annotations that are associated with each method parameter.
125      *
126      * @see #setAnnotations(Annotation[][])
127      */

128     public Annotation[][] getAnnotations() {
129         try {
130             return new Parser(info, constPool).parseParameters();
131         }
132         catch (Exception JavaDoc e) {
133             throw new RuntimeException JavaDoc(e.toString());
134         }
135     }
136
137     /**
138      * Changes the annotations represented by this object according to
139      * the given array of <code>Annotation</code> objects.
140      *
141      * @param params the data structure representing the
142      * new annotations. Every element of this array
143      * is an array of <code>Annotation</code> and
144      * it represens annotations of each method parameter.
145      */

146     public void setAnnotations(Annotation[][] params) {
147         ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
148         AnnotationsWriter writer = new AnnotationsWriter(output, constPool);
149         try {
150             int n = params.length;
151             writer.numParameters(n);
152             for (int i = 0; i < n; ++i) {
153                 Annotation[] anno = params[i];
154                 writer.numAnnotations(anno.length);
155                 for (int j = 0; j < anno.length; ++j)
156                     anno[j].write(writer);
157             }
158
159             writer.close();
160         }
161         catch (IOException JavaDoc e) {
162             throw new RuntimeException JavaDoc(e); // should never reach here.
163
}
164
165         set(output.toByteArray());
166     }
167 }
168
Popular Tags