blob: 05562da3b145b9d0dfb6c031f4b1929799f54d92 [file] [log] [blame]
// Copyright (c) 2021, 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.desugar.desugaredlibrary.jdk11;
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.desugar.desugaredlibrary.DesugaredLibraryTestBase;
import com.android.tools.r8.transformers.MethodTransformer;
import com.android.tools.r8.utils.BooleanUtils;
import com.android.tools.r8.utils.StringUtils;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.objectweb.asm.Opcodes;
@RunWith(Parameterized.class)
public class JavaTimeJDK11Test extends DesugaredLibraryTestBase {
private static final String EXPECTED_RESULT =
StringUtils.lines("0", "1", "2", "3", "4", "5", "6", "7", "8", "00:00", "0");
@Parameter(0)
public TestParameters parameters;
@Parameter(1)
public boolean shrinkDesugaredLibrary;
@Parameters(name = "{1}, shrinkDesugaredLibrary: {0}")
public static List<Object[]> data() {
return buildParameters(
getTestParameters().withDexRuntimes().withAllApiLevels().build(),
BooleanUtils.falseValues());
}
@Test
public void testD8() throws Exception {
Assume.assumeTrue(isJDK11DesugaredLibrary());
KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
testForD8(parameters.getBackend())
.addLibraryFiles(getLibraryFile())
.addProgramClassFileData(getProgramClassFileData())
.setMinApi(parameters.getApiLevel())
.enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
.compile()
.addDesugaredCoreLibraryRunClassPath(
this::buildDesugaredLibrary,
parameters.getApiLevel(),
keepRuleConsumer.get(),
shrinkDesugaredLibrary)
.run(parameters.getRuntime(), TestClass.class)
.assertSuccessWithOutput(EXPECTED_RESULT);
}
@Test
public void testR8() throws Exception {
Assume.assumeTrue(isJDK11DesugaredLibrary());
KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
testForR8(Backend.DEX)
.addLibraryFiles(getLibraryFile())
.addProgramClassFileData(getProgramClassFileData())
.setMinApi(parameters.getApiLevel())
.addKeepMainRule(TestClass.class)
.enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
.compile()
.addDesugaredCoreLibraryRunClassPath(
this::buildDesugaredLibrary,
parameters.getApiLevel(),
keepRuleConsumer.get(),
shrinkDesugaredLibrary)
.run(parameters.getRuntime(), TestClass.class)
.assertSuccessWithOutput(EXPECTED_RESULT);
}
private Collection<byte[]> getProgramClassFileData() throws IOException {
Set<String> methodsToRewriteToDurationVirtualInvoke =
ImmutableSet.of(
"toDaysPart",
"toHoursPart",
"toMillisPart",
"toMinutesPart",
"toNanosPart",
"toSeconds",
"toSecondsPart",
"dividedBy",
"truncatedTo");
return ImmutableList.of(
transformer(TestClass.class)
.addMethodTransformer(
new MethodTransformer() {
@Override
public void visitMethodInsn(
int opcode,
String owner,
String name,
String descriptor,
boolean isInterface) {
if (opcode == Opcodes.INVOKESTATIC
&& methodsToRewriteToDurationVirtualInvoke.contains(name)) {
super.visitMethodInsn(
Opcodes.INVOKEVIRTUAL,
"java/time/Duration",
name,
withoutFirstObjectArg(descriptor),
isInterface);
return;
}
if (opcode == Opcodes.INVOKESTATIC && name.equals("ofInstant")) {
super.visitMethodInsn(
opcode, "java/time/LocalTime", name, descriptor, isInterface);
return;
}
if (opcode == Opcodes.INVOKESTATIC && name.equals("toEpochSecond")) {
super.visitMethodInsn(
Opcodes.INVOKEVIRTUAL,
"java/time/LocalTime",
name,
withoutFirstObjectArg(descriptor),
isInterface);
return;
}
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
}
})
.transform());
}
private String withoutFirstObjectArg(String descriptor) {
int i = descriptor.indexOf(";");
return "(" + descriptor.substring(i + 1);
}
public static class TestClass {
public static void main(String[] args) {
// Test all java.time.Duration methods added in Android S (where added in JDK 9).
System.out.println(toSecondsPart(truncatedTo(Duration.ofSeconds(62), ChronoUnit.MINUTES)));
System.out.println(toDaysPart(Duration.ofHours(31)));
System.out.println(toHoursPart(Duration.ofHours(26)));
System.out.println(toSeconds(Duration.ofSeconds(3)));
System.out.println(toSecondsPart(Duration.ofSeconds(64)));
System.out.println(toMinutesPart(Duration.ofSeconds(301)));
System.out.println(toMillisPart(Duration.ofNanos(6000002)));
System.out.println(toNanosPart(Duration.ofNanos(1000000007)));
System.out.println(dividedBy(Duration.ofHours(4), Duration.ofMinutes(30)));
// Test all java.time.LocalTime methods added in Android S (where added in JDK 9).
System.out.println(ofInstant(Instant.ofEpochSecond(0), ZoneId.of("UTC")));
System.out.println(
toEpochSecond(LocalTime.of(0, 0), LocalDate.ofEpochDay(0), ZoneOffset.UTC));
}
// Replaced in the transformer by JDK 11 virtual Duration#toDaysPart().
private static long toDaysPart(Duration receiver) {
return -1;
}
// Replaced in the transformer by JDK 11 virtual Duration#toHoursPart().
private static int toHoursPart(Duration receiver) {
return -1;
}
// Replaced in the transformer by JDK 11 virtual Duration#toMillisPart().
private static int toMillisPart(Duration receiver) {
return -1;
}
// Replaced in the transformer by JDK 11 virtual Duration#toMinutesPart().
private static int toMinutesPart(Duration receiver) {
return -1;
}
// Replaced in the transformer by JDK 11 virtual Duration#toNanosPart().
private static int toNanosPart(Duration receiver) {
return -1;
}
// Replaced in the transformer by JDK 11 virtual Duration#toSeconds().
private static long toSeconds(Duration receiver) {
return -1;
}
// Replaced in the transformer by JDK 11 virtual Duration#toSecondsPart().
private static int toSecondsPart(Duration receiver) {
return -1;
}
// Replaced in the transformer by JDK 11 virtual Duration#dividedBy(Duration).
private static long dividedBy(Duration receiver, Duration divisor) {
return -1;
}
// Replaced in the transformer by JDK 11 virtual Duration#truncatedTo(TemporalUnit).
private static Duration truncatedTo(Duration receiver, TemporalUnit unit) {
return null;
}
// Replaced in the transformer by JDK 11 static LocalTime#ofInstant.
private static LocalTime ofInstant(Instant instant, ZoneId zone) {
return null;
}
// Replaced in the transformer by JDK 11 virtual LocalTime#toEpochSecond.
private static long toEpochSecond(LocalTime receiver, LocalDate date, ZoneOffset offset) {
return -1;
}
}
}