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 java.io.IOException;
26 import java.util.ArrayList;
27 
28 /**
29  * JVMS 4.7.16.
30  *
31  * annotation {
32  *     u2 type_index;
33  *     u2 num_element_value_pairs;
34  *     {   u2            element_name_index;
35  *         element_value value;
36  *     } element_value_pairs[num_element_value_pairs];
37  * }
38  */
39 class AnnotationData implements Data {
40 
41     boolean invisible;
42     Argument typeCPX;
43     ArrayList<ElemValuePair> elemValuePairs;
44     int annotationLength = 0;
45 
46     /**
47      * AnnotationElemValue
48      *
49      * Used to store Annotation Data
50      */
51     static public class ElemValuePair implements Data {
52 
53         ConstantPool.ConstCell name;
54         Data value;
55 
56         public ElemValuePair(ConstantPool.ConstCell name, Data value) {
57             this.name = name;
58             this.value = value;
59         }
60 
61         @Override
62         public void write(CheckedDataOutputStream out) throws IOException {
63             name.write(out);
64             value.write(out);
65         }
66 
67         @Override
68         public int getLength() {
69             return 2 + value.getLength();
70         }
71     }
72 
73     public AnnotationData(Argument typeCPX, boolean invisible) {
74         this.typeCPX = typeCPX;
75         this.elemValuePairs = new ArrayList<>();
76         this.invisible = invisible;
77     }
78 
79     public void add(ElemValuePair elemValuePair) {
80         elemValuePairs.add(elemValuePair);
81         annotationLength += elemValuePair.getLength();
82     }
83 
84     @Override
85     public void write(CheckedDataOutputStream out) throws IOException {
86         out.writeShort(typeCPX.arg);
87         out.writeShort(elemValuePairs.size());
88 
89         for (Data pair : elemValuePairs) {
90             pair.write(out);
91         }
92     }
93 
94     @Override
95     public int getLength() {
96         return 4 + annotationLength;
97     }
98 }