/* * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package vm.runtime.defmeth.shared.builder; import vm.runtime.defmeth.shared.data.Clazz; import vm.runtime.defmeth.shared.data.ConcreteClass; import vm.runtime.defmeth.shared.data.method.body.CallMethod; import vm.runtime.defmeth.shared.data.method.body.MethodBody; /** * Context-specific builder for method instances of type {@code }. * Allows to construct type-safe nested builders for classes with methods. * * Example: * * Interface I = * new TestBuilder().intf("I") * .defaultMethod("m", "()V").emptyBody().build() * .build(); * * * @param */ public class ClassMethodBuilder { /* Enclosing Clazz builder */ private ClassBuilder cb; private MethodBuilder mb; /* package-private */ ClassMethodBuilder(ClassBuilder cb, MethodBuilder mb) { this.cb = cb; this.mb = mb; } public ClassMethodBuilder name(String name) { mb.name(name); return this; } public ClassMethodBuilder desc(String desc) { mb.desc(desc); return this; } public ClassMethodBuilder sig(String sig) { mb.sig(sig); return this; } public ClassMethodBuilder body(MethodBody body) { mb.body(body); return this; } public ClassMethodBuilder emptyBody() { mb.empty(); return this; } public ClassMethodBuilder returns(int i) { mb.returns(i); return this; } public ClassMethodBuilder returnsNull() { mb.returnsNull(); return this; } public ClassMethodBuilder throw_(Clazz clz) { mb.throws_(clz); return this; } public ClassMethodBuilder throw_(Class exc) { mb.throws_(exc); return this; } public ClassMethodBuilder returnsNewInstance(ConcreteClass clz) { mb.returnsNewInstance(clz); return this; } public ClassMethodBuilder callSuper(Clazz callee, String methodName, String methodDesc) { mb.superCall(callee, methodName, methodDesc); return this; } public ClassMethodBuilder invokeSpecial(Clazz callee, String methodName, String methodDesc) { mb.invokeSpecial(callee, methodName, methodDesc); return this; } public ClassMethodBuilder invokeStatic(Clazz callee, String methodName, String methodDesc) { mb.invokeStatic(callee, methodName, methodDesc); return this; } public ClassMethodBuilder invoke(CallMethod.Invoke callInsn, Clazz staticCallee, ConcreteClass callee, String methodName, String methodDesc, CallMethod.IndexbyteOp generateIndexbyteOp) { mb.invoke(callInsn, staticCallee, callee, methodName, methodDesc, generateIndexbyteOp); return this; } public ClassMethodBuilder public_() { mb.public_(); return this; } public ClassMethodBuilder protected_() { mb.protected_(); return this; } public ClassMethodBuilder private_() { mb.private_(); return this; } public ClassMethodBuilder package_private() { mb.package_private(); return this; } public ClassMethodBuilder static_() { mb.static_(); return this; } public ClassMethodBuilder synthetic() { mb.synthetic(); return this; } public ClassMethodBuilder flags(int flags) { mb.flags(flags); return this; } public ClassMethodBuilder addFlags(int flags) { mb.addFlags(flags); return this; } @SuppressWarnings("unchecked") public T build() { cb.method(mb.build()); return (T)cb; } }