FibonacciMaker.java revision 0e49fb9243b7463835ab80ef7cc62435f55846ce
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 DexMaker dexMaker = new DexMaker(); 34 TypeId<?> fibonacci = TypeId.get("Lcom/google/dexmaker/examples/Fibonacci;"); 35 String fileName = "Fibonacci.generated"; 36 dexMaker.declare(fibonacci, fileName, Modifier.PUBLIC, TypeId.OBJECT); 37 38 MethodId<?, Integer> fib = fibonacci.getMethod(TypeId.INT, "fib", TypeId.INT); 39 Code code = dexMaker.declare(fib, Modifier.PUBLIC | Modifier.STATIC); 40 Local<Integer> i = code.getParameter(0, TypeId.INT); 41 Local<Integer> constant1 = code.newLocal(TypeId.INT); 42 Local<Integer> constant2 = code.newLocal(TypeId.INT); 43 Local<Integer> a = code.newLocal(TypeId.INT); 44 Local<Integer> b = code.newLocal(TypeId.INT); 45 Local<Integer> c = code.newLocal(TypeId.INT); 46 Local<Integer> d = code.newLocal(TypeId.INT); 47 Local<Integer> result = code.newLocal(TypeId.INT); 48 code.loadConstant(constant1, 1); 49 code.loadConstant(constant2, 2); 50 Label baseCase = code.newLabel(); 51 code.compare(Comparison.LT, baseCase, i, constant2); 52 code.op(BinaryOp.SUBTRACT, a, i, constant1); 53 code.op(BinaryOp.SUBTRACT, b, i, constant2); 54 code.invokeStatic(fib, c, a); 55 code.invokeStatic(fib, d, b); 56 code.op(BinaryOp.ADD, result, c, d); 57 code.returnValue(result); 58 code.mark(baseCase); 59 code.returnValue(i); 60 61 ClassLoader loader = dexMaker.generateAndLoad( 62 FibonacciMaker.class.getClassLoader(), getDataDirectory(), getDataDirectory()); 63 Class<?> fibonacciClass = loader.loadClass("com.google.dexmaker.examples.Fibonacci"); 64 Method fibMethod = fibonacciClass.getMethod("fib", int.class); 65 System.out.println(fibMethod.invoke(null, 8)); 66 } 67 68 public static File getDataDirectory() throws Exception { 69 Class<?> environmentClass = Class.forName("android.os.Environment"); 70 Method method = environmentClass.getMethod("getDataDirectory"); 71 Object dataDirectory = method.invoke(null); 72 return (File) dataDirectory; 73 } 74} 75