summaryrefslogtreecommitdiffstats
path: root/jack/tests/com/android/jack/TestTools.java
blob: b851bb83ac60fb2a1ace15da9de6962923de24da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.jack;

import com.android.dx.command.dexer.Main.Arguments;
import com.android.jack.backend.dex.DexInLibraryProduct;
import com.android.jack.backend.jayce.JayceInLibraryProduct;
import com.android.jack.ir.ast.JDefinedClassOrInterface;
import com.android.jack.ir.ast.JMethod;
import com.android.jack.ir.ast.JSession;
import com.android.jack.ir.formatter.MethodFormatter;
import com.android.jack.library.JackLibraryFactory;
import com.android.jack.library.OutputJackLibrary;
import com.android.jack.lookup.JMethodSignatureLookupException;
import com.android.jack.scheduling.marker.ClassDefItemMarker;
import com.android.jack.shrob.ListingComparator;
import com.android.jack.shrob.proguard.GrammarActions;
import com.android.jack.shrob.shrink.ShrinkStructurePrinter;
import com.android.jack.shrob.spec.Flags;
import com.android.jack.util.ExecuteFile;
import com.android.jack.util.TextUtils;
import com.android.jack.util.filter.SignatureMethodFilter;
import com.android.sched.scheduler.PlanBuilder;
import com.android.sched.scheduler.Request;
import com.android.sched.util.RunnableHooks;
import com.android.sched.util.config.ThreadConfig;
import com.android.sched.util.file.Directory;
import com.android.sched.util.file.FileOrDirectory.ChangePermission;
import com.android.sched.util.file.FileOrDirectory.Existence;
import com.android.sched.util.file.FileOrDirectory.Permission;
import com.android.sched.util.file.FileUtils;
import com.android.sched.vfs.DirectVFS;

import junit.framework.Assert;

import org.jf.dexlib.ClassDataItem;
import org.jf.dexlib.ClassDataItem.EncodedMethod;
import org.jf.dexlib.ClassDefItem;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
 * Tools used by tests.
 */
public class TestTools {

  @Nonnull
  private static final String JACK_UNIT_TESTS_PATH = "toolchain/jack/jack/tests/";

  @Nonnull
  private static final String JACK_TESTS_PATH = "toolchain/jack/jack-tests/tests/";

  @Nonnull
  private static final String JACK_PACKAGE = "com/android/jack/";

  private static class ExternalTools {
    @Nonnull
    private static final File JARJAR = getFromAndroidTree("out/host/linux-x86/framework/jarjar.jar");

    @Nonnull
    private static final File PROGUARD = getFromAndroidTree("prebuilts/tools/common/proguard/proguard4.7/lib/proguard.jar");
  }

  public static class ReferenceCompilerFiles {
    @Nonnull
    public File jarFile;
    @Nonnull
    public File dexFile;
    public ReferenceCompilerFiles(@Nonnull File jarFile, @Nonnull File dexFile){
      this.jarFile = jarFile;
      this.dexFile = dexFile;
    }
  }

  @Nonnull
  public static JMethod getMethod(@Nonnull JDefinedClassOrInterface declaringClass,
      @Nonnull final String signature) throws JMethodSignatureLookupException {
    MethodFormatter formatter = Jack.getLookupFormatter();
    for (JMethod m : declaringClass.getMethods()) {
      if (formatter.getName(m).equals(signature)) {
        return m;
      }
    }
    throw new JMethodSignatureLookupException(declaringClass, signature);
  }

  @Nonnull
  public static File getJackTestsWithJackFolder(@Nonnull String testName) {
    return getFromAndroidTree(JACK_TESTS_PATH + JACK_PACKAGE + testName + "/jack");
  }

  @Nonnull
  public static File getJackTestFolder(@Nonnull String testName) {
    return getFromAndroidTree(JACK_TESTS_PATH + JACK_PACKAGE + testName);
  }

  @Nonnull
  public static File getJackTestFromBinaryName(@Nonnull String signature) {
    return getFromAndroidTree(JACK_TESTS_PATH + signature + ".java");
  }

  @Nonnull
  public static File getJackTestLibFolder(@Nonnull String testName) {
    return getFromAndroidTree(JACK_TESTS_PATH + JACK_PACKAGE + testName + "/lib");
  }

  @Nonnull
  public static File getJackUnitTestSrc(@Nonnull String path) {
    return getFromAndroidTree(JACK_UNIT_TESTS_PATH + path);
  }

  @Nonnull
  public static File getJackUnitTestFromBinaryName(@Nonnull String signature) {
    return getFromAndroidTree(JACK_UNIT_TESTS_PATH + signature + ".java");
  }

  @Nonnull
  public static File getOpcodeTestFolder(@Nonnull String testName) {
    return getFromAndroidTree(JACK_TESTS_PATH + JACK_PACKAGE + "opcodes/" + testName + "/jm");
  }

  @Nonnull
  public static File getArtTestFolder(@Nonnull String testName) {
    return getFromAndroidTree("art/test/" + testName + "/src");
  }

  @Nonnull
  public static Sourcelist getSourcelistWithAbsPath(@Nonnull String fileName) {
    File sourcelist = new File(getAndroidTop(), fileName);

    if (!sourcelist.exists()) {
      throw new AssertionError("Failed to locate sourcelist for \"" + fileName + "\".");
    }
    try {
      File fileWithAbsPath = TestTools.createTempFile("tmpSourceList", "txt");
      Sourcelist sourcelistWithAbsPath =
          new Sourcelist(fileWithAbsPath);
      BufferedWriter outBr = new BufferedWriter(new FileWriter(sourcelistWithAbsPath));
      BufferedReader inBr = new BufferedReader(new FileReader(sourcelist));

      String line;
      while ((line = inBr.readLine()) != null) {
        outBr.write(getAndroidTop() + File.separator + line + TextUtils.LINE_SEPARATOR);
      }

      outBr.close();
      inBr.close();

      return sourcelistWithAbsPath;
    } catch (IOException e) {
      throw new AssertionError("Failed to build sourcelist for \"" + fileName + "\".");
    }
  }

  @Nonnull
  public static Sourcelist getTargetLibSourcelist(@Nonnull String moduleName) {
    return getSourcelistWithAbsPath("out/target/common/obj/JAVA_LIBRARIES/" + moduleName
        + "_intermediates/jack.java-source-list");
  }

  @Nonnull
  public static Sourcelist getHostLibSourcelist(@Nonnull String moduleName) {
    return getSourcelistWithAbsPath("out/host/common/obj/JAVA_LIBRARIES/" + moduleName
        + "_intermediates/jack.java-source-list");
  }

  @Nonnull
  public static Sourcelist getTargetAppSourcelist(@Nonnull String moduleName) {
    return getSourcelistWithAbsPath("out/target/common/obj/APPS/" + moduleName
        + "_intermediates/jack.java-source-list");
  }

  @Nonnull
  public static File getFromAndroidTree(@Nonnull String filePath) {
    File sourceFile = new File(getAndroidTop(), filePath);
    if (!sourceFile.exists()) {
      throw new AssertionError("Failed to locate file \"" + filePath + "\".");
    }
    return sourceFile;
  }

  public static void getJavaFiles(@Nonnull File fileObject, @Nonnull List<File> filePaths) throws IOException {
    if (fileObject.isDirectory()) {
      File allFiles[] = fileObject.listFiles();
      for (File aFile : allFiles) {
        getJavaFiles(aFile, filePaths);
      }
    } else if (fileObject.isFile() && fileObject.getName().endsWith(".java")) {
      filePaths.add(fileObject.getCanonicalFile());
    }
  }

  public static void compileJackToDex(
      Options options, File in, File out, boolean zip) throws Exception {
    options.importedLibraries = new ArrayList<File>(1);
    options.importedLibraries.add(in);
    if (zip) {
      options.outZip = out;
    } else {
      options.out = out;
    }
    Jack.run(options);
  }

  public static void compileSourceToDex(@Nonnull Options options,
      @Nonnull File sourceFolderOrSourceList,
      @CheckForNull String classpath,
      @Nonnull File out,
      boolean zip,
      @CheckForNull JarJarRules jarjarRules,
      @CheckForNull ProguardFlags[] flagFiles,
      boolean withDebugInfo) throws Exception {
    options.ecjArguments = buildEcjArgs();
    addFile(sourceFolderOrSourceList, options.ecjArguments);
    options.classpath = classpath;
    if (zip) {
      options.outZip = out;
    } else {
      options.out = out;
    }
    options.jarjarRulesFile = jarjarRules;
    if (flagFiles != null) {
      options.proguardFlagsFiles = new ArrayList<File>();
      for (ProguardFlags flagFile : flagFiles) {
        options.proguardFlagsFiles.add(flagFile);
      }
    }
    options.emitLocalDebugInfo = withDebugInfo;
    Jack.run(options);
  }

  @Nonnull
  public static File getDefaultDexBootclasspath() {
    return getFromAndroidTree(
        "out/host/common/obj/JAVA_LIBRARIES/core-hostdex_intermediates/classes.dex");
  }



  @Nonnull
  public static File[] getDefaultClasspath() {
    return new File[] {getFromAndroidTree(
        "toolchain/jack/jack/prebuilts/core-stubs-mini.jack")};
  }

  @Nonnull
  public static String getDefaultClasspathString() {
    return getFromAndroidTree(
        "toolchain/jack/jack/prebuilts/core-stubs-mini.jack")
          .getAbsolutePath();
  }

  @CheckForNull
  public static String getClasspathAsString(@CheckForNull File[] files) {
    if (files == null || files.length == 0) {
      return null;
    }
    StringBuilder classpathStr = new StringBuilder();
    for (int i = 0; i < files.length; i++) {
      classpathStr.append(files[i].getAbsolutePath());
      if (i != files.length -1) {
        classpathStr.append(File.pathSeparatorChar);
      }
    }
    return classpathStr.toString();
  }

  @CheckForNull
  public static String getClasspathsAsString(
      @CheckForNull File[] bootClasspath, @CheckForNull File[] classpath) {
    if (bootClasspath == null) {
      return getClasspathAsString(classpath);
    } else if (classpath == null) {
      return getClasspathAsString(bootClasspath);
    } else {
      return concatClasspathStrings(
          getClasspathAsString(bootClasspath), getClasspathAsString(classpath));
    }
  }

  @CheckForNull
  private static String concatClasspathStrings(
      @CheckForNull String bootclasspath, @CheckForNull String classpath) {
    if (bootclasspath == null || bootclasspath.isEmpty()) {
      return classpath;
    } else if (classpath == null || classpath.isEmpty()) {
      return bootclasspath;
    } else {
      StringBuilder classpathStr = new StringBuilder(bootclasspath);
      classpathStr.append(File.pathSeparatorChar);
      classpathStr.append(classpath);
      return classpathStr.toString();
    }
  }

  @Nonnull
  protected static List<String> buildEcjArgs() {
    List<String> ecjArgs = new ArrayList<String>();
    ecjArgs.add("-nowarn");

    return ecjArgs;
  }

  protected static void addFile(@Nonnull File fileOrSourceList, @Nonnull List<String> args) {
    if (fileOrSourceList instanceof Sourcelist) {
      args.add("@" + fileOrSourceList.getAbsolutePath());
    } else {
      List<File> sourceFiles = new ArrayList<File>();
      try {
        getJavaFiles(fileOrSourceList, sourceFiles);
      } catch (IOException e) {
      }
      for (File sourceFile : sourceFiles) {
        args.add(sourceFile.getAbsolutePath());
      }
    }
  }

  @Nonnull
  public static Options buildCommandLineArgs(@Nonnull File fileOrSourcelist) throws IOException {
    return buildCommandLineArgs(fileOrSourcelist, null);
  }

  @Nonnull
  public static Options buildCommandLineArgs(@Nonnull File fileOrSourcelist,
      @CheckForNull File jarjarRules) throws IOException {
    Options options = buildCommandLineArgs(null /* classpath */, new File[] {fileOrSourcelist});
    options.jarjarRulesFile = jarjarRules;

    return options;
  }

  @Nonnull
  public static Options buildCommandLineArgs(@Nonnull File[] filesOrSourcelists)
      throws IOException {
    return buildCommandLineArgs(null /* classpath */, filesOrSourcelists);
  }

  @Nonnull
  public static Options buildCommandLineArgs(@CheckForNull File[] classpath,
      @Nonnull File fileOrSourcelist) throws IOException {
    return buildCommandLineArgs(classpath, new File[] {fileOrSourcelist});
  }

  @Nonnull
  public static Options buildCommandLineArgs(@CheckForNull File[] classpath,
      @Nonnull File[] filesOrSourcelists) throws IOException {
    Options options = new Options();

    String classpathStr = getDefaultClasspathString();
    classpathStr += File.pathSeparatorChar;

    if (classpath != null && classpath.length != 0) {
      classpathStr += getClasspathAsString(classpath);
    }

    options.classpath = classpathStr;

    List<String> ecjArgs = buildEcjArgs();
    for (File file : filesOrSourcelists) {
      addFile(file, ecjArgs);
    }
    options.ecjArguments = ecjArgs;
    options.setOutputDir(TestTools.createTempDir("test", "dex"));

    return options;
  }

  @Nonnull
  public static JSession buildJAst(@Nonnull Options options) throws Exception {
    RunnableHooks hooks = new RunnableHooks();
    try {
      options.checkValidity(hooks);
      ThreadConfig.setConfig(options.getConfig());

      JSession session = Jack.buildSession(options, hooks);

      return (session);
    } finally {
      hooks.runHooks();
    }
  }

  /**
   * Build a {@code JSession} by using the monolithic plan.
   */
  @Nonnull
  public static JSession buildSession(@Nonnull Options options) throws Exception {
    RunnableHooks hooks = new RunnableHooks();
    try {
      return buildSession(options, hooks);
    } finally {
      hooks.runHooks();
    }

  }

  /**
   * Build a {@code JSession} by using the monolithic plan.
   */
  @Nonnull
  public static JSession buildSession(@Nonnull Options options, @Nonnull RunnableHooks hooks)
      throws Exception {
    if (options.proguardFlagsFiles != null && !options.proguardFlagsFiles.isEmpty()) {
      if (options.flags == null) {
        options.flags = new Flags();
      }
      for (File proguardFlagsFile : options.proguardFlagsFiles) {
        GrammarActions.parse(proguardFlagsFile.getAbsolutePath(), ".", options.flags);
      }
      options.applyShrobFlags();
    }

    options.checkValidity(hooks);
    ThreadConfig.setConfig(options.getConfig());

    JSession session = Jack.buildSession(options, hooks);

    Request request = Jack.createInitialRequest();
    request.addInitialTagsOrMarkers(Jack.getJavaSourceInitialTagSet());
    request.addProduction(DexInLibraryProduct.class);
    if (ThreadConfig.get(Options.GENERATE_JAYCE_IN_LIBRARY).booleanValue()) {
      request.addProduction(JayceInLibraryProduct.class);
    }

    OutputJackLibrary outputLibrary = null;
    try {
      outputLibrary = JackLibraryFactory.getOutputLibrary(new DirectVFS(new Directory(
          TestTools.createTempDir("unused", "").getPath(), hooks, Existence.MUST_EXIST,
          Permission.WRITE, ChangePermission.NOCHANGE)), Jack.getEmitterId(),
          Jack.getVersionString());
      session.setJackOutputLibrary(outputLibrary);

      PlanBuilder<JSession> planBuilder = request.getPlanBuilder(JSession.class);
      Jack.fillDexPlan(options, planBuilder);
      request.addTargetIncludeTagOrMarker(ClassDefItemMarker.Complete.class);

      planBuilder.getPlan().getScheduleInstance().process(session);
    } finally {
      if (outputLibrary != null) {
        outputLibrary.close();
      }
    }

    return (session);
  }

  public static void checkListing(@CheckForNull File[] jackBootclasspath,
      @CheckForNull File[] jackClasspath,
      @Nonnull File fileOrSourceList,
      @CheckForNull ProguardFlags[] proguardFlags,
      @Nonnull File refNodeListing) throws Exception {
    Options options = new Options();
    File candidateNodeListing = TestTools.createTempFile("nodeListing", ".txt");
    options.addProperty(ShrinkStructurePrinter.STRUCTURE_PRINTING.getName(), "true");
    options.addProperty(ShrinkStructurePrinter.STRUCTURE_PRINTING_FILE.getName(),
        candidateNodeListing.getPath());
    options.addProperty(Options.METHOD_FILTER.getName(), "supported-methods");
    options.disableDxOptimizations();

    File outFolder = TestTools.createTempDir("checklisting", "dex");
    TestTools.compileSourceToDex(options,
        fileOrSourceList,
        TestTools.getClasspathsAsString(jackBootclasspath, jackClasspath),
        outFolder,
        false /* zip */,
        null /* jarjarRules */,
        proguardFlags,
        true /* emitDebugInfo */);

    ListingComparator.compare(refNodeListing, candidateNodeListing);
  }

  @Nonnull
  public static String getAndroidTop() {
    String androidTop = System.getenv("ANDROID_BUILD_TOP");
    if (androidTop == null) {
      throw new AssertionError("Failed to locate environment variable ANDROID_BUILD_TOP.");
    }
    return androidTop;
  }

  @Nonnull
  public static JMethod getJMethodWithRejectAllFilter(@Nonnull File fileName,
      @Nonnull String className, @Nonnull String methodSignature) throws Exception {
    Options commandLineArgs = TestTools.buildCommandLineArgs(fileName);
    commandLineArgs.addProperty(Options.METHOD_FILTER.getName(), "reject-all-methods");
    commandLineArgs.keepMethodBody = true;
    JSession session = TestTools.buildSession(commandLineArgs);
    Assert.assertNotNull(session);

    JDefinedClassOrInterface type =
        (JDefinedClassOrInterface) session.getLookup().getType(className);
    Assert.assertNotNull(type);

    JMethod foundMethod = null;
    foundMethod = getMethod(type, methodSignature);

    Assert.assertNotNull(foundMethod);

    return foundMethod;
  }

  @Nonnull
  public static JMethod getJMethodWithSignatureFilter(@Nonnull File fileName,
      @Nonnull String className, @Nonnull String methodSignature) throws Exception {
    Options commandLineArgs = TestTools.buildCommandLineArgs(fileName);
    commandLineArgs.addProperty(Options.METHOD_FILTER.getName(), "method-with-signature");
    commandLineArgs.addProperty(SignatureMethodFilter.METHOD_SIGNATURE_FILTER.getName(),
        methodSignature);
    commandLineArgs.keepMethodBody = true;
    JSession session = TestTools.buildSession(commandLineArgs);
    Assert.assertNotNull(session);

    JDefinedClassOrInterface type =
        (JDefinedClassOrInterface) session.getLookup().getType(className);
    Assert.assertNotNull(type);

    JMethod foundMethod = null;
    foundMethod = getMethod(type, methodSignature);

    Assert.assertNotNull(foundMethod);

    return foundMethod;
  }

  public static void compileWithRefCompiler(
      Options compilerArgs, boolean useEcjAsRefCompiler, File refCompilerOut) {
    if (useEcjAsRefCompiler) {
      compileWithEcj(compilerArgs, refCompilerOut);
    } else {
      compileWithExternalRefCompiler(compilerArgs, refCompilerOut);
    }
  }

  /**
   * Creates the reference compiler files.
   *
   * @param compilerArgs the arguments given to a reference compiler
   * @throws IOException
   * @throws InterruptedException
   */
  public static ReferenceCompilerFiles createReferenceCompilerFiles(@Nonnull File testDir,
      @Nonnull Options compilerArgs,
      @CheckForNull ProguardFlags[] proguardFlags,
      @CheckForNull File[] bootclasspath,
      @CheckForNull File[] classpath,
      boolean withDebugInfo,
      boolean useEcjAsRefCompiler,
      @CheckForNull JarJarRules jarjarRules) throws IOException, InterruptedException {

    if (withDebugInfo) {
      compilerArgs.ecjArguments.add(0, "-g");
    }

    File refCompilerOut = new File(testDir, "refcompilerout");
    File refDex = new File(testDir, "testref.dex");
    if (!refCompilerOut.exists() && !refCompilerOut.mkdir()) {
      throw new IOException("Could not create directory \"" + refCompilerOut.getName() + "\"");
    }

    // Create reference Dex file
    if (classpath != null) {
      for (File f : classpath) {
        unzip(f, refCompilerOut);
      }
    }
    compileWithRefCompiler(compilerArgs, useEcjAsRefCompiler, refCompilerOut);
    File refJar = new File(testDir, "ref.jar");
    File refJarJar = new File(testDir, "refJarJar.jar");
    File refProguard = new File(testDir, "refProguard.jar");
    createjar(refJar, refCompilerOut);
    if (jarjarRules != null) {
      processWithJarJar(jarjarRules, refJar, refJarJar);
    } else {
      refJarJar = refJar;
    }
    if (proguardFlags != null) {
      processWithProguard(proguardFlags, refJarJar, refProguard, bootclasspath);
    } else {
      refProguard = refJarJar;
    }

    compileWithDx(refProguard, refDex, withDebugInfo);
    return new ReferenceCompilerFiles(refProguard, refDex);
  }

  private static void unzip(@Nonnull File jarfile, @Nonnull File outputFolder) {
    String[] args = new String[]{"unzip", "-qo", jarfile.getAbsolutePath(),
        "-d", outputFolder.getAbsolutePath(),};

    ExecuteFile execFile = new ExecuteFile(args);
    if (!execFile.run()) {
      throw new RuntimeException("Unzip exited with an error");
    }
  }

  private static void createjar(@Nonnull File jarfile, @Nonnull File inputFiles) {
    String[] args = new String[]{"jar", "cf", jarfile.getAbsolutePath(),
        "-C", inputFiles.getAbsolutePath(), "."};

    ExecuteFile execFile = new ExecuteFile(args);
    if (!execFile.run()) {
      throw new RuntimeException("Reference compiler exited with an error");
    }
  }

  private static void processWithJarJar(@Nonnull File jarjarRules,
      @Nonnull File inJar, @Nonnull File outJar) {
    String[] args = new String[]{"java", "-jar", ExternalTools.JARJAR.getAbsolutePath(),
        "process", jarjarRules.getAbsolutePath(),
        inJar.getAbsolutePath(), outJar.getAbsolutePath()};

    ExecuteFile execFile = new ExecuteFile(args);
    if (!execFile.run()) {
      throw new RuntimeException("JarJar exited with an error");
    }
  }

  private static void processWithProguard(@Nonnull ProguardFlags[] proguardFlagsFiles,
      @Nonnull File inJar, @Nonnull File outJar, @CheckForNull File[] bootclasspath) {
    String bootclasspathStr = null;
    if (bootclasspath == null) {
      bootclasspathStr = getDefaultClasspathString();
    } else {
      bootclasspathStr = getClasspathAsString(bootclasspath);
    }
    String[] args = new String[12 + proguardFlagsFiles.length * 2];
    int i = 0;
    args[i++] = "java";
    args[i++] = "-jar";
    args[i++] = ExternalTools.PROGUARD.getAbsolutePath();
    args[i++] = "-injars";
    args[i++] = inJar.getAbsolutePath();
    args[i++] = "-outjars";
    args[i++] = outJar.getAbsolutePath();
    args[i++] = "-libraryjars";
    args[i++] = bootclasspathStr;
    args[i++] = "-verbose";
    args[i++] = "-forceprocessing";
    args[i++] = "-dontoptimize";
    for (ProguardFlags proguardFlags : proguardFlagsFiles) {
      args[i++] = "-include";
      args[i++] = proguardFlags.getAbsolutePath();
    }

    ExecuteFile execFile = new ExecuteFile(args);
    execFile.setOut(System.out);
    execFile.setErr(System.err);
    execFile.setVerbose(true);
    if (!execFile.run()) {
      throw new RuntimeException("Proguard exited with an error");
    }
  }

  private static void compileWithExternalRefCompiler(@Nonnull Options compilerArgs, @Nonnull File out) {

    List<String> arguments = getRefCompilerArguments(compilerArgs);

    String[] args = new String[arguments.size() + 3];
    String refCompilerPath = System.getenv("REF_JAVA_COMPILER");
    if (refCompilerPath == null) {
      throw new RuntimeException("REF_JAVA_COMPILER environment variable not set");
    }
    int i = 0;
    args[i++] = refCompilerPath.trim();

    for (String compilerArg : arguments) {
      args[i++] = compilerArg;
    }
    args[i++] = "-d";
    args[i++] = out.getAbsolutePath();

    ExecuteFile execFile = new ExecuteFile(args);
    if (!execFile.run()) {
      throw new RuntimeException("Reference compiler exited with an error");
    }
  }

  private static List<String> getRefCompilerArguments(Options compilerArgs) {
    List<String> arguments = new ArrayList<String>(compilerArgs.ecjArguments);
    if (compilerArgs.classpath != null) {
      arguments.add("-classpath");
      // TODO(jmhenaff): This hack will be removed as soon as TestTools will be removed
      arguments.add(compilerArgs.classpath.replace("core-stubs-mini.jack", "core-stubs-mini.jar"));
    }
    return arguments;
  }

  private static void compileWithEcj(Options compilerArgs, File out) {
    List<String> jackEcjArgs = getRefCompilerArguments(compilerArgs);
    String[] args = new String[jackEcjArgs.size() + 5];
    int i = 0;
    args[i++] = "-noExit";
    args[i++] = "-1.6";
    args[i++] = "-preserveAllLocals";
    for (String compilerArg : jackEcjArgs) {
      args[i++] = compilerArg;
    }
    args[i++] = "-d";
    args[i++] = out.getAbsolutePath();

    org.eclipse.jdt.internal.compiler.batch.Main.main(args);
  }

  private static void compileWithDx(@Nonnull File src, @Nonnull File refDex, boolean withDebugInfo)
      throws IOException {

    Arguments arguments = new Arguments();

    arguments.jarOutput = false;
    arguments.outName = refDex.getAbsolutePath();
    arguments.optimize = !withDebugInfo;
    // this only means we deactivate the check that no core classes are included
    arguments.coreLibrary = true;
    arguments.parse(new String[] {src.getAbsolutePath()});

    int retValue = com.android.dx.command.dexer.Main.run(arguments);
    if (retValue != 0) {
      throw new RuntimeException("Dx failed and returned " + retValue);
    }
  }

  public static File createTempDir(String prefix, String suffix) throws IOException {
    final File tmp = File.createTempFile(prefix, suffix);
    if (!tmp.delete()) {
      throw new IOException("Failed to delete file " + tmp.getAbsolutePath());
    }
    if (!tmp.mkdirs()) {
      throw new IOException("Failed to create folder " + tmp.getAbsolutePath());
    }
    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        try {
          FileUtils.deleteDir(tmp);
        } catch (IOException e) {
          System.err.println(e.getMessage());
        }
      }
    });
    return tmp;
  }

  public static File createTempFile(String prefix, String suffix) throws IOException {
    File tmp = File.createTempFile(prefix, suffix);
    tmp.deleteOnExit();
    return tmp;
  }

  @Nonnull
  public static EncodedMethod getEncodedMethod(@Nonnull org.jf.dexlib.DexFile dexFile,
      @Nonnull String typeSig, @Nonnull String methodName, @Nonnull String methodSig) {
    for (ClassDefItem classDef : dexFile.ClassDefsSection.getItems()) {
      if (classDef.getClassType().getTypeDescriptor().equals(typeSig)) {
        ClassDataItem classData = classDef.getClassData();
        for (EncodedMethod em : classData.getDirectMethods()) {
          if (em.method.getMethodName().getStringValue().equals(methodName)
              && em.method.getPrototype().getPrototypeString().equals(methodSig)) {
            return em;
          }
        }
        for (EncodedMethod em : classData.getVirtualMethods()) {
          if (em.method.getMethodName().getStringValue().equals(methodName)
              && em.method.getPrototype().getPrototypeString().equals(methodSig)) {
            return em;
          }
        }
      }
    }
    throw new AssertionError("Encoded method not found.");
  }
}