Home | History | Annotate | Download | only in classfile
      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  */
     18 package org.apache.bcel.classfile;
     19 
     20 import java.io.DataInput;
     21 import java.io.DataOutputStream;
     22 import java.io.IOException;
     23 import java.util.ArrayList;
     24 import java.util.Collections;
     25 import java.util.List;
     26 
     27 /**
     28  * represents one parameter annotation in the parameter annotation table
     29  *
     30  * @version $Id: ParameterAnnotationEntry
     31  * @since 6.0
     32  */
     33 public class ParameterAnnotationEntry implements Node {
     34 
     35     private final AnnotationEntry[] annotation_table;
     36 
     37 
     38     /**
     39      * Construct object from input stream.
     40      *
     41      * @param input Input stream
     42      * @throws IOException
     43      */
     44     ParameterAnnotationEntry(final DataInput input, final ConstantPool constant_pool) throws IOException {
     45         final int annotation_table_length = input.readUnsignedShort();
     46         annotation_table = new AnnotationEntry[annotation_table_length];
     47         for (int i = 0; i < annotation_table_length; i++) {
     48             // TODO isRuntimeVisible
     49             annotation_table[i] = AnnotationEntry.read(input, constant_pool, false);
     50         }
     51     }
     52 
     53 
     54     /**
     55      * Called by objects that are traversing the nodes of the tree implicitely
     56      * defined by the contents of a Java class. I.e., the hierarchy of methods,
     57      * fields, attributes, etc. spawns a tree of objects.
     58      *
     59      * @param v Visitor object
     60      */
     61     @Override
     62     public void accept( final Visitor v ) {
     63         v.visitParameterAnnotationEntry(this);
     64     }
     65 
     66     /**
     67      * returns the array of annotation entries in this annotation
     68      */
     69     public AnnotationEntry[] getAnnotationEntries() {
     70         return annotation_table;
     71     }
     72 
     73     public void dump(final DataOutputStream dos) throws IOException {
     74         dos.writeShort(annotation_table.length);
     75         for (final AnnotationEntry entry : annotation_table) {
     76             entry.dump(dos);
     77         }
     78     }
     79 
     80   public static ParameterAnnotationEntry[] createParameterAnnotationEntries(final Attribute[] attrs) {
     81       // Find attributes that contain parameter annotation data
     82       final List<ParameterAnnotationEntry> accumulatedAnnotations = new ArrayList<>(attrs.length);
     83       for (final Attribute attribute : attrs) {
     84           if (attribute instanceof ParameterAnnotations) {
     85               final ParameterAnnotations runtimeAnnotations = (ParameterAnnotations)attribute;
     86               Collections.addAll(accumulatedAnnotations, runtimeAnnotations.getParameterAnnotationEntries());
     87           }
     88       }
     89       return accumulatedAnnotations.toArray(new ParameterAnnotationEntry[accumulatedAnnotations.size()]);
     90   }
     91 }
     92 
     93