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