ParameterAnnotationEntry.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.ArrayList;
  22. import java.util.Collections;
  23. import java.util.List;

  24. /**
  25.  * represents one parameter annotation in the parameter annotation table
  26.  *
  27.  * @since 6.0
  28.  */
  29. public class ParameterAnnotationEntry implements Node {

  30.     static final ParameterAnnotationEntry[] EMPTY_ARRAY = {};

  31.     public static ParameterAnnotationEntry[] createParameterAnnotationEntries(final Attribute[] attributes) {
  32.         if (attributes == null) {
  33.             return EMPTY_ARRAY;
  34.         }
  35.         // Find attributes that contain parameter annotation data
  36.         final List<ParameterAnnotationEntry> accumulatedAnnotations = new ArrayList<>(attributes.length);
  37.         for (final Attribute attribute : attributes) {
  38.             if (attribute instanceof ParameterAnnotations) {
  39.                 final ParameterAnnotations runtimeAnnotations = (ParameterAnnotations) attribute;
  40.                 final ParameterAnnotationEntry[] parameterAnnotationEntries = runtimeAnnotations.getParameterAnnotationEntries();
  41.                 if (parameterAnnotationEntries != null) {
  42.                     Collections.addAll(accumulatedAnnotations, parameterAnnotationEntries);
  43.                 }
  44.             }
  45.         }
  46.         return accumulatedAnnotations.toArray(EMPTY_ARRAY);
  47.     }

  48.     private final AnnotationEntry[] annotationTable;

  49.     /**
  50.      * Constructs object from input stream.
  51.      *
  52.      * @param input Input stream
  53.      * @throws IOException if an I/O error occurs.
  54.      */
  55.     ParameterAnnotationEntry(final DataInput input, final ConstantPool constantPool) throws IOException {
  56.         final int annotationTableLength = input.readUnsignedShort();
  57.         annotationTable = new AnnotationEntry[annotationTableLength];
  58.         for (int i = 0; i < annotationTableLength; i++) {
  59.             // TODO isRuntimeVisible
  60.             annotationTable[i] = AnnotationEntry.read(input, constantPool, false);
  61.         }
  62.     }

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

  73.     public void dump(final DataOutputStream dos) throws IOException {
  74.         dos.writeShort(annotationTable.length);
  75.         for (final AnnotationEntry entry : annotationTable) {
  76.             entry.dump(dos);
  77.         }
  78.     }

  79.     /**
  80.      * returns the array of annotation entries in this annotation
  81.      */
  82.     public AnnotationEntry[] getAnnotationEntries() {
  83.         return annotationTable;
  84.     }
  85. }