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;
023import java.util.Iterator;
024import java.util.stream.Stream;
025
026import org.apache.bcel.Const;
027
028/**
029 * base class for annotations
030 *
031 * @since 6.0
032 */
033public abstract class Annotations extends Attribute implements Iterable<AnnotationEntry> {
034
035    private AnnotationEntry[] annotationTable;
036    private final boolean isRuntimeVisible;
037
038    /**
039     * Constructs an instance.
040     *
041     * @param annotationType   the subclass type of the annotation
042     * @param nameIndex        Index pointing to the name <em>Code</em>
043     * @param length           Content length in bytes
044     * @param annotationTable  the actual annotations
045     * @param constantPool     Array of constants
046     * @param isRuntimeVisible whether this Annotation visible at runtime
047     */
048    public Annotations(final byte annotationType, final int nameIndex, final int length, final AnnotationEntry[] annotationTable,
049            final ConstantPool constantPool, final boolean isRuntimeVisible) {
050        super(annotationType, nameIndex, length, constantPool);
051        this.annotationTable = annotationTable;
052        this.isRuntimeVisible = isRuntimeVisible;
053    }
054
055    /**
056     * Constructs an instance.
057     *
058     * @param annotationType   the subclass type of the annotation
059     * @param nameIndex        Index pointing to the name <em>Code</em>
060     * @param length           Content length in bytes
061     * @param input            Input stream
062     * @param constantPool     Array of constants
063     * @param isRuntimeVisible whether this Annotation visible at runtime
064     * @throws IOException if an I/O error occurs.
065     */
066    Annotations(final byte annotationType, final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool,
067            final boolean isRuntimeVisible) throws IOException {
068        this(annotationType, nameIndex, length, (AnnotationEntry[]) null, constantPool, isRuntimeVisible);
069        final int annotation_table_length = input.readUnsignedShort();
070        annotationTable = new AnnotationEntry[annotation_table_length];
071        for (int i = 0; i < annotation_table_length; i++) {
072            annotationTable[i] = AnnotationEntry.read(input, constantPool, isRuntimeVisible);
073        }
074    }
075
076    /**
077     * Called by objects that are traversing the nodes of the tree implicitly
078     * defined by the contents of a Java class. I.e., the hierarchy of methods,
079     * fields, attributes, etc. spawns a tree of objects.
080     *
081     * @param v Visitor object
082     */
083    @Override
084    public void accept(final Visitor v) {
085        v.visitAnnotation(this);
086    }
087
088    @Override
089    public Attribute copy(final ConstantPool constantPool) {
090        // TODO Auto-generated method stub
091        return null;
092    }
093
094    /**
095     * Gets the array of annotation entries in this annotation
096     */
097    public AnnotationEntry[] getAnnotationEntries() {
098        return annotationTable;
099    }
100
101    /**
102     * Gets the number of annotation entries in this annotation.
103     *
104     * @return the number of annotation entries in this annotation
105     */
106    public final int getNumAnnotations() {
107        if (annotationTable == null) {
108            return 0;
109        }
110        return annotationTable.length;
111    }
112
113    public boolean isRuntimeVisible() {
114        return isRuntimeVisible;
115    }
116
117    @Override
118    public Iterator<AnnotationEntry> iterator() {
119        return Stream.of(annotationTable).iterator();
120    }
121
122    /**
123     * Sets the entries to set in this annotation.
124     *
125     * @param annotationTable the entries to set in this annotation
126     */
127    public final void setAnnotationTable(final AnnotationEntry[] annotationTable) {
128        this.annotationTable = annotationTable;
129    }
130
131    /**
132     * Converts to a String representation.
133     *
134     * @return String representation
135     */
136    @Override
137    public final String toString() {
138        final StringBuilder buf = new StringBuilder(Const.getAttributeName(getTag()));
139        buf.append(":\n");
140        for (int i = 0; i < annotationTable.length; i++) {
141            buf.append("  ").append(annotationTable[i]);
142            if (i < annotationTable.length - 1) {
143                buf.append('\n');
144            }
145        }
146        return buf.toString();
147    }
148
149    protected void writeAnnotations(final DataOutputStream dos) throws IOException {
150        if (annotationTable == null) {
151            return;
152        }
153        dos.writeShort(annotationTable.length);
154        for (final AnnotationEntry element : annotationTable) {
155            element.dump(dos);
156        }
157    }
158
159}