FibonacciMaker.java revision 5692b3b0303c55524ff206dc7840ffdb1fa47628
1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.google.dexmaker.examples; 18 19import com.google.dexmaker.BinaryOp; 20import com.google.dexmaker.Code; 21import com.google.dexmaker.Comparison; 22import com.google.dexmaker.DexMaker; 23import com.google.dexmaker.Label; 24import com.google.dexmaker.Local; 25import com.google.dexmaker.MethodId; 26import com.google.dexmaker.TypeId; 27import java.io.File; 28import java.lang.reflect.Method; 29import java.lang.reflect.Modifier; 30 31public final class FibonacciMaker { 32 public static void main(String[] args) throws Exception { 33 TypeId<?> fibonacci = TypeId.get("Lcom/google/dexmaker/examples/Fibonacci;"); 34 35 String fileName = "Fibonacci.generated"; 36 DexMaker dexMaker = new DexMaker(); 37 dexMaker.declare(fibonacci, fileName, Modifier.PUBLIC, TypeId.OBJECT); 38 39 MethodId<?, Integer> fib = fibonacci.getMethod(TypeId.INT, "fib", TypeId.INT); 40 Code code = dexMaker.declare(fib, Modifier.PUBLIC | Modifier.STATIC); 41 42 Local<Integer> i = code.getParameter(0, TypeId.INT); 43 Local<Integer> constant1 = code.newLocal(TypeId.INT); 44 Local<Integer> constant2 = code.newLocal(TypeId.INT); 45 Local<Integer> a = code.newLocal(TypeId.INT); 46 Local<Integer> b = code.newLocal(TypeId.INT); 47 Local<Integer> c = code.newLocal(TypeId.INT); 48 Local<Integer> d = code.newLocal(TypeId.INT); 49 Local<Integer> result = code.newLocal(TypeId.INT); 50 51 code.loadConstant(constant1, 1); 52 code.loadConstant(constant2, 2); 53 Label baseCase = new Label(); 54 code.compare(Comparison.LT, baseCase, i, constant2); 55 code.op(BinaryOp.SUBTRACT, a, i, constant1); 56 code.op(BinaryOp.SUBTRACT, b, i, constant2); 57 code.invokeStatic(fib, c, a); 58 code.invokeStatic(fib, d, b); 59 code.op(BinaryOp.ADD, result, c, d); 60 code.returnValue(result); 61 code.mark(baseCase); 62 code.returnValue(i); 63 64 ClassLoader loader = dexMaker.generateAndLoad( 65 FibonacciMaker.class.getClassLoader(), getDataDirectory()); 66 67 Class<?> fibonacciClass = loader.loadClass("com.google.dexmaker.examples.Fibonacci"); 68 Method fibMethod = fibonacciClass.getMethod("fib", int.class); 69 System.out.println(fibMethod.invoke(null, 8)); 70 } 71 72 public static File getDataDirectory() throws Exception { 73 Class<?> environmentClass = Class.forName("android.os.Environment"); 74 Method method = environmentClass.getMethod("getDataDirectory"); 75 Object dataDirectory = method.invoke(null); 76 return (File) dataDirectory; 77 } 78} 79