View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  /**
25   * base class for annotations
26   *
27   * @version $Id: Annotations
28   * @since 6.0
29   */
30  public abstract class Annotations extends Attribute {
31  
32      private AnnotationEntry[] annotation_table;
33      private final boolean isRuntimeVisible;
34  
35      /**
36       * @param annotation_type the subclass type of the annotation
37       * @param name_index Index pointing to the name <em>Code</em>
38       * @param length Content length in bytes
39       * @param input Input stream
40       * @param constant_pool Array of constants
41       */
42      Annotations(final byte annotation_type, final int name_index, final int length, final DataInput input,
43              final ConstantPool constant_pool, final boolean isRuntimeVisible) throws IOException {
44          this(annotation_type, name_index, length, (AnnotationEntry[]) null, constant_pool, isRuntimeVisible);
45          final int annotation_table_length = input.readUnsignedShort();
46          annotation_table = new AnnotationEntry[annotation_table_length];
47          for (int i = 0; i < annotation_table_length; i++) {
48              annotation_table[i] = AnnotationEntry.read(input, constant_pool, isRuntimeVisible);
49          }
50      }
51  
52      /**
53       * @param annotation_type the subclass type of the annotation
54       * @param name_index Index pointing to the name <em>Code</em>
55       * @param length Content length in bytes
56       * @param annotation_table the actual annotations
57       * @param constant_pool Array of constants
58       */
59      public Annotations(final byte annotation_type, final int name_index, final int length, final AnnotationEntry[] annotation_table,
60              final ConstantPool constant_pool, final boolean isRuntimeVisible) {
61          super(annotation_type, name_index, length, constant_pool);
62          this.annotation_table = annotation_table;
63          this.isRuntimeVisible = isRuntimeVisible;
64      }
65  
66      /**
67       * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class.
68       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
69       *
70       * @param v Visitor object
71       */
72      @Override
73      public void accept(final Visitor v) {
74          v.visitAnnotation(this);
75      }
76  
77      /**
78       * @param annotation_table the entries to set in this annotation
79       */
80      public final void setAnnotationTable(final AnnotationEntry[] annotation_table) {
81          this.annotation_table = annotation_table;
82      }
83  
84      /**
85       * returns the array of annotation entries in this annotation
86       */
87      public AnnotationEntry[] getAnnotationEntries() {
88          return annotation_table;
89      }
90  
91      /**
92       * @return the number of annotation entries in this annotation
93       */
94      public final int getNumAnnotations() {
95          if (annotation_table == null) {
96              return 0;
97          }
98          return annotation_table.length;
99      }
100 
101     public boolean isRuntimeVisible() {
102         return isRuntimeVisible;
103     }
104 
105     protected void writeAnnotations(final DataOutputStream dos) throws IOException {
106         if (annotation_table == null) {
107             return;
108         }
109         dos.writeShort(annotation_table.length);
110         for (final AnnotationEntry element : annotation_table) {
111             element.dump(dos);
112         }
113     }
114 }