1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
/*
* Javassist, a Java-bytecode translator toolkit.
* Copyright (C) 1999-2004 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;
import java.util.Map;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.ByteArrayOutputStream;
import javassist.bytecode.AnnotationsAttribute.Copier;
import javassist.bytecode.AnnotationsAttribute.Parser;
import javassist.bytecode.annotation.*;
/**
* A class representing <code>RuntimeVisibleAnnotations_attribute</code> and
* <code>RuntimeInvisibleAnnotations_attribute</code>.
*
* <p>To obtain an ParameterAnnotationAttribute object, invoke
* <code>getAttribute(ParameterAnnotationsAttribute.invisibleTag)</code>
* in <code>MethodInfo</code>.
* The obtained attribute is a
* runtime invisible annotations attribute.
* If the parameter is
* <code>ParameterAnnotationAttribute.visibleTag</code>, then the obtained
* attribute is a runtime visible one.
*/
public class ParameterAnnotationsAttribute extends AttributeInfo {
/**
* The name of the <code>RuntimeVisibleParameterAnnotations</code>
* attribute.
*/
public static final String visibleTag
= "RuntimeVisibleParameterAnnotations";
/**
* The name of the <code>RuntimeInvisibleParameterAnnotations</code>
* attribute.
*/
public static final String invisibleTag
= "RuntimeInvisibleParameterAnnotations";
/**
* Constructs
* a <code>Runtime(In)VisisbleParameterAnnotations_attribute</code>.
*
* @param cp constant pool
* @param attrname attribute name (<code>visibleTag</code> or
* <code>invisibleTag</code>).
* @param info the contents of this attribute. It does not
* include <code>attribute_name_index</code> or
* <code>attribute_length</code>.
*/
public ParameterAnnotationsAttribute(ConstPool cp, String attrname,
byte[] info) {
super(cp, attrname, info);
}
/**
* Constructs an empty
* <code>Runtime(In)VisisbleParameterAnnotations_attribute</code>.
* A new annotation can be later added to the created attribute
* by <code>setAnnotations()</code>.
*
* @param cp constant pool
* @param attrname attribute name (<code>visibleTag</code> or
* <code>invisibleTag</code>).
* @see #setAnnotations(Annotation[][])
*/
public ParameterAnnotationsAttribute(ConstPool cp, String attrname) {
this(cp, attrname, new byte[] { 0 });
}
/**
* @param n the attribute name.
*/
ParameterAnnotationsAttribute(ConstPool cp, int n, DataInputStream in)
throws IOException
{
super(cp, n, in);
}
/**
* Returns <code>num_parameters</code>.
*/
public int numParameters() {
return info[0] & 0xff;
}
/**
* Copies this attribute and returns a new copy.
*/
public AttributeInfo copy(ConstPool newCp, Map classnames) {
Copier copier = new Copier(info, constPool, newCp, classnames);
try {
copier.parameters();
return new ParameterAnnotationsAttribute(newCp, getName(),
copier.close());
}
catch (Exception e) {
throw new RuntimeException(e.toString());
}
}
/**
* Parses the annotations and returns a data structure representing
* that parsed annotations. Note that changes of the node values of the
* returned tree are not reflected on the annotations represented by
* this object unless the tree is copied back to this object by
* <code>setAnnotations()</code>.
*
* @return Each element of the returned array represents an array of
* annotations that are associated with each method parameter.
*
* @see #setAnnotations(Annotation[][])
*/
public Annotation[][] getAnnotations() {
try {
return new Parser(info, constPool).parseParameters();
}
catch (Exception e) {
throw new RuntimeException(e.toString());
}
}
/**
* Changes the annotations represented by this object according to
* the given array of <code>Annotation</code> objects.
*
* @param params the data structure representing the
* new annotations. Every element of this array
* is an array of <code>Annotation</code> and
* it represens annotations of each method parameter.
*/
public void setAnnotations(Annotation[][] params) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
AnnotationsWriter writer = new AnnotationsWriter(output, constPool);
try {
int n = params.length;
writer.numParameters(n);
for (int i = 0; i < n; ++i) {
Annotation[] anno = params[i];
writer.numAnnotations(anno.length);
for (int j = 0; j < anno.length; ++j)
anno[j].write(writer);
}
writer.close();
}
catch (IOException e) {
throw new RuntimeException(e); // should never reach here.
}
set(output.toByteArray());
}
}
|