ParameterAnnotations.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. /**
  24.  * base class for parameter annotations
  25.  *
  26.  * @since 6.0
  27.  */
  28. public abstract class ParameterAnnotations extends Attribute implements Iterable<ParameterAnnotationEntry> {

  29.     private static final ParameterAnnotationEntry[] EMPTY_ARRAY = {};

  30.     /** Table of parameter annotations */
  31.     private ParameterAnnotationEntry[] parameterAnnotationTable;

  32.     /**
  33.      * Constructs a new instance.
  34.      *
  35.      * @param parameterAnnotationType the subclass type of the parameter annotation
  36.      * @param nameIndex Index pointing to the name <em>Code</em>
  37.      * @param length Content length in bytes
  38.      * @param input Input stream
  39.      * @param constantPool Array of constants
  40.      */
  41.     ParameterAnnotations(final byte parameterAnnotationType, final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool)
  42.         throws IOException {
  43.         this(parameterAnnotationType, nameIndex, length, (ParameterAnnotationEntry[]) null, constantPool);
  44.         final int numParameters = input.readUnsignedByte();
  45.         parameterAnnotationTable = new ParameterAnnotationEntry[numParameters];
  46.         for (int i = 0; i < numParameters; i++) {
  47.             parameterAnnotationTable[i] = new ParameterAnnotationEntry(input, constantPool);
  48.         }
  49.     }

  50.     /**
  51.      * Constructs a new instance.
  52.      *
  53.      * @param parameterAnnotationType the subclass type of the parameter annotation
  54.      * @param nameIndex Index pointing to the name <em>Code</em>
  55.      * @param length Content length in bytes
  56.      * @param parameterAnnotationTable the actual parameter annotations
  57.      * @param constantPool Array of constants
  58.      */
  59.     public ParameterAnnotations(final byte parameterAnnotationType, final int nameIndex, final int length,
  60.         final ParameterAnnotationEntry[] parameterAnnotationTable, final ConstantPool constantPool) {
  61.         super(parameterAnnotationType, nameIndex, length, constantPool);
  62.         this.parameterAnnotationTable = parameterAnnotationTable;
  63.     }

  64.     /**
  65.      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
  66.      * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  67.      *
  68.      * @param v Visitor object
  69.      */
  70.     @Override
  71.     public void accept(final Visitor v) {
  72.         v.visitParameterAnnotation(this);
  73.     }

  74.     /**
  75.      * @return deep copy of this attribute
  76.      */
  77.     @Override
  78.     public Attribute copy(final ConstantPool constantPool) {
  79.         return (Attribute) clone();
  80.     }

  81.     @Override
  82.     public void dump(final DataOutputStream dos) throws IOException {
  83.         super.dump(dos);
  84.         dos.writeByte(parameterAnnotationTable.length);

  85.         for (final ParameterAnnotationEntry element : parameterAnnotationTable) {
  86.             element.dump(dos);
  87.         }

  88.     }

  89.     /**
  90.      * returns the array of parameter annotation entries in this parameter annotation
  91.      */
  92.     public ParameterAnnotationEntry[] getParameterAnnotationEntries() {
  93.         return parameterAnnotationTable;
  94.     }

  95.     /**
  96.      * @return the parameter annotation entry table
  97.      */
  98.     public final ParameterAnnotationEntry[] getParameterAnnotationTable() {
  99.         return parameterAnnotationTable;
  100.     }

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

  105.     /**
  106.      * @param parameterAnnotationTable the entries to set in this parameter annotation
  107.      */
  108.     public final void setParameterAnnotationTable(final ParameterAnnotationEntry[] parameterAnnotationTable) {
  109.         this.parameterAnnotationTable = parameterAnnotationTable != null ? parameterAnnotationTable : EMPTY_ARRAY;
  110.     }
  111. }