1 /*
  2  * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.asmtools.jdis;
 24 
 25 import java.io.DataInputStream;
 26 import java.io.IOException;
 27 import java.io.PrintWriter;
 28 import java.util.ArrayList;
 29 
 30 /**
 31  *
 32  */
 33 public class ParameterAnnotationData {
 34     /*-------------------------------------------------------- */
 35     /* AnnotData Fields */
 36 
 37     private boolean invisible = false;
 38     private static final String initialTab = "";
 39     private ArrayList<ArrayList<AnnotationData>> array = null;
 40 
 41     private ClassData cls;
 42     /*-------------------------------------------------------- */
 43 
 44     public ParameterAnnotationData(ClassData cls, boolean invisible) {
 45         this.cls = cls;
 46         this.invisible = invisible;
 47     }
 48 
 49     public int numParams() {
 50         if (array == null) {
 51             return 0;
 52         }
 53 
 54         return array.size();
 55     }
 56 
 57     public ArrayList<AnnotationData> get(int i) {
 58         return array.get(i);
 59     }
 60 
 61     public void read(DataInputStream in) throws IOException {
 62         int numParams = in.readByte();
 63         TraceUtils.traceln("             ParameterAnnotationData[" + numParams + "]");
 64         array = new ArrayList<>(numParams);
 65         for (int paramNum = 0; paramNum < numParams; paramNum++) {
 66 
 67             int numAnnots = in.readShort();
 68             TraceUtils.traceln("             Param#[" + paramNum + "]: numAnnots=" + numAnnots);
 69 
 70             if (numAnnots > 0) {
 71                 // read annotation
 72                 ArrayList<AnnotationData> p_annots = new ArrayList<>(numAnnots);
 73                 for (int annotIndex = 0; annotIndex < numAnnots; annotIndex++) {
 74                     AnnotationData annot = new AnnotationData(invisible, cls);
 75                     annot.read(in);
 76                     p_annots.add(annot);
 77                 }
 78                 array.add(paramNum, p_annots);
 79             } else {
 80                 array.add(paramNum, null);
 81             }
 82         }
 83     }
 84 
 85     // Don't need to do this --
 86     // we need to print annotations (both vis and invisible) per each param number
 87     public void print(PrintWriter out, String tab) {
 88         if (array != null && array.size() > 0) {
 89             out.println();
 90             int paramNum = 0;
 91             for (ArrayList<AnnotationData> p_annot : array) {
 92                 if (p_annot != null && p_annot.size() > 0) {
 93                     out.print("\t" + paramNum + ": ");
 94                     boolean firstTime = true;
 95                     for (AnnotationData annot : p_annot) {
 96                         if (!firstTime) {
 97                             out.print("\t   ");
 98                         }
 99                         annot.print(out, initialTab);
100                         firstTime = false;
101                     }
102                 }
103 
104                 paramNum += 1;
105                 out.println();
106             }
107         }
108     }
109 
110 }