1 /*
 2  * Copyright (c) 2020, 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.jasm;
24 
25 import org.openjdk.asmtools.jasm.TypeAnnotationTypes.TypePathEntry;
26 
27 import java.io.IOException;
28 import java.io.PrintWriter;
29 import java.util.ArrayList;
30 
31 /**
32  * JVMS 4.7.20.2. The type_path structure
33  *
34  * type_path {
35  *     u1 path_length;
36  *     {   u1 type_path_kind;
37  *         u1 type_argument_index;
38  *     } path[path_length];
39  * }
40  */
41 public class TypeAnnotationTypePathData implements Data {
42 
43     private ArrayList<TypePathEntry> typePathEntries = new ArrayList<>();
44 
45     public void addTypePathEntry( TypePathEntry entry) {
46         typePathEntries.add(entry);
47     }
48 
49     @Override
50     public void write(CheckedDataOutputStream out) throws IOException {
51         out.writeByte(typePathEntries.size());
52         for (TypePathEntry entry : typePathEntries) {
53             out.writeByte(entry.getTypePathKind());
54             out.writeByte(entry.getTypeArgumentIndex());
55         }
56     }
57 
58     @Override
59     public int getLength() {
60         return 1 + typePathEntries.size() * 2;
61     }
62 
63     public String toString(int tabLevel) {
64         String buffer = "";
65         if( typePathEntries.size() > 0 ) {
66         StringBuilder sb = new StringBuilder(tabString(tabLevel));
67         sb.append(" [ ");
68         boolean first = true;
69         for (TypePathEntry entry : typePathEntries) {
70                 if (!first)
71                     sb.append(", ");
72                 first = false;
73             sb.append(entry.toString());
74             }
75             sb.append("]");
76         buffer = sb.toString();
77         }
78         return buffer;
79     }
80 
81     /**
82      * jdis: print the type_path structure
83      */
84     public void print(PrintWriter out, String tab) {
85         if( typePathEntries.size() > 0 ) {
86             out.print(tab + " {");
87             boolean first = true;
88             for (TypePathEntry entry : typePathEntries) {
89                 if (!first) {
90                     out.print(", ");
91                 }
92                 first = false;
93                 out.print(entry.toString());
94             }
95             out.print(tab + "} ");
96         }
97     }
98 }