1579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson/* 2579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Copyright (C) 2011 The Android Open Source Project 3579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 4579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); 5579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * you may not use this file except in compliance with the License. 6579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * You may obtain a copy of the License at 7579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 8579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 9579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 10579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * Unless required by applicable law or agreed to in writing, software 11579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 12579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * See the License for the specific language governing permissions and 14579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * limitations under the License. 15579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 16579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 17b8a5896885d4da2b798888b688a46df1adbf5b89Paul Duffinpackage com.android.dx; 18579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 19b8a5896885d4da2b798888b688a46df1adbf5b89Paul Duffinimport com.android.dex.DexFormat; 20579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.dex.DexOptions; 21579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.dex.code.DalvCode; 22579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.dex.code.PositionList; 23579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.dex.code.RopTranslator; 24579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.dex.file.ClassDefItem; 25579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.dex.file.DexFile; 26579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.dex.file.EncodedField; 27579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.dex.file.EncodedMethod; 285624228626d7cdf206de25a6981ba8107be61057Jesse Wilsonimport com.android.dx.rop.code.AccessFlags; 29579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.code.LocalVariableInfo; 30579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.code.RopMethod; 31579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.cst.CstString; 32579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.cst.CstType; 33579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.type.StdTypeList; 34db20bbcc82de39f499a804d215851995041f3bcfPaul Duffinimport com.android.dx.stock.ProxyBuilder; 35579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.io.File; 36579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.io.FileOutputStream; 37579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.io.IOException; 38579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.lang.reflect.InvocationTargetException; 3923abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilsonimport java.lang.reflect.Modifier; 40db20bbcc82de39f499a804d215851995041f3bcfPaul Duffinimport dalvik.system.DexClassLoader; 41db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin 42054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousefimport java.util.Arrays; 43054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousefimport java.util.Iterator; 44579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.util.LinkedHashMap; 45579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.util.Map; 46054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousefimport java.util.Set; 47579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.util.jar.JarEntry; 48579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.util.jar.JarOutputStream; 49579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 50b8a5896885d4da2b798888b688a46df1adbf5b89Paul Duffinimport static com.android.dx.rop.code.AccessFlags.ACC_CONSTRUCTOR; 51b8a5896885d4da2b798888b688a46df1adbf5b89Paul Duffinimport static java.lang.reflect.Modifier.PRIVATE; 52b8a5896885d4da2b798888b688a46df1adbf5b89Paul Duffinimport static java.lang.reflect.Modifier.STATIC; 53b8a5896885d4da2b798888b688a46df1adbf5b89Paul Duffin 54579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson/** 55b8a5896885d4da2b798888b688a46df1adbf5b89Paul Duffin * Generates a <strong>D</strong>alvik <strong>EX</strong>ecutable (dex) 56b0f6ea8cec29bd1b2453e8fd15d9c6f65ca3ea2cJesse Wilson * file for execution on Android. Dex files define classes and interfaces, 570e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * including their member methods and fields, executable code, and debugging 580e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * information. They also define annotations, though this API currently has no 590e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * facility to create a dex file that contains annotations. 600e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 610e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <p>This library is intended to satisfy two use cases: 620e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <ul> 630e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <li><strong>For runtime code generation.</strong> By embedding this library 640e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * in your Android application, you can dynamically generate and load 650e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * executable code. This approach takes advantage of the fact that the 660e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * host environment and target environment are both Android. 670e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <li><strong>For compile time code generation.</strong> You may use this 680e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * library as a part of a compiler that targets Android. In this scenario 690e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * the generated dex file must be installed on an Android device before it 700e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * can be executed. 710e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * </ul> 720e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 730e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <h3>Example: Fibonacci</h3> 740e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * To illustrate how this API is used, we'll use DexMaker to generate a class 75008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * equivalent to the following Java source: <pre> {@code 760e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 770e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * package com.publicobject.fib; 780e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 790e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * public class Fibonacci { 800e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * public static int fib(int i) { 810e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * if (i < 2) { 820e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * return i; 830e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * } 840e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * return fib(i - 1) + fib(i - 2); 850e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * } 860e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }}</pre> 870e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 880e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <p>We start by creating a {@link TypeId} to identify the generated {@code 890e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Fibonacci} class. DexMaker identifies types by their internal names like 900e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * {@code Ljava/lang/Object;} rather than their Java identifiers like {@code 910e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * java.lang.Object}. <pre> {@code 920e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 930e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * TypeId<?> fibonacci = TypeId.get("Lcom/google/dexmaker/examples/Fibonacci;"); 940e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 950e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 960e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <p>Next we declare the class. It allows us to specify the type's source file 970e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * for stack traces, its modifiers, its superclass, and the interfaces it 980e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * implements. In this case, {@code Fibonacci} is a public class that extends 990e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * from {@code Object}: <pre> {@code 1000e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1010e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * String fileName = "Fibonacci.generated"; 1020e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * DexMaker dexMaker = new DexMaker(); 1030e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * dexMaker.declare(fibonacci, fileName, Modifier.PUBLIC, TypeId.OBJECT); 1040e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 1050e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * It is illegal to declare members of a class without also declaring the class 1060e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * itself. 1070e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1080e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <p>To make it easier to go from our Java method to dex instructions, we'll 1090e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * manually translate it to pseudocode fit for an assembler. We need to replace 1100e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * control flow like {@code if()} blocks and {@code for()} loops with labels and 1110e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * branches. We'll also avoid performing multiple operations in one statement, 1120e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * using local variables to hold intermediate values as necessary: 1130e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <pre> {@code 1140e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1150e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * int constant1 = 1; 1160e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * int constant2 = 2; 1170e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * if (i < constant2) goto baseCase; 1180e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * int a = i - constant1; 1190e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * int b = i - constant2; 1200e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * int c = fib(a); 1210e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * int d = fib(b); 1220e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * int result = c + d; 1230e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * return result; 1240e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * baseCase: 1250e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * return i; 1260e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 1270e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 128008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * <p>We look up the {@code MethodId} for the method on the declaring type. This 129008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * takes the method's return type (possibly {@link TypeId#VOID}), its name and 130008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * its parameters types. Next we declare the method, specifying its modifiers by 131008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * bitwise ORing constants from {@link java.lang.reflect.Modifier}. The declare 132008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * call returns a {@link Code} object, which we'll use to define the method's 133008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * instructions. <pre> {@code 1340e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1350e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * MethodId<?, Integer> fib = fibonacci.getMethod(TypeId.INT, "fib", TypeId.INT); 1360e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Code code = dexMaker.declare(fib, Modifier.PUBLIC | Modifier.STATIC); 1370e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 1380e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1390e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <p>One limitation of {@code DexMaker}'s API is that it requires all local 1400e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * variables to be created before any instructions are emitted. Use {@link 141008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * Code#newLocal newLocal()} to create a new local variable. The method's 142008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * parameters are exposed as locals using {@link Code#getParameter 143008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * getParameter()}. For non-static methods the {@code this} pointer is exposed 144008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * using {@link Code#getThis getThis()}. Here we declare all of the local 145008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * variables that we'll need for our {@code fib()} method: <pre> {@code 1460e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1470e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> i = code.getParameter(0, TypeId.INT); 1480e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> constant1 = code.newLocal(TypeId.INT); 1490e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> constant2 = code.newLocal(TypeId.INT); 1500e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> a = code.newLocal(TypeId.INT); 1510e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> b = code.newLocal(TypeId.INT); 1520e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> c = code.newLocal(TypeId.INT); 1530e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> d = code.newLocal(TypeId.INT); 1540e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> result = code.newLocal(TypeId.INT); 1550e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 1560e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 157008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * <p>Notice that {@link Local} has a type parameter of {@code Integer}. This is 158008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * useful for generating code that works with existing types like {@code String} 159008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * and {@code Integer}, but it can be a hindrance when generating code that 160008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * involves new types. For this reason you may prefer to use raw types only and 161008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * add {@code @SuppressWarnings("unsafe")} on your calling code. This will yield 162008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * the same result but you won't get IDE support if you make a type error. 1630e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 164b0f6ea8cec29bd1b2453e8fd15d9c6f65ca3ea2cJesse Wilson * <p>We're ready to start defining our method's instructions. The {@link Code} 165b0f6ea8cec29bd1b2453e8fd15d9c6f65ca3ea2cJesse Wilson * class catalogs the available instructions and their use. <pre> {@code 1660e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1670e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.loadConstant(constant1, 1); 1680e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.loadConstant(constant2, 2); 16923abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Label baseCase = new Label(); 1700e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.compare(Comparison.LT, baseCase, i, constant2); 1710e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.op(BinaryOp.SUBTRACT, a, i, constant1); 1720e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.op(BinaryOp.SUBTRACT, b, i, constant2); 1730e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.invokeStatic(fib, c, a); 1740e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.invokeStatic(fib, d, b); 1750e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.op(BinaryOp.ADD, result, c, d); 1760e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.returnValue(result); 1770e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.mark(baseCase); 1780e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.returnValue(i); 1790e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 1800e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 181008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * <p>We're done defining the dex file. We just need to write it to the 182008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * filesystem or load it into the current process. For this example we'll load 1830e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * the generated code into the current process. This only works when the current 1843e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * process is running on Android. We use {@link #generateAndLoad 1853e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * generateAndLoad()} which takes the class loader that will be used as our 1863e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * generated code's parent class loader. It also requires a directory where 1873e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * temporary files can be written. <pre> {@code 1880e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1890e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * ClassLoader loader = dexMaker.generateAndLoad( 1905692b3b0303c55524ff206dc7840ffdb1fa47628Jesse Wilson * FibonacciMaker.class.getClassLoader(), getDataDirectory()); 1910e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 1920e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Finally we'll use reflection to lookup our generated class on its class 1930e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * loader and invoke its {@code fib()} method: <pre> {@code 1940e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1950e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Class<?> fibonacciClass = loader.loadClass("com.google.dexmaker.examples.Fibonacci"); 1960e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Method fibMethod = fibonacciClass.getMethod("fib", int.class); 1970e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * System.out.println(fibMethod.invoke(null, 8)); 1980e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 199579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 200ab220f004db90fa94ef9349ca1adde5f89012e8dJesse Wilsonpublic final class DexMaker { 201b8a5896885d4da2b798888b688a46df1adbf5b89Paul Duffin private final Map<TypeId<?>, TypeDeclaration> types = new LinkedHashMap<>(); 202579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 2033e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 2043e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Creates a new {@code DexMaker} instance, which can be used to create a 2053e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * single dex file. 2063e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 2073e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson public DexMaker() { 2083e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson } 2093e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson 2100e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson private TypeDeclaration getTypeDeclaration(TypeId<?> type) { 211579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson TypeDeclaration result = types.get(type); 212579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (result == null) { 213579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson result = new TypeDeclaration(type); 214579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson types.put(type, result); 215579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 216579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return result; 217579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 218579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 219579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 22023abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Declares {@code type}. 22123abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * 22223abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * @param flags a bitwise combination of {@link Modifier#PUBLIC}, {@link 22323abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Modifier#FINAL} and {@link Modifier#ABSTRACT}. 224579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 2250e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public void declare(TypeId<?> type, String sourceFile, int flags, 2260e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson TypeId<?> supertype, TypeId<?>... interfaces) { 227579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson TypeDeclaration declaration = getTypeDeclaration(type); 228c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson int supportedFlags = Modifier.PUBLIC | Modifier.FINAL | Modifier.ABSTRACT; 229c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson if ((flags & ~supportedFlags) != 0) { 230c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson throw new IllegalArgumentException("Unexpected flag: " 231c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson + Integer.toHexString(flags)); 232c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson } 233579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (declaration.declared) { 234579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalStateException("already declared: " + type); 235579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 236579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson declaration.declared = true; 237579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson declaration.flags = flags; 238579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson declaration.supertype = supertype; 239579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson declaration.sourceFile = sourceFile; 240579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson declaration.interfaces = new TypeList(interfaces); 241579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 242579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 243579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 244c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson * Declares a method or constructor. 24523abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * 24623abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * @param flags a bitwise combination of {@link Modifier#PUBLIC}, {@link 24723abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Modifier#PRIVATE}, {@link Modifier#PROTECTED}, {@link Modifier#STATIC}, 248c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson * {@link Modifier#FINAL} and {@link Modifier#SYNCHRONIZED}. 2495624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * <p><strong>Warning:</strong> the {@link Modifier#SYNCHRONIZED} flag 2505624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * is insufficient to generate a synchronized method. You must also use 2515624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * {@link Code#monitorEnter} and {@link Code#monitorExit} to acquire 2525624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * a monitor. 25390699b97998f1582a921202fb909f17f9718d177Jesse Wilson */ 254579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public Code declare(MethodId<?, ?> method, int flags) { 255579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson TypeDeclaration typeDeclaration = getTypeDeclaration(method.declaringType); 256579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (typeDeclaration.methods.containsKey(method)) { 257579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalStateException("already declared: " + method); 258579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 259c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson 260c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson int supportedFlags = Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED 261c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson | Modifier.STATIC | Modifier.FINAL | Modifier.SYNCHRONIZED; 262c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson if ((flags & ~supportedFlags) != 0) { 263c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson throw new IllegalArgumentException("Unexpected flag: " 264c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson + Integer.toHexString(flags)); 265c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson } 266c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson 2675624228626d7cdf206de25a6981ba8107be61057Jesse Wilson // replace the SYNCHRONIZED flag with the DECLARED_SYNCHRONIZED flag 2685624228626d7cdf206de25a6981ba8107be61057Jesse Wilson if ((flags & Modifier.SYNCHRONIZED) != 0) { 2695624228626d7cdf206de25a6981ba8107be61057Jesse Wilson flags = (flags & ~Modifier.SYNCHRONIZED) | AccessFlags.ACC_DECLARED_SYNCHRONIZED; 2705624228626d7cdf206de25a6981ba8107be61057Jesse Wilson } 271c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson 272c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson if (method.isConstructor()) { 273c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson flags |= ACC_CONSTRUCTOR; 274c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson } 275c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson 276579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson MethodDeclaration methodDeclaration = new MethodDeclaration(method, flags); 277579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson typeDeclaration.methods.put(method, methodDeclaration); 278579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return methodDeclaration.code; 279579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 280579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 281579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 28223abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Declares a field. 28323abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * 28423abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * @param flags a bitwise combination of {@link Modifier#PUBLIC}, {@link 28523abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Modifier#PRIVATE}, {@link Modifier#PROTECTED}, {@link Modifier#STATIC}, 28623abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * {@link Modifier#FINAL}, {@link Modifier#VOLATILE}, and {@link 28723abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Modifier#TRANSIENT}. 288c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson * @param staticValue a constant representing the initial value for the 289c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson * static field, possibly null. This must be null if this field is 290c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson * non-static. 291579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 292579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public void declare(FieldId<?, ?> fieldId, int flags, Object staticValue) { 293579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson TypeDeclaration typeDeclaration = getTypeDeclaration(fieldId.declaringType); 294579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (typeDeclaration.fields.containsKey(fieldId)) { 295579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalStateException("already declared: " + fieldId); 296579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 297c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson 298c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson int supportedFlags = Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED 299c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson | Modifier.STATIC | Modifier.FINAL | Modifier.VOLATILE | Modifier.TRANSIENT; 300c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson if ((flags & ~supportedFlags) != 0) { 301c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson throw new IllegalArgumentException("Unexpected flag: " 302c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson + Integer.toHexString(flags)); 303c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson } 304c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson 305c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson if ((flags & Modifier.STATIC) == 0 && staticValue != null) { 306c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson throw new IllegalArgumentException("staticValue is non-null, but field is not static"); 307c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson } 308c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson 309579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson FieldDeclaration fieldDeclaration = new FieldDeclaration(fieldId, flags, staticValue); 310579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson typeDeclaration.fields.put(fieldId, fieldDeclaration); 311579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 312579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 313579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 31423abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Generates a dex file and returns its bytes. 315579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 316579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public byte[] generate() { 317579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson DexOptions options = new DexOptions(); 318579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson options.targetApiLevel = DexFormat.API_NO_EXTENDED_OPCODES; 319579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson DexFile outputDex = new DexFile(options); 320579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 321579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (TypeDeclaration typeDeclaration : types.values()) { 322579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson outputDex.add(typeDeclaration.toClassDefItem()); 323579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 324579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 325579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson try { 326579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return outputDex.toDex(null, false); 327579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } catch (IOException e) { 328579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new RuntimeException(e); 329579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 330579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 331579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 332054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef // Generate a file name for the jar by taking a checksum of MethodIds and 333054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef // parent class types. 334054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef private String generateFileName() { 335054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef int checksum = 1; 336054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef 337054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef Set<TypeId<?>> typesKeySet = types.keySet(); 338054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef Iterator<TypeId<?>> it = typesKeySet.iterator(); 339054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef int[] checksums = new int[typesKeySet.size()]; 340054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef int i = 0; 341054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef 342054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef while (it.hasNext()) { 343054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef TypeId<?> typeId = it.next(); 344054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef TypeDeclaration decl = getTypeDeclaration(typeId); 345054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef Set<MethodId> methodSet = decl.methods.keySet(); 346054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef if (decl.supertype != null) { 347054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef checksums[i++] = 31 * decl.supertype.hashCode() + methodSet.hashCode(); 348054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } 349054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } 350054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef Arrays.sort(checksums); 351054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef 352054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef for (int sum : checksums) { 353054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef checksum *= 31; 354054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef checksum += sum; 355054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } 356054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef 357054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef return "Generated_" + checksum +".jar"; 358054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } 359054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef 360db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin private ClassLoader generateClassLoader(ClassLoader classLoader, File result, File dexCache, 361db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin ClassLoader parent) { 362054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef try { 363db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin boolean shareClassLoader = Boolean.parseBoolean(System.getProperty( 364db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin "dexmaker.share_classloader", "false")); 365db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin if (shareClassLoader) { 366db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin ClassLoader loader = parent != null ? parent : classLoader; 367db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin loader.getClass().getMethod("addDexPath", String.class).invoke(loader, 368db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin result.getPath()); 369db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin return loader; 370db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin } else { 371db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin return (ClassLoader) Class.forName("dalvik.system.DexClassLoader") 372db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin .getConstructor(String.class, String.class, String.class, ClassLoader.class) 373db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin .newInstance(result.getPath(), dexCache.getAbsolutePath(), null, parent); 374db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin } 375054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } catch (ClassNotFoundException e) { 376054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef throw new UnsupportedOperationException("load() requires a Dalvik VM", e); 377054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } catch (InvocationTargetException e) { 378054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef throw new RuntimeException(e.getCause()); 379054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } catch (InstantiationException e) { 380054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef throw new AssertionError(); 381054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } catch (NoSuchMethodException e) { 382054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef throw new AssertionError(); 383054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } catch (IllegalAccessException e) { 384054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef throw new AssertionError(); 385054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } 386054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } 387054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef 388579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 38923abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Generates a dex file and loads its types into the current process. 390579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 39173cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * <h3>Picking a dex cache directory</h3> 39273cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * The {@code dexCache} should be an application-private directory. If 39373cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * you pass a world-writable directory like {@code /sdcard} a malicious app 39473cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * could inject code into your process. Most applications should use this: 39573cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * <pre> {@code 396579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 39773cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * File dexCache = getApplicationContext().getDir("dx", Context.MODE_PRIVATE); 39873cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * }</pre> 39973cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * If the {@code dexCache} is null, this method will consult the {@code 40073cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * dexmaker.dexcache} system property. If that exists, it will be used for 40173cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * the dex cache. If it doesn't exist, this method will attempt to guess 40273cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * the application's private data directory as a last resort. If that fails, 40373cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * this method will fail with an unchecked exception. You can avoid the 40473cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * exception by either providing a non-null value or setting the system 40573cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * property. 406579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 40773cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * @param parent the parent ClassLoader to be used when loading our 40873cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * generated types 40973cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * @param dexCache the destination directory where generated and optimized 41073cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * dex files will be written. If null, this class will try to guess the 41173cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * application's private data dir. 412579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 41373cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson public ClassLoader generateAndLoad(ClassLoader parent, File dexCache) throws IOException { 414db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin return generateAndLoad(parent, parent, dexCache); 415db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin } 416db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin 417db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin public ClassLoader generateAndLoad(ClassLoader classLoader, ClassLoader parent, File dexCache) 418db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin throws IOException { 41973cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson if (dexCache == null) { 42073cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson String property = System.getProperty("dexmaker.dexcache"); 42173cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson if (property != null) { 42273cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson dexCache = new File(property); 42373cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson } else { 42473cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson dexCache = new AppDataDirGuesser().guess(); 42573cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson if (dexCache == null) { 42673cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson throw new IllegalArgumentException("dexcache == null (and no default could be" 42773cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson + " found; consider setting the 'dexmaker.dexcache' system property)"); 42873cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson } 42973cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson } 43073cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson } 43173cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson 432054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef File result = new File(dexCache, generateFileName()); 433054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef // Check that the file exists. If it does, return a DexClassLoader and skip all 434054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef // the dex bytecode generation. 435054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef if (result.exists()) { 436db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin return generateClassLoader(classLoader, result, dexCache, parent); 437054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } 438054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef 439579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson byte[] dex = generate(); 440579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 441579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /* 442579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * This implementation currently dumps the dex to the filesystem. It 443579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * jars the emitted .dex for the benefit of Gingerbread and earlier 444579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * devices, which can't load .dex files directly. 445579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 446579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * TODO: load the dex from memory where supported. 447579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 448054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef result.createNewFile(); 449579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(result)); 4505999ddea21d4d5887cecdcb7730b0d16cdc54d93Andreas Gampe JarEntry entry = new JarEntry(DexFormat.DEX_IN_JAR_NAME); 4515999ddea21d4d5887cecdcb7730b0d16cdc54d93Andreas Gampe entry.setSize(dex.length); 4525999ddea21d4d5887cecdcb7730b0d16cdc54d93Andreas Gampe jarOut.putNextEntry(entry); 453579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson jarOut.write(dex); 454579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson jarOut.closeEntry(); 455579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson jarOut.close(); 456db20bbcc82de39f499a804d215851995041f3bcfPaul Duffin return generateClassLoader(classLoader, result, dexCache, parent); 457579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 458579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 459579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private static class TypeDeclaration { 4600e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson private final TypeId<?> type; 461579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 462579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** declared state */ 463579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private boolean declared; 464579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private int flags; 4650e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson private TypeId<?> supertype; 466579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private String sourceFile; 467579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private TypeList interfaces; 468579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 469b8a5896885d4da2b798888b688a46df1adbf5b89Paul Duffin private final Map<FieldId, FieldDeclaration> fields = new LinkedHashMap<>(); 470b8a5896885d4da2b798888b688a46df1adbf5b89Paul Duffin private final Map<MethodId, MethodDeclaration> methods = new LinkedHashMap<>(); 471579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 4720e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson TypeDeclaration(TypeId<?> type) { 473579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.type = type; 474579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 475579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 476579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson ClassDefItem toClassDefItem() { 477579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (!declared) { 478579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalStateException("Undeclared type " + type + " declares members: " 479579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson + fields.keySet() + " " + methods.keySet()); 480579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 481579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 482579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson DexOptions dexOptions = new DexOptions(); 483579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson dexOptions.targetApiLevel = DexFormat.API_NO_EXTENDED_OPCODES; 484579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 485579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson CstType thisType = type.constant; 486579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 487579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson ClassDefItem out = new ClassDefItem(thisType, flags, supertype.constant, 488579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson interfaces.ropTypes, new CstString(sourceFile)); 489579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 490579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (MethodDeclaration method : methods.values()) { 491579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson EncodedMethod encoded = method.toEncodedMethod(dexOptions); 492579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (method.isDirect()) { 493579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson out.addDirectMethod(encoded); 494579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } else { 495579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson out.addVirtualMethod(encoded); 496579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 497579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 498579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (FieldDeclaration field : fields.values()) { 499579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson EncodedField encoded = field.toEncodedField(); 500579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (field.isStatic()) { 501579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson out.addStaticField(encoded, Constants.getConstant(field.staticValue)); 502579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } else { 503579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson out.addInstanceField(encoded); 504579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 505579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 506579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 507579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return out; 508579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 509579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 510579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 511579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson static class FieldDeclaration { 512579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson final FieldId<?, ?> fieldId; 513579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final int accessFlags; 514579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final Object staticValue; 515579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 516579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson FieldDeclaration(FieldId<?, ?> fieldId, int accessFlags, Object staticValue) { 51790699b97998f1582a921202fb909f17f9718d177Jesse Wilson if ((accessFlags & STATIC) == 0 && staticValue != null) { 518579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException("instance fields may not have a value"); 519579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 520579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.fieldId = fieldId; 521579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.accessFlags = accessFlags; 522579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.staticValue = staticValue; 523579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 524579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 525579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson EncodedField toEncodedField() { 526579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return new EncodedField(fieldId.constant, accessFlags); 527579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 528579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 529579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public boolean isStatic() { 53090699b97998f1582a921202fb909f17f9718d177Jesse Wilson return (accessFlags & STATIC) != 0; 531579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 532579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 533579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 534579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson static class MethodDeclaration { 535579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson final MethodId<?, ?> method; 536579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final int flags; 537579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final Code code; 538579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 539579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public MethodDeclaration(MethodId<?, ?> method, int flags) { 540579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.method = method; 541579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.flags = flags; 542579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.code = new Code(this); 543579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 544579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 545579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson boolean isStatic() { 54690699b97998f1582a921202fb909f17f9718d177Jesse Wilson return (flags & STATIC) != 0; 547579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 548579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 549579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson boolean isDirect() { 55090699b97998f1582a921202fb909f17f9718d177Jesse Wilson return (flags & (STATIC | PRIVATE | ACC_CONSTRUCTOR)) != 0; 551579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 552579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 553579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson EncodedMethod toEncodedMethod(DexOptions dexOptions) { 554579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RopMethod ropMethod = new RopMethod(code.toBasicBlocks(), 0); 555579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson LocalVariableInfo locals = null; 556579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson DalvCode dalvCode = RopTranslator.translate( 557579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson ropMethod, PositionList.NONE, locals, code.paramSize(), dexOptions); 558579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return new EncodedMethod(method.constant, flags, dalvCode, StdTypeList.EMPTY); 559579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 560579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 561579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson} 562