Debian Codegraph @

src/main/java/org/jboss/classfilewriter/annotations/AnnotationsAttribute.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.List;
29
30 /**
31 * An annotations attribute
32 *
33 * @author Stuart Douglas
34 *
35 */
36 public class AnnotationsAttribute extends Attribute {
37
38 public enum Type {
39 RUNTIME_VISIBLE("RuntimeVisibleAnnotations"), RUNTIME_INVISIBLE("RuntimeInvisibleAnnotations");
40
41 Type(String tag) {
42 this.tag = tag;
43 }
44
45 private final String tag;
46
47 public String getTag() {
48 return tag;
49 }
50 }
51
52 private final List<ClassAnnotation> annotations;
53
54 public AnnotationsAttribute(Type type, ConstPool constPool) {
55 super(type.getTag(), constPool);
56 this.annotations = new ArrayList<ClassAnnotation>();
57 }
58
59
60 @Override
61 public void writeData(ByteArrayDataOutputStream stream) throws IOException {
62 LazySize sizeMarker = stream.writeSize();
63 stream.writeShort(annotations.size());
64 for (ClassAnnotation annotation : annotations) {
65 annotation.write(stream);
66 }
67 sizeMarker.markEnd();
68 }
69
70 public void addAnnotation(Annotation annotation) {
71 annotations.add(AnnotationBuilder.createAnnotation(constPool, annotation));
72 }
73
74 public void addAnnotation(ClassAnnotation annotation) {
75 annotations.add(annotation);
76 }
77
78 }