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 17 package com.android.dx.examples; 18 19 import com.android.dx.Code; 20 import com.android.dx.Comparison; 21 import com.android.dx.DexMaker; 22 import com.android.dx.Label; 23 import com.android.dx.Local; 24 import com.android.dx.MethodId; 25 import com.android.dx.TypeId; 26 import com.android.dx.BinaryOp; 27 28 import java.io.File; 29 import java.lang.reflect.Method; 30 import java.lang.reflect.Modifier; 31 32 public final class FibonacciMaker { main(String[] args)33 public static void main(String[] args) throws Exception { 34 TypeId<?> fibonacci = TypeId.get("Lcom/google/dexmaker/examples/Fibonacci;"); 35 36 String fileName = "Fibonacci.generated"; 37 DexMaker dexMaker = new DexMaker(); 38 dexMaker.declare(fibonacci, fileName, Modifier.PUBLIC, TypeId.OBJECT); 39 40 MethodId<?, Integer> fib = fibonacci.getMethod(TypeId.INT, "fib", TypeId.INT); 41 Code code = dexMaker.declare(fib, Modifier.PUBLIC | Modifier.STATIC); 42 43 Local<Integer> i = code.getParameter(0, TypeId.INT); 44 Local<Integer> constant1 = code.newLocal(TypeId.INT); 45 Local<Integer> constant2 = code.newLocal(TypeId.INT); 46 Local<Integer> a = code.newLocal(TypeId.INT); 47 Local<Integer> b = code.newLocal(TypeId.INT); 48 Local<Integer> c = code.newLocal(TypeId.INT); 49 Local<Integer> d = code.newLocal(TypeId.INT); 50 Local<Integer> result = code.newLocal(TypeId.INT); 51 52 code.loadConstant(constant1, 1); 53 code.loadConstant(constant2, 2); 54 Label baseCase = new Label(); 55 code.compare(Comparison.LT, baseCase, i, constant2); 56 code.op(BinaryOp.SUBTRACT, a, i, constant1); 57 code.op(BinaryOp.SUBTRACT, b, i, constant2); 58 code.invokeStatic(fib, c, a); 59 code.invokeStatic(fib, d, b); 60 code.op(BinaryOp.ADD, result, c, d); 61 code.returnValue(result); 62 code.mark(baseCase); 63 code.returnValue(i); 64 65 ClassLoader loader = dexMaker.generateAndLoad( 66 FibonacciMaker.class.getClassLoader(), getDataDirectory()); 67 68 Class<?> fibonacciClass = loader.loadClass("com.google.dexmaker.examples.Fibonacci"); 69 Method fibMethod = fibonacciClass.getMethod("fib", int.class); 70 System.out.println(fibMethod.invoke(null, 8)); 71 } 72 getDataDirectory()73 public static File getDataDirectory() { 74 String envVariable = "ANDROID_DATA"; 75 String defaultLoc = "/data"; 76 String path = System.getenv(envVariable); 77 return path == null ? new File(defaultLoc) : new File(path); 78 } 79 } 80