/* * Copyright (c) 2007 Christian Plesner Hansen (plesner@quenta.org) * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package org.quenta.clafig.test; import org.quenta.clafig.ClassFile; import org.quenta.clafig.ClassFileLoader; import org.quenta.clafig.Code; import org.quenta.clafig.Label; public class Main { public static int twice(int x) { return 2 * x; } public interface ICallable { public int call(int value); } public static void main(String[] args) throws Exception { // class foo.bar.TestClass implements ICallable ClassFile file = new ClassFile("foo.bar.TestClass"); file.addImplements(ICallable.class); // TestClass() { super(); } file.addConstructor(new Code(file) {{ aload(0); invokesuper(Object.class.getConstructor()); rethurn(); }}); // public int call(int x) { // while (x < 10) // x = Main.twice(x); // return x; // } Code callMe = file.addMethod("call", new Code(file) {{ // --- Labels --- Label loopStart = new Label(); Label loopEnd = new Label(); // --- Code --- bind(loopStart); push(15); iload(1); isub(); iflt(loopEnd); iload(1); invokestatic(Main.class.getDeclaredMethod("twice", Integer.TYPE)); istore(1); ghoto(loopStart); bind(loopEnd); iload(1); ireturn(); }}); callMe.returnType(Integer.TYPE); callMe.argumentTypes(Integer.TYPE); Class chlass = new ClassFileLoader().createClass(file); ICallable obj = (ICallable) chlass.newInstance(); int value = obj.call(7); System.out.println(value); } }