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.ConcreteClass; 30 import vm.runtime.defmeth.shared.data.Interface; 31 import vm.runtime.defmeth.shared.data.method.Method; 32 33 import static jdk.internal.org.objectweb.asm.Opcodes.*; 34 import vm.runtime.defmeth.shared.data.ConcreteClassImpl; 35 36 /** 37 * Builder of {@link Data.ConcreteClass} instances. 38 */ 39 public class ConcreteClassBuilder extends ClassBuilder<ConcreteClassBuilder,ConcreteClass>{ 40 // Super class 41 private ConcreteClass parent; 42 43 // Implemented interfaces 44 private List<Interface> interfaces = new ArrayList<>(); 45 ConcreteClassBuilder(TestBuilder builder)46 /* package-private */ ConcreteClassBuilder(TestBuilder builder) { 47 super(builder); 48 } 49 extend(ConcreteClass parent)50 public ConcreteClassBuilder extend(ConcreteClass parent) { 51 this.parent = parent; 52 53 return this; 54 } 55 extend(Class<?> parent)56 public ConcreteClassBuilder extend(Class<?> parent) { 57 this.parent = builder.toConcreteClass(parent); 58 59 return this; 60 } 61 implement(Interface... ifaces)62 public ConcreteClassBuilder implement(Interface... ifaces) { 63 interfaces.addAll(Arrays.asList(ifaces)); 64 65 return this; 66 } 67 abstractMethod(String name, String desc)68 public ClassMethodBuilder<ConcreteClassBuilder> abstractMethod(String name, String desc) { 69 MethodBuilder mb = new MethodBuilder(builder).name(name).desc(desc).type(MethodType.ABSTRACT); 70 71 // Ensure that the class enclosing abstract method is also abstract 72 addFlags(ACC_ABSTRACT); 73 74 return new ClassMethodBuilder<>(this, mb); 75 } 76 concreteMethod(String name, String desc)77 public ClassMethodBuilder<ConcreteClassBuilder> concreteMethod(String name, String desc) { 78 MethodBuilder mb = new MethodBuilder(builder).name(name).desc(desc).type(MethodType.CONCRETE); 79 return new ClassMethodBuilder<>(this, mb); 80 } 81 82 /** 83 * Construct Data.ConcreteClass instance. 84 * @return instance of Data.Interface class 85 */ 86 @Override build()87 public ConcreteClass build() { 88 if (name == null) { 89 throw new IllegalStateException(); 90 } 91 92 if (builder.hasElement(name)) { 93 throw new IllegalStateException(name); 94 } 95 96 ConcreteClass clz = new ConcreteClassImpl(name, flags, majorVer, sig, 97 (parent != null) ? parent : ConcreteClass.OBJECT, 98 interfaces.toArray(new Interface[0]), 99 methods.toArray(new Method[0])); 100 101 builder.register(clz); 102 builder.finishConstruction(this); 103 104 return clz; 105 } 106 } 107