blob: 47659e253e5eeeefd4a58c0a009fd495e09126dc [file] [log] [blame]
// Copyright (c) 2019, 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.shaking.keptgraph;
import static com.android.tools.r8.references.Reference.methodFromMethod;
import static org.junit.Assert.assertEquals;
import com.android.tools.r8.TestBase;
import com.android.tools.r8.ToolHelper;
import com.android.tools.r8.origin.Origin;
import com.android.tools.r8.references.MethodReference;
import com.android.tools.r8.utils.StringUtils;
import com.android.tools.r8.utils.graphinspector.GraphInspector;
import com.android.tools.r8.utils.graphinspector.GraphInspector.QueryNode;
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 KeptByAnnotatedClassTestRunner extends TestBase {
private static final Class<KeptByAnnotatedClassTest> CLASS = KeptByAnnotatedClassTest.class;
private final String EXPECTED = StringUtils.lines("called bar");
private final Backend backend;
@Parameters(name = "{0}")
public static Backend[] data() {
return ToolHelper.getBackends();
}
public KeptByAnnotatedClassTestRunner(Backend backend) {
this.backend = backend;
}
@Test
public void testKeptMethod() throws Exception {
MethodReference mainMethod = methodFromMethod(CLASS.getDeclaredMethod("main", String[].class));
MethodReference barMethod = methodFromMethod(CLASS.getDeclaredMethod("bar"));
MethodReference bazMethod = methodFromMethod(CLASS.getDeclaredMethod("baz"));
GraphInspector inspector =
testForR8(backend)
.enableGraphInspector()
.enableInliningAnnotations()
.addProgramClasses(CLASS)
.addKeepRules("-keep @com.android.tools.r8.Keep class * { public *; }")
.run(CLASS)
.assertSuccessWithOutput(EXPECTED)
.graphInspector();
// The only root should be the keep annotation rule.
assertEquals(1, inspector.getRoots().size());
QueryNode root = inspector.rule(Origin.unknown(), 1, 1).assertRoot();
// Check that the call chain goes from root -> main(unchanged) -> bar(renamed).
inspector.method(barMethod).assertRenamed().assertInvokedFrom(mainMethod);
inspector.method(mainMethod).assertNotRenamed().assertKeptBy(root);
// Check baz is removed.
inspector.method(bazMethod).assertAbsent();
}
}