Home | History | Annotate | Download | only in junit-tests
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 import com.android.dx.gen.BinaryOp;
     18 import com.android.dx.gen.Code;
     19 import com.android.dx.gen.DexGenerator;
     20 import com.android.dx.gen.FieldId;
     21 import com.android.dx.gen.Local;
     22 import com.android.dx.gen.MethodId;
     23 import com.android.dx.gen.Type;
     24 import com.android.dx.rop.code.AccessFlags;
     25 import java.io.PrintStream;
     26 
     27 public class HelloWorldMaker {
     28     private static final Type<PrintStream> PRINT_STREAM = Type.get(PrintStream.class);
     29     private static final FieldId<System, PrintStream> SYSTEM_OUT
     30             = Type.get(System.class).getField(PRINT_STREAM, "out");
     31     private static final MethodId<Integer, String> TO_HEX_STRING
     32             = Type.get(Integer.class).getMethod(Type.STRING, "toHexString", Type.INT);
     33     private static final MethodId<PrintStream, Void> PRINTLN
     34             = PRINT_STREAM.getMethod(Type.VOID, "println", Type.STRING);
     35 
     36     public static void main(String[] args) throws Exception {
     37 
     38         /*
     39          * This code generates Dalvik bytecode equivalent to the following
     40          * program.
     41          *
     42          *  public class HelloWorld {
     43          *      public static void hello() {
     44          *          int a = 0xabcd;
     45          *          int b = 0xaaaa;
     46          *          int c = a - b;
     47          *          String s = Integer.toHexString(c);
     48          *          System.out.println(s);
     49          *      }
     50          *  }
     51          */
     52 
     53         DexGenerator generator = new DexGenerator();
     54 
     55         // lookup the symbols of interest
     56         Type<?> helloWorld = Type.get("LHelloWorld;");
     57         MethodId hello = helloWorld.getMethod(Type.VOID, "hello");
     58 
     59         // create some registers
     60         //    (I'd like a better syntax for this)
     61         Code code = generator.declare(hello, AccessFlags.ACC_STATIC | AccessFlags.ACC_PUBLIC);
     62         Local<Integer> a = code.newLocal(Type.INT);
     63         Local<Integer> b = code.newLocal(Type.INT);
     64         Local<Integer> c = code.newLocal(Type.INT);
     65         Local<String> s = code.newLocal(Type.STRING);
     66         Local<PrintStream> localSystemOut = code.newLocal(PRINT_STREAM);
     67 
     68         // specify the code instruction-by-instruction (approximately)
     69         code.loadConstant(a, 0xabcd);
     70         code.loadConstant(b, 0xaaaa);
     71         code.op(BinaryOp.SUBTRACT, c, a, b);
     72         code.invokeStatic(TO_HEX_STRING, s, c);
     73         code.sget(SYSTEM_OUT, localSystemOut);
     74         code.invokeVirtual(PRINTLN, null, localSystemOut, s);
     75         code.returnVoid();
     76 
     77         // TODO: create the constructor
     78 
     79         generator.declare(helloWorld, "Generated.java", AccessFlags.ACC_PUBLIC, Type.OBJECT);
     80 
     81         // load the dex
     82         ClassLoader loader = generator.load(HelloWorldMaker.class.getClassLoader());
     83         Class<?> helloWorldClass = loader.loadClass("HelloWorld");
     84         helloWorldClass.getMethod("hello").invoke(null);
     85     }
     86 }
     87