blob: c5f6ad53d5524f8325243cbc82adeb9d40245ac9 [file]
// 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.nestaccesscontrol;
import static com.android.tools.r8.TestRuntime.CfVm.JDK11;
import com.android.tools.r8.TestBase;
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.TestParametersCollection;
import com.android.tools.r8.desugar.nestaccesscontrol.NestBasedAccessToNativeMethodTest.Main.Inner;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.util.List;
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;
@RunWith(Parameterized.class)
public class NestBasedAccessToNativeMethodTest extends TestBase {
@Parameter(0)
public TestParameters parameters;
@Parameters(name = "{0}")
public static TestParametersCollection data() {
return getTestParameters()
.withCfRuntimesStartingFromIncluding(JDK11)
.withDexRuntimes()
.withAllApiLevels()
.withPartialCompilation()
.build();
}
@Test
public void testD8() throws Exception {
parameters.assumeDexRuntime();
testForD8(parameters)
.addProgramClassFileData(getProgramClassFileData())
.compile()
.run(parameters.getRuntime(), Main.class)
.assertSuccessWithOutputLines("Hello from native");
}
@Test
public void testR8() throws Exception {
testForR8(parameters)
.addProgramClassFileData(getProgramClassFileData())
.addKeepMainRule(Main.class)
.compile()
.run(parameters.getRuntime(), Main.class)
.assertSuccessWithOutputLines("Hello from native");
}
private List<byte[]> getProgramClassFileData() throws IOException, NoSuchMethodException {
return ImmutableList.of(
transformer(Main.class).setNest(Main.class, Inner.class).transform(),
transformer(Inner.class)
.setPrivate(Inner.class.getDeclaredMethod("goingToBePrivate"))
.setNest(Main.class, Inner.class)
.transform());
}
static class Main {
public static void main(String[] args) {
try {
Inner.goingToBePrivate();
} catch (UnsatisfiedLinkError e) {
System.out.println("Hello from native");
}
}
static class Inner {
/*private*/ static native void goingToBePrivate();
}
}
}