1 /*
 2  * Copyright (c) 1996, 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.ETargetType;
26 import org.openjdk.asmtools.jasm.TypeAnnotationTypes.TypePathEntry;
27 
28 import java.io.IOException;
29 
30 /**
31  * JVMS 4.7.20.
32  * type_annotation {
33  *     u1 target_type;
34  *     union {
35  *         type_parameter_target;
36  *         supertype_target;
37  *         type_parameter_bound_target;
38  *         empty_target;
39  *         formal_parameter_target;
40  *         throws_target;
41  *         localvar_target;
42  *         catch_target;
43  *         offset_target;
44  *         type_argument_target;
45  *     } target_info;
46  *     type_path target_path;
47  *     u2        type_index;
48  *     //
49  *     //
50  *     u2        num_element_value_pairs;
51  *     {   u2            element_name_index;
52  *         element_value value;
53  *     } element_value_pairs[num_element_value_pairs];
54  * }
55  */
56 public class TypeAnnotationData extends AnnotationData {
57 
58     protected ETargetType targetType;
59     protected TypeAnnotationTargetInfoData targetInfo;
60     protected TypeAnnotationTypePathData typePath;
61 
62     public TypeAnnotationData(Argument typeCPX, boolean invisible) {
63         super(typeCPX, invisible);
64         typePath = new TypeAnnotationTypePathData();
65     }
66 
67     @Override
68     public int getLength() {
69         // lengthOf(annotations[]) + lengthOf(targetType) + lengthOf(targetInfo) + lengthOf(targetInfo)
70         return super.getLength() + 1 + targetInfo.getLength() + typePath.getLength();
71     }
72 
73     @Override
74     public void write(CheckedDataOutputStream out) throws IOException {
75         out.writeByte(targetType.value);
76         targetInfo.write(out);
77         typePath.write(out);
78         super.write(out);
79     }
80 
81     public void addTypePathEntry(TypePathEntry path) {
82         typePath.addTypePathEntry(path);
83     }
84 
85     @Override
86     public String toString() {
87         return toString(0);
88     }
89 
90     public String toString(int tabLevel) {
91         StringBuilder sb = new StringBuilder(tabString(tabLevel));
92         sb.append(targetType.toString()).
93                 append(' ').
94                 append(targetInfo.toString(tabLevel)).
95                 append(typePath.toString(tabLevel));
96         return sb.toString();
97     }
98 }