46174
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
7 |
* contributor license agreements. See the NOTICE file distributed with
|
|
8 |
* this work for additional information regarding copyright ownership.
|
|
9 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
10 |
* (the "License"); you may not use this file except in compliance with
|
|
11 |
* the License. You may obtain a copy of the License at
|
|
12 |
*
|
|
13 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
14 |
*
|
|
15 |
* Unless required by applicable law or agreed to in writing, software
|
|
16 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
18 |
* See the License for the specific language governing permissions and
|
|
19 |
* limitations under the License.
|
|
20 |
*/
|
|
21 |
|
|
22 |
package com.sun.org.apache.bcel.internal.classfile;
|
|
23 |
|
|
24 |
import java.io.DataInput;
|
|
25 |
import java.io.DataOutputStream;
|
|
26 |
import java.io.IOException;
|
|
27 |
import java.util.ArrayList;
|
|
28 |
import java.util.Collections;
|
|
29 |
import java.util.List;
|
|
30 |
|
|
31 |
import com.sun.org.apache.bcel.internal.Const;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* represents one annotation in the annotation table
|
|
35 |
*
|
|
36 |
* @version $Id: AnnotationEntry
|
|
37 |
* @since 6.0
|
|
38 |
*/
|
|
39 |
public class AnnotationEntry implements Node {
|
|
40 |
|
|
41 |
private final int type_index;
|
|
42 |
private final ConstantPool constant_pool;
|
|
43 |
private final boolean isRuntimeVisible;
|
|
44 |
|
|
45 |
private List<ElementValuePair> element_value_pairs;
|
|
46 |
|
|
47 |
/*
|
|
48 |
* Factory method to create an AnnotionEntry from a DataInput
|
|
49 |
*
|
|
50 |
* @param input
|
|
51 |
* @param constant_pool
|
|
52 |
* @param isRuntimeVisible
|
|
53 |
* @return the entry
|
|
54 |
* @throws IOException
|
|
55 |
*/
|
|
56 |
public static AnnotationEntry read(final DataInput input, final ConstantPool constant_pool, final boolean isRuntimeVisible) throws IOException {
|
|
57 |
|
|
58 |
final AnnotationEntry annotationEntry = new AnnotationEntry(input.readUnsignedShort(), constant_pool, isRuntimeVisible);
|
|
59 |
final int num_element_value_pairs = input.readUnsignedShort();
|
|
60 |
annotationEntry.element_value_pairs = new ArrayList<>();
|
|
61 |
for (int i = 0; i < num_element_value_pairs; i++) {
|
|
62 |
annotationEntry.element_value_pairs.add(
|
|
63 |
new ElementValuePair(input.readUnsignedShort(), ElementValue.readElementValue(input, constant_pool),
|
|
64 |
constant_pool));
|
|
65 |
}
|
|
66 |
return annotationEntry;
|
|
67 |
}
|
|
68 |
|
|
69 |
public AnnotationEntry(final int type_index, final ConstantPool constant_pool, final boolean isRuntimeVisible) {
|
|
70 |
this.type_index = type_index;
|
|
71 |
this.constant_pool = constant_pool;
|
|
72 |
this.isRuntimeVisible = isRuntimeVisible;
|
|
73 |
}
|
|
74 |
|
|
75 |
public int getTypeIndex() {
|
|
76 |
return type_index;
|
|
77 |
}
|
|
78 |
|
|
79 |
public ConstantPool getConstantPool() {
|
|
80 |
return constant_pool;
|
|
81 |
}
|
|
82 |
|
|
83 |
public boolean isRuntimeVisible() {
|
|
84 |
return isRuntimeVisible;
|
|
85 |
}
|
|
86 |
|
|
87 |
/**
|
|
88 |
* Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class.
|
|
89 |
* I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
|
|
90 |
*
|
|
91 |
* @param v Visitor object
|
|
92 |
*/
|
|
93 |
@Override
|
|
94 |
public void accept(final Visitor v) {
|
|
95 |
v.visitAnnotationEntry(this);
|
|
96 |
}
|
|
97 |
|
|
98 |
/**
|
|
99 |
* @return the annotation type name
|
|
100 |
*/
|
|
101 |
public String getAnnotationType() {
|
|
102 |
final ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(type_index, Const.CONSTANT_Utf8);
|
|
103 |
return c.getBytes();
|
|
104 |
}
|
|
105 |
|
|
106 |
/**
|
|
107 |
* @return the annotation type index
|
|
108 |
*/
|
|
109 |
public int getAnnotationTypeIndex() {
|
|
110 |
return type_index;
|
|
111 |
}
|
|
112 |
|
|
113 |
/**
|
|
114 |
* @return the number of element value pairs in this annotation entry
|
|
115 |
*/
|
|
116 |
public final int getNumElementValuePairs() {
|
|
117 |
return element_value_pairs.size();
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* @return the element value pairs in this annotation entry
|
|
122 |
*/
|
|
123 |
public ElementValuePair[] getElementValuePairs() {
|
|
124 |
// TODO return List
|
|
125 |
return element_value_pairs.toArray(new ElementValuePair[element_value_pairs.size()]);
|
|
126 |
}
|
|
127 |
|
|
128 |
public void dump(final DataOutputStream dos) throws IOException {
|
|
129 |
dos.writeShort(type_index); // u2 index of type name in cpool
|
|
130 |
dos.writeShort(element_value_pairs.size()); // u2 element_value pair
|
|
131 |
// count
|
|
132 |
for (final ElementValuePair envp : element_value_pairs) {
|
|
133 |
envp.dump(dos);
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
public void addElementNameValuePair(final ElementValuePair elementNameValuePair) {
|
|
138 |
element_value_pairs.add(elementNameValuePair);
|
|
139 |
}
|
|
140 |
|
|
141 |
public String toShortString() {
|
|
142 |
final StringBuilder result = new StringBuilder();
|
|
143 |
result.append("@");
|
|
144 |
result.append(getAnnotationType());
|
|
145 |
final ElementValuePair[] evPairs = getElementValuePairs();
|
|
146 |
if (evPairs.length > 0) {
|
|
147 |
result.append("(");
|
|
148 |
for (final ElementValuePair element : evPairs) {
|
|
149 |
result.append(element.toShortString());
|
|
150 |
}
|
|
151 |
result.append(")");
|
|
152 |
}
|
|
153 |
return result.toString();
|
|
154 |
}
|
|
155 |
|
|
156 |
@Override
|
|
157 |
public String toString() {
|
|
158 |
return toShortString();
|
|
159 |
}
|
|
160 |
|
|
161 |
public static AnnotationEntry[] createAnnotationEntries(final Attribute[] attrs) {
|
|
162 |
// Find attributes that contain annotation data
|
|
163 |
final List<AnnotationEntry> accumulatedAnnotations = new ArrayList<>(attrs.length);
|
|
164 |
for (final Attribute attribute : attrs) {
|
|
165 |
if (attribute instanceof Annotations) {
|
|
166 |
final Annotations runtimeAnnotations = (Annotations) attribute;
|
|
167 |
Collections.addAll(accumulatedAnnotations, runtimeAnnotations.getAnnotationEntries());
|
|
168 |
}
|
|
169 |
}
|
|
170 |
return accumulatedAnnotations.toArray(new AnnotationEntry[accumulatedAnnotations.size()]);
|
|
171 |
}
|
|
172 |
}
|