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 17579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonpackage com.google.dexmaker; 18579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 19579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.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; 2990699b97998f1582a921202fb909f17f9718d177Jesse Wilsonimport static com.android.dx.rop.code.AccessFlags.ACC_CONSTRUCTOR; 30579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.code.LocalVariableInfo; 31579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.code.RopMethod; 32579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.cst.CstString; 33579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.cst.CstType; 34579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport com.android.dx.rop.type.StdTypeList; 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; 4090699b97998f1582a921202fb909f17f9718d177Jesse Wilsonimport static java.lang.reflect.Modifier.PRIVATE; 4190699b97998f1582a921202fb909f17f9718d177Jesse Wilsonimport static java.lang.reflect.Modifier.STATIC; 42054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef 43054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousefimport java.util.Arrays; 44054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousefimport java.util.Iterator; 45579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.util.LinkedHashMap; 46579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.util.Map; 47054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousefimport java.util.Set; 48579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.util.jar.JarEntry; 49579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilsonimport java.util.jar.JarOutputStream; 50579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 51579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson/** 520e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Generates a </i><strong>D</strong>alvik <strong>EX</strong>ecutable (dex) 53b0f6ea8cec29bd1b2453e8fd15d9c6f65ca3ea2cJesse Wilson * file for execution on Android. Dex files define classes and interfaces, 540e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * including their member methods and fields, executable code, and debugging 550e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * information. They also define annotations, though this API currently has no 560e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * facility to create a dex file that contains annotations. 570e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 580e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <p>This library is intended to satisfy two use cases: 590e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <ul> 600e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <li><strong>For runtime code generation.</strong> By embedding this library 610e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * in your Android application, you can dynamically generate and load 620e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * executable code. This approach takes advantage of the fact that the 630e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * host environment and target environment are both Android. 640e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <li><strong>For compile time code generation.</strong> You may use this 650e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * library as a part of a compiler that targets Android. In this scenario 660e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * the generated dex file must be installed on an Android device before it 670e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * can be executed. 680e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * </ul> 690e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 700e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <h3>Example: Fibonacci</h3> 710e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * To illustrate how this API is used, we'll use DexMaker to generate a class 72008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * equivalent to the following Java source: <pre> {@code 730e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 740e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * package com.publicobject.fib; 750e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 760e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * public class Fibonacci { 770e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * public static int fib(int i) { 780e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * if (i < 2) { 790e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * return i; 800e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * } 810e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * return fib(i - 1) + fib(i - 2); 820e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * } 830e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }}</pre> 840e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 850e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <p>We start by creating a {@link TypeId} to identify the generated {@code 860e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Fibonacci} class. DexMaker identifies types by their internal names like 870e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * {@code Ljava/lang/Object;} rather than their Java identifiers like {@code 880e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * java.lang.Object}. <pre> {@code 890e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 900e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * TypeId<?> fibonacci = TypeId.get("Lcom/google/dexmaker/examples/Fibonacci;"); 910e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 920e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 930e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <p>Next we declare the class. It allows us to specify the type's source file 940e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * for stack traces, its modifiers, its superclass, and the interfaces it 950e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * implements. In this case, {@code Fibonacci} is a public class that extends 960e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * from {@code Object}: <pre> {@code 970e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 980e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * String fileName = "Fibonacci.generated"; 990e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * DexMaker dexMaker = new DexMaker(); 1000e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * dexMaker.declare(fibonacci, fileName, Modifier.PUBLIC, TypeId.OBJECT); 1010e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 1020e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * It is illegal to declare members of a class without also declaring the class 1030e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * itself. 1040e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1050e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <p>To make it easier to go from our Java method to dex instructions, we'll 1060e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * manually translate it to pseudocode fit for an assembler. We need to replace 1070e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * control flow like {@code if()} blocks and {@code for()} loops with labels and 1080e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * branches. We'll also avoid performing multiple operations in one statement, 1090e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * using local variables to hold intermediate values as necessary: 1100e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <pre> {@code 1110e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1120e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * int constant1 = 1; 1130e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * int constant2 = 2; 1140e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * if (i < constant2) goto baseCase; 1150e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * int a = i - constant1; 1160e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * int b = i - constant2; 1170e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * int c = fib(a); 1180e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * int d = fib(b); 1190e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * int result = c + d; 1200e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * return result; 1210e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * baseCase: 1220e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * return i; 1230e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 1240e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 125008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * <p>We look up the {@code MethodId} for the method on the declaring type. This 126008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * takes the method's return type (possibly {@link TypeId#VOID}), its name and 127008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * its parameters types. Next we declare the method, specifying its modifiers by 128008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * bitwise ORing constants from {@link java.lang.reflect.Modifier}. The declare 129008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * call returns a {@link Code} object, which we'll use to define the method's 130008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * instructions. <pre> {@code 1310e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1320e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * MethodId<?, Integer> fib = fibonacci.getMethod(TypeId.INT, "fib", TypeId.INT); 1330e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Code code = dexMaker.declare(fib, Modifier.PUBLIC | Modifier.STATIC); 1340e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 1350e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1360e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * <p>One limitation of {@code DexMaker}'s API is that it requires all local 1370e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * variables to be created before any instructions are emitted. Use {@link 138008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * Code#newLocal newLocal()} to create a new local variable. The method's 139008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * parameters are exposed as locals using {@link Code#getParameter 140008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * getParameter()}. For non-static methods the {@code this} pointer is exposed 141008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * using {@link Code#getThis getThis()}. Here we declare all of the local 142008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * variables that we'll need for our {@code fib()} method: <pre> {@code 1430e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1440e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> i = code.getParameter(0, TypeId.INT); 1450e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> constant1 = code.newLocal(TypeId.INT); 1460e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> constant2 = code.newLocal(TypeId.INT); 1470e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> a = code.newLocal(TypeId.INT); 1480e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> b = code.newLocal(TypeId.INT); 1490e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> c = code.newLocal(TypeId.INT); 1500e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> d = code.newLocal(TypeId.INT); 1510e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Local<Integer> result = code.newLocal(TypeId.INT); 1520e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 1530e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 154008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * <p>Notice that {@link Local} has a type parameter of {@code Integer}. This is 155008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * useful for generating code that works with existing types like {@code String} 156008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * and {@code Integer}, but it can be a hindrance when generating code that 157008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * involves new types. For this reason you may prefer to use raw types only and 158008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * add {@code @SuppressWarnings("unsafe")} on your calling code. This will yield 159008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * the same result but you won't get IDE support if you make a type error. 1600e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 161b0f6ea8cec29bd1b2453e8fd15d9c6f65ca3ea2cJesse Wilson * <p>We're ready to start defining our method's instructions. The {@link Code} 162b0f6ea8cec29bd1b2453e8fd15d9c6f65ca3ea2cJesse Wilson * class catalogs the available instructions and their use. <pre> {@code 1630e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1640e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.loadConstant(constant1, 1); 1650e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.loadConstant(constant2, 2); 16623abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Label baseCase = new Label(); 1670e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.compare(Comparison.LT, baseCase, i, constant2); 1680e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.op(BinaryOp.SUBTRACT, a, i, constant1); 1690e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.op(BinaryOp.SUBTRACT, b, i, constant2); 1700e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.invokeStatic(fib, c, a); 1710e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.invokeStatic(fib, d, b); 1720e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.op(BinaryOp.ADD, result, c, d); 1730e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.returnValue(result); 1740e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.mark(baseCase); 1750e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * code.returnValue(i); 1760e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 1770e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 178008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * <p>We're done defining the dex file. We just need to write it to the 179008290ab55ac24ef656d254e41a03ad2b1fba7d2Jesse Wilson * filesystem or load it into the current process. For this example we'll load 1800e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * the generated code into the current process. This only works when the current 1813e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * process is running on Android. We use {@link #generateAndLoad 1823e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * generateAndLoad()} which takes the class loader that will be used as our 1833e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * generated code's parent class loader. It also requires a directory where 1843e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * temporary files can be written. <pre> {@code 1850e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1860e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * ClassLoader loader = dexMaker.generateAndLoad( 1875692b3b0303c55524ff206dc7840ffdb1fa47628Jesse Wilson * FibonacciMaker.class.getClassLoader(), getDataDirectory()); 1880e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 1890e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Finally we'll use reflection to lookup our generated class on its class 1900e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * loader and invoke its {@code fib()} method: <pre> {@code 1910e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * 1920e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Class<?> fibonacciClass = loader.loadClass("com.google.dexmaker.examples.Fibonacci"); 1930e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * Method fibMethod = fibonacciClass.getMethod("fib", int.class); 1940e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * System.out.println(fibMethod.invoke(null, 8)); 1950e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson * }</pre> 196579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 197ab220f004db90fa94ef9349ca1adde5f89012e8dJesse Wilsonpublic final class DexMaker { 1980e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson private final Map<TypeId<?>, TypeDeclaration> types 1990e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson = new LinkedHashMap<TypeId<?>, TypeDeclaration>(); 200579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 2013e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson /** 2023e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * Creates a new {@code DexMaker} instance, which can be used to create a 2033e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson * single dex file. 2043e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson */ 2053e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson public DexMaker() { 2063e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson } 2073e7a2230ec75b59ae9b4aad292f51df2542ced7dJesse Wilson 2080e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson private TypeDeclaration getTypeDeclaration(TypeId<?> type) { 209579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson TypeDeclaration result = types.get(type); 210579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (result == null) { 211579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson result = new TypeDeclaration(type); 212579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson types.put(type, result); 213579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 214579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return result; 215579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 216579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 217579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 21823abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Declares {@code type}. 21923abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * 22023abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * @param flags a bitwise combination of {@link Modifier#PUBLIC}, {@link 22123abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Modifier#FINAL} and {@link Modifier#ABSTRACT}. 222579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 2230e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson public void declare(TypeId<?> type, String sourceFile, int flags, 2240e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson TypeId<?> supertype, TypeId<?>... interfaces) { 225579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson TypeDeclaration declaration = getTypeDeclaration(type); 226c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson int supportedFlags = Modifier.PUBLIC | Modifier.FINAL | Modifier.ABSTRACT; 227c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson if ((flags & ~supportedFlags) != 0) { 228c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson throw new IllegalArgumentException("Unexpected flag: " 229c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson + Integer.toHexString(flags)); 230c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson } 231579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (declaration.declared) { 232579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalStateException("already declared: " + type); 233579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 234579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson declaration.declared = true; 235579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson declaration.flags = flags; 236579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson declaration.supertype = supertype; 237579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson declaration.sourceFile = sourceFile; 238579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson declaration.interfaces = new TypeList(interfaces); 239579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 240579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 241579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 242c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson * Declares a method or constructor. 24323abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * 24423abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * @param flags a bitwise combination of {@link Modifier#PUBLIC}, {@link 24523abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Modifier#PRIVATE}, {@link Modifier#PROTECTED}, {@link Modifier#STATIC}, 246c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson * {@link Modifier#FINAL} and {@link Modifier#SYNCHRONIZED}. 2475624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * <p><strong>Warning:</strong> the {@link Modifier#SYNCHRONIZED} flag 2485624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * is insufficient to generate a synchronized method. You must also use 2495624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * {@link Code#monitorEnter} and {@link Code#monitorExit} to acquire 2505624228626d7cdf206de25a6981ba8107be61057Jesse Wilson * a monitor. 25190699b97998f1582a921202fb909f17f9718d177Jesse Wilson */ 252579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public Code declare(MethodId<?, ?> method, int flags) { 253579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson TypeDeclaration typeDeclaration = getTypeDeclaration(method.declaringType); 254579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (typeDeclaration.methods.containsKey(method)) { 255579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalStateException("already declared: " + method); 256579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 257c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson 258c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson int supportedFlags = Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED 259c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson | Modifier.STATIC | Modifier.FINAL | Modifier.SYNCHRONIZED; 260c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson if ((flags & ~supportedFlags) != 0) { 261c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson throw new IllegalArgumentException("Unexpected flag: " 262c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson + Integer.toHexString(flags)); 263c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson } 264c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson 2655624228626d7cdf206de25a6981ba8107be61057Jesse Wilson // replace the SYNCHRONIZED flag with the DECLARED_SYNCHRONIZED flag 2665624228626d7cdf206de25a6981ba8107be61057Jesse Wilson if ((flags & Modifier.SYNCHRONIZED) != 0) { 2675624228626d7cdf206de25a6981ba8107be61057Jesse Wilson flags = (flags & ~Modifier.SYNCHRONIZED) | AccessFlags.ACC_DECLARED_SYNCHRONIZED; 2685624228626d7cdf206de25a6981ba8107be61057Jesse Wilson } 269c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson 270c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson if (method.isConstructor()) { 271c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson flags |= ACC_CONSTRUCTOR; 272c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson } 273c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson 274579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson MethodDeclaration methodDeclaration = new MethodDeclaration(method, flags); 275579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson typeDeclaration.methods.put(method, methodDeclaration); 276579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return methodDeclaration.code; 277579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 278579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 279579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 28023abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Declares a field. 28123abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * 28223abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * @param flags a bitwise combination of {@link Modifier#PUBLIC}, {@link 28323abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Modifier#PRIVATE}, {@link Modifier#PROTECTED}, {@link Modifier#STATIC}, 28423abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * {@link Modifier#FINAL}, {@link Modifier#VOLATILE}, and {@link 28523abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Modifier#TRANSIENT}. 286c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson * @param staticValue a constant representing the initial value for the 287c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson * static field, possibly null. This must be null if this field is 288c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson * non-static. 289579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 290579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public void declare(FieldId<?, ?> fieldId, int flags, Object staticValue) { 291579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson TypeDeclaration typeDeclaration = getTypeDeclaration(fieldId.declaringType); 292579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (typeDeclaration.fields.containsKey(fieldId)) { 293579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalStateException("already declared: " + fieldId); 294579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 295c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson 296c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson int supportedFlags = Modifier.PUBLIC | Modifier.PRIVATE | Modifier.PROTECTED 297c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson | Modifier.STATIC | Modifier.FINAL | Modifier.VOLATILE | Modifier.TRANSIENT; 298c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson if ((flags & ~supportedFlags) != 0) { 299c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson throw new IllegalArgumentException("Unexpected flag: " 300c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson + Integer.toHexString(flags)); 301c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson } 302c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson 303c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson if ((flags & Modifier.STATIC) == 0 && staticValue != null) { 304c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson throw new IllegalArgumentException("staticValue is non-null, but field is not static"); 305c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson } 306c0271e9981ddd85a13ed88defd0b5b1a5ccc6f46Jesse Wilson 307579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson FieldDeclaration fieldDeclaration = new FieldDeclaration(fieldId, flags, staticValue); 308579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson typeDeclaration.fields.put(fieldId, fieldDeclaration); 309579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 310579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 311579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 31223abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Generates a dex file and returns its bytes. 313579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 314579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public byte[] generate() { 315579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson DexOptions options = new DexOptions(); 316579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson options.targetApiLevel = DexFormat.API_NO_EXTENDED_OPCODES; 317579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson DexFile outputDex = new DexFile(options); 318579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 319579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (TypeDeclaration typeDeclaration : types.values()) { 320579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson outputDex.add(typeDeclaration.toClassDefItem()); 321579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 322579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 323579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson try { 324579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return outputDex.toDex(null, false); 325579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } catch (IOException e) { 326579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new RuntimeException(e); 327579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 328579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 329579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 330054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef // Generate a file name for the jar by taking a checksum of MethodIds and 331054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef // parent class types. 332054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef private String generateFileName() { 333054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef int checksum = 1; 334054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef 335054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef Set<TypeId<?>> typesKeySet = types.keySet(); 336054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef Iterator<TypeId<?>> it = typesKeySet.iterator(); 337054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef int[] checksums = new int[typesKeySet.size()]; 338054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef int i = 0; 339054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef 340054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef while (it.hasNext()) { 341054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef TypeId<?> typeId = it.next(); 342054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef TypeDeclaration decl = getTypeDeclaration(typeId); 343054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef Set<MethodId> methodSet = decl.methods.keySet(); 344054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef if (decl.supertype != null) { 345054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef checksums[i++] = 31 * decl.supertype.hashCode() + methodSet.hashCode(); 346054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } 347054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } 348054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef Arrays.sort(checksums); 349054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef 350054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef for (int sum : checksums) { 351054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef checksum *= 31; 352054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef checksum += sum; 353054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } 354054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef 355054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef return "Generated_" + checksum +".jar"; 356054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } 357054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef 358054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef private ClassLoader generateClassLoader(File result, File dexCache, ClassLoader parent) { 359054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef try { 360054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef return (ClassLoader) Class.forName("dalvik.system.DexClassLoader") 361054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef .getConstructor(String.class, String.class, String.class, ClassLoader.class) 362054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef .newInstance(result.getPath(), dexCache.getAbsolutePath(), null, parent); 363054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } catch (ClassNotFoundException e) { 364054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef throw new UnsupportedOperationException("load() requires a Dalvik VM", e); 365054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } catch (InvocationTargetException e) { 366054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef throw new RuntimeException(e.getCause()); 367054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } catch (InstantiationException e) { 368054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef throw new AssertionError(); 369054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } catch (NoSuchMethodException e) { 370054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef throw new AssertionError(); 371054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } catch (IllegalAccessException e) { 372054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef throw new AssertionError(); 373054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } 374054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } 375054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef 376579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** 37723abc2fe89ec3713645d64bdb74415a9090084f4Jesse Wilson * Generates a dex file and loads its types into the current process. 378579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 37973cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * <h3>Picking a dex cache directory</h3> 38073cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * The {@code dexCache} should be an application-private directory. If 38173cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * you pass a world-writable directory like {@code /sdcard} a malicious app 38273cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * could inject code into your process. Most applications should use this: 38373cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * <pre> {@code 384579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 38573cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * File dexCache = getApplicationContext().getDir("dx", Context.MODE_PRIVATE); 38673cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * }</pre> 38773cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * If the {@code dexCache} is null, this method will consult the {@code 38873cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * dexmaker.dexcache} system property. If that exists, it will be used for 38973cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * the dex cache. If it doesn't exist, this method will attempt to guess 39073cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * the application's private data directory as a last resort. If that fails, 39173cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * this method will fail with an unchecked exception. You can avoid the 39273cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * exception by either providing a non-null value or setting the system 39373cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * property. 394579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 39573cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * @param parent the parent ClassLoader to be used when loading our 39673cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * generated types 39773cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * @param dexCache the destination directory where generated and optimized 39873cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * dex files will be written. If null, this class will try to guess the 39973cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson * application's private data dir. 400579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 40173cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson public ClassLoader generateAndLoad(ClassLoader parent, File dexCache) throws IOException { 40273cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson if (dexCache == null) { 40373cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson String property = System.getProperty("dexmaker.dexcache"); 40473cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson if (property != null) { 40573cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson dexCache = new File(property); 40673cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson } else { 40773cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson dexCache = new AppDataDirGuesser().guess(); 40873cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson if (dexCache == null) { 40973cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson throw new IllegalArgumentException("dexcache == null (and no default could be" 41073cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson + " found; consider setting the 'dexmaker.dexcache' system property)"); 41173cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson } 41273cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson } 41373cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson } 41473cfa4498f640e0915b95fc806db4a0d54172fe8Jesse Wilson 415054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef File result = new File(dexCache, generateFileName()); 416054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef // Check that the file exists. If it does, return a DexClassLoader and skip all 417054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef // the dex bytecode generation. 418054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef if (result.exists()) { 419054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef return generateClassLoader(result, dexCache, parent); 420054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef } 421054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef 422579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson byte[] dex = generate(); 423579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 424579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /* 425579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * This implementation currently dumps the dex to the filesystem. It 426579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * jars the emitted .dex for the benefit of Gingerbread and earlier 427579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * devices, which can't load .dex files directly. 428579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * 429579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson * TODO: load the dex from memory where supported. 430579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson */ 431054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef result.createNewFile(); 432579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(result)); 4335999ddea21d4d5887cecdcb7730b0d16cdc54d93Andreas Gampe JarEntry entry = new JarEntry(DexFormat.DEX_IN_JAR_NAME); 4345999ddea21d4d5887cecdcb7730b0d16cdc54d93Andreas Gampe entry.setSize(dex.length); 4355999ddea21d4d5887cecdcb7730b0d16cdc54d93Andreas Gampe jarOut.putNextEntry(entry); 436579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson jarOut.write(dex); 437579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson jarOut.closeEntry(); 438579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson jarOut.close(); 439054604d4c95cb530bea955718d79dbe3dc0962cfAndrew Yousef return generateClassLoader(result, dexCache, parent); 440579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 441579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 442579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private static class TypeDeclaration { 4430e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson private final TypeId<?> type; 444579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 445579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson /** declared state */ 446579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private boolean declared; 447579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private int flags; 4480e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson private TypeId<?> supertype; 449579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private String sourceFile; 450579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private TypeList interfaces; 451579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 452579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final Map<FieldId, FieldDeclaration> fields 453579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson = new LinkedHashMap<FieldId, FieldDeclaration>(); 454579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final Map<MethodId, MethodDeclaration> methods 455579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson = new LinkedHashMap<MethodId, MethodDeclaration>(); 456579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 4570e49fb9243b7463835ab80ef7cc62435f55846ceJesse Wilson TypeDeclaration(TypeId<?> type) { 458579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.type = type; 459579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 460579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 461579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson ClassDefItem toClassDefItem() { 462579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (!declared) { 463579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalStateException("Undeclared type " + type + " declares members: " 464579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson + fields.keySet() + " " + methods.keySet()); 465579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 466579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 467579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson DexOptions dexOptions = new DexOptions(); 468579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson dexOptions.targetApiLevel = DexFormat.API_NO_EXTENDED_OPCODES; 469579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 470579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson CstType thisType = type.constant; 471579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 472579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson ClassDefItem out = new ClassDefItem(thisType, flags, supertype.constant, 473579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson interfaces.ropTypes, new CstString(sourceFile)); 474579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 475579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (MethodDeclaration method : methods.values()) { 476579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson EncodedMethod encoded = method.toEncodedMethod(dexOptions); 477579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (method.isDirect()) { 478579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson out.addDirectMethod(encoded); 479579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } else { 480579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson out.addVirtualMethod(encoded); 481579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 482579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 483579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson for (FieldDeclaration field : fields.values()) { 484579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson EncodedField encoded = field.toEncodedField(); 485579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson if (field.isStatic()) { 486579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson out.addStaticField(encoded, Constants.getConstant(field.staticValue)); 487579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } else { 488579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson out.addInstanceField(encoded); 489579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 490579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 491579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 492579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return out; 493579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 494579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 495579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 496579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson static class FieldDeclaration { 497579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson final FieldId<?, ?> fieldId; 498579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final int accessFlags; 499579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final Object staticValue; 500579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 501579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson FieldDeclaration(FieldId<?, ?> fieldId, int accessFlags, Object staticValue) { 50290699b97998f1582a921202fb909f17f9718d177Jesse Wilson if ((accessFlags & STATIC) == 0 && staticValue != null) { 503579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson throw new IllegalArgumentException("instance fields may not have a value"); 504579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 505579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.fieldId = fieldId; 506579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.accessFlags = accessFlags; 507579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.staticValue = staticValue; 508579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 509579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 510579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson EncodedField toEncodedField() { 511579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return new EncodedField(fieldId.constant, accessFlags); 512579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 513579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 514579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public boolean isStatic() { 51590699b97998f1582a921202fb909f17f9718d177Jesse Wilson return (accessFlags & STATIC) != 0; 516579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 517579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 518579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 519579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson static class MethodDeclaration { 520579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson final MethodId<?, ?> method; 521579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final int flags; 522579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson private final Code code; 523579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 524579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson public MethodDeclaration(MethodId<?, ?> method, int flags) { 525579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.method = method; 526579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.flags = flags; 527579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson this.code = new Code(this); 528579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 529579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 530579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson boolean isStatic() { 53190699b97998f1582a921202fb909f17f9718d177Jesse Wilson return (flags & STATIC) != 0; 532579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 533579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 534579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson boolean isDirect() { 53590699b97998f1582a921202fb909f17f9718d177Jesse Wilson return (flags & (STATIC | PRIVATE | ACC_CONSTRUCTOR)) != 0; 536579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 537579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson 538579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson EncodedMethod toEncodedMethod(DexOptions dexOptions) { 539579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson RopMethod ropMethod = new RopMethod(code.toBasicBlocks(), 0); 540579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson LocalVariableInfo locals = null; 541579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson DalvCode dalvCode = RopTranslator.translate( 542579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson ropMethod, PositionList.NONE, locals, code.paramSize(), dexOptions); 543579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson return new EncodedMethod(method.constant, flags, dalvCode, StdTypeList.EMPTY); 544579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 545579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson } 546579d7739c53a2707ad711a2d2cae46d7d782f06Jesse Wilson} 547