| // Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file | |
| // for details. All rights reserved. Use of this source code is governed by a | |
| // BSD-style license that can be found in the LICENSE file. | |
| package com.android.tools.r8.examples; | |
| import com.android.tools.r8.ByteDataView; | |
| import com.android.tools.r8.ClassFileConsumer; | |
| import com.android.tools.r8.CompilationMode; | |
| import com.android.tools.r8.OutputMode; | |
| import com.android.tools.r8.R8Command; | |
| import com.android.tools.r8.TestBase; | |
| import com.android.tools.r8.TestDeps; | |
| import com.android.tools.r8.TestParameters; | |
| import com.android.tools.r8.TestRuntime.CfRuntime; | |
| import com.android.tools.r8.ToolHelper; | |
| import com.android.tools.r8.ToolHelper.ProcessResult; | |
| import com.android.tools.r8.cf.LambdaTest; | |
| import com.android.tools.r8.utils.DescriptorUtils; | |
| import com.google.common.collect.ImmutableList; | |
| import com.google.common.io.ByteStreams; | |
| import java.io.FileInputStream; | |
| import java.io.InputStream; | |
| import java.nio.file.Path; | |
| import java.util.List; | |
| import org.junit.Assert; | |
| import org.junit.Test; | |
| import org.junit.runner.RunWith; | |
| import org.junit.runners.Parameterized; | |
| import org.junit.runners.Parameterized.Parameters; | |
| @RunWith(Parameterized.class) | |
| public class R8CFExamplesTests extends TestBase { | |
| private final TestParameters parameters; | |
| private final TestMode testMode; | |
| private final CompilationMode compilationMode; | |
| public enum TestMode { | |
| CF_SKIP_IR, | |
| CF_TO_IR, | |
| } | |
| @Parameters(name = "{0}, {1}:{2}") | |
| public static List<Object[]> data() { | |
| return buildParameters( | |
| getTestParameters().withCfRuntimes().build(), TestMode.values(), CompilationMode.values()); | |
| } | |
| public R8CFExamplesTests( | |
| TestParameters parameters, TestMode testMode, CompilationMode compilationMode) { | |
| this.parameters = parameters; | |
| this.testMode = testMode; | |
| this.compilationMode = compilationMode; | |
| } | |
| @Test | |
| public void testConstMethodHandle() throws Exception { | |
| Path testDirectory = TestDeps.getArtTestsPath("dx", "979-const-method-handle", "classes"); | |
| String classNames[] = { | |
| "constmethodhandle.ConstTest", "Main", | |
| }; | |
| runTest(writeInput(testDirectory, classNames), "Main"); | |
| } | |
| @Test | |
| public void testLambda() throws Exception { | |
| runTest(LambdaTest.class); | |
| } | |
| private void runTest(Class<?> clazz) throws Exception { | |
| runTest(writeInput(clazz), clazz.getName()); | |
| } | |
| private Path writeInput(Class<?> clazz) throws Exception { | |
| Path inputJar = temp.getRoot().toPath().resolve("input.jar"); | |
| ClassFileConsumer inputConsumer = new ClassFileConsumer.ArchiveConsumer(inputJar); | |
| String descriptor = DescriptorUtils.javaTypeToDescriptor(clazz.getName()); | |
| inputConsumer.accept(ByteDataView.of(ToolHelper.getClassAsBytes(clazz)), descriptor, null); | |
| inputConsumer.finished(null); | |
| return inputJar; | |
| } | |
| private Path writeInput(Path testDirectory, String[] classNames) throws Exception { | |
| Path inputJar = temp.getRoot().toPath().resolve("input.jar"); | |
| ClassFileConsumer inputConsumer = new ClassFileConsumer.ArchiveConsumer(inputJar); | |
| for (String className : classNames) { | |
| Path path = testDirectory.resolve(className.replace('.', '/') + ".class"); | |
| String descriptor = DescriptorUtils.javaTypeToDescriptor(className); | |
| try (InputStream inputStream = new FileInputStream(path.toFile())) { | |
| inputConsumer.accept( | |
| ByteDataView.of(ByteStreams.toByteArray(inputStream)), descriptor, null); | |
| } | |
| } | |
| inputConsumer.finished(null); | |
| return inputJar; | |
| } | |
| private void runTest(Path inputJar, String mainClass) throws Exception { | |
| Path outputJar = runR8(inputJar, compilationMode, "output.jar"); | |
| for (CfRuntime cfRuntime : parameters.getRuntime().asCfRuntimes()) { | |
| ProcessResult runInput = ToolHelper.runJava(cfRuntime, ImmutableList.of(inputJar), mainClass); | |
| Assert.assertEquals(0, runInput.exitCode); | |
| ProcessResult runOutput = | |
| ToolHelper.runJava(cfRuntime, ImmutableList.of(outputJar), mainClass); | |
| Assert.assertEquals(runInput.toString(), runOutput.toString()); | |
| } | |
| } | |
| private Path runR8(Path inputJar, CompilationMode mode, String outputName) throws Exception { | |
| Path outputJar = temp.getRoot().toPath().resolve(outputName); | |
| ToolHelper.runR8( | |
| R8Command.builder() | |
| .addLibraryFiles(TestDeps.getJava8RuntimeJar()) | |
| .setMode(mode) | |
| .setDisableTreeShaking(true) | |
| .setDisableMinification(true) | |
| .addProgramFiles(inputJar) | |
| .setOutput(outputJar, OutputMode.ClassFile) | |
| .build(), | |
| o -> o.skipIR = this.testMode == TestMode.CF_SKIP_IR); | |
| return outputJar; | |
| } | |
| } |