summaryrefslogtreecommitdiffstats
path: root/jack/tests/com/android/jack/TestTools.java
blob: eb2dc4d8083f6709ac8b53b723c899a0ee1d5132 (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
/*
 * 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.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.feature.DropMethodBody;
import com.android.jack.scheduling.marker.ClassDefItemMarker;
import com.android.jack.shrob.proguard.GrammarActions;
import com.android.jack.shrob.spec.Flags;
import com.android.jack.test.TestsProperties;
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.CachedDirectFS;

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.Arrays;
import java.util.Collections;
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 = "jack/tests/";

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

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

  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 new File(getJackTestFolder(testName), "jack");
  }

  @Nonnull
  public static File getJackTestFolder(@Nonnull String testName) {
    return new File(TestsProperties.getJackRootDir(),
        JACK_TESTS_PATH + JACK_PACKAGE + testName);
  }

  @Nonnull
  public static File getJackTestFromBinaryName(@Nonnull String signature) {
    return new File(TestsProperties.getJackRootDir(), JACK_TESTS_PATH +
        signature + ".java");
  }

  @Nonnull
  public static File getJackUnitTestFromBinaryName(@Nonnull String signature) {
    return new File(TestsProperties.getJackRootDir(),
        JACK_UNIT_TESTS_PATH + signature + ".java");
  }

  @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(TestsProperties.getAndroidRootDir(), 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(TestsProperties.getAndroidRootDir().getPath() + 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 File getFromAndroidTree(@Nonnull String filePath) {
    File sourceFile = new File(TestsProperties.getAndroidRootDir(), 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());
    }
  }

  @Nonnull
  public static String getDefaultClasspathString() {
    return new File(TestsProperties.getJackRootDir(),
        "jack-tests/prebuilts/core-stubs-mini.jack").getAbsolutePath();
  }

  @Nonnull
  public static String getClasspathAsString(@CheckForNull File[] files) {
    if (files == null || files.length == 0) {
      return "";
    }
    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();
  }

  @Nonnull
  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));
    }
  }

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

  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, Collections.<File>emptyList());
  }

  @Nonnull
  public static Options buildCommandLineArgs(@Nonnull File fileOrSourcelist,
      @Nonnull List<File> jarjarRules) throws IOException {
    Options options = buildCommandLineArgs(null /* classpath */, new File[] {fileOrSourcelist});
    options.setJarjarRulesFiles(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[] filesOrSourceDirs) throws IOException {
    Options options = new Options();

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

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

    options.classpath = classpathStr;

    options.setInputSources(Arrays.asList(filesOrSourceDirs));
    options.setOutputDir(TestTools.createTempDir("test", "dex"));

    return options;
  }

  @Nonnull
  public static JSession buildJAst(@Nonnull Options options) throws Exception {
    RunnableHooks hooks = new RunnableHooks();

    try {
      options.addProperty(Options.USE_DEFAULT_LIBRARIES.getName(), "false");
      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 {
    options.addProperty(Options.USE_DEFAULT_LIBRARIES.getName(), "false");
    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);
    }

    if (options.getConfig().get(Options.DROP_METHOD_BODY).booleanValue()) {
      request.addFeature(DropMethodBody.class);
    }

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

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

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

    return (session);
  }

  @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.addProperty(Options.DROP_METHOD_BODY.getName(), "false");
    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.addProperty(Options.DROP_METHOD_BODY.getName(), "false");
    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 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.");
  }
}