• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.google.dexmaker.examples;
18 
19 import com.google.dexmaker.BinaryOp;
20 import com.google.dexmaker.Code;
21 import com.google.dexmaker.Comparison;
22 import com.google.dexmaker.DexMaker;
23 import com.google.dexmaker.Label;
24 import com.google.dexmaker.Local;
25 import com.google.dexmaker.MethodId;
26 import com.google.dexmaker.TypeId;
27 import java.io.File;
28 import java.lang.reflect.Method;
29 import java.lang.reflect.Modifier;
30 
31 public final class FibonacciMaker {
main(String[] args)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 
getDataDirectory()72     public static File getDataDirectory() {
73         String envVariable = "ANDROID_DATA";
74         String defaultLoc = "/data";
75         String path = System.getenv(envVariable);
76         return path == null ? new File(defaultLoc) : new File(path);
77     }
78 }
79