1 /*
   2  * Copyright (c) 2013, 2018, 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 vm.runtime.defmeth.shared.builder;
  25 
  26 import java.util.ArrayList;
  27 import java.util.Arrays;
  28 import java.util.List;
  29 import vm.runtime.defmeth.shared.data.Interface;
  30 import vm.runtime.defmeth.shared.data.InterfaceImpl;
  31 import vm.runtime.defmeth.shared.data.method.Method;
  32 
  33 /**
  34  * Builder for Interface instances.
  35  */
  36 public class InterfaceBuilder extends ClassBuilder<InterfaceBuilder,Interface> {
  37 
  38     // Superinterfaces
  39     private List<Interface> parents = new ArrayList<>();
  40 
  41     /* package-private */ InterfaceBuilder(TestBuilder builder) {
  42         super(builder);
  43     }
  44 
  45     public InterfaceBuilder extend(Interface... intf) {
  46         parents.addAll(Arrays.asList(intf));
  47 
  48         return this;
  49     }
  50 
  51     public ClassMethodBuilder<InterfaceBuilder> defaultMethod(String name, String desc) {
  52         MethodBuilder mb = builder.method().name(name).desc(desc).type(MethodType.DEFAULT);
  53         return new ClassMethodBuilder<>(this, mb);
  54     }
  55 
  56     public ClassMethodBuilder<InterfaceBuilder> abstractMethod(String name, String desc) {
  57         MethodBuilder mb = builder.method().name(name).desc(desc).type(MethodType.ABSTRACT);
  58         return new ClassMethodBuilder<>(this, mb);
  59     }
  60 
  61     /**
  62      * Construct Data.Interface instance.
  63      * @return instance of Data.Interface class
  64      */
  65     @Override
  66     public Interface build() {
  67         if (name == null) {
  68             throw new IllegalStateException();
  69         }
  70 
  71         Interface intf = new InterfaceImpl(flags, name, majorVer, sig, parents.toArray(new Interface[0]),
  72                 methods.toArray(new Method[0]));
  73 
  74         if (builder.hasElement(name)) {
  75             throw new IllegalStateException();
  76         }
  77 
  78         builder.register(intf);
  79         builder.finishConstruction(this);
  80 
  81         return intf;
  82     }
  83 }