blob: 3e0724e0ae45ffe49fd36fa1f794b0759a91d7c2 [file]
// Copyright (c) 2026, 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.androidapi;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import com.android.tools.r8.TestBase;
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.TestParametersCollection;
import com.android.tools.r8.ToolHelper;
import com.android.tools.r8.references.ClassReference;
import com.android.tools.r8.references.FieldReference;
import com.android.tools.r8.references.MethodReference;
import com.android.tools.r8.utils.AndroidApiLevel;
import com.android.tools.r8.utils.internal.FileUtils;
import com.android.tools.r8.utils.internal.StringUtils;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
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 GenerateApiAmendments extends TestBase {
private static final String API_MISSING_FILE = "missing.api.txt";
private static final String API_HIDDEN_FILE = "hidden.api.txt";
private static final String JAR_HIDDEN_FILE = "hidden.jar.txt";
private static Path getOutputDir() {
return Paths.get(ToolHelper.SOURCE_DIR, "main", "resources", "resources");
}
private static final Charset CHARSET = StandardCharsets.UTF_8;
@Parameter() public TestParameters parameters;
@Parameters(name = "{0}")
public static TestParametersCollection data() {
return getTestParameters().withNoneRuntime().build();
}
@Test
public void testGeneratedFilesUpToDate() throws Exception {
Path outputDir = getOutputDir();
assertEquals(
FileUtils.readTextFile(outputDir.resolve(API_MISSING_FILE), CHARSET), generateApiMissing());
assertEquals(
FileUtils.readTextFile(outputDir.resolve(API_HIDDEN_FILE), CHARSET), generateApiHidden());
assertEquals(
FileUtils.readTextFile(outputDir.resolve(JAR_HIDDEN_FILE), CHARSET), generateJarHidden());
}
/** OBS: This cannot be run via the editor, run it via the matching Gradle task. */
public static void main(String[] args) throws IOException {
// ----------------------------------------- IMPORTANT -----------------------------------------
// When these amendments are semantically changed (i.e. a format change doesn't matter)
// DATABASE_FORMAT_VERSION must be incremented to reflect that the output of
// ApiDatabaseGenerator has changed.
// ----------------------------------------- IMPORTANT -----------------------------------------
Path outputDir = getOutputDir();
if (!Files.exists(outputDir)) {
Files.createDirectories(outputDir);
}
Files.write(outputDir.resolve(API_MISSING_FILE), generateApiMissing().getBytes(CHARSET));
Files.write(outputDir.resolve(API_HIDDEN_FILE), generateApiHidden().getBytes(CHARSET));
Files.write(outputDir.resolve(JAR_HIDDEN_FILE), generateJarHidden().getBytes(CHARSET));
}
private static String generateApiMissing() {
return makeFileContent(generateApiMissingLines());
}
public static String generateApiHidden() throws IOException {
return makeFileContent(generateApiHiddenLines());
}
public static String generateJarHidden() throws IOException {
return makeFileContent(generateJarHiddenLines());
}
private static String makeFileContent(List<String> lines) {
// Since this is a stable internal resource, line endings should NOT be platform dependent.
String header =
StringUtils.join(
"\n",
ImmutableList.of(
"// Copyright (c) 2026, 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.",
"//",
"// This file is generated by GenerateApiAmendments.java.",
"// Do not edit manually."));
String content = StringUtils.join("\n", lines);
return header + "\n" + content + "\n";
}
private static List<String> generateApiMissingLines() {
ArrayList<String> lines = new ArrayList<>();
AndroidApiLevelDatabaseTestHelper.visitAdditionalKnownApiReferences(
(methodReference, apiLevel) -> lines.add(formatApiMethod(methodReference, apiLevel)));
Collections.sort(lines);
return lines;
}
/** See {@link com.android.tools.r8.apimodel.ApiAmendmentsParser} for the format. */
private static String formatApiClass(ClassReference classReference, AndroidApiLevel apiLevel) {
return "class " + classReference.getDescriptor() + " " + apiLevel.getMajor();
}
/** See {@link com.android.tools.r8.apimodel.ApiAmendmentsParser} for the format. */
private static String formatApiMethod(MethodReference methodReference, AndroidApiLevel apiLevel) {
String holder = methodReference.getHolderClass().getDescriptor();
String methodName = methodReference.getMethodName();
String descriptor = methodReference.getMethodDescriptor();
return "method " + holder + " " + methodName + " " + descriptor + " " + apiLevel.getMajor();
}
/** See {@link com.android.tools.r8.apimodel.ApiAmendmentsParser} for the format. */
private static String formatApiField(FieldReference fieldReference, AndroidApiLevel apiLevel) {
String holder = fieldReference.getHolderClass().getDescriptor();
String name = fieldReference.getFieldName();
return "field " + holder + " " + name + " " + apiLevel.getMajor();
}
private static List<String> generateJarHiddenLines() throws IOException {
ArrayList<String> lines = new ArrayList<>();
Map<String, Boolean> skipClasses = getSkipClassesMap();
AndroidApiLevelDatabaseTestHelper.visitHiddenReferences(
(classReference, superReference, interfaces, apiLevel) -> {
if (checkForSkip(classReference.getBinaryName(), skipClasses)) {
return;
}
if (superReference != null) {
lines.add(formatJarClass(classReference, superReference, interfaces));
} else {
lines.add(formatJarInterface(classReference, interfaces));
}
},
(methodReference, isStatic, apiLevel) -> {
if (checkForSkip(methodReference.getHolderClass().getBinaryName(), skipClasses)) {
return;
}
lines.add(formatJarMethod(methodReference, isStatic));
},
(fieldReference, apiLevel) -> {
if (checkForSkip(fieldReference.getHolderClass().getBinaryName(), skipClasses)) {
return;
}
lines.add(formatJarField(fieldReference));
});
ensureSkippedClasses(skipClasses);
Collections.sort(lines);
return lines;
}
/** See {@link com.android.tools.r8.apimodel.ApiAmendmentsParser} for the format. */
private static String formatJarClass(
ClassReference classReference,
ClassReference superReference,
List<ClassReference> interfaces) {
String clazz = classReference.getDescriptor();
String superclass = superReference.getDescriptor();
String implString = formatInheritances(interfaces);
return "class " + clazz + " extends " + superclass + " implements " + implString;
}
/** See {@link com.android.tools.r8.apimodel.ApiAmendmentsParser} for the format. */
private static String formatJarInterface(
ClassReference classReference, List<ClassReference> interfaces) {
String clazz = classReference.getDescriptor();
String extString = formatInheritances(interfaces);
return "interface " + clazz + " extends " + extString;
}
/** See {@link com.android.tools.r8.apimodel.ApiAmendmentsParser} for the format. */
private static String formatInheritances(List<ClassReference> interfaces) {
String extString;
if (interfaces.isEmpty()) {
extString = "none";
} else {
extString =
interfaces.stream()
.map(ClassReference::getDescriptor)
.sorted()
.collect(Collectors.joining(","));
}
return extString;
}
/** See {@link com.android.tools.r8.apimodel.ApiAmendmentsParser} for the format. */
private static String formatJarMethod(MethodReference methodReference, Boolean isStatic) {
String holder = methodReference.getHolderClass().getDescriptor();
String name = methodReference.getMethodName();
String descriptor = methodReference.getMethodDescriptor();
String virtualness = isStatic ? "static" : "instance";
return "method " + holder + " " + name + " " + descriptor + " " + virtualness;
}
/** See {@link com.android.tools.r8.apimodel.ApiAmendmentsParser} for the format. */
private static String formatJarField(FieldReference fieldReference) {
String holder = fieldReference.getHolderClass().getDescriptor();
String name = fieldReference.getFieldName();
return "field " + holder + " " + name;
}
private static List<String> generateApiHiddenLines() throws IOException {
ArrayList<String> lines = new ArrayList<>();
Map<String, Boolean> skipClasses = getSkipClassesMap();
AndroidApiLevelDatabaseTestHelper.visitHiddenReferences(
(classReference, superReference, interfaces, apiLevel) -> {
if (checkForSkip(classReference.getBinaryName(), skipClasses)) {
return;
}
lines.add(formatApiClass(classReference, apiLevel));
},
(methodReference, isStatic, apiLevel) -> {
if (checkForSkip(methodReference.getHolderClass().getBinaryName(), skipClasses)) {
return;
}
lines.add(formatApiMethod(methodReference, apiLevel));
},
(fieldReference, apiLevel) -> {
if (checkForSkip(fieldReference.getHolderClass().getBinaryName(), skipClasses)) {
return;
}
lines.add(formatApiField(fieldReference, apiLevel));
});
ensureSkippedClasses(skipClasses);
Collections.sort(lines);
return lines;
}
private static Map<String, Boolean> getSkipClassesMap() {
// These are present because of annotation inheritance, but they are not public classes.
Map<String, Boolean> skipClasses = new HashMap<>();
skipClasses.put("java/nio/DirectByteBuffer", false);
return skipClasses;
}
private static boolean checkForSkip(String holderBinaryName, Map<String, Boolean> skipClasses) {
boolean skip = false;
if (skipClasses.containsKey(holderBinaryName)) {
skipClasses.put(holderBinaryName, true);
skip = true;
}
return skip;
}
private static void ensureSkippedClasses(Map<String, Boolean> skippedClasses) {
for (Map.Entry<String, Boolean> entry : skippedClasses.entrySet()) {
assertTrue(entry.getKey() + " was never skipped", entry.getValue());
}
}
}