1 /*
   2  * Copyright (c) 2016, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.experimental.bytecode;
  27 
  28 /**
  29  * Class member builder.
  30  *
  31  * @param <S> the type of symbol representation
  32  * @param <T> the type of type descriptors representation
  33  * @param <E> the type of pool entries
  34  * @param <M> the type of this builder
  35  */
  36 public class MemberBuilder<S, T, E, M extends MemberBuilder<S, T, E, M>> extends DeclBuilder<S, T, E, M> {
  37 
  38     CharSequence name;
  39     T desc;
  40 
  41     /**
  42      * Create a member builder.
  43      *
  44      * @param name the name of the class member
  45      * @param type the type descriptor of the class member
  46      * @param poolHelper the helper to build the constant pool
  47      * @param typeHelper the helper to use to manipulate type descriptors
  48      */
  49     MemberBuilder(CharSequence name, T type, PoolHelper<S, T, E> poolHelper, TypeHelper<S, T> typeHelper) {
  50         super(poolHelper, typeHelper);
  51         this.name = name;
  52         this.desc = type;
  53     }
  54 
  55     /**
  56      * Build the member.
  57      *
  58      * @param buf the {@code GrowableByteBuffer} to build the member into
  59      */
  60     protected void build(GrowableByteBuffer buf) {
  61         addAnnotations();
  62         buf.writeChar(flags);
  63         buf.writeChar(poolHelper.putUtf8(name));
  64         buf.writeChar(poolHelper.putType(desc));
  65         buf.writeChar(nattrs);
  66         buf.writeBytes(attributes);
  67     }
  68 
  69     /**
  70      * Build the member.
  71      *
  72      * @return a byte array representation of the member
  73      */
  74     protected byte[] build() {
  75         GrowableByteBuffer buf = new GrowableByteBuffer();
  76         addAnnotations();
  77         build(buf);
  78         return buf.bytes();
  79     }
  80 }