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