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
22package com.sun.org.apache.bcel.internal.classfile;
23
24import java.io.DataInput;
25import java.io.DataOutputStream;
26import java.io.IOException;
27
28/**
29 * base class for annotations
30 *
31 * @version $Id: Annotations
32 * @since 6.0
33 */
34public abstract class Annotations extends Attribute {
35
36    private AnnotationEntry[] annotation_table;
37    private final boolean isRuntimeVisible;
38
39    /**
40     * @param annotation_type the subclass type of the annotation
41     * @param name_index Index pointing to the name <em>Code</em>
42     * @param length Content length in bytes
43     * @param input Input stream
44     * @param constant_pool Array of constants
45     */
46    Annotations(final byte annotation_type, final int name_index, final int length, final DataInput input,
47            final ConstantPool constant_pool, final boolean isRuntimeVisible) throws IOException {
48        this(annotation_type, name_index, length, (AnnotationEntry[]) null, constant_pool, isRuntimeVisible);
49        final int annotation_table_length = input.readUnsignedShort();
50        annotation_table = new AnnotationEntry[annotation_table_length];
51        for (int i = 0; i < annotation_table_length; i++) {
52            annotation_table[i] = AnnotationEntry.read(input, constant_pool, isRuntimeVisible);
53        }
54    }
55
56    /**
57     * @param annotation_type the subclass type of the annotation
58     * @param name_index Index pointing to the name <em>Code</em>
59     * @param length Content length in bytes
60     * @param annotation_table the actual annotations
61     * @param constant_pool Array of constants
62     */
63    public Annotations(final byte annotation_type, final int name_index, final int length, final AnnotationEntry[] annotation_table,
64            final ConstantPool constant_pool, final boolean isRuntimeVisible) {
65        super(annotation_type, name_index, length, constant_pool);
66        this.annotation_table = annotation_table;
67        this.isRuntimeVisible = isRuntimeVisible;
68    }
69
70    /**
71     * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class.
72     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
73     *
74     * @param v Visitor object
75     */
76    @Override
77    public void accept(final Visitor v) {
78        v.visitAnnotation(this);
79    }
80
81    /**
82     * @param annotation_table the entries to set in this annotation
83     */
84    public final void setAnnotationTable(final AnnotationEntry[] annotation_table) {
85        this.annotation_table = annotation_table;
86    }
87
88    /**
89     * returns the array of annotation entries in this annotation
90     */
91    public AnnotationEntry[] getAnnotationEntries() {
92        return annotation_table;
93    }
94
95    /**
96     * @return the number of annotation entries in this annotation
97     */
98    public final int getNumAnnotations() {
99        if (annotation_table == null) {
100            return 0;
101        }
102        return annotation_table.length;
103    }
104
105    public boolean isRuntimeVisible() {
106        return isRuntimeVisible;
107    }
108
109    protected void writeAnnotations(final DataOutputStream dos) throws IOException {
110        if (annotation_table == null) {
111            return;
112        }
113        dos.writeShort(annotation_table.length);
114        for (final AnnotationEntry element : annotation_table) {
115            element.dump(dos);
116        }
117    }
118}
119