001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018package org.apache.bcel.classfile;
019
020import java.io.DataInput;
021import java.io.DataOutputStream;
022import java.io.IOException;
023
024/**
025 * base class for annotations
026 *
027 * @version $Id: Annotations
028 * @since 6.0
029 */
030public abstract class Annotations extends Attribute {
031
032    private AnnotationEntry[] annotation_table;
033    private final boolean isRuntimeVisible;
034
035    /**
036     * @param annotation_type the subclass type of the annotation
037     * @param name_index Index pointing to the name <em>Code</em>
038     * @param length Content length in bytes
039     * @param input Input stream
040     * @param constant_pool Array of constants
041     */
042    Annotations(final byte annotation_type, final int name_index, final int length, final DataInput input,
043            final ConstantPool constant_pool, final boolean isRuntimeVisible) throws IOException {
044        this(annotation_type, name_index, length, (AnnotationEntry[]) null, constant_pool, isRuntimeVisible);
045        final int annotation_table_length = input.readUnsignedShort();
046        annotation_table = new AnnotationEntry[annotation_table_length];
047        for (int i = 0; i < annotation_table_length; i++) {
048            annotation_table[i] = AnnotationEntry.read(input, constant_pool, isRuntimeVisible);
049        }
050    }
051
052    /**
053     * @param annotation_type the subclass type of the annotation
054     * @param name_index Index pointing to the name <em>Code</em>
055     * @param length Content length in bytes
056     * @param annotation_table the actual annotations
057     * @param constant_pool Array of constants
058     */
059    public Annotations(final byte annotation_type, final int name_index, final int length, final AnnotationEntry[] annotation_table,
060            final ConstantPool constant_pool, final boolean isRuntimeVisible) {
061        super(annotation_type, name_index, length, constant_pool);
062        this.annotation_table = annotation_table;
063        this.isRuntimeVisible = isRuntimeVisible;
064    }
065
066    /**
067     * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class.
068     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
069     *
070     * @param v Visitor object
071     */
072    @Override
073    public void accept(final Visitor v) {
074        v.visitAnnotation(this);
075    }
076
077    /**
078     * @param annotation_table the entries to set in this annotation
079     */
080    public final void setAnnotationTable(final AnnotationEntry[] annotation_table) {
081        this.annotation_table = annotation_table;
082    }
083
084    /**
085     * returns the array of annotation entries in this annotation
086     */
087    public AnnotationEntry[] getAnnotationEntries() {
088        return annotation_table;
089    }
090
091    /**
092     * @return the number of annotation entries in this annotation
093     */
094    public final int getNumAnnotations() {
095        if (annotation_table == null) {
096            return 0;
097        }
098        return annotation_table.length;
099    }
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}