Source code

    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 * @since 6.0
    028 */
    029public abstract class Annotations extends Attribute {
    030
    031    private AnnotationEntry[] annotation_table;
    032    private final boolean isRuntimeVisible;
    033
    034    /**
    035     * @param annotation_type the subclass type of the annotation
    036     * @param name_index Index pointing to the name <em>Code</em>
    037     * @param length Content length in bytes
    038     * @param input Input stream
    039     * @param constant_pool Array of constants
    040     */
    041    Annotations(final byte annotation_type, final int name_index, final int length, final DataInput input,
    042            final ConstantPool constant_pool, final boolean isRuntimeVisible) throws IOException {
    043        this(annotation_type, name_index, length, (AnnotationEntry[]) null, constant_pool, isRuntimeVisible);
    044        final int annotation_table_length = input.readUnsignedShort();
    045        annotation_table = new AnnotationEntry[annotation_table_length];
    046        for (int i = 0; i < annotation_table_length; i++) {
    047            annotation_table[i] = AnnotationEntry.read(input, constant_pool, isRuntimeVisible);
    048        }
    049    }
    050
    051    /**
    052     * @param annotation_type the subclass type of the annotation
    053     * @param name_index Index pointing to the name <em>Code</em>
    054     * @param length Content length in bytes
    055     * @param annotation_table the actual annotations
    056     * @param constant_pool Array of constants
    057     */
    058    public Annotations(final byte annotation_type, final int name_index, final int length, final AnnotationEntry[] annotation_table,
    059            final ConstantPool constant_pool, final boolean isRuntimeVisible) {
    060        super(annotation_type, name_index, length, constant_pool);
    061        this.annotation_table = annotation_table;
    062        this.isRuntimeVisible = isRuntimeVisible;
    063    }
    064
    065    /**
    066     * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class.
    067     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    068     *
    069     * @param v Visitor object
    070     */
    071    @Override
    072    public void accept(final Visitor v) {
    073        v.visitAnnotation(this);
    074    }
    075
    076    /**
    077     * @param annotation_table the entries to set in this annotation
    078     */
    079    public final void setAnnotationTable(final AnnotationEntry[] annotation_table) {
    080        this.annotation_table = annotation_table;
    081    }
    082
    083    /**
    084     * returns the array of annotation entries in this annotation
    085     */
    086    public AnnotationEntry[] getAnnotationEntries() {
    087        return annotation_table;
    088    }
    089
    090    /**
    091     * @return the number of annotation entries in this annotation
    092     */
    093    public final int getNumAnnotations() {
    094        if (annotation_table == null) {
    095            return 0;
    096        }
    097        return annotation_table.length;
    098    }
    099
    100    public boolean isRuntimeVisible() {
    101        return isRuntimeVisible;
    102    }
    103
    104    protected void writeAnnotations(final DataOutputStream dos) throws IOException {
    105        if (annotation_table == null) {
    106            return;
    107        }
    108        dos.writeShort(annotation_table.length);
    109        for (final AnnotationEntry element : annotation_table) {
    110            element.dump(dos);
    111        }
    112    }
    113}