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 vm.runtime.defmeth.shared.data.Clazz; 27 import vm.runtime.defmeth.shared.data.ConcreteClass; 28 import vm.runtime.defmeth.shared.data.method.body.CallMethod; 29 import vm.runtime.defmeth.shared.data.method.body.MethodBody; 30 31 /** 32 * Context-specific builder for method instances of type {@code <T>}. 33 * Allows to construct type-safe nested builders for classes with methods. 34 * 35 * Example: 36 * <code> 37 * Interface I = 38 * new TestBuilder().intf("I") 39 * .defaultMethod("m", "()V").emptyBody().build() 40 * .build(); 41 * </code> 42 * 43 * @param <T> 44 */ 45 public class ClassMethodBuilder<T> { 46 /* Enclosing Clazz builder */ 47 private ClassBuilder<T,?> cb; 48 49 private MethodBuilder mb; 50 51 /* package-private */ ClassMethodBuilder(ClassBuilder<T,?> cb, MethodBuilder mb) { 52 this.cb = cb; 53 this.mb = mb; 54 } 55 56 public ClassMethodBuilder<T> name(String name) { mb.name(name); return this; } 57 public ClassMethodBuilder<T> desc(String desc) { mb.desc(desc); return this; } 58 public ClassMethodBuilder<T> sig(String sig) { mb.sig(sig); return this; } 59 60 public ClassMethodBuilder<T> body(MethodBody body) { mb.body(body); return this; } 61 62 public ClassMethodBuilder<T> emptyBody() { mb.empty(); return this; } 63 public ClassMethodBuilder<T> returns(int i) { mb.returns(i); return this; } 64 public ClassMethodBuilder<T> returnsNull() { mb.returnsNull(); return this; } 65 public ClassMethodBuilder<T> throw_(Clazz clz) { mb.throws_(clz); return this; } 66 public ClassMethodBuilder<T> throw_(Class<? extends Throwable> exc) { 67 mb.throws_(exc); 68 return this; 69 } 70 public ClassMethodBuilder<T> returnsNewInstance(ConcreteClass clz) { mb.returnsNewInstance(clz); return this; } 71 72 public ClassMethodBuilder<T> callSuper(Clazz callee, String methodName, String methodDesc) { 73 mb.superCall(callee, methodName, methodDesc); 74 return this; 75 } 76 77 public ClassMethodBuilder<T> invokeSpecial(Clazz callee, String methodName, String methodDesc) { 78 mb.invokeSpecial(callee, methodName, methodDesc); 79 return this; 80 } 81 82 public ClassMethodBuilder<T> invokeStatic(Clazz callee, String methodName, String methodDesc) { 83 mb.invokeStatic(callee, methodName, methodDesc); 84 return this; 85 } 86 87 public ClassMethodBuilder<T> invoke(CallMethod.Invoke callInsn, Clazz staticCallee, 88 ConcreteClass callee, String methodName, 89 String methodDesc, CallMethod.IndexbyteOp generateIndexbyteOp) { 90 mb.invoke(callInsn, staticCallee, callee, methodName, methodDesc, generateIndexbyteOp); 91 return this; 92 } 93 94 public ClassMethodBuilder<T> public_() { mb.public_(); return this; } 95 public ClassMethodBuilder<T> protected_() { mb.protected_(); return this; } 96 public ClassMethodBuilder<T> private_() { mb.private_(); return this; } 97 public ClassMethodBuilder<T> package_private() { mb.package_private(); return this; } 98 99 public ClassMethodBuilder<T> static_() { mb.static_(); return this; } 100 public ClassMethodBuilder<T> synthetic() { mb.synthetic(); return this; } 101 102 public ClassMethodBuilder<T> flags(int flags) { mb.flags(flags); return this; } 103 public ClassMethodBuilder<T> addFlags(int flags) { mb.addFlags(flags); return this; } 104 105 @SuppressWarnings("unchecked") 106 public T build() { cb.method(mb.build()); return (T)cb; } 107 }