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.List; 28 import vm.runtime.defmeth.shared.data.method.Method; 29 import static jdk.internal.org.objectweb.asm.Opcodes.*; 30 31 /** 32 * Generic builder for classes of type {@code <S>} using 33 * builder of type {@code <T>}. 34 * 35 * Builder type is necessary to support type-safe fluent code style. 36 * 37 * Example: 38 * <code> 39 * public class InterfaceBuilder 40 * extends ClassBuilder<InterfaceBuilder,Interface> { ... 41 * </code> 42 * 43 * It produces {@code Interface} instance and all interim method calls return 44 * {@code InterfaceBuilder} instance. 45 * 46 * @param <T> 47 * @param <S> 48 */ 49 public abstract class ClassBuilder<T,S> implements Builder<S> { 50 protected String name; 51 52 // Class flags 53 protected int flags = ACC_PUBLIC; 54 55 // Exact class file major version, if needed 56 protected int majorVer; 57 58 // Class signature (if applicable) 59 protected String sig; 60 61 // Declared methods 62 protected List<Method> methods = new ArrayList<>(); 63 64 // Enclosing test builder 65 protected TestBuilder builder; 66 67 public ClassBuilder(TestBuilder builder) { 68 this.builder = builder; 69 } 70 71 @SuppressWarnings("unchecked") 72 public T flags(int flags) { 73 this.flags = flags; 74 75 return (T)this; 76 } 77 78 @SuppressWarnings("unchecked") 79 public T addFlags(int flags) { 80 this.flags |= flags; 81 82 return (T)this; 83 } 84 85 @SuppressWarnings("unchecked") 86 public T name(String name) { 87 this.name = name; 88 89 return (T)this; 90 } 91 92 @SuppressWarnings("unchecked") 93 public T method(Method m) { 94 methods.add(m); 95 96 return (T)this; 97 } 98 99 @SuppressWarnings("unchecked") 100 public T ver(int ver) { 101 this.majorVer = ver; 102 103 return (T)this; 104 } 105 106 @SuppressWarnings("unchecked") 107 public T sig(String sig) { 108 this.sig = sig; 109 110 return (T)this; 111 } 112 113 @Override 114 public TestBuilder done() { 115 build(); 116 117 return builder; 118 } 119 }