Annotations.java

  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. package org.apache.bcel.classfile;

  18. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;
  21. import java.util.Iterator;
  22. import java.util.stream.Stream;

  23. import org.apache.bcel.Const;

  24. /**
  25.  * base class for annotations
  26.  *
  27.  * @since 6.0
  28.  */
  29. public abstract class Annotations extends Attribute implements Iterable<AnnotationEntry> {

  30.     private AnnotationEntry[] annotationTable;
  31.     private final boolean isRuntimeVisible;

  32.     /**
  33.      * Constructs an instance.
  34.      *
  35.      * @param annotationType   the subclass type of the annotation
  36.      * @param nameIndex        Index pointing to the name <em>Code</em>
  37.      * @param length           Content length in bytes
  38.      * @param annotationTable  the actual annotations
  39.      * @param constantPool     Array of constants
  40.      * @param isRuntimeVisible whether this Annotation visible at runtime
  41.      */
  42.     public Annotations(final byte annotationType, final int nameIndex, final int length, final AnnotationEntry[] annotationTable,
  43.             final ConstantPool constantPool, final boolean isRuntimeVisible) {
  44.         super(annotationType, nameIndex, length, constantPool);
  45.         setAnnotationTable(annotationTable);
  46.         this.isRuntimeVisible = isRuntimeVisible;
  47.     }

  48.     /**
  49.      * Constructs an instance.
  50.      *
  51.      * @param annotationType   the subclass type of the annotation
  52.      * @param nameIndex        Index pointing to the name <em>Code</em>
  53.      * @param length           Content length in bytes
  54.      * @param input            Input stream
  55.      * @param constantPool     Array of constants
  56.      * @param isRuntimeVisible whether this Annotation visible at runtime
  57.      * @throws IOException if an I/O error occurs.
  58.      */
  59.     Annotations(final byte annotationType, final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool,
  60.             final boolean isRuntimeVisible) throws IOException {
  61.         this(annotationType, nameIndex, length, (AnnotationEntry[]) null, constantPool, isRuntimeVisible);
  62.         final int annotationTableLength = input.readUnsignedShort();
  63.         annotationTable = new AnnotationEntry[annotationTableLength];
  64.         for (int i = 0; i < annotationTableLength; i++) {
  65.             annotationTable[i] = AnnotationEntry.read(input, constantPool, isRuntimeVisible);
  66.         }
  67.     }

  68.     /**
  69.      * Called by objects that are traversing the nodes of the tree implicitly
  70.      * defined by the contents of a Java class. I.e., the hierarchy of methods,
  71.      * 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.     @Override
  80.     public Attribute copy(final ConstantPool constantPool) {
  81.         // TODO Auto-generated method stub
  82.         return null;
  83.     }

  84.     /**
  85.      * Gets the array of annotation entries in this annotation
  86.      */
  87.     public AnnotationEntry[] getAnnotationEntries() {
  88.         return annotationTable;
  89.     }

  90.     /**
  91.      * Gets the number of annotation entries in this annotation.
  92.      *
  93.      * @return the number of annotation entries in this annotation
  94.      */
  95.     public final int getNumAnnotations() {
  96.         return annotationTable.length;
  97.     }

  98.     public boolean isRuntimeVisible() {
  99.         return isRuntimeVisible;
  100.     }

  101.     @Override
  102.     public Iterator<AnnotationEntry> iterator() {
  103.         return Stream.of(annotationTable).iterator();
  104.     }

  105.     /**
  106.      * Sets the entries to set in this annotation.
  107.      *
  108.      * @param annotationTable the entries to set in this annotation
  109.      */
  110.     public final void setAnnotationTable(final AnnotationEntry[] annotationTable) {
  111.         this.annotationTable = annotationTable != null ? annotationTable : AnnotationEntry.EMPTY_ARRAY;
  112.     }

  113.     /**
  114.      * Converts to a String representation.
  115.      *
  116.      * @return String representation
  117.      */
  118.     @Override
  119.     public final String toString() {
  120.         final StringBuilder buf = new StringBuilder(Const.getAttributeName(getTag()));
  121.         buf.append(":\n");
  122.         for (int i = 0; i < annotationTable.length; i++) {
  123.             buf.append("  ").append(annotationTable[i]);
  124.             if (i < annotationTable.length - 1) {
  125.                 buf.append('\n');
  126.             }
  127.         }
  128.         return buf.toString();
  129.     }

  130.     protected void writeAnnotations(final DataOutputStream dos) throws IOException {
  131.         dos.writeShort(annotationTable.length);
  132.         for (final AnnotationEntry element : annotationTable) {
  133.             element.dump(dos);
  134.         }
  135.     }

  136. }