1 /*
 2  * Copyright (c) 2017, 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 
24 package jdk.experimental.bytecode;
25 
26 /**
27  * Class member builder.
28  *
29  * @param <S> the type of symbol representation
30  * @param <T> the type of type descriptors representation
31  * @param <E> the type of pool entries
32  * @param <M> the type of this builder
33  */
34 public class MemberBuilder<S, T, E, M extends MemberBuilder<S, T, E, M>> extends DeclBuilder<S, T, E, M> {
35 
36     CharSequence name;
37     T desc;
38 
39     /**
40      * Create a member builder.
41      *
42      * @param name the name of the class member
43      * @param type the type descriptor of the class member
44      * @param poolHelper the helper to build the constant pool
45      * @param typeHelper the helper to use to manipulate type descriptors
46      */
47     MemberBuilder(CharSequence name, T type, PoolHelper<S, T, E> poolHelper, TypeHelper<S, T> typeHelper) {
48         super(poolHelper, typeHelper);
49         this.name = name;
50         this.desc = type;
51     }
52 
53     /**
54      * Build the member.
55      *
56      * @param buf the {@code GrowableByteBuffer} to build the member into
57      */
58     protected void build(GrowableByteBuffer buf) {
59         addAnnotations();
60         buf.writeChar(flags);
61         buf.writeChar(poolHelper.putUtf8(name));
62         buf.writeChar(poolHelper.putType(desc));
63         buf.writeChar(nattrs);
64         buf.writeBytes(attributes);
65     }
66 
67     /**
68      * Build the member.
69      *
70      * @return a byte array representation of the member
71      */
72     protected byte[] build() {
73         GrowableByteBuffer buf = new GrowableByteBuffer();
74         addAnnotations();
75         build(buf);
76         return buf.bytes();
77     }
78 }