Debian Codegraph @

src/main/java/org/jboss/classfilewriter/annotations/ParameterAnnotationsAttribute.java

raw
1 /*
2 * JBoss, Home of Professional Open Source.
3 *
4 * Copyright 2012 Red Hat, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.jboss.classfilewriter.annotations;
19
20 import org.jboss.classfilewriter.attributes.Attribute;
21 import org.jboss.classfilewriter.constpool.ConstPool;
22 import org.jboss.classfilewriter.util.ByteArrayDataOutputStream;
23 import org.jboss.classfilewriter.util.LazySize;
24
25 import java.io.IOException;
26 import java.lang.annotation.Annotation;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 /**
33 * A parameter annotations attribute
34 *
35 * @author Stuart Douglas
36 *
37 */
38 public class ParameterAnnotationsAttribute extends Attribute {
39
40 public enum Type {
41 RUNTIME_VISIBLE("RuntimeVisibleParameterAnnotations"), RUNTIME_INVISIBLE("RuntimeInvisibleParameterAnnotations");
42
43 Type(String tag) {
44 this.tag = tag;
45 }
46
47 private final String tag;
48
49 public String getTag() {
50 return tag;
51 }
52 }
53
54 private final Map<Integer, List<ClassAnnotation>> annotations;
55 private final int noParameters;
56
57 public ParameterAnnotationsAttribute(Type type, ConstPool constPool, int noParameters) {
58 super(type.getTag(), constPool);
59 this.annotations = new HashMap<Integer, List<ClassAnnotation>>();
60 this.noParameters = noParameters;
61 }
62
63
64 @Override
65 public void writeData(ByteArrayDataOutputStream stream) throws IOException {
66 LazySize sizeMarker = stream.writeSize();
67 stream.writeByte(noParameters);
68 for(int i = 0; i < noParameters; ++ i) {
69 if(!annotations.containsKey(i)) {
70 stream.writeShort(0);
71 } else {
72 List<ClassAnnotation> ans = annotations.get(i);
73 stream.writeShort(ans.size());
74 for (ClassAnnotation annotation : ans) {
75 annotation.write(stream);
76 }
77 }
78 }
79 sizeMarker.markEnd();
80 }
81
82 public void addAnnotation(int parameter, Annotation annotation) {
83 if (!annotations.containsKey(parameter)) {
84 annotations.put(parameter, new ArrayList<ClassAnnotation>());
85 }
86 annotations.get(parameter).add(AnnotationBuilder.createAnnotation(constPool, annotation));
87 }
88
89 public void addAnnotation(int parameter, ClassAnnotation annotation) {
90 if (!annotations.containsKey(parameter)) {
91 annotations.put(parameter, new ArrayList<ClassAnnotation>());
92 }
93 annotations.get(parameter).add(annotation);
94 }
95
96 }