Merge "Revert "Add library licenses for kotlinx-metadata-jvm and its dependencies.""
diff --git a/build.gradle b/build.gradle
index 7720cf3..92d7471 100644
--- a/build.gradle
+++ b/build.gradle
@@ -37,7 +37,6 @@
gsonVersion = '2.7'
junitVersion = '4.12'
kotlinVersion = '1.2.30'
- kotlinExtMetadataJVMVersion = '0.0.2'
protobufVersion = '3.0.0'
smaliVersion = '2.2b4'
}
@@ -61,7 +60,10 @@
'-Xep:MissingDefault:ERROR',
'-Xep:MultipleTopLevelClasses:ERROR',
'-Xep:NarrowingCompoundAssignment:ERROR',
- '-Xep:BoxedPrimitiveConstructor:ERROR']
+ '-Xep:BoxedPrimitiveConstructor:ERROR',
+ '-Xep:LogicalAssignment:ERROR',
+ '-Xep:FloatCast:ERROR',
+ '-Xep:ReturnValueIgnored:ERROR']
apply from: 'copyAdditionalJctfCommonFiles.gradle'
@@ -72,7 +74,6 @@
repositories {
maven { url 'https://maven.google.com' }
- maven { url 'https://kotlin.bintray.com/kotlinx' }
mavenCentral()
}
@@ -222,7 +223,6 @@
exclude group: 'org.codehaus.mojo'
})
compile group: 'it.unimi.dsi', name: 'fastutil', version: fastutilVersion
- compile "org.jetbrains.kotlinx:kotlinx-metadata-jvm:$kotlinExtMetadataJVMVersion"
compile group: 'org.ow2.asm', name: 'asm', version: asmVersion
compile group: 'org.ow2.asm', name: 'asm-commons', version: asmVersion
compile group: 'org.ow2.asm', name: 'asm-tree', version: asmVersion
diff --git a/src/main/java/com/android/tools/r8/ArchiveClassFileProvider.java b/src/main/java/com/android/tools/r8/ArchiveClassFileProvider.java
index b3d59fb..af83158 100644
--- a/src/main/java/com/android/tools/r8/ArchiveClassFileProvider.java
+++ b/src/main/java/com/android/tools/r8/ArchiveClassFileProvider.java
@@ -17,6 +17,7 @@
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
@@ -60,7 +61,7 @@
assert isArchive(archive);
origin = new PathOrigin(archive);
try {
- zipFile = new ZipFile(archive.toFile());
+ zipFile = new ZipFile(archive.toFile(), StandardCharsets.UTF_8);
} catch (IOException e) {
if (!Files.exists(archive)) {
throw new NoSuchFileException(archive.toString());
diff --git a/src/main/java/com/android/tools/r8/ArchiveProgramResourceProvider.java b/src/main/java/com/android/tools/r8/ArchiveProgramResourceProvider.java
index baac9bc..425b3c3 100644
--- a/src/main/java/com/android/tools/r8/ArchiveProgramResourceProvider.java
+++ b/src/main/java/com/android/tools/r8/ArchiveProgramResourceProvider.java
@@ -16,6 +16,7 @@
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
@@ -60,7 +61,10 @@
public static ArchiveProgramResourceProvider fromArchive(
Path archive, Predicate<String> include) {
- return fromSupplier(new PathOrigin(archive), () -> new ZipFile(archive.toFile()), include);
+ return fromSupplier(
+ new PathOrigin(archive),
+ () -> new ZipFile(archive.toFile(), StandardCharsets.UTF_8),
+ include);
}
public static ArchiveProgramResourceProvider fromSupplier(
diff --git a/src/main/java/com/android/tools/r8/D8CommandParser.java b/src/main/java/com/android/tools/r8/D8CommandParser.java
index 9e5d36a..93c0126 100644
--- a/src/main/java/com/android/tools/r8/D8CommandParser.java
+++ b/src/main/java/com/android/tools/r8/D8CommandParser.java
@@ -3,16 +3,79 @@
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8;
+import static com.android.tools.r8.utils.FileUtils.isArchive;
+
import com.android.tools.r8.errors.CompilationError;
import com.android.tools.r8.origin.Origin;
+import com.android.tools.r8.origin.PathOrigin;
+import com.android.tools.r8.utils.ExceptionDiagnostic;
import com.android.tools.r8.utils.FlagFile;
import com.android.tools.r8.utils.StringDiagnostic;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Sets;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
public class D8CommandParser extends BaseCompilerCommandParser {
+ static class OrderedClassFileResourceProvider implements ClassFileResourceProvider {
+ static class Builder {
+ private final ImmutableList.Builder<ClassFileResourceProvider> builder =
+ ImmutableList.builder();
+ boolean empty = true;
+
+ OrderedClassFileResourceProvider build() {
+ return new OrderedClassFileResourceProvider(builder.build());
+ }
+
+ Builder addClassFileResourceProvider(ClassFileResourceProvider provider) {
+ builder.add(provider);
+ empty = false;
+ return this;
+ }
+
+ boolean isEmpty() {
+ return empty;
+ }
+ }
+
+ final List<ClassFileResourceProvider> providers;
+ final Set<String> descriptors = Sets.newHashSet();
+
+ private OrderedClassFileResourceProvider(ImmutableList<ClassFileResourceProvider> providers) {
+ this.providers = providers;
+ // Collect all descriptors that can be provided.
+ this.providers.forEach(provider -> this.descriptors.addAll(provider.getClassDescriptors()));
+ }
+
+ static Builder builder() {
+ return new Builder();
+ }
+
+ @Override
+ public Set<String> getClassDescriptors() {
+ return descriptors;
+ }
+
+ @Override
+ public ProgramResource getProgramResource(String descriptor) {
+ // Search the providers in order. Return the program resource from the first provider that
+ // can provide it.
+ for (ClassFileResourceProvider provider : providers) {
+ if (provider.getClassDescriptors().contains(descriptor)) {
+ return provider.getProgramResource(descriptor);
+ }
+ }
+ return null;
+ }
+ }
+
public static void main(String[] args) throws CompilationFailedException {
D8Command command = parse(args, Origin.root()).build();
if (command.isPrintHelp()) {
@@ -76,6 +139,8 @@
Path outputPath = null;
OutputMode outputMode = null;
boolean hasDefinedApiLevel = false;
+ OrderedClassFileResourceProvider.Builder classpathBuilder =
+ OrderedClassFileResourceProvider.builder();
String[] expandedArgs = FlagFile.expandFlagFiles(args, builder);
try {
for (int i = 0; i < expandedArgs.length; i++) {
@@ -115,7 +180,22 @@
} else if (arg.equals("--lib")) {
builder.addLibraryFiles(Paths.get(expandedArgs[++i]));
} else if (arg.equals("--classpath")) {
- builder.addClasspathFiles(Paths.get(expandedArgs[++i]));
+ Path file = Paths.get(expandedArgs[++i]);
+ try {
+ if (!Files.exists(file)) {
+ throw new NoSuchFileException(file.toString());
+ }
+ if (isArchive(file)) {
+ classpathBuilder.addClassFileResourceProvider(new ArchiveClassFileProvider(file));
+ } else if (Files.isDirectory(file)) {
+ classpathBuilder.addClassFileResourceProvider(
+ DirectoryClassFileProvider.fromDirectory(file));
+ } else {
+ throw new CompilationError("Unsupported classpath file type", new PathOrigin(file));
+ }
+ } catch (IOException e) {
+ builder.error(new ExceptionDiagnostic(e, new PathOrigin(file)));
+ }
} else if (arg.equals("--main-dex-list")) {
builder.addMainDexListFiles(Paths.get(expandedArgs[++i]));
} else if (arg.equals("--optimize-multidex-for-linearalloc")) {
@@ -140,6 +220,9 @@
builder.addProgramFiles(Paths.get(arg));
}
}
+ if (!classpathBuilder.isEmpty()) {
+ builder.addClasspathResourceProvider(classpathBuilder.build());
+ }
if (compilationMode != null) {
builder.setMode(compilationMode);
}
diff --git a/src/main/java/com/android/tools/r8/Disassemble.java b/src/main/java/com/android/tools/r8/Disassemble.java
index 9b1599f..7b17ec8 100644
--- a/src/main/java/com/android/tools/r8/Disassemble.java
+++ b/src/main/java/com/android/tools/r8/Disassemble.java
@@ -14,7 +14,6 @@
import com.android.tools.r8.utils.StringDiagnostic;
import com.android.tools.r8.utils.ThreadUtils;
import com.android.tools.r8.utils.Timing;
-import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -33,6 +32,7 @@
private Path proguardMapFile = null;
private boolean useSmali = false;
private boolean allInfo = false;
+ private boolean useIr;
@Override
Builder self() {
@@ -63,6 +63,11 @@
return this;
}
+ public Builder setUseIr(boolean useIr) {
+ this.useIr = useIr;
+ return this;
+ }
+
@Override
protected DisassembleCommand makeCommand() {
// If printing versions ignore everything else.
@@ -74,24 +79,26 @@
getOutputPath(),
proguardMapFile == null ? null : StringResource.fromFile(proguardMapFile),
allInfo,
- useSmali);
+ useSmali,
+ useIr);
}
}
- static final String USAGE_MESSAGE = String.join("\n", ImmutableList.of(
- "Usage: disasm [options] <input-files>",
- " where <input-files> are dex files",
- " and options are:",
- " --all # Include all information in disassembly.",
- " --smali # Disassemble using smali syntax.",
- " --pg-map <file> # Proguard map <file> for mapping names.",
- " --output # Specify a file or directory to write to.",
- " --version # Print the version of r8.",
- " --help # Print this message."));
-
+ static final String USAGE_MESSAGE =
+ "Usage: disasm [options] <input-files>\n"
+ + " where <input-files> are dex files\n"
+ + " and options are:\n"
+ + " --all # Include all information in disassembly.\n"
+ + " --smali # Disassemble using smali syntax.\n"
+ + " --ir # Print IR before and after optimization.\n"
+ + " --pg-map <file> # Proguard map <file> for mapping names.\n"
+ + " --output # Specify a file or directory to write to.\n"
+ + " --version # Print the version of r8.\n"
+ + " --help # Print this message.";
private final boolean allInfo;
private final boolean useSmali;
+ private final boolean useIr;
public static Builder builder() {
return new Builder();
@@ -112,10 +119,12 @@
builder.setPrintHelp(true);
} else if (arg.equals("--version")) {
builder.setPrintVersion(true);
- } else if (arg.equals("--all")) {
+ } else if (arg.equals("--all")) {
builder.setAllInfo(true);
} else if (arg.equals("--smali")) {
builder.setUseSmali(true);
+ } else if (arg.equals("--ir")) {
+ builder.setUseIr(true);
} else if (arg.equals("--pg-map")) {
builder.setProguardMapFile(Paths.get(args[++i]));
} else if (arg.equals("--output")) {
@@ -132,13 +141,18 @@
}
private DisassembleCommand(
- AndroidApp inputApp, Path outputPath, StringResource proguardMap,
- boolean allInfo, boolean useSmali) {
+ AndroidApp inputApp,
+ Path outputPath,
+ StringResource proguardMap,
+ boolean allInfo,
+ boolean useSmali,
+ boolean useIr) {
super(inputApp);
this.outputPath = outputPath;
this.proguardMap = proguardMap;
this.allInfo = allInfo;
this.useSmali = useSmali;
+ this.useIr = useIr;
}
private DisassembleCommand(boolean printHelp, boolean printVersion) {
@@ -147,6 +161,7 @@
proguardMap = null;
allInfo = false;
useSmali = false;
+ useIr = false;
}
public Path getOutputPath() {
@@ -157,6 +172,10 @@
return useSmali;
}
+ public boolean useIr() {
+ return useIr;
+ }
+
@Override
InternalOptions getInternalOptions() {
InternalOptions internal = new InternalOptions();
@@ -190,9 +209,10 @@
try {
DexApplication application =
new ApplicationReader(app, options, timing).read(command.proguardMap, executor);
- DexByteCodeWriter writer = command.useSmali()
- ? new SmaliWriter(application, options)
- : new AssemblyWriter(application, options, command.allInfo);
+ DexByteCodeWriter writer =
+ command.useSmali()
+ ? new SmaliWriter(application, options)
+ : new AssemblyWriter(application, options, command.allInfo, command.useIr());
if (command.getOutputPath() != null) {
writer.write(command.getOutputPath());
} else {
diff --git a/src/main/java/com/android/tools/r8/JarSizeCompare.java b/src/main/java/com/android/tools/r8/JarSizeCompare.java
new file mode 100644
index 0000000..aa16b1f
--- /dev/null
+++ b/src/main/java/com/android/tools/r8/JarSizeCompare.java
@@ -0,0 +1,482 @@
+// 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;
+
+import com.android.tools.r8.dex.ApplicationReader;
+import com.android.tools.r8.errors.Unreachable;
+import com.android.tools.r8.graph.Code;
+import com.android.tools.r8.graph.DexApplication;
+import com.android.tools.r8.graph.DexClass;
+import com.android.tools.r8.graph.DexEncodedField;
+import com.android.tools.r8.graph.DexEncodedMethod;
+import com.android.tools.r8.graph.DexProgramClass;
+import com.android.tools.r8.naming.ClassNameMapper;
+import com.android.tools.r8.naming.ClassNamingForNameMapper;
+import com.android.tools.r8.naming.MemberNaming.FieldSignature;
+import com.android.tools.r8.naming.MemberNaming.MethodSignature;
+import com.android.tools.r8.utils.AndroidApiLevel;
+import com.android.tools.r8.utils.AndroidApp;
+import com.android.tools.r8.utils.AndroidAppConsumers;
+import com.android.tools.r8.utils.DescriptorUtils;
+import com.android.tools.r8.utils.InternalOptions;
+import com.android.tools.r8.utils.Timing;
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableMap;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.function.BiConsumer;
+
+/**
+ * JarSizeCompare outputs the class, method, field sizes of the given JAR files. For each input, a
+ * ProGuard map can be passed that is used to resolve minified names.
+ *
+ * <p>By default, only shows methods where R8's DEX output is 5 or more instructions larger than
+ * ProGuard+D8's output. Pass {@code --threshold 0} to display all methods.
+ */
+public class JarSizeCompare {
+
+ private static final String USAGE =
+ "Arguments:\n"
+ + " [--threshold <threshold>]\n"
+ + " [--lib <lib.jar>]\n"
+ + " --input <name1> <input1.jar> [<map1.txt>]\n"
+ + " --input <name2> <input2.jar> [<map2.txt>] ...\n"
+ + "\n"
+ + "JarSizeCompare outputs the class, method, field sizes of the given JAR files.\n"
+ + "For each input, a ProGuard map can be passed that is used to resolve minified names.\n";
+
+ private static final ImmutableMap<String, String> R8_RELOCATIONS =
+ ImmutableMap.<String, String>builder()
+ .put("com.google.common", "com.android.tools.r8.com.google.common")
+ .put("com.google.gson", "com.android.tools.r8.com.google.gson")
+ .put("com.google.thirdparty", "com.android.tools.r8.com.google.thirdparty")
+ .put("joptsimple", "com.android.tools.r8.joptsimple")
+ .put("org.apache.commons", "com.android.tools.r8.org.apache.commons")
+ .put("org.objectweb.asm", "com.android.tools.r8.org.objectweb.asm")
+ .put("it.unimi.dsi.fastutil", "com.android.tools.r8.it.unimi.dsi.fastutil")
+ .build();
+
+ private final List<Path> libraries;
+ private final List<InputParameter> inputParameters;
+ private final int threshold;
+ private final InternalOptions options;
+ private int pgIndex;
+ private int r8Index;
+
+ private JarSizeCompare(
+ List<Path> libraries, List<InputParameter> inputParameters, int threshold) {
+ this.libraries = libraries;
+ this.inputParameters = inputParameters;
+ this.threshold = threshold;
+ options = new InternalOptions();
+ options.enableCfFrontend = true;
+ }
+
+ public void run() throws Exception {
+ List<String> names = new ArrayList<>();
+ List<InputApplication> inputApplicationList = new ArrayList<>();
+ Timing timing = new Timing("JarSizeCompare");
+ for (InputParameter inputParameter : inputParameters) {
+ AndroidApp inputApp = inputParameter.getInputApp(libraries);
+ DexApplication input = inputParameter.getReader(options, inputApp, timing);
+ AndroidAppConsumers appConsumer = new AndroidAppConsumers();
+ D8.run(
+ D8Command.builder(inputApp)
+ .setMinApiLevel(AndroidApiLevel.P.getLevel())
+ .setProgramConsumer(appConsumer.wrapDexIndexedConsumer(null))
+ .build());
+ DexApplication d8Input = inputParameter.getReader(options, appConsumer.build(), timing);
+ InputApplication inputApplication =
+ new InputApplication(input, translateClassNames(input, input.classes()));
+ InputApplication d8Classes =
+ new InputApplication(input, translateClassNames(input, d8Input.classes()));
+ names.add(inputParameter.name + "-input");
+ inputApplicationList.add(inputApplication);
+ names.add(inputParameter.name + "-d8");
+ inputApplicationList.add(d8Classes);
+ }
+ if (threshold != 0) {
+ pgIndex = names.indexOf("pg-d8");
+ r8Index = names.indexOf("r8-d8");
+ }
+ Map<String, InputClass[]> inputClasses = new HashMap<>();
+ for (int i = 0; i < names.size(); i++) {
+ InputApplication classes = inputApplicationList.get(i);
+ for (String className : classes.getClasses()) {
+ inputClasses.computeIfAbsent(className, k -> new InputClass[names.size()])[i] =
+ classes.getInputClass(className);
+ }
+ }
+ for (Entry<String, Map<String, InputClass[]>> library : byLibrary(inputClasses)) {
+ System.out.println("");
+ System.out.println(Strings.repeat("=", 100));
+ String commonPrefix = getCommonPrefix(library.getValue().keySet());
+ if (library.getKey().isEmpty()) {
+ System.out.println("PROGRAM (" + commonPrefix + ")");
+ } else {
+ System.out.println("LIBRARY: " + library.getKey() + " (" + commonPrefix + ")");
+ }
+ printLibrary(library.getValue(), commonPrefix);
+ }
+ }
+
+ private Map<String, DexProgramClass> translateClassNames(
+ DexApplication input, List<DexProgramClass> classes) {
+ Map<String, DexProgramClass> result = new HashMap<>();
+ ClassNameMapper classNameMapper = input.getProguardMap();
+ for (DexProgramClass programClass : classes) {
+ ClassNamingForNameMapper classNaming;
+ if (classNameMapper == null) {
+ classNaming = null;
+ } else {
+ classNaming = classNameMapper.getClassNaming(programClass.type);
+ }
+ String type =
+ classNaming == null ? programClass.type.toSourceString() : classNaming.originalName;
+ result.put(type, programClass);
+ }
+ return result;
+ }
+
+ private String getCommonPrefix(Set<String> classes) {
+ if (classes.size() <= 1) {
+ return "";
+ }
+ String commonPrefix = null;
+ for (String clazz : classes) {
+ if (clazz.equals("r8.GeneratedOutlineSupport")) {
+ continue;
+ }
+ if (commonPrefix == null) {
+ commonPrefix = clazz;
+ } else {
+ int i = 0;
+ while (i < clazz.length()
+ && i < commonPrefix.length()
+ && clazz.charAt(i) == commonPrefix.charAt(i)) {
+ i++;
+ }
+ commonPrefix = commonPrefix.substring(0, i);
+ }
+ }
+ return commonPrefix;
+ }
+
+ private void printLibrary(Map<String, InputClass[]> classMap, String commonPrefix) {
+ List<Entry<String, InputClass[]>> classes = new ArrayList<>(classMap.entrySet());
+ classes.sort(Comparator.comparing(Entry::getKey));
+ for (Entry<String, InputClass[]> clazz : classes) {
+ printClass(
+ clazz.getKey().substring(commonPrefix.length()), new ClassCompare(clazz.getValue()));
+ }
+ }
+
+ private void printClass(String name, ClassCompare inputClasses) {
+ List<MethodSignature> methods = getMethods(inputClasses);
+ List<FieldSignature> fields = getFields(inputClasses);
+ if (methods.isEmpty() && fields.isEmpty()) {
+ return;
+ }
+ System.out.println(name);
+ for (MethodSignature sig : methods) {
+ printSignature(getMethodString(sig), inputClasses.sizes(sig));
+ }
+ for (FieldSignature sig : fields) {
+ printSignature(getFieldString(sig), inputClasses.sizes(sig));
+ }
+ }
+
+ private String getMethodString(MethodSignature sig) {
+ StringBuilder builder = new StringBuilder().append('(');
+ for (int i = 0; i < sig.parameters.length; i++) {
+ builder.append(DescriptorUtils.javaTypeToShorty(sig.parameters[i]));
+ }
+ builder.append(')').append(DescriptorUtils.javaTypeToShorty(sig.type)).append(' ');
+ return builder.append(sig.name).toString();
+ }
+
+ private String getFieldString(FieldSignature sig) {
+ return DescriptorUtils.javaTypeToShorty(sig.type) + ' ' + sig.name;
+ }
+
+ private void printSignature(String key, int[] sizes) {
+ System.out.print(padItem(key));
+ for (int size : sizes) {
+ System.out.print(padValue(size));
+ }
+ System.out.print('\n');
+ }
+
+ private List<MethodSignature> getMethods(ClassCompare inputClasses) {
+ List<MethodSignature> methods = new ArrayList<>();
+ for (MethodSignature methodSignature : inputClasses.getMethods()) {
+ if (threshold == 0 || methodExceedsThreshold(inputClasses, methodSignature)) {
+ methods.add(methodSignature);
+ }
+ }
+ return methods;
+ }
+
+ private boolean methodExceedsThreshold(
+ ClassCompare inputClasses, MethodSignature methodSignature) {
+ assert threshold > 0;
+ assert pgIndex != r8Index;
+ int pgSize = inputClasses.size(methodSignature, pgIndex);
+ int r8Size = inputClasses.size(methodSignature, r8Index);
+ return pgSize != -1 && r8Size != -1 && pgSize + threshold <= r8Size;
+ }
+
+ private List<FieldSignature> getFields(ClassCompare inputClasses) {
+ return threshold == 0 ? inputClasses.getFields() : Collections.emptyList();
+ }
+
+ private String padItem(String s) {
+ return String.format("%-52s", s);
+ }
+
+ private String padValue(int v) {
+ return String.format("%8s", v == -1 ? "---" : v);
+ }
+
+ private List<Map.Entry<String, Map<String, InputClass[]>>> byLibrary(
+ Map<String, InputClass[]> inputClasses) {
+ Map<String, Map<String, InputClass[]>> byLibrary = new HashMap<>();
+ for (Entry<String, InputClass[]> entry : inputClasses.entrySet()) {
+ Map<String, InputClass[]> library =
+ byLibrary.computeIfAbsent(getLibraryName(entry.getKey()), k -> new HashMap<>());
+ library.put(entry.getKey(), entry.getValue());
+ }
+ List<Entry<String, Map<String, InputClass[]>>> list = new ArrayList<>(byLibrary.entrySet());
+ list.sort(Comparator.comparing(Entry::getKey));
+ return list;
+ }
+
+ private String getLibraryName(String className) {
+ for (Entry<String, String> relocation : R8_RELOCATIONS.entrySet()) {
+ if (className.startsWith(relocation.getValue())) {
+ return relocation.getKey();
+ }
+ }
+ return "";
+ }
+
+ static class InputParameter {
+
+ private final String name;
+ private final Path jar;
+ private final Path map;
+
+ InputParameter(String name, Path jar, Path map) {
+ this.name = name;
+ this.jar = jar;
+ this.map = map;
+ }
+
+ DexApplication getReader(InternalOptions options, AndroidApp inputApp, Timing timing)
+ throws Exception {
+ ApplicationReader applicationReader = new ApplicationReader(inputApp, options, timing);
+ return applicationReader.read(map == null ? null : StringResource.fromFile(map)).toDirect();
+ }
+
+ AndroidApp getInputApp(List<Path> libraries) throws Exception {
+ return AndroidApp.builder().addLibraryFiles(libraries).addProgramFiles(jar).build();
+ }
+ }
+
+ static class InputApplication {
+
+ private final DexApplication dexApplication;
+ private final Map<String, DexProgramClass> classMap;
+
+ private InputApplication(DexApplication dexApplication, Map<String, DexProgramClass> classMap) {
+ this.dexApplication = dexApplication;
+ this.classMap = classMap;
+ }
+
+ public Set<String> getClasses() {
+ return classMap.keySet();
+ }
+
+ private InputClass getInputClass(String type) {
+ DexProgramClass inputClass = classMap.get(type);
+ ClassNameMapper proguardMap = dexApplication.getProguardMap();
+ return new InputClass(inputClass, proguardMap);
+ }
+ }
+
+ static class InputClass {
+ private final DexProgramClass programClass;
+ private final ClassNameMapper proguardMap;
+
+ InputClass(DexClass dexClass, ClassNameMapper proguardMap) {
+ this.programClass = dexClass == null ? null : dexClass.asProgramClass();
+ this.proguardMap = proguardMap;
+ }
+
+ void forEachMethod(BiConsumer<MethodSignature, DexEncodedMethod> consumer) {
+ if (programClass == null) {
+ return;
+ }
+ programClass.forEachMethod(
+ dexEncodedMethod -> {
+ MethodSignature originalSignature =
+ proguardMap == null
+ ? null
+ : ((MethodSignature) proguardMap.originalSignatureOf(dexEncodedMethod.method));
+ MethodSignature signature = MethodSignature.fromDexMethod(dexEncodedMethod.method);
+ consumer.accept(
+ originalSignature == null ? signature : originalSignature, dexEncodedMethod);
+ });
+ }
+
+ void forEachField(BiConsumer<FieldSignature, DexEncodedField> consumer) {
+ if (programClass == null) {
+ return;
+ }
+ programClass.forEachField(
+ dexEncodedField -> {
+ FieldSignature originalSignature =
+ proguardMap == null ? null : proguardMap.originalSignatureOf(dexEncodedField.field);
+ FieldSignature signature = FieldSignature.fromDexField(dexEncodedField.field);
+ consumer.accept(
+ originalSignature == null ? signature : originalSignature, dexEncodedField);
+ });
+ }
+ }
+
+ private static class ClassCompare {
+ final Map<MethodSignature, DexEncodedMethod[]> methods = new HashMap<>();
+ final Map<FieldSignature, DexEncodedField[]> fields = new HashMap<>();
+ final int classes;
+
+ ClassCompare(InputClass[] inputs) {
+ for (int i = 0; i < inputs.length; i++) {
+ InputClass inputClass = inputs[i];
+ int finalI = i;
+ if (inputClass == null) {
+ continue;
+ }
+ inputClass.forEachMethod(
+ (sig, m) ->
+ methods.computeIfAbsent(sig, o -> new DexEncodedMethod[inputs.length])[finalI] = m);
+ inputClass.forEachField(
+ (sig, f) ->
+ fields.computeIfAbsent(sig, o -> new DexEncodedField[inputs.length])[finalI] = f);
+ }
+ classes = inputs.length;
+ }
+
+ List<MethodSignature> getMethods() {
+ List<MethodSignature> methods = new ArrayList<>(this.methods.keySet());
+ methods.sort(Comparator.comparing(MethodSignature::toString));
+ return methods;
+ }
+
+ List<FieldSignature> getFields() {
+ List<FieldSignature> fields = new ArrayList<>(this.fields.keySet());
+ fields.sort(Comparator.comparing(FieldSignature::toString));
+ return fields;
+ }
+
+ int size(MethodSignature method, int classIndex) {
+ DexEncodedMethod dexEncodedMethod = methods.get(method)[classIndex];
+ if (dexEncodedMethod == null) {
+ return -1;
+ }
+ Code code = dexEncodedMethod.getCode();
+ if (code == null) {
+ return 0;
+ }
+ if (code.isCfCode()) {
+ return code.asCfCode().getInstructions().size();
+ }
+ if (code.isDexCode()) {
+ return code.asDexCode().instructions.length;
+ }
+ throw new Unreachable();
+ }
+
+ int[] sizes(MethodSignature method) {
+ int[] result = new int[classes];
+ for (int i = 0; i < classes; i++) {
+ result[i] = size(method, i);
+ }
+ return result;
+ }
+
+ int size(FieldSignature field, int classIndex) {
+ return fields.get(field)[classIndex] == null ? -1 : 1;
+ }
+
+ int[] sizes(FieldSignature field) {
+ int[] result = new int[classes];
+ for (int i = 0; i < classes; i++) {
+ result[i] = size(field, i);
+ }
+ return result;
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+ JarSizeCompare program = JarSizeCompare.parse(args);
+ if (program == null) {
+ System.out.println(USAGE);
+ } else {
+ program.run();
+ }
+ }
+
+ public static JarSizeCompare parse(String[] args) {
+ int i = 0;
+ int threshold = 0;
+ List<Path> libraries = new ArrayList<>();
+ List<InputParameter> inputs = new ArrayList<>();
+ Set<String> names = new HashSet<>();
+ while (i < args.length) {
+ if (args[i].equals("--threshold") && i + 1 < args.length) {
+ threshold = Integer.parseInt(args[i + 1]);
+ i += 2;
+ } else if (args[i].equals("--lib") && i + 1 < args.length) {
+ libraries.add(Paths.get(args[i + 1]));
+ i += 2;
+ } else if (args[i].equals("--input") && i + 2 < args.length) {
+ String name = args[i + 1];
+ Path jar = Paths.get(args[i + 2]);
+ Path map = null;
+ if (i + 3 < args.length && !args[i + 3].startsWith("-")) {
+ map = Paths.get(args[i + 3]);
+ i += 4;
+ } else {
+ i += 3;
+ }
+ inputs.add(new InputParameter(name, jar, map));
+ if (!names.add(name)) {
+ System.out.println("Duplicate name: " + name);
+ return null;
+ }
+ } else {
+ return null;
+ }
+ }
+ if (inputs.size() < 2) {
+ return null;
+ }
+ if (threshold != 0 && (!names.contains("r8") || !names.contains("pg"))) {
+ System.out.println(
+ "You must either specify names \"pg\" and \"r8\" for input files "
+ + "or use \"--threshold 0\".");
+ return null;
+ }
+ return new JarSizeCompare(libraries, inputs, threshold);
+ }
+}
diff --git a/src/main/java/com/android/tools/r8/PrintSeeds.java b/src/main/java/com/android/tools/r8/PrintSeeds.java
index b1bc7ca..782c91d 100644
--- a/src/main/java/com/android/tools/r8/PrintSeeds.java
+++ b/src/main/java/com/android/tools/r8/PrintSeeds.java
@@ -20,17 +20,32 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
+/**
+ * PrintSeeds prints the classes, interfaces, methods and fields selected by a given ProGuard
+ * configuration <pg-conf.txt> when compiling a given program <r8.jar> alongside a given
+ * library <rt.jar>.
+ *
+ * <p>The output format is identical to what is printed when {@code -printseeds} is specified in
+ * <pg-conf.txt>, but running PrintSeeds can be faster than running R8 with {@code
+ * -printseeds}. See also the {@link PrintUses} program in R8.
+ */
public class PrintSeeds {
private static final String USAGE =
"Arguments: <rt.jar> <r8.jar> <pg-conf.txt>\n"
+ "\n"
+ "PrintSeeds prints the classes, interfaces, methods and fields selected by\n"
- + "<pg-conf.txt> when compiling <r8.jar> alongside <rt.jar>.";
+ + "<pg-conf.txt> when compiling <r8.jar> alongside <rt.jar>.\n"
+ + "\n"
+ + "The output format is identical to what is printed when -printseeds is specified in\n"
+ + "<pg-conf.txt>, but running PrintSeeds can be faster than running R8 with \n"
+ + "-printseeds. See also the "
+ + PrintUses.class.getSimpleName()
+ + " program in R8.";
public static void main(String[] args) throws Exception {
if (args.length != 3) {
- System.out.println(USAGE);
+ System.out.println(USAGE.replace("\n", System.lineSeparator()));
System.exit(1);
}
Path rtJar = Paths.get(args[0]);
diff --git a/src/main/java/com/android/tools/r8/PrintUses.java b/src/main/java/com/android/tools/r8/PrintUses.java
index 0e69de6..c762182 100644
--- a/src/main/java/com/android/tools/r8/PrintUses.java
+++ b/src/main/java/com/android/tools/r8/PrintUses.java
@@ -31,6 +31,17 @@
import java.util.Map;
import java.util.Set;
+/**
+ * PrintUses prints the classes, interfaces, methods and fields used by a given program
+ * <sample.jar>, restricted to classes and interfaces in a given library <r8.jar> that
+ * are not in <sample.jar>.
+ *
+ * <p>The output is in the same format as what is printed when specifying {@code -printseeds} in a
+ * ProGuard configuration file. See also the {@link PrintSeeds} program in R8.
+ *
+ * <p>Note that this tool is not related to the {@code -printusage} option of ProGuard configuration
+ * files.
+ */
public class PrintUses {
private static final String USAGE =
@@ -38,7 +49,12 @@
+ "\n"
+ "PrintUses prints the classes, interfaces, methods and fields used by <sample.jar>,\n"
+ "restricted to classes and interfaces in <r8.jar> that are not in <sample.jar>.\n"
- + "<rt.jar> and <r8.jar> should point to libraries used by <sample.jar>.";
+ + "<rt.jar> and <r8.jar> should point to libraries used by <sample.jar>.\n"
+ + "\n"
+ + "The output is in the same format as what is printed when specifying -printseeds in\n"
+ + "a ProGuard configuration file. See also the "
+ + PrintSeeds.class.getSimpleName()
+ + " program in R8.";
private final Set<String> descriptors;
private final PrintStream out;
@@ -191,7 +207,7 @@
public static void main(String[] args) throws Exception {
if (args.length != 3) {
- System.out.println(USAGE);
+ System.out.println(USAGE.replace("\n", System.lineSeparator()));
return;
}
AndroidApp.Builder builder = AndroidApp.builder();
diff --git a/src/main/java/com/android/tools/r8/R8.java b/src/main/java/com/android/tools/r8/R8.java
index e42d8b0..3239dbe 100644
--- a/src/main/java/com/android/tools/r8/R8.java
+++ b/src/main/java/com/android/tools/r8/R8.java
@@ -11,7 +11,6 @@
import com.android.tools.r8.dex.Marker.Tool;
import com.android.tools.r8.errors.CompilationError;
import com.android.tools.r8.graph.AppInfoWithSubtyping;
-import com.android.tools.r8.graph.ClassAndMemberPublicizer;
import com.android.tools.r8.graph.DexApplication;
import com.android.tools.r8.graph.DexProgramClass;
import com.android.tools.r8.graph.DexType;
@@ -28,6 +27,7 @@
import com.android.tools.r8.naming.ProguardMapSupplier;
import com.android.tools.r8.naming.SeedMapper;
import com.android.tools.r8.naming.SourceFileRewriter;
+import com.android.tools.r8.optimize.ClassAndMemberPublicizer;
import com.android.tools.r8.optimize.MemberRebindingAnalysis;
import com.android.tools.r8.optimize.VisibilityBridgeRemover;
import com.android.tools.r8.origin.CommandLineOrigin;
@@ -344,11 +344,11 @@
.prunedCopyFrom(application, classMerger.getRemovedClasses());
}
if (options.proguardConfiguration.hasApplyMappingFile()) {
- SeedMapper seedMapper = SeedMapper.seedMapperFromFile(
- options.proguardConfiguration.getApplyMappingFile());
+ SeedMapper seedMapper =
+ SeedMapper.seedMapperFromFile(options.proguardConfiguration.getApplyMappingFile());
timing.begin("apply-mapping");
- graphLense = new ProguardMapApplier(appInfo.withLiveness(), graphLense, seedMapper)
- .run(timing);
+ graphLense =
+ new ProguardMapApplier(appInfo.withLiveness(), graphLense, seedMapper).run(timing);
timing.end();
}
application = application.asDirect().rewrittenWithLense(graphLense);
diff --git a/src/main/java/com/android/tools/r8/SwissArmyKnife.java b/src/main/java/com/android/tools/r8/SwissArmyKnife.java
index 28e1f93..f54233b 100644
--- a/src/main/java/com/android/tools/r8/SwissArmyKnife.java
+++ b/src/main/java/com/android/tools/r8/SwissArmyKnife.java
@@ -61,6 +61,9 @@
case "jardiff":
JarDiff.main(shift(args));
break;
+ case "jarsizecompare":
+ JarSizeCompare.main(shift(args));
+ break;
case "maindex":
GenerateMainDexList.main(shift(args));
break;
diff --git a/src/main/java/com/android/tools/r8/UsageInformationConsumer.java b/src/main/java/com/android/tools/r8/UsageInformationConsumer.java
deleted file mode 100644
index a572908..0000000
--- a/src/main/java/com/android/tools/r8/UsageInformationConsumer.java
+++ /dev/null
@@ -1,133 +0,0 @@
-// Copyright (c) 2017, 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;
-
-import com.android.tools.r8.origin.Origin;
-import com.android.tools.r8.origin.PathOrigin;
-import com.android.tools.r8.utils.ExceptionDiagnostic;
-import com.android.tools.r8.utils.FileUtils;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.nio.file.Path;
-
-/**
- * Interface for receiving usage feedback from R8.
- *
- * The data is in the format defined for Proguard's <code>-printusage</code> flag. The information
- * will be produced if a consumer is provided. A consumer is automatically setup when running R8
- * with the Proguard <code>-printusage</code> flag set.
- */
-public interface UsageInformationConsumer {
-
- /**
- * Callback to receive the usage-information data.
- *
- * <p>The consumer is expected not to throw, but instead report any errors via the diagnostics
- * {@param handler}. If an error is reported via {@param handler} and no exceptions are thrown,
- * then the compiler guaranties to exit with an error.
- *
- * @param data UTF-8 encoded usage information.
- * @param handler Diagnostics handler for reporting.
- */
- void acceptUsageInformation(byte[] data, DiagnosticsHandler handler);
-
- static EmptyConsumer emptyConsumer() {
- return EmptyConsumer.EMPTY_CONSUMER;
- }
-
- /** Empty consumer to request usage information but ignore the result. */
- class EmptyConsumer implements UsageInformationConsumer {
-
- private static final EmptyConsumer EMPTY_CONSUMER = new EmptyConsumer();
-
- private EmptyConsumer() {}
-
- @Override
- public void acceptUsageInformation(byte[] data, DiagnosticsHandler handler) {
- // Ignore content.
- }
- }
-
- /** Forwarding consumer to delegate to an optional existing consumer. */
- class ForwardingConsumer implements UsageInformationConsumer {
-
- private final UsageInformationConsumer consumer;
-
- /** @param consumer Consumer to forward to, if null, nothing will be forwarded. */
- public ForwardingConsumer(UsageInformationConsumer consumer) {
- this.consumer = consumer;
- }
-
- @Override
- public void acceptUsageInformation(byte[] data, DiagnosticsHandler handler) {
- if (consumer != null) {
- consumer.acceptUsageInformation(data, handler);
- }
- }
- }
-
- /** File consumer to write contents to a file-system file. */
- class FileConsumer extends ForwardingConsumer {
-
- private final Path outputPath;
-
- /** Consumer that writes to {@param outputPath}. */
- public FileConsumer(Path outputPath) {
- this(outputPath, null);
- }
-
- /** Consumer that forwards to {@param consumer} and also writes to {@param outputPath}. */
- public FileConsumer(Path outputPath, UsageInformationConsumer consumer) {
- super(consumer);
- this.outputPath = outputPath;
- }
-
- @Override
- public void acceptUsageInformation(byte[] data, DiagnosticsHandler handler) {
- super.acceptUsageInformation(data, handler);
- try {
- FileUtils.writeToFile(outputPath, null, data);
- } catch (IOException e) {
- Origin origin = new PathOrigin(outputPath);
- handler.error(new ExceptionDiagnostic(e, origin));
- }
- }
- }
-
- /**
- * Stream consumer to write contents to an output stream.
- *
- * <p>Note: No close events are given to this stream so it should either be a permanent stream or
- * the closing needs to happen outside of the compilation itself. If the stream is not one of the
- * standard streams, i.e., System.out or System.err, you should likely implement yor own consumer.
- */
- class StreamConsumer extends ForwardingConsumer {
-
- private final Origin origin;
- private final OutputStream outputStream;
-
- /** Consumer that writes to {@param outputStream}. */
- public StreamConsumer(Origin origin, OutputStream outputStream) {
- this(origin, outputStream, null);
- }
-
- /** Consumer that forwards to {@param consumer} and also writes to {@param outputStream}. */
- public StreamConsumer(
- Origin origin, OutputStream outputStream, UsageInformationConsumer consumer) {
- super(consumer);
- this.origin = origin;
- this.outputStream = outputStream;
- }
-
- @Override
- public void acceptUsageInformation(byte[] data, DiagnosticsHandler handler) {
- super.acceptUsageInformation(data, handler);
- try {
- outputStream.write(data);
- } catch (IOException e) {
- handler.error(new ExceptionDiagnostic(e, origin));
- }
- }
- }
-}
diff --git a/src/main/java/com/android/tools/r8/Version.java b/src/main/java/com/android/tools/r8/Version.java
index fb3b067..1783f30 100644
--- a/src/main/java/com/android/tools/r8/Version.java
+++ b/src/main/java/com/android/tools/r8/Version.java
@@ -11,7 +11,7 @@
// This field is accessed from release scripts using simple pattern matching.
// Therefore, changing this field could break our release scripts.
- public static final String LABEL = "1.2.21-dev";
+ public static final String LABEL = "1.2.23-dev";
private Version() {
}
diff --git a/src/main/java/com/android/tools/r8/compatdexbuilder/CompatDexBuilder.java b/src/main/java/com/android/tools/r8/compatdexbuilder/CompatDexBuilder.java
index 90b863c..2621979 100644
--- a/src/main/java/com/android/tools/r8/compatdexbuilder/CompatDexBuilder.java
+++ b/src/main/java/com/android/tools/r8/compatdexbuilder/CompatDexBuilder.java
@@ -17,6 +17,7 @@
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
@@ -127,7 +128,7 @@
List<ZipEntry> toDex = new ArrayList<>();
- try (ZipFile zipFile = new ZipFile(input)) {
+ try (ZipFile zipFile = new ZipFile(input, StandardCharsets.UTF_8)) {
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
diff --git a/src/main/java/com/android/tools/r8/compatdx/CompatDx.java b/src/main/java/com/android/tools/r8/compatdx/CompatDx.java
index f512a34..871f6d8 100644
--- a/src/main/java/com/android/tools/r8/compatdx/CompatDx.java
+++ b/src/main/java/com/android/tools/r8/compatdx/CompatDx.java
@@ -34,6 +34,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -572,7 +573,7 @@
// For each input archive file, add all class files within.
for (Path input : inputs) {
if (isArchive(input)) {
- try (ZipFile zipFile = new ZipFile(input.toFile())) {
+ try (ZipFile zipFile = new ZipFile(input.toFile(), StandardCharsets.UTF_8)) {
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
diff --git a/src/main/java/com/android/tools/r8/graph/AssemblyWriter.java b/src/main/java/com/android/tools/r8/graph/AssemblyWriter.java
index 89abadd..23dbfbf 100644
--- a/src/main/java/com/android/tools/r8/graph/AssemblyWriter.java
+++ b/src/main/java/com/android/tools/r8/graph/AssemblyWriter.java
@@ -3,9 +3,15 @@
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.graph;
+import com.android.tools.r8.ClassFileConsumer;
+import com.android.tools.r8.ir.conversion.IRConverter;
+import com.android.tools.r8.ir.conversion.OptimizationFeedback;
+import com.android.tools.r8.ir.conversion.OptimizationFeedbackIgnore;
import com.android.tools.r8.naming.ClassNameMapper;
import com.android.tools.r8.naming.MemberNaming.FieldSignature;
+import com.android.tools.r8.utils.CfgPrinter;
import com.android.tools.r8.utils.InternalOptions;
+import com.android.tools.r8.utils.Timing;
import java.io.PrintStream;
public class AssemblyWriter extends DexByteCodeWriter {
@@ -13,12 +19,29 @@
private final boolean writeAllClassInfo;
private final boolean writeFields;
private final boolean writeAnnotations;
+ private final boolean writeIR;
+ private final AppInfoWithSubtyping appInfo;
+ private final Timing timing = new Timing("AssemblyWriter");
+ private final OptimizationFeedback ignoreOptimizationFeedback = new OptimizationFeedbackIgnore();
- public AssemblyWriter(DexApplication application, InternalOptions options, boolean allInfo) {
+ public AssemblyWriter(
+ DexApplication application, InternalOptions options, boolean allInfo, boolean writeIR) {
super(application, options);
this.writeAllClassInfo = allInfo;
this.writeFields = allInfo;
this.writeAnnotations = allInfo;
+ this.writeIR = writeIR;
+ if (writeIR) {
+ this.appInfo = new AppInfoWithSubtyping(application.toDirect());
+ if (options.programConsumer == null) {
+ // Use class-file backend, since the CF frontend for testing does not support desugaring of
+ // synchronized methods for the DEX backend (b/109789541).
+ options.programConsumer = ClassFileConsumer.emptyConsumer();
+ }
+ options.outline.enabled = false;
+ } else {
+ this.appInfo = null;
+ }
}
@Override
@@ -88,10 +111,21 @@
ps.println();
Code code = method.getCode();
if (code != null) {
- ps.println(code.toString(method, naming));
+ if (writeIR) {
+ writeIR(method, ps);
+ } else {
+ ps.println(code.toString(method, naming));
+ }
}
}
+ private void writeIR(DexEncodedMethod method, PrintStream ps) {
+ CfgPrinter printer = new CfgPrinter();
+ new IRConverter(appInfo, options, timing, printer)
+ .processMethod(method, ignoreOptimizationFeedback, null, null, null);
+ ps.println(printer.toString());
+ }
+
private void writeAnnotations(DexAnnotationSet annotations, PrintStream ps) {
if (writeAnnotations) {
if (!annotations.isEmpty()) {
diff --git a/src/main/java/com/android/tools/r8/graph/DexEncodedMethod.java b/src/main/java/com/android/tools/r8/graph/DexEncodedMethod.java
index 831edbe..026c2b5 100644
--- a/src/main/java/com/android/tools/r8/graph/DexEncodedMethod.java
+++ b/src/main/java/com/android/tools/r8/graph/DexEncodedMethod.java
@@ -790,6 +790,7 @@
private Code code;
private CompilationState compilationState = CompilationState.NOT_PROCESSED;
private OptimizationInfo optimizationInfo = DefaultOptimizationInfo.DEFAULT;
+ private final int classFileVersion;
private Builder(DexEncodedMethod from) {
// Copy all the mutable state of a DexEncodedMethod here.
@@ -800,6 +801,7 @@
code = from.code;
compilationState = from.compilationState;
optimizationInfo = from.optimizationInfo.copy();
+ classFileVersion = from.classFileVersion;
}
public void setMethod(DexMethod method) {
@@ -816,7 +818,8 @@
assert annotations != null;
assert parameterAnnotations != null;
DexEncodedMethod result =
- new DexEncodedMethod(method, accessFlags, annotations, parameterAnnotations, code);
+ new DexEncodedMethod(
+ method, accessFlags, annotations, parameterAnnotations, code, classFileVersion);
result.compilationState = compilationState;
result.optimizationInfo = optimizationInfo;
return result;
diff --git a/src/main/java/com/android/tools/r8/graph/DexMethodHandle.java b/src/main/java/com/android/tools/r8/graph/DexMethodHandle.java
index 06ea28d..cd3c093 100644
--- a/src/main/java/com/android/tools/r8/graph/DexMethodHandle.java
+++ b/src/main/java/com/android/tools/r8/graph/DexMethodHandle.java
@@ -6,6 +6,7 @@
import com.android.tools.r8.dex.Constants;
import com.android.tools.r8.dex.IndexedItemCollection;
import com.android.tools.r8.errors.Unreachable;
+import com.android.tools.r8.ir.code.Invoke.Type;
import com.android.tools.r8.naming.NamingLens;
import org.objectweb.asm.Handle;
import org.objectweb.asm.Opcodes;
@@ -159,6 +160,26 @@
public boolean isInvokeConstructor() {
return this == MethodHandleType.INVOKE_CONSTRUCTOR;
}
+
+ public Type toInvokeType() {
+ assert isMethodType();
+ switch (this) {
+ case INVOKE_STATIC:
+ return Type.STATIC;
+ case INVOKE_INSTANCE:
+ return Type.VIRTUAL;
+ case INVOKE_CONSTRUCTOR:
+ return Type.DIRECT;
+ case INVOKE_DIRECT:
+ return Type.DIRECT;
+ case INVOKE_INTERFACE:
+ return Type.INTERFACE;
+ case INVOKE_SUPER:
+ return Type.SUPER;
+ default:
+ throw new Unreachable("DexMethodHandle with unexpected type: " + this);
+ }
+ }
}
public MethodHandleType type;
diff --git a/src/main/java/com/android/tools/r8/graph/DexValue.java b/src/main/java/com/android/tools/r8/graph/DexValue.java
index ed99d25..2310891 100644
--- a/src/main/java/com/android/tools/r8/graph/DexValue.java
+++ b/src/main/java/com/android/tools/r8/graph/DexValue.java
@@ -557,7 +557,7 @@
@Override
public int hashCode() {
- return (int) value * 19;
+ return (int) (value * 19);
}
@Override
@@ -615,7 +615,7 @@
@Override
public int hashCode() {
- return (int) value * 29;
+ return (int) (value * 29);
}
@Override
diff --git a/src/main/java/com/android/tools/r8/graph/DirectMappedDexApplication.java b/src/main/java/com/android/tools/r8/graph/DirectMappedDexApplication.java
index 8d5f0d0..e4f1c8c 100644
--- a/src/main/java/com/android/tools/r8/graph/DirectMappedDexApplication.java
+++ b/src/main/java/com/android/tools/r8/graph/DirectMappedDexApplication.java
@@ -69,7 +69,6 @@
}
public DirectMappedDexApplication rewrittenWithLense(GraphLense graphLense) {
- assert graphLense.isContextFree();
assert mappingIsValid(graphLense, programClasses.getAllTypes());
assert mappingIsValid(graphLense, libraryClasses.keySet());
// As a side effect, this will rebuild the program classes and library classes maps.
@@ -81,7 +80,7 @@
// (e.g. relinking a type) or it might encode a type that was renamed, in which case the
// original type will point to a definition that was renamed.
for (DexType type : types) {
- DexType renamed = graphLense.lookupType(type, null);
+ DexType renamed = graphLense.lookupType(type);
if (renamed != type) {
if (definitionFor(type).type != renamed && definitionFor(renamed) == null) {
return false;
diff --git a/src/main/java/com/android/tools/r8/graph/GraphLense.java b/src/main/java/com/android/tools/r8/graph/GraphLense.java
index ed4106e..096de5a 100644
--- a/src/main/java/com/android/tools/r8/graph/GraphLense.java
+++ b/src/main/java/com/android/tools/r8/graph/GraphLense.java
@@ -3,8 +3,12 @@
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.graph;
+import com.android.tools.r8.ir.code.Invoke.Type;
+import com.google.common.collect.ImmutableSet;
+import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Map;
+import java.util.Set;
/**
* A GraphLense implements a virtual view on top of the graph, used to delay global rewrites until
@@ -59,13 +63,33 @@
return new Builder();
}
- public abstract DexType lookupType(DexType type, DexEncodedMethod context);
+ public abstract DexType lookupType(DexType type);
- public abstract DexMethod lookupMethod(DexMethod method, DexEncodedMethod context);
+ // This overload can be used when the graph lense is known to be context insensitive.
+ public DexMethod lookupMethod(DexMethod method) {
+ assert isContextFreeForMethod(method);
+ return lookupMethod(method, null, null);
+ }
- public abstract DexField lookupField(DexField field, DexEncodedMethod context);
+ public abstract DexMethod lookupMethod(DexMethod method, DexEncodedMethod context, Type type);
- public abstract boolean isContextFree();
+ // Context sensitive graph lenses should override this method.
+ public Set<DexMethod> lookupMethodInAllContexts(DexMethod method) {
+ assert isContextFreeForMethod(method);
+ DexMethod result = lookupMethod(method);
+ if (result != null) {
+ return ImmutableSet.of(result);
+ }
+ return ImmutableSet.of();
+ }
+
+ public abstract DexField lookupField(DexField field);
+
+ public abstract boolean isContextFreeForMethods();
+
+ public boolean isContextFreeForMethod(DexMethod method) {
+ return isContextFreeForMethods();
+ }
public static GraphLense getIdentityLense() {
return new IdentityGraphLense();
@@ -78,22 +102,22 @@
private static class IdentityGraphLense extends GraphLense {
@Override
- public DexType lookupType(DexType type, DexEncodedMethod context) {
+ public DexType lookupType(DexType type) {
return type;
}
@Override
- public DexMethod lookupMethod(DexMethod method, DexEncodedMethod context) {
+ public DexMethod lookupMethod(DexMethod method, DexEncodedMethod context, Type type) {
return method;
}
@Override
- public DexField lookupField(DexField field, DexEncodedMethod context) {
+ public DexField lookupField(DexField field) {
return field;
}
@Override
- public boolean isContextFree() {
+ public boolean isContextFreeForMethods() {
return true;
}
}
@@ -118,14 +142,14 @@
}
@Override
- public DexType lookupType(DexType type, DexEncodedMethod context) {
+ public DexType lookupType(DexType type) {
if (type.isArrayType()) {
synchronized (this) {
// This block need to be synchronized due to arrayTypeCache.
DexType result = arrayTypeCache.get(type);
if (result == null) {
DexType baseType = type.toBaseType(dexItemFactory);
- DexType newType = lookupType(baseType, context);
+ DexType newType = lookupType(baseType);
if (baseType == newType) {
result = type;
} else {
@@ -136,25 +160,39 @@
return result;
}
}
- DexType previous = previousLense.lookupType(type, context);
+ DexType previous = previousLense.lookupType(type);
return typeMap.getOrDefault(previous, previous);
}
@Override
- public DexMethod lookupMethod(DexMethod method, DexEncodedMethod context) {
- DexMethod previous = previousLense.lookupMethod(method, context);
+ public DexMethod lookupMethod(DexMethod method, DexEncodedMethod context, Type type) {
+ DexMethod previous = previousLense.lookupMethod(method, context, type);
return methodMap.getOrDefault(previous, previous);
}
@Override
- public DexField lookupField(DexField field, DexEncodedMethod context) {
- DexField previous = previousLense.lookupField(field, context);
+ public Set<DexMethod> lookupMethodInAllContexts(DexMethod method) {
+ Set<DexMethod> result = new HashSet<>();
+ for (DexMethod previous : previousLense.lookupMethodInAllContexts(method)) {
+ result.add(methodMap.getOrDefault(previous, previous));
+ }
+ return result;
+ }
+
+ @Override
+ public DexField lookupField(DexField field) {
+ DexField previous = previousLense.lookupField(field);
return fieldMap.getOrDefault(previous, previous);
}
@Override
- public boolean isContextFree() {
- return previousLense.isContextFree();
+ public boolean isContextFreeForMethods() {
+ return previousLense.isContextFreeForMethods();
+ }
+
+ @Override
+ public boolean isContextFreeForMethod(DexMethod method) {
+ return previousLense.isContextFreeForMethod(method);
}
@Override
diff --git a/src/main/java/com/android/tools/r8/ir/code/BasicBlock.java b/src/main/java/com/android/tools/r8/ir/code/BasicBlock.java
index 113d522..8771b72 100644
--- a/src/main/java/com/android/tools/r8/ir/code/BasicBlock.java
+++ b/src/main/java/com/android/tools/r8/ir/code/BasicBlock.java
@@ -9,7 +9,9 @@
import com.android.tools.r8.errors.Unreachable;
import com.android.tools.r8.graph.DebugLocalInfo;
import com.android.tools.r8.graph.DebugLocalInfo.PrintLevel;
+import com.android.tools.r8.graph.DexItemFactory;
import com.android.tools.r8.graph.DexType;
+import com.android.tools.r8.graph.GraphLense;
import com.android.tools.r8.ir.conversion.DexBuilder;
import com.android.tools.r8.ir.conversion.IRBuilder;
import com.android.tools.r8.utils.CfgPrinter;
@@ -20,10 +22,13 @@
import com.google.common.base.Equivalence.Wrapper;
import com.google.common.collect.ImmutableList;
import it.unimi.dsi.fastutil.ints.Int2ReferenceMap;
+import it.unimi.dsi.fastutil.ints.IntArrayList;
+import it.unimi.dsi.fastutil.ints.IntList;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
@@ -173,7 +178,7 @@
public void removeSuccessor(BasicBlock block) {
int index = successors.indexOf(block);
assert index >= 0 : "removeSuccessor did not find the successor to remove";
- removeSuccessorsByIndex(Collections.singletonList(index));
+ removeSuccessorsByIndex(new IntArrayList(new int[] {index}));
}
public void removePredecessor(BasicBlock block) {
@@ -328,7 +333,7 @@
assert false : "replaceSuccessor did not find the predecessor to replace";
}
- public void removeSuccessorsByIndex(List<Integer> successorsToRemove) {
+ public void removeSuccessorsByIndex(IntList successorsToRemove) {
if (successorsToRemove.isEmpty()) {
return;
}
@@ -678,6 +683,44 @@
catchHandlers = new CatchHandlers<>(guards, successorIndexes);
}
+ // Due to class merging, it is possible that two exception classes have been merged into one.
+ // This function renames the guards according to the given graph lense.
+ public void renameGuardsInCatchHandlers(GraphLense graphLense) {
+ assert hasCatchHandlers();
+ List<DexType> newGuards = new ArrayList<>(catchHandlers.getGuards().size());
+ for (DexType guard : catchHandlers.getGuards()) {
+ // The type may have changed due to class merging.
+ newGuards.add(graphLense.lookupType(guard));
+ }
+ this.catchHandlers = new CatchHandlers<>(newGuards, catchHandlers.getAllTargets());
+ }
+
+ public boolean consistentCatchHandlers() {
+ // Check that catch handlers are always the first successors of a block.
+ if (hasCatchHandlers()) {
+ assert exit().isGoto() || exit().isThrow();
+ CatchHandlers<Integer> catchHandlers = getCatchHandlersWithSuccessorIndexes();
+ // If there is a catch-all guard it must be the last.
+ List<DexType> guards = catchHandlers.getGuards();
+ int lastGuardIndex = guards.size() - 1;
+ for (int i = 0; i < guards.size(); i++) {
+ assert guards.get(i) != DexItemFactory.catchAllType || i == lastGuardIndex;
+ }
+ // Check that all successors except maybe the last are catch successors.
+ List<Integer> sortedHandlerIndices = new ArrayList<>(catchHandlers.getAllTargets());
+ sortedHandlerIndices.sort(Comparator.naturalOrder());
+ int firstIndex = sortedHandlerIndices.get(0);
+ int lastIndex = sortedHandlerIndices.get(sortedHandlerIndices.size() - 1);
+ assert firstIndex == 0;
+ assert lastIndex < sortedHandlerIndices.size();
+ int lastSuccessorIndex = getSuccessors().size() - 1;
+ assert lastIndex == lastSuccessorIndex // All successors are catch successors.
+ || lastIndex == lastSuccessorIndex - 1; // All but one successors are catch successors.
+ assert lastIndex == lastSuccessorIndex || !exit().isThrow();
+ }
+ return true;
+ }
+
public void clearCurrentDefinitions() {
currentDefinitions = null;
for (Phi phi : getPhis()) {
diff --git a/src/main/java/com/android/tools/r8/ir/code/IRCode.java b/src/main/java/com/android/tools/r8/ir/code/IRCode.java
index a5d3e4a..c832991 100644
--- a/src/main/java/com/android/tools/r8/ir/code/IRCode.java
+++ b/src/main/java/com/android/tools/r8/ir/code/IRCode.java
@@ -5,8 +5,6 @@
import com.android.tools.r8.graph.DebugLocalInfo;
import com.android.tools.r8.graph.DexEncodedMethod;
-import com.android.tools.r8.graph.DexItemFactory;
-import com.android.tools.r8.graph.DexType;
import com.android.tools.r8.utils.CfgPrinter;
import com.android.tools.r8.utils.InternalOptions;
import com.google.common.collect.ImmutableList;
@@ -473,28 +471,7 @@
private boolean consistentCatchHandlers() {
for (BasicBlock block : blocks) {
- // Check that catch handlers are always the first successors of a block.
- if (block.hasCatchHandlers()) {
- assert block.exit().isGoto() || block.exit().isThrow();
- CatchHandlers<Integer> catchHandlers = block.getCatchHandlersWithSuccessorIndexes();
- // If there is a catch-all guard it must be the last.
- List<DexType> guards = catchHandlers.getGuards();
- int lastGuardIndex = guards.size() - 1;
- for (int i = 0; i < guards.size(); i++) {
- assert guards.get(i) != DexItemFactory.catchAllType || i == lastGuardIndex;
- }
- // Check that all successors except maybe the last are catch successors.
- List<Integer> sortedHandlerIndices = new ArrayList<>(catchHandlers.getAllTargets());
- sortedHandlerIndices.sort(Comparator.naturalOrder());
- int firstIndex = sortedHandlerIndices.get(0);
- int lastIndex = sortedHandlerIndices.get(sortedHandlerIndices.size() - 1);
- assert firstIndex == 0;
- assert lastIndex < sortedHandlerIndices.size();
- int lastSuccessorIndex = block.getSuccessors().size() - 1;
- assert lastIndex == lastSuccessorIndex // All successors are catch successors.
- || lastIndex == lastSuccessorIndex - 1; // All but one successors are catch successors.
- assert lastIndex == lastSuccessorIndex || !block.exit().isThrow();
- }
+ assert block.consistentCatchHandlers();
}
return true;
}
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/CallGraph.java b/src/main/java/com/android/tools/r8/ir/conversion/CallGraph.java
index 8330e06..21b8dc4 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/CallGraph.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/CallGraph.java
@@ -363,7 +363,7 @@
private void processInvoke(Type type, DexMethod method) {
DexEncodedMethod source = caller.method;
- method = graphLense.lookupMethod(method, source);
+ method = graphLense.lookupMethod(method, source, type);
DexEncodedMethod definition = appInfo.lookup(type, method, source.method.holder);
if (definition != null) {
assert !source.accessFlags.isBridge() || definition != caller.method;
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/CfBuilder.java b/src/main/java/com/android/tools/r8/ir/conversion/CfBuilder.java
index e798fd2..de86cb4 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/CfBuilder.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/CfBuilder.java
@@ -22,6 +22,7 @@
import com.android.tools.r8.graph.DexField;
import com.android.tools.r8.graph.DexItemFactory;
import com.android.tools.r8.graph.DexType;
+import com.android.tools.r8.graph.GraphLense;
import com.android.tools.r8.ir.code.Argument;
import com.android.tools.r8.ir.code.BasicBlock;
import com.android.tools.r8.ir.code.CatchHandlers;
@@ -118,13 +119,16 @@
}
public CfCode build(
- CodeRewriter rewriter, InternalOptions options, AppInfoWithSubtyping appInfo) {
+ CodeRewriter rewriter,
+ GraphLense graphLense,
+ InternalOptions options,
+ AppInfoWithSubtyping appInfo) {
computeInitializers();
types = new TypeVerificationHelper(code, factory, appInfo).computeVerificationTypes();
splitExceptionalBlocks();
LoadStoreHelper loadStoreHelper = new LoadStoreHelper(code, types);
loadStoreHelper.insertLoadsAndStores();
- DeadCodeRemover.removeDeadCode(code, rewriter, options);
+ DeadCodeRemover.removeDeadCode(code, rewriter, graphLense, options);
removeUnneededLoadsAndStores();
registerAllocator = new CfRegisterAllocator(code, options);
registerAllocator.allocateRegisters();
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/CfSourceCode.java b/src/main/java/com/android/tools/r8/ir/conversion/CfSourceCode.java
index ca79b0f..309e1d0 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/CfSourceCode.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/CfSourceCode.java
@@ -367,6 +367,13 @@
}
setLocalVariableLists();
readEndingLocals(builder);
+ if (currentBlockInfo != null && instruction.canThrow()) {
+ Snapshot exceptionTransfer =
+ state.getSnapshot().exceptionTransfer(builder.getFactory().throwableType);
+ for (int target : currentBlockInfo.exceptionalSuccessors) {
+ recordStateForTarget(target, exceptionTransfer);
+ }
+ }
if (isControlFlow(instruction)) {
ensureDebugValueLivenessControl(builder);
instruction.buildIR(builder, state, this);
@@ -376,13 +383,6 @@
}
state.clear();
} else {
- if (currentBlockInfo != null && instruction.canThrow()) {
- Snapshot exceptionTransfer =
- state.getSnapshot().exceptionTransfer(builder.getFactory().throwableType);
- for (int target : currentBlockInfo.exceptionalSuccessors) {
- recordStateForTarget(target, exceptionTransfer);
- }
- }
instruction.buildIR(builder, state, this);
ensureDebugValueLiveness(builder);
if (builder.getCFG().containsKey(currentInstructionIndex + 1)) {
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/IRConverter.java b/src/main/java/com/android/tools/r8/ir/conversion/IRConverter.java
index 81dc436..a52637f 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/IRConverter.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/IRConverter.java
@@ -482,7 +482,7 @@
// StringBuilder/StringBuffer method invocations, and removeDeadCode() to remove
// unused out-values.
codeRewriter.rewriteMoveResult(code);
- DeadCodeRemover.removeDeadCode(code, codeRewriter, options);
+ DeadCodeRemover.removeDeadCode(code, codeRewriter, graphLense, options);
consumer.accept(code, method);
return null;
}));
@@ -705,7 +705,7 @@
// Dead code removal. Performed after simplifications to remove code that becomes dead
// as a result of those simplifications. The following optimizations could reveal more
// dead code which is removed right before register allocation in performRegisterAllocation.
- DeadCodeRemover.removeDeadCode(code, codeRewriter, options);
+ DeadCodeRemover.removeDeadCode(code, codeRewriter, graphLense, options);
assert code.isConsistentSSA();
if (options.enableDesugaring && enableTryWithResourcesDesugaring()) {
@@ -776,7 +776,7 @@
private void finalizeToCf(DexEncodedMethod method, IRCode code, OptimizationFeedback feedback) {
assert !method.getCode().isDexCode();
CfBuilder builder = new CfBuilder(method, code, options.itemFactory);
- CfCode result = builder.build(codeRewriter, options, appInfo.withSubtyping());
+ CfCode result = builder.build(codeRewriter, graphLense, options, appInfo.withSubtyping());
method.setCode(result);
markProcessed(method, code, feedback);
}
@@ -818,7 +818,7 @@
private RegisterAllocator performRegisterAllocation(IRCode code, DexEncodedMethod method) {
// Always perform dead code elimination before register allocation. The register allocator
// does not allow dead code (to make sure that we do not waste registers for unneeded values).
- DeadCodeRemover.removeDeadCode(code, codeRewriter, options);
+ DeadCodeRemover.removeDeadCode(code, codeRewriter, graphLense, options);
materializeInstructionBeforeLongOperationsWorkaround(code, options);
LinearScanRegisterAllocator registerAllocator = new LinearScanRegisterAllocator(code, options);
registerAllocator.allocateRegisters(options.debug);
diff --git a/src/main/java/com/android/tools/r8/ir/conversion/LensCodeRewriter.java b/src/main/java/com/android/tools/r8/ir/conversion/LensCodeRewriter.java
index cdb419f..1926b14 100644
--- a/src/main/java/com/android/tools/r8/ir/conversion/LensCodeRewriter.java
+++ b/src/main/java/com/android/tools/r8/ir/conversion/LensCodeRewriter.java
@@ -96,7 +96,7 @@
if (!invokedHolder.isClassType()) {
continue;
}
- DexMethod actualTarget = graphLense.lookupMethod(invokedMethod, method);
+ DexMethod actualTarget = graphLense.lookupMethod(invokedMethod, method, invoke.getType());
Invoke.Type invokeType = getInvokeType(invoke, actualTarget, invokedMethod);
if (actualTarget != invokedMethod || invoke.getType() != invokeType) {
Invoke newInvoke = Invoke.create(invokeType, actualTarget, null,
@@ -107,10 +107,11 @@
&& newInvoke.outValue() != null) {
Value newValue = code.createValue(newInvoke.outType(), invoke.getLocalInfo());
newInvoke.outValue().replaceUsers(newValue);
- CheckCast cast = new CheckCast(
- newValue,
- newInvoke.outValue(),
- graphLense.lookupType(invokedMethod.proto.returnType, method));
+ CheckCast cast =
+ new CheckCast(
+ newValue,
+ newInvoke.outValue(),
+ graphLense.lookupType(invokedMethod.proto.returnType));
cast.setPosition(current.getPosition());
iterator.add(cast);
// If the current block has catch handlers split the check cast into its own block.
@@ -123,7 +124,7 @@
} else if (current.isInstanceGet()) {
InstanceGet instanceGet = current.asInstanceGet();
DexField field = instanceGet.getField();
- DexField actualField = graphLense.lookupField(field, method);
+ DexField actualField = graphLense.lookupField(field);
if (actualField != field) {
InstanceGet newInstanceGet =
new InstanceGet(
@@ -133,7 +134,7 @@
} else if (current.isInstancePut()) {
InstancePut instancePut = current.asInstancePut();
DexField field = instancePut.getField();
- DexField actualField = graphLense.lookupField(field, method);
+ DexField actualField = graphLense.lookupField(field);
if (actualField != field) {
InstancePut newInstancePut =
new InstancePut(
@@ -143,7 +144,7 @@
} else if (current.isStaticGet()) {
StaticGet staticGet = current.asStaticGet();
DexField field = staticGet.getField();
- DexField actualField = graphLense.lookupField(field, method);
+ DexField actualField = graphLense.lookupField(field);
if (actualField != field) {
StaticGet newStaticGet =
new StaticGet(staticGet.getType(), staticGet.dest(), actualField);
@@ -152,7 +153,7 @@
} else if (current.isStaticPut()) {
StaticPut staticPut = current.asStaticPut();
DexField field = staticPut.getField();
- DexField actualField = graphLense.lookupField(field, method);
+ DexField actualField = graphLense.lookupField(field);
if (actualField != field) {
StaticPut newStaticPut =
new StaticPut(staticPut.getType(), staticPut.inValue(), actualField);
@@ -160,7 +161,7 @@
}
} else if (current.isCheckCast()) {
CheckCast checkCast = current.asCheckCast();
- DexType newType = graphLense.lookupType(checkCast.getType(), method);
+ DexType newType = graphLense.lookupType(checkCast.getType());
if (newType != checkCast.getType()) {
CheckCast newCheckCast =
new CheckCast(makeOutValue(checkCast, code), checkCast.object(), newType);
@@ -168,14 +169,14 @@
}
} else if (current.isConstClass()) {
ConstClass constClass = current.asConstClass();
- DexType newType = graphLense.lookupType(constClass.getValue(), method);
+ DexType newType = graphLense.lookupType(constClass.getValue());
if (newType != constClass.getValue()) {
ConstClass newConstClass = new ConstClass(makeOutValue(constClass, code), newType);
iterator.replaceCurrentInstruction(newConstClass);
}
} else if (current.isInstanceOf()) {
InstanceOf instanceOf = current.asInstanceOf();
- DexType newType = graphLense.lookupType(instanceOf.type(), method);
+ DexType newType = graphLense.lookupType(instanceOf.type());
if (newType != instanceOf.type()) {
InstanceOf newInstanceOf = new InstanceOf(makeOutValue(instanceOf, code),
instanceOf.value(), newType);
@@ -183,7 +184,7 @@
}
} else if (current.isInvokeNewArray()) {
InvokeNewArray newArray = current.asInvokeNewArray();
- DexType newType = graphLense.lookupType(newArray.getArrayType(), method);
+ DexType newType = graphLense.lookupType(newArray.getArrayType());
if (newType != newArray.getArrayType()) {
InvokeNewArray newNewArray = new InvokeNewArray(newType, makeOutValue(newArray, code),
newArray.inValues());
@@ -191,7 +192,7 @@
}
} else if (current.isNewArrayEmpty()) {
NewArrayEmpty newArrayEmpty = current.asNewArrayEmpty();
- DexType newType = graphLense.lookupType(newArrayEmpty.type, method);
+ DexType newType = graphLense.lookupType(newArrayEmpty.type);
if (newType != newArrayEmpty.type) {
NewArrayEmpty newNewArray = new NewArrayEmpty(makeOutValue(newArrayEmpty, code),
newArrayEmpty.size(), newType);
@@ -199,7 +200,7 @@
}
} else if (current.isNewInstance()) {
NewInstance newInstance= current.asNewInstance();
- DexType newClazz = graphLense.lookupType(newInstance.clazz, method);
+ DexType newClazz = graphLense.lookupType(newInstance.clazz);
if (newClazz != newInstance.clazz) {
NewInstance newNewInstance =
new NewInstance(newClazz, makeOutValue(newInstance, code));
@@ -215,7 +216,8 @@
DexEncodedMethod method, DexMethodHandle methodHandle) {
if (methodHandle.isMethodHandle()) {
DexMethod invokedMethod = methodHandle.asMethod();
- DexMethod actualTarget = graphLense.lookupMethod(invokedMethod, method);
+ DexMethod actualTarget =
+ graphLense.lookupMethod(invokedMethod, method, methodHandle.type.toInvokeType());
if (actualTarget != invokedMethod) {
DexClass clazz = appInfo.definitionFor(actualTarget.holder);
MethodHandleType newType = methodHandle.type;
@@ -229,7 +231,7 @@
}
} else {
DexField field = methodHandle.asField();
- DexField actualField = graphLense.lookupField(field, method);
+ DexField actualField = graphLense.lookupField(field);
if (actualField != field) {
return new DexMethodHandle(methodHandle.type, actualField);
}
diff --git a/src/main/java/com/android/tools/r8/ir/optimize/DeadCodeRemover.java b/src/main/java/com/android/tools/r8/ir/optimize/DeadCodeRemover.java
index 531989e..8e587b9 100644
--- a/src/main/java/com/android/tools/r8/ir/optimize/DeadCodeRemover.java
+++ b/src/main/java/com/android/tools/r8/ir/optimize/DeadCodeRemover.java
@@ -3,6 +3,8 @@
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.ir.optimize;
+import com.android.tools.r8.graph.DexType;
+import com.android.tools.r8.graph.GraphLense;
import com.android.tools.r8.ir.code.BasicBlock;
import com.android.tools.r8.ir.code.CatchHandlers;
import com.android.tools.r8.ir.code.IRCode;
@@ -11,15 +13,19 @@
import com.android.tools.r8.ir.code.Phi;
import com.android.tools.r8.ir.code.Value;
import com.android.tools.r8.utils.InternalOptions;
+import java.util.ArrayList;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
+import java.util.List;
import java.util.Queue;
+import java.util.Set;
public class DeadCodeRemover {
public static void removeDeadCode(
- IRCode code, CodeRewriter codeRewriter, InternalOptions options) {
- removeUnneededCatchHandlers(code);
+ IRCode code, CodeRewriter codeRewriter, GraphLense graphLense, InternalOptions options) {
+ removeUnneededCatchHandlers(code, graphLense, options);
Queue<BasicBlock> worklist = new LinkedList<>();
worklist.addAll(code.blocks);
for (BasicBlock block = worklist.poll(); block != null; block = worklist.poll()) {
@@ -98,15 +104,49 @@
}
}
- private static void removeUnneededCatchHandlers(IRCode code) {
+ private static void removeUnneededCatchHandlers(
+ IRCode code, GraphLense graphLense, InternalOptions options) {
for (BasicBlock block : code.blocks) {
- if (block.hasCatchHandlers() && !block.canThrow()) {
- CatchHandlers<BasicBlock> handlers = block.getCatchHandlers();
- for (BasicBlock target : handlers.getUniqueTargets()) {
- target.unlinkCatchHandler();
+ if (block.hasCatchHandlers()) {
+ if (block.canThrow()) {
+ if (options.enableClassMerging) {
+ // Handle the case where an exception class has been merged into its sub class.
+ block.renameGuardsInCatchHandlers(graphLense);
+ unlinkDeadCatchHandlers(block, graphLense);
+ }
+ } else {
+ CatchHandlers<BasicBlock> handlers = block.getCatchHandlers();
+ for (BasicBlock target : handlers.getUniqueTargets()) {
+ target.unlinkCatchHandler();
+ }
}
}
}
code.removeUnreachableBlocks();
}
+
+ // Due to class merging, it is possible that two exception classes have been merged into one. This
+ // function removes catch handlers where the guards ended up being the same as a previous one.
+ private static void unlinkDeadCatchHandlers(BasicBlock block, GraphLense graphLense) {
+ assert block.hasCatchHandlers();
+ CatchHandlers<BasicBlock> catchHandlers = block.getCatchHandlers();
+ List<DexType> guards = catchHandlers.getGuards();
+ List<BasicBlock> targets = catchHandlers.getAllTargets();
+
+ Set<DexType> previouslySeenGuards = new HashSet<>();
+ List<BasicBlock> deadCatchHandlers = new ArrayList<>();
+ for (int i = 0; i < guards.size(); i++) {
+ // The type may have changed due to class merging.
+ DexType guard = graphLense.lookupType(guards.get(i));
+ boolean guardSeenBefore = !previouslySeenGuards.add(guard);
+ if (guardSeenBefore) {
+ deadCatchHandlers.add(targets.get(i));
+ }
+ }
+ // Remove the guards that are guaranteed to be dead.
+ for (BasicBlock deadCatchHandler : deadCatchHandlers) {
+ deadCatchHandler.unlinkCatchHandler();
+ }
+ assert block.consistentCatchHandlers();
+ }
}
diff --git a/src/main/java/com/android/tools/r8/kotlin/Kotlin.java b/src/main/java/com/android/tools/r8/kotlin/Kotlin.java
index 0af41b1..cf2b2bc 100644
--- a/src/main/java/com/android/tools/r8/kotlin/Kotlin.java
+++ b/src/main/java/com/android/tools/r8/kotlin/Kotlin.java
@@ -5,11 +5,19 @@
package com.android.tools.r8.kotlin;
import com.android.tools.r8.DiagnosticsHandler;
+import com.android.tools.r8.graph.DexAnnotation;
+import com.android.tools.r8.graph.DexAnnotationElement;
import com.android.tools.r8.graph.DexClass;
import com.android.tools.r8.graph.DexItemFactory;
import com.android.tools.r8.graph.DexMethod;
import com.android.tools.r8.graph.DexString;
import com.android.tools.r8.graph.DexType;
+import com.android.tools.r8.graph.DexValue;
+import com.android.tools.r8.graph.DexValue.DexValueArray;
+import com.android.tools.r8.graph.DexValue.DexValueInt;
+import com.android.tools.r8.graph.DexValue.DexValueString;
+import com.android.tools.r8.kotlin.KotlinSyntheticClass.Flavour;
+import com.android.tools.r8.utils.StringDiagnostic;
import com.google.common.collect.Sets;
import java.util.Set;
@@ -50,13 +58,10 @@
public final DexString kotlinStyleLambdaInstanceName = factory.createString("INSTANCE");
- public final DexType functionBase = factory.createType("Lkotlin/jvm/internal/FunctionBase;");
public final DexType lambdaType = factory.createType("Lkotlin/jvm/internal/Lambda;");
- public final DexMethod lambdaInitializerMethod = factory.createMethod(
- lambdaType,
- factory.createProto(factory.voidType, factory.intType),
- factory.constructorMethodName);
+ public final DexMethod lambdaInitializerMethod = factory.createMethod(lambdaType,
+ factory.createProto(factory.voidType, factory.intType), factory.constructorMethodName);
public boolean isFunctionInterface(DexType type) {
return functions.contains(type);
@@ -65,14 +70,9 @@
public final class Metadata {
public final DexType kotlinMetadataType = factory.createType("Lkotlin/Metadata;");
- public final DexString kind = factory.createString("k");
- public final DexString metadataVersion = factory.createString("mv");
- public final DexString bytecodeVersion = factory.createString("bv");
- public final DexString data1 = factory.createString("d1");
- public final DexString data2 = factory.createString("d2");
- public final DexString extraString = factory.createString("xs");
- public final DexString packageName = factory.createString("pn");
- public final DexString extraInt = factory.createString("xi");
+ public final DexString elementNameK = factory.createString("k");
+ public final DexString elementNameD1 = factory.createString("d1");
+ public final DexString elementNameD2 = factory.createString("d2");
}
// kotlin.jvm.internal.Intrinsics class
@@ -86,6 +86,98 @@
// Calculates kotlin info for a class.
public KotlinInfo getKotlinInfo(DexClass clazz, DiagnosticsHandler reporter) {
- return KotlinClassMetadataReader.getKotlinInfo(this, clazz, reporter);
+ if (clazz.annotations.isEmpty()) {
+ return null;
+ }
+ DexAnnotation meta = clazz.annotations.getFirstMatching(metadata.kotlinMetadataType);
+ if (meta != null) {
+ try {
+ return createKotlinInfo(clazz, meta);
+ } catch (MetadataError e) {
+ reporter.warning(
+ new StringDiagnostic("Class " + clazz.type.toSourceString() +
+ " has malformed kotlin.Metadata: " + e.getMessage()));
+ }
+ }
+ return null;
+ }
+
+ private KotlinInfo createKotlinInfo(DexClass clazz, DexAnnotation meta) {
+ DexAnnotationElement kindElement = getAnnotationElement(meta, metadata.elementNameK);
+ if (kindElement == null) {
+ throw new MetadataError("element 'k' is missing");
+ }
+
+ DexValue value = kindElement.value;
+ if (!(value instanceof DexValueInt)) {
+ throw new MetadataError("invalid 'k' value: " + value.toSourceString());
+ }
+
+ DexValueInt intValue = (DexValueInt) value;
+ switch (intValue.value) {
+ case 1:
+ return new KotlinClass();
+ case 2:
+ return new KotlinFile();
+ case 3:
+ return createSyntheticClass(clazz, meta);
+ case 4:
+ return new KotlinClassFacade();
+ case 5:
+ return new KotlinClassPart();
+ default:
+ throw new MetadataError("unsupported 'k' value: " + value.toSourceString());
+ }
+ }
+
+ private KotlinSyntheticClass createSyntheticClass(DexClass clazz, DexAnnotation meta) {
+ if (isKotlinStyleLambda(clazz)) {
+ return new KotlinSyntheticClass(Flavour.KotlinStyleLambda);
+ }
+ if (isJavaStyleLambda(clazz, meta)) {
+ return new KotlinSyntheticClass(Flavour.JavaStyleLambda);
+ }
+ return new KotlinSyntheticClass(Flavour.Unclassified);
+ }
+
+ private boolean isKotlinStyleLambda(DexClass clazz) {
+ // TODO: replace with direct hints from kotlin metadata when available.
+ return clazz.superType == this.functional.lambdaType;
+ }
+
+ private boolean isJavaStyleLambda(DexClass clazz, DexAnnotation meta) {
+ assert !isKotlinStyleLambda(clazz);
+ return clazz.superType == this.factory.objectType &&
+ clazz.interfaces.size() == 1 &&
+ isAnnotationElementNotEmpty(meta, metadata.elementNameD1);
+ }
+
+ private DexAnnotationElement getAnnotationElement(DexAnnotation annotation, DexString name) {
+ for (DexAnnotationElement element : annotation.annotation.elements) {
+ if (element.name == name) {
+ return element;
+ }
+ }
+ return null;
+ }
+
+ private boolean isAnnotationElementNotEmpty(DexAnnotation annotation, DexString name) {
+ for (DexAnnotationElement element : annotation.annotation.elements) {
+ if (element.name == name && element.value instanceof DexValueArray) {
+ DexValue[] values = ((DexValueArray) element.value).getValues();
+ if (values.length == 1 && values[0] instanceof DexValueString) {
+ return true;
+ }
+ // Must be broken metadata.
+ assert false;
+ }
+ }
+ return false;
+ }
+
+ private static class MetadataError extends RuntimeException {
+ MetadataError(String cause) {
+ super(cause);
+ }
}
}
diff --git a/src/main/java/com/android/tools/r8/kotlin/KotlinClass.java b/src/main/java/com/android/tools/r8/kotlin/KotlinClass.java
index 74184a5..fc81994 100644
--- a/src/main/java/com/android/tools/r8/kotlin/KotlinClass.java
+++ b/src/main/java/com/android/tools/r8/kotlin/KotlinClass.java
@@ -4,31 +4,7 @@
package com.android.tools.r8.kotlin;
-import kotlinx.metadata.KmClassVisitor;
-import kotlinx.metadata.jvm.KotlinClassMetadata;
-
-public class KotlinClass extends KotlinInfo<KotlinClassMetadata.Class> {
-
- static KotlinClass fromKotlinClassMetadata(KotlinClassMetadata kotlinClassMetadata) {
- assert kotlinClassMetadata instanceof KotlinClassMetadata.Class;
- KotlinClassMetadata.Class kClass = (KotlinClassMetadata.Class) kotlinClassMetadata;
- return new KotlinClass(kClass);
- }
-
- private KotlinClass(KotlinClassMetadata.Class metadata) {
- super(metadata);
- }
-
- @Override
- void validateMetadata(KotlinClassMetadata.Class metadata) {
- ClassMetadataVisitor visitor = new ClassMetadataVisitor();
- // To avoid lazy parsing/verifying metadata.
- metadata.accept(visitor);
- }
-
- private static class ClassMetadataVisitor extends KmClassVisitor {
- }
-
+public class KotlinClass extends KotlinInfo {
@Override
public Kind getKind() {
return Kind.Class;
@@ -44,4 +20,6 @@
return this;
}
+ KotlinClass() {
+ }
}
diff --git a/src/main/java/com/android/tools/r8/kotlin/KotlinClassFacade.java b/src/main/java/com/android/tools/r8/kotlin/KotlinClassFacade.java
index 62db3d1..e829a27 100644
--- a/src/main/java/com/android/tools/r8/kotlin/KotlinClassFacade.java
+++ b/src/main/java/com/android/tools/r8/kotlin/KotlinClassFacade.java
@@ -4,26 +4,7 @@
package com.android.tools.r8.kotlin;
-import kotlinx.metadata.jvm.KotlinClassMetadata;
-
-public final class KotlinClassFacade extends KotlinInfo<KotlinClassMetadata.MultiFileClassFacade> {
-
- static KotlinClassFacade fromKotlinClassMetadata(KotlinClassMetadata kotlinClassMetadata) {
- assert kotlinClassMetadata instanceof KotlinClassMetadata.MultiFileClassFacade;
- KotlinClassMetadata.MultiFileClassFacade multiFileClassFacade =
- (KotlinClassMetadata.MultiFileClassFacade) kotlinClassMetadata;
- return new KotlinClassFacade(multiFileClassFacade);
- }
-
- private KotlinClassFacade(KotlinClassMetadata.MultiFileClassFacade metadata) {
- super(metadata);
- }
-
- @Override
- void validateMetadata(KotlinClassMetadata.MultiFileClassFacade metadata) {
- // No worries about lazy parsing/verifying, since no API to explore metadata details.
- }
-
+public final class KotlinClassFacade extends KotlinInfo {
@Override
public Kind getKind() {
return Kind.Facade;
@@ -39,4 +20,7 @@
return this;
}
+ KotlinClassFacade() {
+ super();
+ }
}
diff --git a/src/main/java/com/android/tools/r8/kotlin/KotlinClassMetadataReader.java b/src/main/java/com/android/tools/r8/kotlin/KotlinClassMetadataReader.java
deleted file mode 100644
index 32aebff..0000000
--- a/src/main/java/com/android/tools/r8/kotlin/KotlinClassMetadataReader.java
+++ /dev/null
@@ -1,133 +0,0 @@
-// 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.kotlin;
-
-import com.android.tools.r8.DiagnosticsHandler;
-import com.android.tools.r8.graph.DexAnnotation;
-import com.android.tools.r8.graph.DexAnnotationElement;
-import com.android.tools.r8.graph.DexClass;
-import com.android.tools.r8.graph.DexString;
-import com.android.tools.r8.graph.DexValue;
-import com.android.tools.r8.graph.DexValue.DexValueArray;
-import com.android.tools.r8.graph.DexValue.DexValueString;
-import com.android.tools.r8.utils.StringDiagnostic;
-import java.util.IdentityHashMap;
-import java.util.Map;
-import kotlinx.metadata.InconsistentKotlinMetadataException;
-import kotlinx.metadata.jvm.KotlinClassHeader;
-import kotlinx.metadata.jvm.KotlinClassMetadata;
-
-final class KotlinClassMetadataReader {
-
- static KotlinInfo getKotlinInfo(
- Kotlin kotlin,
- DexClass clazz,
- DiagnosticsHandler reporter) {
- if (clazz.annotations.isEmpty()) {
- return null;
- }
- DexAnnotation meta = clazz.annotations.getFirstMatching(kotlin.metadata.kotlinMetadataType);
- if (meta != null) {
- try {
- return createKotlinInfo(kotlin, clazz, meta);
- } catch (ClassCastException | InconsistentKotlinMetadataException | MetadataError e) {
- reporter.warning(
- new StringDiagnostic("Class " + clazz.type.toSourceString()
- + " has malformed kotlin.Metadata: " + e.getMessage()));
- } catch (Throwable e) {
- reporter.warning(
- new StringDiagnostic("Unexpected error while reading " + clazz.type.toSourceString()
- + "'s kotlin.Metadata: " + e.getMessage()));
- }
- }
- return null;
- }
-
- private static KotlinInfo createKotlinInfo(
- Kotlin kotlin,
- DexClass clazz,
- DexAnnotation meta) {
- Map<DexString, DexAnnotationElement> elementMap = new IdentityHashMap<>();
- for (DexAnnotationElement element : meta.annotation.elements) {
- elementMap.put(element.name, element);
- }
-
- DexAnnotationElement kind = elementMap.get(kotlin.metadata.kind);
- if (kind == null) {
- throw new MetadataError("element 'k' is missing.");
- }
- Integer k = (Integer) kind.value.getBoxedValue();
- DexAnnotationElement metadataVersion = elementMap.get(kotlin.metadata.metadataVersion);
- int[] mv = metadataVersion == null ? null : getUnboxedIntArray(metadataVersion.value, "mv");
- DexAnnotationElement bytecodeVersion = elementMap.get(kotlin.metadata.bytecodeVersion);
- int[] bv = bytecodeVersion == null ? null : getUnboxedIntArray(bytecodeVersion.value, "bv");
- DexAnnotationElement data1 = elementMap.get(kotlin.metadata.data1);
- String[] d1 = data1 == null ? null : getUnboxedStringArray(data1.value, "d1");
- DexAnnotationElement data2 = elementMap.get(kotlin.metadata.data2);
- String[] d2 = data2 == null ? null : getUnboxedStringArray(data2.value, "d2");
- DexAnnotationElement extraString = elementMap.get(kotlin.metadata.extraString);
- String xs = extraString == null ? null : getUnboxedString(extraString.value, "xs");
- DexAnnotationElement packageName = elementMap.get(kotlin.metadata.packageName);
- String pn = packageName == null ? null : getUnboxedString(packageName.value, "pn");
- DexAnnotationElement extraInt = elementMap.get(kotlin.metadata.extraInt);
- Integer xi = extraInt == null ? null : (Integer) extraInt.value.getBoxedValue();
-
- KotlinClassHeader header = new KotlinClassHeader(k, mv, bv, d1, d2, xs, pn, xi);
- KotlinClassMetadata kMetadata = KotlinClassMetadata.read(header);
-
- if (kMetadata instanceof KotlinClassMetadata.Class) {
- return KotlinClass.fromKotlinClassMetadata(kMetadata);
- } else if (kMetadata instanceof KotlinClassMetadata.FileFacade) {
- return KotlinFile.fromKotlinClassMetadata(kMetadata);
- } else if (kMetadata instanceof KotlinClassMetadata.MultiFileClassFacade) {
- return KotlinClassFacade.fromKotlinClassMetadata(kMetadata);
- } else if (kMetadata instanceof KotlinClassMetadata.MultiFileClassPart) {
- return KotlinClassPart.fromKotlinClassMetdata(kMetadata);
- } else if (kMetadata instanceof KotlinClassMetadata.SyntheticClass) {
- return KotlinSyntheticClass.fromKotlinClassMetadata(kMetadata, kotlin, clazz);
- } else {
- throw new MetadataError("unsupported 'k' value: " + k);
- }
- }
-
- private static int[] getUnboxedIntArray(DexValue v, String elementName) {
- if (!(v instanceof DexValueArray)) {
- throw new MetadataError("invalid '" + elementName + "' value: " + v.toSourceString());
- }
- DexValueArray intArrayValue = (DexValueArray) v;
- DexValue[] values = intArrayValue.getValues();
- int[] result = new int [values.length];
- for (int i = 0; i < values.length; i++) {
- result[i] = (Integer) values[i].getBoxedValue();
- }
- return result;
- }
-
- private static String[] getUnboxedStringArray(DexValue v, String elementName) {
- if (!(v instanceof DexValueArray)) {
- throw new MetadataError("invalid '" + elementName + "' value: " + v.toSourceString());
- }
- DexValueArray stringArrayValue = (DexValueArray) v;
- DexValue[] values = stringArrayValue.getValues();
- String[] result = new String [values.length];
- for (int i = 0; i < values.length; i++) {
- result[i] = getUnboxedString(values[i], elementName + "[" + i + "]");
- }
- return result;
- }
-
- private static String getUnboxedString(DexValue v, String elementName) {
- if (!(v instanceof DexValueString)) {
- throw new MetadataError("invalid '" + elementName + "' value: " + v.toSourceString());
- }
- return ((DexValueString) v).getValue().toString();
- }
-
- private static class MetadataError extends RuntimeException {
- MetadataError(String cause) {
- super(cause);
- }
- }
-
-}
diff --git a/src/main/java/com/android/tools/r8/kotlin/KotlinClassPart.java b/src/main/java/com/android/tools/r8/kotlin/KotlinClassPart.java
index da66e6c..d6da817 100644
--- a/src/main/java/com/android/tools/r8/kotlin/KotlinClassPart.java
+++ b/src/main/java/com/android/tools/r8/kotlin/KotlinClassPart.java
@@ -4,31 +4,7 @@
package com.android.tools.r8.kotlin;
-import kotlinx.metadata.KmPackageVisitor;
-import kotlinx.metadata.jvm.KotlinClassMetadata;
-
-public final class KotlinClassPart extends KotlinInfo<KotlinClassMetadata.MultiFileClassPart> {
-
- static KotlinClassPart fromKotlinClassMetdata(KotlinClassMetadata kotlinClassMetadata) {
- assert kotlinClassMetadata instanceof KotlinClassMetadata.MultiFileClassPart;
- KotlinClassMetadata.MultiFileClassPart multiFileClassPart =
- (KotlinClassMetadata.MultiFileClassPart) kotlinClassMetadata;
- return new KotlinClassPart(multiFileClassPart);
- }
-
- private KotlinClassPart(KotlinClassMetadata.MultiFileClassPart metadata) {
- super(metadata);
- }
-
- @Override
- void validateMetadata(KotlinClassMetadata.MultiFileClassPart metadata) {
- // To avoid lazy parsing/verifying metadata.
- metadata.accept(new MultiFileClassPartMetadataVisitor());
- }
-
- private static class MultiFileClassPartMetadataVisitor extends KmPackageVisitor {
- }
-
+public final class KotlinClassPart extends KotlinInfo {
@Override
public Kind getKind() {
return Kind.Part;
@@ -44,4 +20,6 @@
return this;
}
+ KotlinClassPart() {
+ }
}
diff --git a/src/main/java/com/android/tools/r8/kotlin/KotlinFile.java b/src/main/java/com/android/tools/r8/kotlin/KotlinFile.java
index bcb70ed..38a77ad 100644
--- a/src/main/java/com/android/tools/r8/kotlin/KotlinFile.java
+++ b/src/main/java/com/android/tools/r8/kotlin/KotlinFile.java
@@ -4,31 +4,7 @@
package com.android.tools.r8.kotlin;
-import kotlinx.metadata.KmPackageVisitor;
-import kotlinx.metadata.jvm.KotlinClassMetadata;
-
-public final class KotlinFile extends KotlinInfo<KotlinClassMetadata.FileFacade> {
-
- static KotlinFile fromKotlinClassMetadata(KotlinClassMetadata kotlinClassMetadata) {
- assert kotlinClassMetadata instanceof KotlinClassMetadata.FileFacade;
- KotlinClassMetadata.FileFacade fileFacade =
- (KotlinClassMetadata.FileFacade) kotlinClassMetadata;
- return new KotlinFile(fileFacade);
- }
-
- private KotlinFile(KotlinClassMetadata.FileFacade metadata) {
- super(metadata);
- }
-
- @Override
- void validateMetadata(KotlinClassMetadata.FileFacade metadata) {
- // To avoid lazy parsing/verifying metadata.
- metadata.accept(new FileFacadeMetadataVisitor());
- }
-
- private static class FileFacadeMetadataVisitor extends KmPackageVisitor {
- }
-
+public final class KotlinFile extends KotlinInfo {
@Override
public Kind getKind() {
return Kind.File;
@@ -44,4 +20,6 @@
return this;
}
+ KotlinFile() {
+ }
}
diff --git a/src/main/java/com/android/tools/r8/kotlin/KotlinInfo.java b/src/main/java/com/android/tools/r8/kotlin/KotlinInfo.java
index 702e0eb..4043bc6 100644
--- a/src/main/java/com/android/tools/r8/kotlin/KotlinInfo.java
+++ b/src/main/java/com/android/tools/r8/kotlin/KotlinInfo.java
@@ -4,23 +4,11 @@
package com.android.tools.r8.kotlin;
-import kotlinx.metadata.jvm.KotlinClassMetadata;
-
// Provides access to kotlin information.
-public abstract class KotlinInfo<MetadataKind extends KotlinClassMetadata> {
- MetadataKind metadata;
-
+public abstract class KotlinInfo {
KotlinInfo() {
}
- KotlinInfo(MetadataKind metadata) {
- validateMetadata(metadata);
- this.metadata = metadata;
- }
-
- // Subtypes will define how to validate the given metadata.
- abstract void validateMetadata(MetadataKind metadata);
-
public enum Kind {
Class, File, Synthetic, Part, Facade
}
diff --git a/src/main/java/com/android/tools/r8/kotlin/KotlinSyntheticClass.java b/src/main/java/com/android/tools/r8/kotlin/KotlinSyntheticClass.java
index 4660800..68721bb 100644
--- a/src/main/java/com/android/tools/r8/kotlin/KotlinSyntheticClass.java
+++ b/src/main/java/com/android/tools/r8/kotlin/KotlinSyntheticClass.java
@@ -4,11 +4,7 @@
package com.android.tools.r8.kotlin;
-import com.android.tools.r8.graph.DexClass;
-import kotlinx.metadata.KmLambdaVisitor;
-import kotlinx.metadata.jvm.KotlinClassMetadata;
-
-public final class KotlinSyntheticClass extends KotlinInfo<KotlinClassMetadata.SyntheticClass> {
+public final class KotlinSyntheticClass extends KotlinInfo {
public enum Flavour {
KotlinStyleLambda,
JavaStyleLambda,
@@ -17,40 +13,12 @@
private final Flavour flavour;
- static KotlinSyntheticClass fromKotlinClassMetadata(
- KotlinClassMetadata kotlinClassMetadata, Kotlin kotlin, DexClass clazz) {
- assert kotlinClassMetadata instanceof KotlinClassMetadata.SyntheticClass;
- KotlinClassMetadata.SyntheticClass syntheticClass =
- (KotlinClassMetadata.SyntheticClass) kotlinClassMetadata;
- if (isKotlinStyleLambda(syntheticClass, kotlin, clazz)) {
- return new KotlinSyntheticClass(Flavour.KotlinStyleLambda, syntheticClass);
- } else if (isJavaStyleLambda(syntheticClass, kotlin, clazz)) {
- return new KotlinSyntheticClass(Flavour.JavaStyleLambda, syntheticClass);
- } else {
- return new KotlinSyntheticClass(Flavour.Unclassified, syntheticClass);
- }
- }
-
- private KotlinSyntheticClass(Flavour flavour, KotlinClassMetadata.SyntheticClass metadata) {
+ KotlinSyntheticClass(Flavour flavour) {
this.flavour = flavour;
- validateMetadata(metadata);
- this.metadata = metadata;
- }
-
- @Override
- void validateMetadata(KotlinClassMetadata.SyntheticClass metadata) {
- if (metadata.isLambda()) {
- SyntheticClassMetadataVisitor visitor = new SyntheticClassMetadataVisitor();
- // To avoid lazy parsing/verifying metadata.
- metadata.accept(visitor);
- }
- }
-
- private static class SyntheticClassMetadataVisitor extends KmLambdaVisitor {
}
public boolean isLambda() {
- return isKotlinStyleLambda() || isJavaStyleLambda();
+ return flavour == Flavour.KotlinStyleLambda || flavour == Flavour.JavaStyleLambda;
}
public boolean isKotlinStyleLambda() {
@@ -75,31 +43,4 @@
public KotlinSyntheticClass asSyntheticClass() {
return this;
}
-
- /**
- * Returns {@code true} if the given {@link DexClass} is a Kotlin-style lambda:
- * a class that
- * 1) is recognized as lambda in its Kotlin metadata;
- * 2) directly extends kotlin.jvm.internal.Lambda
- */
- private static boolean isKotlinStyleLambda(
- KotlinClassMetadata.SyntheticClass metadata, Kotlin kotlin, DexClass clazz) {
- return metadata.isLambda()
- && clazz.superType == kotlin.functional.lambdaType;
- }
-
- /**
- * Returns {@code true} if the given {@link DexClass} is a Java-style lambda:
- * a class that
- * 1) is recognized as lambda in its Kotlin metadata;
- * 2) doesn't extend any other class;
- * 3) directly implements only one Java SAM.
- */
- private static boolean isJavaStyleLambda(
- KotlinClassMetadata.SyntheticClass metadata, Kotlin kotlin, DexClass clazz) {
- return metadata.isLambda()
- && clazz.superType == kotlin.factory.objectType
- && clazz.interfaces.size() == 1;
- }
-
}
diff --git a/src/main/java/com/android/tools/r8/naming/ClassNameMinifier.java b/src/main/java/com/android/tools/r8/naming/ClassNameMinifier.java
index 56e381b..e3ec746 100644
--- a/src/main/java/com/android/tools/r8/naming/ClassNameMinifier.java
+++ b/src/main/java/com/android/tools/r8/naming/ClassNameMinifier.java
@@ -8,9 +8,11 @@
import static com.android.tools.r8.utils.DescriptorUtils.getPackageBinaryNameFromJavaType;
import com.android.tools.r8.graph.DexAnnotation;
+import com.android.tools.r8.graph.DexAnnotationSet;
import com.android.tools.r8.graph.DexClass;
import com.android.tools.r8.graph.DexEncodedField;
import com.android.tools.r8.graph.DexEncodedMethod;
+import com.android.tools.r8.graph.DexItem;
import com.android.tools.r8.graph.DexProgramClass;
import com.android.tools.r8.graph.DexProto;
import com.android.tools.r8.graph.DexString;
@@ -18,16 +20,20 @@
import com.android.tools.r8.graph.InnerClassAttribute;
import com.android.tools.r8.naming.signature.GenericSignatureAction;
import com.android.tools.r8.naming.signature.GenericSignatureParser;
+import com.android.tools.r8.origin.Origin;
import com.android.tools.r8.shaking.Enqueuer.AppInfoWithLiveness;
import com.android.tools.r8.shaking.RootSetBuilder.RootSet;
import com.android.tools.r8.utils.DescriptorUtils;
import com.android.tools.r8.utils.InternalOptions;
import com.android.tools.r8.utils.InternalOptions.PackageObfuscationMode;
+import com.android.tools.r8.utils.Reporter;
+import com.android.tools.r8.utils.StringDiagnostic;
import com.android.tools.r8.utils.StringUtils;
import com.android.tools.r8.utils.Timing;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
+import java.lang.reflect.GenericSignatureFormatError;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
@@ -40,6 +46,7 @@
private final AppInfoWithLiveness appInfo;
private final RootSet rootSet;
+ private final Reporter reporter;
private final PackageObfuscationMode packageObfuscationMode;
private final boolean isAccessModificationAllowed;
private final Set<String> noObfuscationPrefixes = Sets.newHashSet();
@@ -66,6 +73,7 @@
InternalOptions options) {
this.appInfo = appInfo;
this.rootSet = rootSet;
+ this.reporter = options.reporter;
this.packageObfuscationMode = options.proguardConfiguration.getPackageObfuscationMode();
this.isAccessModificationAllowed = options.proguardConfiguration.isAccessModificationAllowed();
this.packageDictionary = options.proguardConfiguration.getPackageObfuscationDictionary();
@@ -152,27 +160,75 @@
}
}
+ private void parseError(DexItem item, Origin origin, GenericSignatureFormatError e) {
+ StringBuilder message = new StringBuilder("Invalid class signature for ");
+ if (item instanceof DexClass) {
+ message.append("class ");
+ message.append(((DexClass) item).getType().toSourceString());
+ } else if (item instanceof DexEncodedField) {
+ message.append("field ");
+ message.append(item.toSourceString());
+ } else {
+ assert item instanceof DexEncodedMethod;
+ message.append("method ");
+ message.append(item.toSourceString());
+ }
+ message.append(".\n");
+ message.append(e.getMessage());
+ reporter.warning(new StringDiagnostic(message.toString(), origin));
+ }
+
private void renameTypesInGenericSignatures() {
for (DexClass clazz : appInfo.classes()) {
- rewriteGenericSignatures(clazz.annotations.annotations,
- genericSignatureParser::parseClassSignature);
+ clazz.annotations = rewriteGenericSignatures(clazz.annotations,
+ genericSignatureParser::parseClassSignature,
+ e -> parseError(clazz, clazz.getOrigin(), e));
clazz.forEachField(field -> rewriteGenericSignatures(
- field.annotations.annotations, genericSignatureParser::parseFieldSignature));
+ field.annotations, genericSignatureParser::parseFieldSignature,
+ e -> parseError(field, clazz.getOrigin(), e)));
clazz.forEachMethod(method -> rewriteGenericSignatures(
- method.annotations.annotations, genericSignatureParser::parseMethodSignature));
+ method.annotations, genericSignatureParser::parseMethodSignature,
+ e -> parseError(method, clazz.getOrigin(), e)));
}
}
- private void rewriteGenericSignatures(DexAnnotation[] annotations, Consumer<String> parser) {
- for (int i = 0; i < annotations.length; i++) {
- DexAnnotation annotation = annotations[i];
+ private DexAnnotationSet rewriteGenericSignatures(
+ DexAnnotationSet annotations,
+ Consumer<String> parser,
+ Consumer<GenericSignatureFormatError> parseError) {
+ // There can be no more than one signature annotation in an annotation set.
+ final int VALID = -1;
+ int invalid = VALID;
+ for (int i = 0; i < annotations.annotations.length && invalid == VALID; i++) {
+ DexAnnotation annotation = annotations.annotations[i];
if (DexAnnotation.isSignatureAnnotation(annotation, appInfo.dexItemFactory)) {
- parser.accept(DexAnnotation.getSignature(annotation));
- annotations[i] = DexAnnotation.createSignatureAnnotation(
- genericSignatureRewriter.getRenamedSignature(),
- appInfo.dexItemFactory);
+ try {
+ parser.accept(DexAnnotation.getSignature(annotation));
+ annotations.annotations[i] = DexAnnotation.createSignatureAnnotation(
+ genericSignatureRewriter.getRenamedSignature(),
+ appInfo.dexItemFactory);
+ } catch (GenericSignatureFormatError e) {
+ parseError.accept(e);
+ invalid = i;
+ }
}
}
+
+ // Return the rewritten signatures if it was valid and could be rewritten.
+ if (invalid == VALID) {
+ return annotations;
+ }
+ // Remove invalid signature if found.
+ DexAnnotation[] prunedAnnotations =
+ new DexAnnotation[annotations.annotations.length - 1];
+ int dest = 0;
+ for (int i = 0; i < annotations.annotations.length; i++) {
+ if (i != invalid) {
+ prunedAnnotations[dest++] = annotations.annotations[i];
+ }
+ }
+ assert dest == prunedAnnotations.length;
+ return new DexAnnotationSet(prunedAnnotations);
}
/**
@@ -453,7 +509,6 @@
String renamed =
getClassBinaryNameFromDescriptor(
renaming.getOrDefault(type, type.descriptor).toString());
- assert renamed.startsWith(enclosingRenamedBinaryName + Minifier.INNER_CLASS_SEPARATOR);
String outName = renamed.substring(enclosingRenamedBinaryName.length() + 1);
renamedSignature.append(outName);
return type;
diff --git a/src/main/java/com/android/tools/r8/naming/ProguardMapApplier.java b/src/main/java/com/android/tools/r8/naming/ProguardMapApplier.java
index 521229e..7dfd840 100644
--- a/src/main/java/com/android/tools/r8/naming/ProguardMapApplier.java
+++ b/src/main/java/com/android/tools/r8/naming/ProguardMapApplier.java
@@ -37,6 +37,7 @@
AppInfoWithLiveness appInfo,
GraphLense previousLense,
SeedMapper seedMapper) {
+ assert previousLense.isContextFreeForMethods();
this.appInfo = appInfo;
this.previousLense = previousLense;
this.seedMapper = seedMapper;
@@ -44,7 +45,7 @@
public GraphLense run(Timing timing) {
timing.begin("from-pg-map-to-lense");
- GraphLense lenseFromMap = new MapToLenseConverter().run(previousLense);
+ GraphLense lenseFromMap = new MapToLenseConverter().run();
timing.end();
timing.begin("fix-types-in-programs");
GraphLense typeFixedLense = new TreeFixer(lenseFromMap).run();
@@ -61,7 +62,7 @@
lenseBuilder = new ConflictFreeBuilder();
}
- private GraphLense run(GraphLense previousLense) {
+ private GraphLense run() {
// To handle inherited yet undefined methods in library classes, we are traversing types in
// a subtyping order. That also helps us detect conflicted mappings in a diamond case:
// LibItfA#foo -> a, LibItfB#foo -> b, while PrgA implements LibItfA and LibItfB.
@@ -308,7 +309,7 @@
}
for (int i = 0; i < methods.length; i++) {
DexEncodedMethod encodedMethod = methods[i];
- DexMethod appliedMethod = appliedLense.lookupMethod(encodedMethod.method, encodedMethod);
+ DexMethod appliedMethod = appliedLense.lookupMethod(encodedMethod.method);
DexType newHolderType = substituteType(appliedMethod.holder, encodedMethod);
DexProto newProto = substituteTypesIn(appliedMethod.proto, encodedMethod);
DexMethod newMethod;
@@ -331,7 +332,7 @@
}
for (int i = 0; i < fields.length; i++) {
DexEncodedField encodedField = fields[i];
- DexField appliedField = appliedLense.lookupField(encodedField.field, null);
+ DexField appliedField = appliedLense.lookupField(encodedField.field);
DexType newHolderType = substituteType(appliedField.clazz, null);
DexType newFieldType = substituteType(appliedField.type, null);
DexField newField;
@@ -427,7 +428,7 @@
return type.replaceBaseType(fixed, appInfo.dexItemFactory);
}
}
- return appliedLense.lookupType(type, context);
+ return appliedLense.lookupType(type);
}
}
diff --git a/src/main/java/com/android/tools/r8/optimize/BridgeMethodAnalysis.java b/src/main/java/com/android/tools/r8/optimize/BridgeMethodAnalysis.java
index c68122c..56089f4 100644
--- a/src/main/java/com/android/tools/r8/optimize/BridgeMethodAnalysis.java
+++ b/src/main/java/com/android/tools/r8/optimize/BridgeMethodAnalysis.java
@@ -9,6 +9,7 @@
import com.android.tools.r8.graph.DexMethod;
import com.android.tools.r8.graph.DexType;
import com.android.tools.r8.graph.GraphLense;
+import com.android.tools.r8.ir.code.Invoke.Type;
import com.android.tools.r8.logging.Log;
import com.android.tools.r8.optimize.InvokeSingleTargetExtractor.InvokeKind;
import com.android.tools.r8.shaking.Enqueuer.AppInfoWithLiveness;
@@ -43,16 +44,17 @@
InvokeKind kind = targetExtractor.getKind();
if (target != null && target.getArity() == method.method.getArity()) {
assert !method.accessFlags.isPrivate() && !method.accessFlags.isConstructor();
- target = lense.lookupMethod(target, method);
if (kind == InvokeKind.STATIC) {
assert method.accessFlags.isStatic();
- DexEncodedMethod targetMethod = appInfo.lookupStaticTarget(target);
+ DexMethod actualTarget = lense.lookupMethod(target, method, Type.STATIC);
+ DexEncodedMethod targetMethod = appInfo.lookupStaticTarget(actualTarget);
if (targetMethod != null) {
addForwarding(method, targetMethod);
}
} else if (kind == InvokeKind.VIRTUAL) {
// TODO(herhut): Add support for bridges with multiple targets.
- DexEncodedMethod targetMethod = appInfo.lookupSingleVirtualTarget(target);
+ DexMethod actualTarget = lense.lookupMethod(target, method, Type.VIRTUAL);
+ DexEncodedMethod targetMethod = appInfo.lookupSingleVirtualTarget(actualTarget);
if (targetMethod != null) {
addForwarding(method, targetMethod);
}
@@ -86,31 +88,29 @@
}
@Override
- public DexType lookupType(DexType type, DexEncodedMethod context) {
- return previousLense.lookupType(type, context);
+ public DexType lookupType(DexType type) {
+ return previousLense.lookupType(type);
}
@Override
- public DexMethod lookupMethod(DexMethod method, DexEncodedMethod context) {
- DexMethod previous = previousLense.lookupMethod(method, context);
+ public DexMethod lookupMethod(DexMethod method, DexEncodedMethod context, Type type) {
+ DexMethod previous = previousLense.lookupMethod(method, context, type);
DexMethod bridge = bridgeTargetToBridgeMap.get(previous);
// Do not forward calls from a bridge method to itself while the bridge method is still
// a bridge.
- if (bridge == null
- || (context.accessFlags.isBridge() && bridge == context.method)) {
+ if (bridge == null || (context.accessFlags.isBridge() && bridge == context.method)) {
return previous;
- } else {
- return bridge;
}
+ return bridge;
}
@Override
- public DexField lookupField(DexField field, DexEncodedMethod context) {
- return previousLense.lookupField(field, context);
+ public DexField lookupField(DexField field) {
+ return previousLense.lookupField(field);
}
@Override
- public boolean isContextFree() {
+ public boolean isContextFreeForMethods() {
return false;
}
diff --git a/src/main/java/com/android/tools/r8/graph/ClassAndMemberPublicizer.java b/src/main/java/com/android/tools/r8/optimize/ClassAndMemberPublicizer.java
similarity index 87%
rename from src/main/java/com/android/tools/r8/graph/ClassAndMemberPublicizer.java
rename to src/main/java/com/android/tools/r8/optimize/ClassAndMemberPublicizer.java
index 7a13348..297dd57 100644
--- a/src/main/java/com/android/tools/r8/graph/ClassAndMemberPublicizer.java
+++ b/src/main/java/com/android/tools/r8/optimize/ClassAndMemberPublicizer.java
@@ -1,7 +1,13 @@
// Copyright (c) 2016, 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.graph;
+package com.android.tools.r8.optimize;
+
+import com.android.tools.r8.graph.DexApplication;
+import com.android.tools.r8.graph.DexClass;
+import com.android.tools.r8.graph.DexEncodedMethod;
+import com.android.tools.r8.graph.DexItemFactory;
+import com.android.tools.r8.graph.MethodAccessFlags;
public final class ClassAndMemberPublicizer {
private final DexItemFactory factory;
diff --git a/src/main/java/com/android/tools/r8/optimize/MemberRebindingAnalysis.java b/src/main/java/com/android/tools/r8/optimize/MemberRebindingAnalysis.java
index 488cf2f..fc408e3 100644
--- a/src/main/java/com/android/tools/r8/optimize/MemberRebindingAnalysis.java
+++ b/src/main/java/com/android/tools/r8/optimize/MemberRebindingAnalysis.java
@@ -29,7 +29,7 @@
private final GraphLense.Builder builder = GraphLense.builder();
public MemberRebindingAnalysis(AppInfoWithLiveness appInfo, GraphLense lense) {
- assert lense.isContextFree();
+ assert lense.isContextFreeForMethods();
this.appInfo = appInfo;
this.lense = lense;
}
@@ -115,7 +115,7 @@
private void computeMethodRebinding(Set<DexMethod> methods,
Function<DexMethod, DexEncodedMethod> lookupTarget) {
for (DexMethod method : methods) {
- method = lense.lookupMethod(method, null);
+ method = lense.lookupMethod(method);
// We can safely ignore array types, as the corresponding methods are defined in a library.
if (!method.getHolder().isClassType()) {
continue;
@@ -188,7 +188,7 @@
BiFunction<DexClass, DexField, DexEncodedField> lookupTargetOnClass) {
for (Map.Entry<DexField, Set<DexEncodedMethod>> entry : fields.entrySet()) {
DexField field = entry.getKey();
- field = lense.lookupField(field, null);
+ field = lense.lookupField(field);
DexEncodedField target = lookup.apply(field.getHolder(), field);
// Rebind to the lowest library class or program class. Do not rebind accesses to fields that
// are not visible from the access context.
diff --git a/src/main/java/com/android/tools/r8/shaking/Enqueuer.java b/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
index 51feb9d..2b51720 100644
--- a/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
+++ b/src/main/java/com/android/tools/r8/shaking/Enqueuer.java
@@ -71,7 +71,6 @@
import java.util.SortedSet;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
-import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -1639,8 +1638,8 @@
this.liveTypes = rewriteItems(previous.liveTypes, lense::lookupType);
this.instantiatedTypes = rewriteItems(previous.instantiatedTypes, lense::lookupType);
this.instantiatedLambdas = rewriteItems(previous.instantiatedLambdas, lense::lookupType);
- this.targetedMethods = rewriteItems(previous.targetedMethods, lense::lookupMethod);
- this.liveMethods = rewriteItems(previous.liveMethods, lense::lookupMethod);
+ this.targetedMethods = rewriteMethodsConservatively(previous.targetedMethods, lense);
+ this.liveMethods = rewriteMethodsConservatively(previous.liveMethods, lense);
this.liveFields = rewriteItems(previous.liveFields, lense::lookupField);
this.instanceFieldReads =
rewriteKeysWhileMergingValues(previous.instanceFieldReads, lense::lookupField);
@@ -1653,11 +1652,11 @@
this.fieldsRead = rewriteItems(previous.fieldsRead, lense::lookupField);
this.fieldsWritten = rewriteItems(previous.fieldsWritten, lense::lookupField);
this.pinnedItems = rewriteMixedItems(previous.pinnedItems, lense);
- this.virtualInvokes = rewriteItems(previous.virtualInvokes, lense::lookupMethod);
- this.interfaceInvokes = rewriteItems(previous.interfaceInvokes, lense::lookupMethod);
- this.superInvokes = rewriteItems(previous.superInvokes, lense::lookupMethod);
- this.directInvokes = rewriteItems(previous.directInvokes, lense::lookupMethod);
- this.staticInvokes = rewriteItems(previous.staticInvokes, lense::lookupMethod);
+ this.virtualInvokes = rewriteMethodsConservatively(previous.virtualInvokes, lense);
+ this.interfaceInvokes = rewriteMethodsConservatively(previous.interfaceInvokes, lense);
+ this.superInvokes = rewriteMethodsConservatively(previous.superInvokes, lense);
+ this.directInvokes = rewriteMethodsConservatively(previous.directInvokes, lense);
+ this.staticInvokes = rewriteMethodsConservatively(previous.staticInvokes, lense);
this.prunedTypes = rewriteItems(previous.prunedTypes, lense::lookupType);
// TODO(herhut): Migrate these to Descriptors, as well.
assert assertNotModifiedByLense(previous.noSideEffects.keySet(), lense);
@@ -1739,16 +1738,16 @@
for (DexItem item : items) {
if (item instanceof DexClass) {
DexType type = ((DexClass) item).type;
- assert lense.lookupType(type, null) == type;
+ assert lense.lookupType(type) == type;
} else if (item instanceof DexEncodedMethod) {
DexEncodedMethod method = (DexEncodedMethod) item;
// We only allow changes to bridge methods, as these get retargeted even if they
// are kept.
assert method.accessFlags.isBridge()
- || lense.lookupMethod(method.method, null) == method.method;
+ || lense.lookupMethod(method.method) == method.method;
} else if (item instanceof DexEncodedField) {
DexField field = ((DexEncodedField) item).field;
- assert lense.lookupField(field, null) == field;
+ assert lense.lookupField(field) == field;
} else {
assert false;
}
@@ -1793,31 +1792,54 @@
return builder.build();
}
+ private static ImmutableSortedSet<DexMethod> rewriteMethodsConservatively(
+ Set<DexMethod> original, GraphLense lense) {
+ ImmutableSortedSet.Builder<DexMethod> builder =
+ new ImmutableSortedSet.Builder<>(PresortedComparable::slowCompare);
+ if (lense.isContextFreeForMethods()) {
+ for (DexMethod item : original) {
+ builder.add(lense.lookupMethod(item));
+ }
+ } else {
+ for (DexMethod item : original) {
+ // Avoid using lookupMethodInAllContexts when possible.
+ if (lense.isContextFreeForMethod(item)) {
+ builder.add(lense.lookupMethod(item));
+ } else {
+ // The lense is context sensitive, but we do not have the context here. Therefore, we
+ // conservatively look up the method in all contexts.
+ builder.addAll(lense.lookupMethodInAllContexts(item));
+ }
+ }
+ }
+ return builder.build();
+ }
+
private static <T extends PresortedComparable<T>> ImmutableSortedSet<T> rewriteItems(
- Set<T> original, BiFunction<T, DexEncodedMethod, T> rewrite) {
+ Set<T> original, Function<T, T> rewrite) {
ImmutableSortedSet.Builder<T> builder =
new ImmutableSortedSet.Builder<>(PresortedComparable::slowCompare);
for (T item : original) {
- builder.add(rewrite.apply(item, null));
+ builder.add(rewrite.apply(item));
}
return builder.build();
}
private static <T extends PresortedComparable<T>, S> ImmutableMap<T, S> rewriteKeys(
- Map<T, S> original, BiFunction<T, DexEncodedMethod, T> rewrite) {
+ Map<T, S> original, Function<T, T> rewrite) {
ImmutableMap.Builder<T, S> builder = new ImmutableMap.Builder<>();
for (T item : original.keySet()) {
- builder.put(rewrite.apply(item, null), original.get(item));
+ builder.put(rewrite.apply(item), original.get(item));
}
return builder.build();
}
- private static <T extends PresortedComparable<T>, S> Map<T, Set<S>>
- rewriteKeysWhileMergingValues(
- Map<T, Set<S>> original, BiFunction<T, DexEncodedMethod, T> rewrite) {
+ private static <T extends PresortedComparable<T>, S>
+ Map<T, Set<S>> rewriteKeysWhileMergingValues(
+ Map<T, Set<S>> original, Function<T, T> rewrite) {
Map<T, Set<S>> result = new IdentityHashMap<>();
for (T item : original.keySet()) {
- T rewrittenKey = rewrite.apply(item, null);
+ T rewrittenKey = rewrite.apply(item);
result.computeIfAbsent(rewrittenKey, k -> Sets.newIdentityHashSet())
.addAll(original.get(item));
}
@@ -1830,11 +1852,11 @@
for (DexItem item : original) {
// TODO(b/67934123) There should be a common interface to perform the dispatch.
if (item instanceof DexType) {
- builder.add(lense.lookupType((DexType) item, null));
+ builder.add(lense.lookupType((DexType) item));
} else if (item instanceof DexMethod) {
- builder.add(lense.lookupMethod((DexMethod) item, null));
+ builder.add(lense.lookupMethod((DexMethod) item));
} else if (item instanceof DexField) {
- builder.add(lense.lookupField((DexField) item, null));
+ builder.add(lense.lookupField((DexField) item));
} else {
throw new Unreachable();
}
@@ -1893,7 +1915,6 @@
public AppInfoWithLiveness rewrittenWithLense(DirectMappedDexApplication application,
GraphLense lense) {
- assert lense.isContextFree();
return new AppInfoWithLiveness(this, application, lense);
}
diff --git a/src/main/java/com/android/tools/r8/shaking/ProguardConfigurationParser.java b/src/main/java/com/android/tools/r8/shaking/ProguardConfigurationParser.java
index ed9cbdb..c4276dc 100644
--- a/src/main/java/com/android/tools/r8/shaking/ProguardConfigurationParser.java
+++ b/src/main/java/com/android/tools/r8/shaking/ProguardConfigurationParser.java
@@ -812,45 +812,45 @@
skipWhitespace();
switch (peekChar()) {
case 'a':
- if (found = acceptString("abstract")) {
+ if ((found = acceptString("abstract"))) {
flags.setAbstract();
}
break;
case 'f':
- if (found = acceptString("final")) {
+ if ((found = acceptString("final"))) {
flags.setFinal();
}
break;
case 'n':
- if (found = acceptString("native")) {
+ if ((found = acceptString("native"))) {
flags.setNative();
}
break;
case 'p':
- if (found = acceptString("public")) {
+ if ((found = acceptString("public"))) {
flags.setPublic();
- } else if (found = acceptString("private")) {
+ } else if ((found = acceptString("private"))) {
flags.setPrivate();
- } else if (found = acceptString("protected")) {
+ } else if ((found = acceptString("protected"))) {
flags.setProtected();
}
break;
case 's':
- if (found = acceptString("synchronized")) {
+ if ((found = acceptString("synchronized"))) {
flags.setSynchronized();
- } else if (found = acceptString("static")) {
+ } else if ((found = acceptString("static"))) {
flags.setStatic();
- } else if (found = acceptString("strictfp")) {
+ } else if ((found = acceptString("strictfp"))) {
flags.setStrict();
}
break;
case 't':
- if (found = acceptString("transient")) {
+ if ((found = acceptString("transient"))) {
flags.setTransient();
}
break;
case 'v':
- if (found = acceptString("volatile")) {
+ if ((found = acceptString("volatile"))) {
flags.setVolatile();
}
break;
diff --git a/src/main/java/com/android/tools/r8/shaking/SimpleClassMerger.java b/src/main/java/com/android/tools/r8/shaking/SimpleClassMerger.java
index a80e758..c9d34ee 100644
--- a/src/main/java/com/android/tools/r8/shaking/SimpleClassMerger.java
+++ b/src/main/java/com/android/tools/r8/shaking/SimpleClassMerger.java
@@ -181,7 +181,8 @@
if (Log.ENABLED) {
Log.debug(getClass(), "Merged %d classes.", numberOfMerges);
}
- return renamedMembersLense.build(application.dexItemFactory, graphLense);
+ return new VerticalClassMergerGraphLense(
+ renamedMembersLense.build(application.dexItemFactory, graphLense));
}
private class ClassMerger {
diff --git a/src/main/java/com/android/tools/r8/shaking/VerticalClassMergerGraphLense.java b/src/main/java/com/android/tools/r8/shaking/VerticalClassMergerGraphLense.java
new file mode 100644
index 0000000..1bdeb7c
--- /dev/null
+++ b/src/main/java/com/android/tools/r8/shaking/VerticalClassMergerGraphLense.java
@@ -0,0 +1,85 @@
+// 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.shaking;
+
+import com.android.tools.r8.graph.DexEncodedMethod;
+import com.android.tools.r8.graph.DexField;
+import com.android.tools.r8.graph.DexMethod;
+import com.android.tools.r8.graph.DexType;
+import com.android.tools.r8.graph.GraphLense;
+import com.android.tools.r8.ir.code.Invoke.Type;
+import com.google.common.collect.ImmutableSet;
+import java.util.Set;
+
+// This graph lense is instantiated during vertical class merging. The graph lense is context
+// sensitive in the enclosing class of a given invoke *and* the type of the invoke (e.g., invoke-
+// super vs invoke-virtual). This is illustrated by the following example.
+//
+// public class A {
+// public void m() { ... }
+// }
+// public class B extends A {
+// @Override
+// public void m() { invoke-super A.m(); ... }
+//
+// public void m2() { invoke-virtual A.m(); ... }
+// }
+//
+// Vertical class merging will merge class A into class B. Since class B already has a method with
+// the signature "void B.m()", the method A.m will be given a fresh name and moved to class B.
+// During this process, the method corresponding to A.m will be made private such that it can be
+// called via an invoke-direct instruction.
+//
+// For the invocation "invoke-super A.m()" in B.m, this graph lense will return the newly created,
+// private method corresponding to A.m (that is now in B.m with a fresh name), such that the
+// invocation will hit the same implementation as the original super.m() call.
+//
+// For the invocation "invoke-virtual A.m()" in B.m2, this graph lense will return the method B.m.
+public class VerticalClassMergerGraphLense extends GraphLense {
+ private final GraphLense previousLense;
+
+ public VerticalClassMergerGraphLense(GraphLense previousLense) {
+ this.previousLense = previousLense;
+ }
+
+ @Override
+ public DexType lookupType(DexType type) {
+ return previousLense.lookupType(type);
+ }
+
+ @Override
+ public DexMethod lookupMethod(DexMethod method, DexEncodedMethod context, Type type) {
+ // TODO(christofferqa): If [type] is Type.SUPER and [method] has been merged into the class of
+ // [context], then return the DIRECT method that has been created for [method] by SimpleClass-
+ // Merger. Otherwise, return the VIRTUAL method corresponding to [method].
+ return previousLense.lookupMethod(method, context, type);
+ }
+
+ @Override
+ public Set<DexMethod> lookupMethodInAllContexts(DexMethod method) {
+ DexMethod result = lookupMethod(method);
+ if (result != null) {
+ return ImmutableSet.of(result);
+ }
+ return ImmutableSet.of();
+ }
+
+ @Override
+ public DexField lookupField(DexField field) {
+ return previousLense.lookupField(field);
+ }
+
+ @Override
+ public boolean isContextFreeForMethods() {
+ return false;
+ }
+
+ @Override
+ public boolean isContextFreeForMethod(DexMethod method) {
+ // TODO(christofferqa): Should return false for methods where this graph lense is context
+ // sensitive.
+ return true;
+ }
+}
diff --git a/src/main/java/com/android/tools/r8/utils/ArchiveResourceProvider.java b/src/main/java/com/android/tools/r8/utils/ArchiveResourceProvider.java
index 499ba47..0f06ca8 100644
--- a/src/main/java/com/android/tools/r8/utils/ArchiveResourceProvider.java
+++ b/src/main/java/com/android/tools/r8/utils/ArchiveResourceProvider.java
@@ -22,6 +22,7 @@
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
@@ -49,7 +50,7 @@
private List<ProgramResource> readArchive() throws IOException {
List<ProgramResource> dexResources = new ArrayList<>();
List<ProgramResource> classResources = new ArrayList<>();
- try (ZipFile zipFile = new ZipFile(archive.getPath().toFile())) {
+ try (ZipFile zipFile = new ZipFile(archive.getPath().toFile(), StandardCharsets.UTF_8)) {
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
@@ -106,7 +107,7 @@
@Override
public void accept(Visitor resourceBrowser) throws ResourceException {
- try (ZipFile zipFile = new ZipFile(archive.getPath().toFile())) {
+ try (ZipFile zipFile = new ZipFile(archive.getPath().toFile(), StandardCharsets.UTF_8)) {
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
diff --git a/src/main/java/com/android/tools/r8/utils/LineNumberOptimizer.java b/src/main/java/com/android/tools/r8/utils/LineNumberOptimizer.java
index b938596..f10ff69 100644
--- a/src/main/java/com/android/tools/r8/utils/LineNumberOptimizer.java
+++ b/src/main/java/com/android/tools/r8/utils/LineNumberOptimizer.java
@@ -20,6 +20,7 @@
import com.android.tools.r8.ir.code.Position;
import com.android.tools.r8.naming.ClassNameMapper;
import com.android.tools.r8.naming.ClassNaming;
+import com.android.tools.r8.naming.ClassNaming.Builder;
import com.android.tools.r8.naming.MemberNaming;
import com.android.tools.r8.naming.MemberNaming.FieldSignature;
import com.android.tools.r8.naming.MemberNaming.MethodSignature;
@@ -187,6 +188,22 @@
}
}
+ // We will be remapping positional debug events and collect them as MappedPositions.
+ private static class MappedPosition {
+ private final DexMethod method;
+ private final int originalLine;
+ private final Position caller;
+ private final int obfuscatedLine;
+
+ private MappedPosition(
+ DexMethod method, int originalLine, Position caller, int obfuscatedLine) {
+ this.method = method;
+ this.originalLine = originalLine;
+ this.caller = caller;
+ this.obfuscatedLine = obfuscatedLine;
+ }
+ }
+
public static ClassNameMapper run(
DexApplication application, NamingLens namingLens, boolean identityMapping) {
IdentityHashMap<DexString, List<DexProgramClass>> classesOfFiles = new IdentityHashMap<>();
@@ -199,25 +216,8 @@
continue;
}
- // Group methods by name
IdentityHashMap<DexString, List<DexEncodedMethod>> methodsByName =
- new IdentityHashMap<>(clazz.directMethods().length + clazz.virtualMethods().length);
- clazz.forEachMethod(
- method -> {
- // Add method only if renamed or contains positions.
- if (namingLens.lookupName(method.method) != method.method.name
- || doesContainPositions(method)) {
- methodsByName.compute(
- method.method.name,
- (name, methods) -> {
- if (methods == null) {
- methods = new ArrayList<>();
- }
- methods.add(method);
- return methods;
- });
- }
- });
+ groupMethodsByName(namingLens, clazz);
// At this point we don't know if we really need to add this class to the builder.
// It depends on whether any methods/fields are renamed or some methods contain positions.
@@ -230,24 +230,11 @@
DescriptorUtils.descriptorToJavaType(renamedClassName.toString()),
clazz.toString()));
- // We do know we need to create a ClassNaming.Builder if the class itself had been renamed.
- if (!clazz.toString().equals(renamedClassName.toString())) {
- // Not using return value, it's registered in classNameMapperBuilder
- onDemandClassNamingBuilder.get();
- }
+ // If the class is renamed add it to the classNamingBuilder.
+ addClassToClassNaming(clazz, renamedClassName, onDemandClassNamingBuilder);
// First transfer renamed fields to classNamingBuilder.
- clazz.forEachField(
- dexEncodedField -> {
- DexField dexField = dexEncodedField.field;
- DexString renamedName = namingLens.lookupName(dexField);
- if (renamedName != dexField.name) {
- FieldSignature signature =
- new FieldSignature(dexField.name.toString(), dexField.type.toString());
- MemberNaming memberNaming = new MemberNaming(signature, renamedName.toString());
- onDemandClassNamingBuilder.get().addMemberEntry(memberNaming);
- }
- });
+ addFieldsToClassNaming(namingLens, clazz, onDemandClassNamingBuilder);
// Then process the methods.
for (List<DexEncodedMethod> methods : methodsByName.values()) {
@@ -256,47 +243,13 @@
// deterministic behaviour: the algorithm will assign new line numbers in this order.
// Methods with different names can share the same line numbers, that's why they don't
// need to be sorted.
- methods.sort(
- (lhs, rhs) -> {
- // Sort by startline, then DexEncodedMethod.slowCompare.
- // Use startLine = 0 if no debuginfo.
- Code lhsCode = lhs.getCode();
- Code rhsCode = rhs.getCode();
- DexCode lhsDexCode =
- lhsCode == null || !lhsCode.isDexCode() ? null : lhsCode.asDexCode();
- DexCode rhsDexCode =
- rhsCode == null || !rhsCode.isDexCode() ? null : rhsCode.asDexCode();
- DexDebugInfo lhsDebugInfo = lhsDexCode == null ? null : lhsDexCode.getDebugInfo();
- DexDebugInfo rhsDebugInfo = rhsDexCode == null ? null : rhsDexCode.getDebugInfo();
- int lhsStartLine = lhsDebugInfo == null ? 0 : lhsDebugInfo.startLine;
- int rhsStartLine = rhsDebugInfo == null ? 0 : rhsDebugInfo.startLine;
- int startLineDiff = lhsStartLine - rhsStartLine;
- if (startLineDiff != 0) return startLineDiff;
- return DexEncodedMethod.slowCompare(lhs, rhs);
- });
+ sortMethods(methods);
}
PositionRemapper positionRemapper =
identityMapping ? new IdentityPositionRemapper() : new OptimizingPositionRemapper();
for (DexEncodedMethod method : methods) {
-
- // We will be remapping positional debug events and collect them as MappedPositions.
- class MappedPosition {
- private final DexMethod method;
- private final int originalLine;
- private final Position caller;
- private final int obfuscatedLine;
-
- private MappedPosition(
- DexMethod method, int originalLine, Position caller, int obfuscatedLine) {
- this.method = method;
- this.originalLine = originalLine;
- this.caller = caller;
- this.obfuscatedLine = obfuscatedLine;
- }
- }
-
List<MappedPosition> mappedPositions = new ArrayList<>();
if (doesContainPositions(method)) {
@@ -429,6 +382,75 @@
return classNameMapperBuilder.build();
}
+ // Sort by startline, then DexEncodedMethod.slowCompare.
+ // Use startLine = 0 if no debuginfo.
+ private static void sortMethods(List<DexEncodedMethod> methods) {
+ methods.sort(
+ (lhs, rhs) -> {
+ Code lhsCode = lhs.getCode();
+ Code rhsCode = rhs.getCode();
+ DexCode lhsDexCode =
+ lhsCode == null || !lhsCode.isDexCode() ? null : lhsCode.asDexCode();
+ DexCode rhsDexCode =
+ rhsCode == null || !rhsCode.isDexCode() ? null : rhsCode.asDexCode();
+ DexDebugInfo lhsDebugInfo = lhsDexCode == null ? null : lhsDexCode.getDebugInfo();
+ DexDebugInfo rhsDebugInfo = rhsDexCode == null ? null : rhsDexCode.getDebugInfo();
+ int lhsStartLine = lhsDebugInfo == null ? 0 : lhsDebugInfo.startLine;
+ int rhsStartLine = rhsDebugInfo == null ? 0 : rhsDebugInfo.startLine;
+ int startLineDiff = lhsStartLine - rhsStartLine;
+ if (startLineDiff != 0) return startLineDiff;
+ return DexEncodedMethod.slowCompare(lhs, rhs);
+ });
+ }
+
+ @SuppressWarnings("ReturnValueIgnored")
+ private static void addClassToClassNaming(DexProgramClass clazz, DexString renamedClassName,
+ Supplier<Builder> onDemandClassNamingBuilder) {
+ // We do know we need to create a ClassNaming.Builder if the class itself had been renamed.
+ if (!clazz.toString().equals(renamedClassName.toString())) {
+ // Not using return value, it's registered in classNameMapperBuilder
+ onDemandClassNamingBuilder.get();
+ }
+ }
+
+ private static void addFieldsToClassNaming(NamingLens namingLens, DexProgramClass clazz,
+ Supplier<Builder> onDemandClassNamingBuilder) {
+ clazz.forEachField(
+ dexEncodedField -> {
+ DexField dexField = dexEncodedField.field;
+ DexString renamedName = namingLens.lookupName(dexField);
+ if (renamedName != dexField.name) {
+ FieldSignature signature =
+ new FieldSignature(dexField.name.toString(), dexField.type.toString());
+ MemberNaming memberNaming = new MemberNaming(signature, renamedName.toString());
+ onDemandClassNamingBuilder.get().addMemberEntry(memberNaming);
+ }
+ });
+ }
+
+ private static IdentityHashMap<DexString, List<DexEncodedMethod>> groupMethodsByName(
+ NamingLens namingLens, DexProgramClass clazz) {
+ IdentityHashMap<DexString, List<DexEncodedMethod>> methodsByName =
+ new IdentityHashMap<>(clazz.directMethods().length + clazz.virtualMethods().length);
+ clazz.forEachMethod(
+ method -> {
+ // Add method only if renamed or contains positions.
+ if (namingLens.lookupName(method.method) != method.method.name
+ || doesContainPositions(method)) {
+ methodsByName.compute(
+ method.method.name,
+ (name, methods) -> {
+ if (methods == null) {
+ methods = new ArrayList<>();
+ }
+ methods.add(method);
+ return methods;
+ });
+ }
+ });
+ return methodsByName;
+ }
+
private static boolean doesContainPositions(DexEncodedMethod method) {
Code code = method.getCode();
if (code == null || !code.isDexCode()) {
diff --git a/src/main/java/com/android/tools/r8/utils/ZipUtils.java b/src/main/java/com/android/tools/r8/utils/ZipUtils.java
index 1b032e5..849377b 100644
--- a/src/main/java/com/android/tools/r8/utils/ZipUtils.java
+++ b/src/main/java/com/android/tools/r8/utils/ZipUtils.java
@@ -10,6 +10,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Enumeration;
@@ -27,7 +28,7 @@
}
public static void iter(String zipFileStr, OnEntryHandler handler) throws IOException {
- try (ZipFile zipFile = new ZipFile(zipFileStr)) {
+ try (ZipFile zipFile = new ZipFile(zipFileStr, StandardCharsets.UTF_8)) {
final Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
diff --git a/src/test/apiUsageSample/com/android/tools/apiusagesample/D8ApiUsageSample.java b/src/test/apiUsageSample/com/android/tools/apiusagesample/D8ApiUsageSample.java
index ad31005..2ef0123 100644
--- a/src/test/apiUsageSample/com/android/tools/apiusagesample/D8ApiUsageSample.java
+++ b/src/test/apiUsageSample/com/android/tools/apiusagesample/D8ApiUsageSample.java
@@ -22,6 +22,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -387,7 +388,7 @@
for (Path file : files) {
if (isArchive(file)) {
Origin zipOrigin = new PathOrigin(file);
- ZipInputStream zip = new ZipInputStream(Files.newInputStream(file));
+ ZipInputStream zip = new ZipInputStream(Files.newInputStream(file), StandardCharsets.UTF_8);
ZipEntry entry;
while (null != (entry = zip.getNextEntry())) {
if (isClassFile(Paths.get(entry.getName()))) {
diff --git a/src/test/apiUsageSample/com/android/tools/apiusagesample/R8ApiUsageSample.java b/src/test/apiUsageSample/com/android/tools/apiusagesample/R8ApiUsageSample.java
index 7230f55..2c2e8d7 100644
--- a/src/test/apiUsageSample/com/android/tools/apiusagesample/R8ApiUsageSample.java
+++ b/src/test/apiUsageSample/com/android/tools/apiusagesample/R8ApiUsageSample.java
@@ -22,6 +22,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -378,7 +379,7 @@
for (Path file : files) {
if (isArchive(file)) {
Origin zipOrigin = new PathOrigin(file);
- ZipInputStream zip = new ZipInputStream(Files.newInputStream(file));
+ ZipInputStream zip = new ZipInputStream(Files.newInputStream(file), StandardCharsets.UTF_8);
ZipEntry entry;
while (null != (entry = zip.getNextEntry())) {
if (isClassFile(Paths.get(entry.getName()))) {
diff --git a/src/test/examples/classmerging/ConflictingInterfaceSignaturesTest.java b/src/test/examples/classmerging/ConflictingInterfaceSignaturesTest.java
new file mode 100644
index 0000000..223ff37
--- /dev/null
+++ b/src/test/examples/classmerging/ConflictingInterfaceSignaturesTest.java
@@ -0,0 +1,32 @@
+// 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 classmerging;
+
+public class ConflictingInterfaceSignaturesTest {
+
+ public static void main(String[] args) {
+ A a = new InterfaceImpl();
+ a.foo();
+
+ B b = new InterfaceImpl();
+ b.foo();
+ }
+
+ public interface A {
+ void foo();
+ }
+
+ public interface B {
+ void foo();
+ }
+
+ public static final class InterfaceImpl implements A, B {
+
+ @Override
+ public void foo() {
+ System.out.println("In foo on InterfaceImpl");
+ }
+ }
+}
diff --git a/src/test/examples/classmerging/ExceptionTest.java b/src/test/examples/classmerging/ExceptionTest.java
new file mode 100644
index 0000000..28442f3
--- /dev/null
+++ b/src/test/examples/classmerging/ExceptionTest.java
@@ -0,0 +1,56 @@
+// 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 classmerging;
+
+public class ExceptionTest {
+ public static void main(String[] args) {
+ // The following will lead to a catch handler for ExceptionA, which is merged into ExceptionB.
+ try {
+ doSomethingThatMightThrowExceptionB();
+ doSomethingThatMightThrowException2();
+ } catch (ExceptionB exception) {
+ System.out.println("Caught exception: " + exception.getMessage());
+ } catch (ExceptionA exception) {
+ System.out.println("Caught exception: " + exception.getMessage());
+ } catch (Exception2 exception) {
+ System.out.println("Caught exception: " + exception.getMessage());
+ } catch (Exception1 exception) {
+ System.out.println("Caught exception: " + exception.getMessage());
+ }
+ }
+
+ private static void doSomethingThatMightThrowExceptionB() throws ExceptionB {
+ throw new ExceptionB("Ouch!");
+ }
+
+ private static void doSomethingThatMightThrowException2() throws Exception2 {
+ throw new Exception2("Ouch!");
+ }
+
+ // Will be merged into ExceptionB when class merging is enabled.
+ public static class ExceptionA extends Exception {
+ public ExceptionA(String message) {
+ super(message);
+ }
+ }
+
+ public static class ExceptionB extends ExceptionA {
+ public ExceptionB(String message) {
+ super(message);
+ }
+ }
+
+ public static class Exception1 extends Exception {
+ public Exception1(String message) {
+ super(message);
+ }
+ }
+
+ public static class Exception2 extends Exception1 {
+ public Exception2(String message) {
+ super(message);
+ }
+ }
+}
diff --git a/src/test/examples/classmerging/SimpleInterface.java b/src/test/examples/classmerging/SimpleInterface.java
new file mode 100644
index 0000000..e8ebcab
--- /dev/null
+++ b/src/test/examples/classmerging/SimpleInterface.java
@@ -0,0 +1,9 @@
+// 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 classmerging;
+
+public interface SimpleInterface {
+ void foo();
+}
diff --git a/src/test/examples/classmerging/SimpleInterfaceAccessTest.java b/src/test/examples/classmerging/SimpleInterfaceAccessTest.java
new file mode 100644
index 0000000..04b5386
--- /dev/null
+++ b/src/test/examples/classmerging/SimpleInterfaceAccessTest.java
@@ -0,0 +1,16 @@
+// 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 classmerging;
+
+import classmerging.pkg.SimpleInterfaceImplRetriever;
+
+public class SimpleInterfaceAccessTest {
+ public static void main(String[] args) {
+ // It is not possible to merge the interface SimpleInterface into SimpleInterfaceImpl, since
+ // this would lead to an illegal class access here.
+ SimpleInterface obj = SimpleInterfaceImplRetriever.getSimpleInterfaceImpl();
+ obj.foo();
+ }
+}
diff --git a/src/test/examples/classmerging/TemplateMethodTest.java b/src/test/examples/classmerging/TemplateMethodTest.java
new file mode 100644
index 0000000..ac9de15
--- /dev/null
+++ b/src/test/examples/classmerging/TemplateMethodTest.java
@@ -0,0 +1,31 @@
+// 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 classmerging;
+
+public class TemplateMethodTest {
+
+ public static void main(String[] args) {
+ AbstractClass obj = new AbstractClassImpl();
+ obj.foo();
+ }
+
+ private abstract static class AbstractClass {
+
+ public void foo() {
+ System.out.println("In foo on AbstractClass");
+ bar();
+ }
+
+ protected abstract void bar();
+ }
+
+ public static final class AbstractClassImpl extends AbstractClass {
+
+ @Override
+ public void bar() {
+ System.out.println("In bar on AbstractClassImpl");
+ }
+ }
+}
diff --git a/src/test/examples/classmerging/keep-rules.txt b/src/test/examples/classmerging/keep-rules.txt
index 1cc5b87..da6ef55 100644
--- a/src/test/examples/classmerging/keep-rules.txt
+++ b/src/test/examples/classmerging/keep-rules.txt
@@ -7,6 +7,18 @@
-keep public class classmerging.Test {
public static void main(...);
}
+-keep public class classmerging.ConflictingInterfaceSignaturesTest {
+ public static void main(...);
+}
+-keep public class classmerging.ExceptionTest {
+ public static void main(...);
+}
+-keep public class classmerging.SimpleInterfaceAccessTest {
+ public static void main(...);
+}
+-keep public class classmerging.TemplateMethodTest {
+ public static void main(...);
+}
# TODO(herhut): Consider supporting merging of inner-class attributes.
# -keepattributes *
\ No newline at end of file
diff --git a/src/test/examples/classmerging/pkg/SimpleInterfaceImplRetriever.java b/src/test/examples/classmerging/pkg/SimpleInterfaceImplRetriever.java
new file mode 100644
index 0000000..5cfa11e
--- /dev/null
+++ b/src/test/examples/classmerging/pkg/SimpleInterfaceImplRetriever.java
@@ -0,0 +1,25 @@
+// 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 classmerging.pkg;
+
+import classmerging.SimpleInterface;
+
+public class SimpleInterfaceImplRetriever {
+
+ public static SimpleInterface getSimpleInterfaceImpl() {
+ return new SimpleInterfaceImpl();
+ }
+
+ // This class is intentionally marked private. It is not possible to merge the interface
+ // SimpleInterface into SimpleInterfaceImpl, since this would lead to an illegal class access
+ // in SimpleInterfaceAccessTest.
+ private static class SimpleInterfaceImpl implements SimpleInterface {
+
+ @Override
+ public void foo() {
+ System.out.println("In foo on SimpleInterfaceImpl");
+ }
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/CfFrontendExamplesTest.java b/src/test/java/com/android/tools/r8/CfFrontendExamplesTest.java
index 4e07849..4f749b5 100644
--- a/src/test/java/com/android/tools/r8/CfFrontendExamplesTest.java
+++ b/src/test/java/com/android/tools/r8/CfFrontendExamplesTest.java
@@ -85,6 +85,11 @@
}
@Test
+ public void testInlining() throws Exception {
+ runTest("inlining.Inlining");
+ }
+
+ @Test
public void testInstanceVariable() throws Exception {
runTest("instancevariable.InstanceVariable");
}
diff --git a/src/test/java/com/android/tools/r8/D8CommandTest.java b/src/test/java/com/android/tools/r8/D8CommandTest.java
index 1b846a5..87453d0 100644
--- a/src/test/java/com/android/tools/r8/D8CommandTest.java
+++ b/src/test/java/com/android/tools/r8/D8CommandTest.java
@@ -6,13 +6,13 @@
import static com.android.tools.r8.R8CommandTest.getOutputPath;
import static com.android.tools.r8.ToolHelper.EXAMPLES_BUILD_DIR;
import static com.android.tools.r8.utils.FileUtils.JAR_EXTENSION;
-import static java.nio.file.StandardOpenOption.CREATE_NEW;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import com.android.sdklib.AndroidVersion;
+import com.android.tools.r8.D8CommandParser.OrderedClassFileResourceProvider;
import com.android.tools.r8.ToolHelper.ProcessResult;
import com.android.tools.r8.dex.Marker;
import com.android.tools.r8.dex.Marker.Tool;
@@ -23,8 +23,8 @@
import com.android.tools.r8.utils.ZipUtils;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
-import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
@@ -263,13 +263,37 @@
tmpClassesDir.toString());
AndroidApp inputApp = ToolHelper.getApp(command);
assertEquals(1, inputApp.getClasspathResourceProviders().size());
+ OrderedClassFileResourceProvider classpathProvider =
+ (OrderedClassFileResourceProvider) inputApp.getClasspathResourceProviders().get(0);
+ assertEquals(1, classpathProvider.providers.size());
assertTrue(Files.isSameFile(tmpClassesDir,
- ((DirectoryClassFileProvider) inputApp.getClasspathResourceProviders().get(0)).getRoot()));
+ ((DirectoryClassFileProvider) classpathProvider.providers.get(0)).getRoot()));
assertEquals(1, inputApp.getLibraryResourceProviders().size());
assertTrue(Files.isSameFile(tmpClassesDir,
((DirectoryClassFileProvider) inputApp.getLibraryResourceProviders().get(0)).getRoot()));
}
+ @Test
+ public void folderClasspathMultiple() throws Throwable {
+ Path inputFile =
+ Paths.get(ToolHelper.EXAMPLES_ANDROID_N_BUILD_DIR, "interfacemethods" + JAR_EXTENSION);
+ Path tmpClassesDir1 = temp.newFolder().toPath();
+ Path tmpClassesDir2 = temp.newFolder().toPath();
+ ZipUtils.unzip(inputFile.toString(), tmpClassesDir1.toFile());
+ ZipUtils.unzip(inputFile.toString(), tmpClassesDir2.toFile());
+ D8Command command = parse("--classpath", tmpClassesDir1.toString(), "--classpath",
+ tmpClassesDir2.toString());
+ AndroidApp inputApp = ToolHelper.getApp(command);
+ assertEquals(1, inputApp.getClasspathResourceProviders().size());
+ OrderedClassFileResourceProvider classpathProvider =
+ (OrderedClassFileResourceProvider) inputApp.getClasspathResourceProviders().get(0);
+ assertEquals(2, classpathProvider.providers.size());
+ assertTrue(Files.isSameFile(tmpClassesDir1,
+ ((DirectoryClassFileProvider) classpathProvider.providers.get(0)).getRoot()));
+ assertTrue(Files.isSameFile(tmpClassesDir2,
+ ((DirectoryClassFileProvider) classpathProvider.providers.get(1)).getRoot()));
+ }
+
@Test(expected = CompilationFailedException.class)
public void classFolderProgram() throws Throwable {
Path inputFile =
@@ -319,7 +343,7 @@
Path input = Paths.get(EXAMPLES_BUILD_DIR, "arithmetic.jar");
ProgramResourceProvider myProvider =
ArchiveProgramResourceProvider.fromSupplier(
- new MyOrigin(), () -> new ZipFile(input.toFile()));
+ new MyOrigin(), () -> new ZipFile(input.toFile(), StandardCharsets.UTF_8));
D8Command command =
D8Command.builder()
.setProgramConsumer(DexIndexedConsumer.emptyConsumer())
@@ -464,7 +488,7 @@
.setOutput(emptyZip, OutputMode.DexIndexed)
.build());
assertTrue(Files.exists(emptyZip));
- assertEquals(0, new ZipFile(emptyZip.toFile()).size());
+ assertEquals(0, new ZipFile(emptyZip.toFile(), StandardCharsets.UTF_8).size());
}
private D8Command parse(String... args) throws CompilationFailedException {
diff --git a/src/test/java/com/android/tools/r8/OrderedClassFileResourceProviderTest.java b/src/test/java/com/android/tools/r8/OrderedClassFileResourceProviderTest.java
new file mode 100644
index 0000000..dc497c4
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/OrderedClassFileResourceProviderTest.java
@@ -0,0 +1,113 @@
+// 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;
+
+import static org.junit.Assert.assertEquals;
+
+import com.android.tools.r8.D8CommandParser.OrderedClassFileResourceProvider;
+import com.android.tools.r8.origin.Origin;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
+import java.io.InputStream;
+import java.util.Map;
+import java.util.Set;
+import org.junit.Test;
+
+public class OrderedClassFileResourceProviderTest extends TestBase {
+ class SimpleClassFileResourceProvider implements ClassFileResourceProvider {
+
+ private final Set<String> descriptors;
+ private final ProgramResource fixedProgramResource;
+
+ SimpleClassFileResourceProvider(int id, Set<String> descriptors) {
+ this.descriptors = descriptors;
+ this.fixedProgramResource = new SimpleProgramResource(id);
+ }
+
+ @Override
+ public Set<String> getClassDescriptors() {
+ return descriptors;
+ }
+
+ @Override
+ public ProgramResource getProgramResource(String descriptor) {
+ return fixedProgramResource;
+ }
+ }
+
+ class SimpleProgramResource implements ProgramResource {
+
+ private final Origin origin;
+
+ SimpleProgramResource(int id) {
+ origin = new SimpleOrigin(id);
+ }
+
+ @Override
+ public Kind getKind() {
+ return null;
+ }
+
+ @Override
+ public InputStream getByteStream() throws ResourceException {
+ return null;
+ }
+
+ @Override
+ public Set<String> getClassDescriptors() {
+ return null;
+ }
+
+ @Override
+ public Origin getOrigin() {
+ return origin;
+ }
+ }
+
+ public class SimpleOrigin extends Origin {
+
+ private final int id;
+
+ private SimpleOrigin(int index) {
+ super(root());
+ this.id = index;
+ }
+
+ int getId() {
+ return id;
+ }
+
+ @Override
+ public String part() {
+ return "Test";
+ }
+ }
+
+ @Test
+ public void test() {
+ OrderedClassFileResourceProvider.Builder builder = OrderedClassFileResourceProvider.builder();
+ builder.addClassFileResourceProvider(new SimpleClassFileResourceProvider(1, ImmutableSet.of(
+ "L/a/a/a", "L/a/a/b", "L/a/a/c"
+ )));
+ builder.addClassFileResourceProvider(new SimpleClassFileResourceProvider(2, ImmutableSet.of(
+ "L/a/a/b", "L/a/a/c", "L/a/a/d"
+ )));
+ ClassFileResourceProvider provider = builder.build();
+ assertEquals(
+ ImmutableSet.of("L/a/a/a", "L/a/a/b", "L/a/a/c", "L/a/a/d"),
+ provider.getClassDescriptors());
+
+ Map<String, Integer> expectations = ImmutableMap.of(
+ "L/a/a/a", 1,
+ "L/a/a/b", 1,
+ "L/a/a/c", 1,
+ "L/a/a/d", 2
+ );
+ expectations.forEach((descriptor, id) ->
+ assertEquals(
+ (int) id,
+ ((SimpleOrigin) provider.getProgramResource(descriptor).getOrigin()).getId()));
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/R8CommandTest.java b/src/test/java/com/android/tools/r8/R8CommandTest.java
index fbd88be..16bbae9 100644
--- a/src/test/java/com/android/tools/r8/R8CommandTest.java
+++ b/src/test/java/com/android/tools/r8/R8CommandTest.java
@@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.lang.reflect.Method;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -459,7 +460,7 @@
.setOutput(emptyZip, OutputMode.DexIndexed)
.build());
assertTrue(Files.exists(emptyZip));
- assertEquals(0, new ZipFile(emptyZip.toFile()).size());
+ assertEquals(0, new ZipFile(emptyZip.toFile(), StandardCharsets.UTF_8).size());
}
private R8Command parse(String... args) throws CompilationFailedException {
diff --git a/src/test/java/com/android/tools/r8/R8RunExamplesTest.java b/src/test/java/com/android/tools/r8/R8RunExamplesTest.java
index 348dbf1..087012c 100644
--- a/src/test/java/com/android/tools/r8/R8RunExamplesTest.java
+++ b/src/test/java/com/android/tools/r8/R8RunExamplesTest.java
@@ -41,6 +41,7 @@
"filledarray.FilledArray",
"hello.Hello",
"ifstatements.IfStatements",
+ "inlining.Inlining",
"instancevariable.InstanceVariable",
"instanceofstring.InstanceofString",
"invoke.Invoke",
diff --git a/src/test/java/com/android/tools/r8/RunExamplesAndroidOTest.java b/src/test/java/com/android/tools/r8/RunExamplesAndroidOTest.java
index 2c6a72a..b0876c9 100644
--- a/src/test/java/com/android/tools/r8/RunExamplesAndroidOTest.java
+++ b/src/test/java/com/android/tools/r8/RunExamplesAndroidOTest.java
@@ -27,6 +27,7 @@
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
@@ -580,7 +581,7 @@
protected DexInspector getMainDexInspector(Path zip)
throws ZipException, IOException, ExecutionException {
- try (ZipFile zipFile = new ZipFile(zip.toFile())) {
+ try (ZipFile zipFile = new ZipFile(zip.toFile(), StandardCharsets.UTF_8)) {
try (InputStream in =
zipFile.getInputStream(zipFile.getEntry(ToolHelper.DEFAULT_DEX_FILENAME))) {
return new DexInspector(
diff --git a/src/test/java/com/android/tools/r8/RunExamplesAndroidPTest.java b/src/test/java/com/android/tools/r8/RunExamplesAndroidPTest.java
index 182fe80..4701ac3 100644
--- a/src/test/java/com/android/tools/r8/RunExamplesAndroidPTest.java
+++ b/src/test/java/com/android/tools/r8/RunExamplesAndroidPTest.java
@@ -25,6 +25,7 @@
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
@@ -256,7 +257,7 @@
protected DexInspector getMainDexInspector(Path zip)
throws ZipException, IOException, ExecutionException {
- try (ZipFile zipFile = new ZipFile(zip.toFile())) {
+ try (ZipFile zipFile = new ZipFile(zip.toFile(), StandardCharsets.UTF_8)) {
try (InputStream in =
zipFile.getInputStream(zipFile.getEntry(ToolHelper.DEFAULT_DEX_FILENAME))) {
return new DexInspector(
diff --git a/src/test/java/com/android/tools/r8/RunExamplesJava9Test.java b/src/test/java/com/android/tools/r8/RunExamplesJava9Test.java
index 284ed68..9560c1f 100644
--- a/src/test/java/com/android/tools/r8/RunExamplesJava9Test.java
+++ b/src/test/java/com/android/tools/r8/RunExamplesJava9Test.java
@@ -25,6 +25,7 @@
import com.google.common.io.ByteStreams;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
@@ -309,7 +310,7 @@
protected DexInspector getMainDexInspector(Path zip)
throws ZipException, IOException, ExecutionException {
- try (ZipFile zipFile = new ZipFile(zip.toFile())) {
+ try (ZipFile zipFile = new ZipFile(zip.toFile(), StandardCharsets.UTF_8)) {
try (InputStream in =
zipFile.getInputStream(zipFile.getEntry(ToolHelper.DEFAULT_DEX_FILENAME))) {
return new DexInspector(
diff --git a/src/test/java/com/android/tools/r8/TestBase.java b/src/test/java/com/android/tools/r8/TestBase.java
index fbf2d02..5200a09 100644
--- a/src/test/java/com/android/tools/r8/TestBase.java
+++ b/src/test/java/com/android/tools/r8/TestBase.java
@@ -151,6 +151,11 @@
return builder.build();
}
+ /** Build an AndroidApp from the specified program files. */
+ protected AndroidApp readProgramFiles(Path... programFiles) throws IOException {
+ return AndroidApp.builder().addProgramFiles(programFiles).build();
+ }
+
/**
* Create a temporary JAR file containing the specified test classes.
*/
diff --git a/src/test/java/com/android/tools/r8/classmerging/ClassMergingTest.java b/src/test/java/com/android/tools/r8/classmerging/ClassMergingTest.java
index d5d2c39..65181dc 100644
--- a/src/test/java/com/android/tools/r8/classmerging/ClassMergingTest.java
+++ b/src/test/java/com/android/tools/r8/classmerging/ClassMergingTest.java
@@ -3,29 +3,46 @@
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.classmerging;
+import static com.android.tools.r8.utils.DexInspectorMatchers.isPresent;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import com.android.tools.r8.CompilationFailedException;
import com.android.tools.r8.OutputMode;
import com.android.tools.r8.R8Command;
+import com.android.tools.r8.TestBase;
import com.android.tools.r8.ToolHelper;
-import com.android.tools.r8.shaking.ProguardRuleParserException;
+import com.android.tools.r8.code.Instruction;
+import com.android.tools.r8.code.MoveException;
+import com.android.tools.r8.graph.DexCode;
+import com.android.tools.r8.utils.AndroidApp;
import com.android.tools.r8.utils.DexInspector;
+import com.android.tools.r8.utils.DexInspector.ClassSubject;
+import com.android.tools.r8.utils.DexInspector.FoundClassSubject;
+import com.android.tools.r8.utils.DexInspector.MethodSubject;
import com.android.tools.r8.utils.InternalOptions;
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
import java.io.IOException;
+import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
+import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;
-import org.junit.Rule;
+import org.junit.Ignore;
import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-public class ClassMergingTest {
+// TODO(christofferqa): Add tests to check that statically typed invocations on method handles
+// continue to work after class merging. Rewriting of method handles should be carried out by
+// LensCodeRewriter.rewriteDexMethodHandle.
+public class ClassMergingTest extends TestBase {
+ private static final Path CF_DIR =
+ Paths.get(ToolHelper.BUILD_DIR).resolve("classes/examples/classmerging");
private static final Path EXAMPLE_JAR = Paths.get(ToolHelper.EXAMPLES_BUILD_DIR)
.resolve("classmerging.jar");
private static final Path EXAMPLE_KEEP = Paths.get(ToolHelper.EXAMPLES_DIR)
@@ -33,17 +50,14 @@
private static final Path DONT_OPTIMIZE = Paths.get(ToolHelper.EXAMPLES_DIR)
.resolve("classmerging").resolve("keep-rules-dontoptimize.txt");
- @Rule
- public TemporaryFolder temp = ToolHelper.getTemporaryFolderForTest();
-
private void configure(InternalOptions options) {
options.enableClassMerging = true;
options.enableClassInlining = false;
+ options.enableMinification = false;
}
private void runR8(Path proguardConfig, Consumer<InternalOptions> optionsConsumer)
- throws IOException, ProguardRuleParserException, ExecutionException,
- CompilationFailedException {
+ throws IOException, ExecutionException, CompilationFailedException {
ToolHelper.runR8(
R8Command.builder()
.setOutput(Paths.get(temp.getRoot().getCanonicalPath()), OutputMode.DexIndexed)
@@ -73,13 +87,12 @@
assertFalse(inspector.clazz(candidate).isPresent());
}
assertTrue(inspector.clazz("classmerging.GenericInterfaceImpl").isPresent());
- assertTrue(inspector.clazz("classmerging.GenericInterfaceImpl").isPresent());
assertTrue(inspector.clazz("classmerging.Outer$SubClass").isPresent());
assertTrue(inspector.clazz("classmerging.SubClass").isPresent());
}
@Test
- public void testClassesShouldNotMerged() throws Exception {
+ public void testClassesHaveNotBeenMerged() throws Exception {
runR8(DONT_OPTIMIZE, null);
for (String candidate : CAN_BE_MERGED) {
assertTrue(inspector.clazz(candidate).isPresent());
@@ -100,4 +113,146 @@
assertTrue(inspector.clazz("classmerging.SubClassThatReferencesSuperMethod").isPresent());
}
+ @Test
+ public void testConflictingInterfaceSignatures() throws Exception {
+ String main = "classmerging.ConflictingInterfaceSignaturesTest";
+ Path[] programFiles =
+ new Path[] {
+ CF_DIR.resolve("ConflictingInterfaceSignaturesTest.class"),
+ CF_DIR.resolve("ConflictingInterfaceSignaturesTest$A.class"),
+ CF_DIR.resolve("ConflictingInterfaceSignaturesTest$B.class"),
+ CF_DIR.resolve("ConflictingInterfaceSignaturesTest$InterfaceImpl.class")
+ };
+ Set<String> preservedClassNames =
+ ImmutableSet.of(
+ "classmerging.ConflictingInterfaceSignaturesTest",
+ "classmerging.ConflictingInterfaceSignaturesTest$InterfaceImpl");
+ runTest(main, programFiles, preservedClassNames);
+ }
+
+ // If an exception class A is merged into another exception class B, then all exception tables
+ // should be updated, and class A should be removed entirely.
+ @Test
+ public void testExceptionTables() throws Exception {
+ String main = "classmerging.ExceptionTest";
+ Path[] programFiles =
+ new Path[] {
+ CF_DIR.resolve("ExceptionTest.class"),
+ CF_DIR.resolve("ExceptionTest$ExceptionA.class"),
+ CF_DIR.resolve("ExceptionTest$ExceptionB.class"),
+ CF_DIR.resolve("ExceptionTest$Exception1.class"),
+ CF_DIR.resolve("ExceptionTest$Exception2.class")
+ };
+ Set<String> preservedClassNames =
+ ImmutableSet.of(
+ "classmerging.ExceptionTest",
+ "classmerging.ExceptionTest$ExceptionB",
+ "classmerging.ExceptionTest$Exception2");
+ DexInspector inspector = runTest(main, programFiles, preservedClassNames);
+
+ ClassSubject mainClass = inspector.clazz(main);
+ assertThat(mainClass, isPresent());
+
+ MethodSubject mainMethod =
+ mainClass.method("void", "main", ImmutableList.of("java.lang.String[]"));
+ assertThat(mainMethod, isPresent());
+
+ // Check that the second catch handler has been removed.
+ DexCode code = mainMethod.getMethod().getCode().asDexCode();
+ int numberOfMoveExceptionInstructions = 0;
+ for (Instruction instruction : code.instructions) {
+ if (instruction instanceof MoveException) {
+ numberOfMoveExceptionInstructions++;
+ }
+ }
+ assertEquals(2, numberOfMoveExceptionInstructions);
+ }
+
+ @Ignore("b/73958515")
+ @Test
+ public void testNoIllegalClassAccess() throws Exception {
+ String main = "classmerging.SimpleInterfaceAccessTest";
+ Path[] programFiles =
+ new Path[] {
+ CF_DIR.resolve("SimpleInterface.class"),
+ CF_DIR.resolve("SimpleInterfaceAccessTest.class"),
+ CF_DIR.resolve("pkg/SimpleInterfaceImplRetriever.class"),
+ CF_DIR.resolve("pkg/SimpleInterfaceImplRetriever$SimpleInterfaceImpl.class")
+ };
+ // SimpleInterface cannot be merged into SimpleInterfaceImpl because SimpleInterfaceImpl
+ // is in a different package and is not public.
+ ImmutableSet<String> preservedClassNames =
+ ImmutableSet.of(
+ "classmerging.SimpleInterface",
+ "classmerging.SimpleInterfaceAccessTest",
+ "classmerging.pkg.SimpleInterfaceImplRetriever",
+ "classmerging.pkg.SimpleInterfaceImplRetriever$SimpleInterfaceImpl");
+ runTest(main, programFiles, preservedClassNames);
+ // If access modifications are allowed then SimpleInterface should be merged into
+ // SimpleInterfaceImpl.
+ preservedClassNames =
+ ImmutableSet.of(
+ "classmerging.SimpleInterfaceAccessTest",
+ "classmerging.pkg.SimpleInterfaceImplRetriever",
+ "classmerging.pkg.SimpleInterfaceImplRetriever$SimpleInterfaceImpl");
+ runTest(
+ main,
+ programFiles,
+ preservedClassNames,
+ getProguardConfig(EXAMPLE_KEEP, "-allowaccessmodification"));
+ }
+
+ @Test
+ public void testTemplateMethodPattern() throws Exception {
+ String main = "classmerging.TemplateMethodTest";
+ Path[] programFiles =
+ new Path[] {
+ CF_DIR.resolve("TemplateMethodTest.class"),
+ CF_DIR.resolve("TemplateMethodTest$AbstractClass.class"),
+ CF_DIR.resolve("TemplateMethodTest$AbstractClassImpl.class")
+ };
+ Set<String> preservedClassNames =
+ ImmutableSet.of(
+ "classmerging.TemplateMethodTest", "classmerging.TemplateMethodTest$AbstractClassImpl");
+ runTest(main, programFiles, preservedClassNames);
+ }
+
+ private DexInspector runTest(String main, Path[] programFiles, Set<String> preservedClassNames)
+ throws Exception {
+ return runTest(main, programFiles, preservedClassNames, getProguardConfig(EXAMPLE_KEEP, null));
+ }
+
+ private DexInspector runTest(
+ String main, Path[] programFiles, Set<String> preservedClassNames, String proguardConfig)
+ throws Exception {
+ AndroidApp input = readProgramFiles(programFiles);
+ AndroidApp output = compileWithR8(input, proguardConfig, this::configure);
+ DexInspector inspector = new DexInspector(output);
+ // Check that all classes in [preservedClassNames] are in fact preserved.
+ for (String className : preservedClassNames) {
+ assertTrue(
+ "Class " + className + " should be present", inspector.clazz(className).isPresent());
+ }
+ // Check that all other classes have been removed.
+ for (FoundClassSubject classSubject : inspector.allClasses()) {
+ String className = classSubject.getDexClass().toSourceString();
+ assertTrue(
+ "Class " + className + " should be absent", preservedClassNames.contains(className));
+ }
+ // Check that the R8-generated code produces the same result as D8-generated code.
+ assertEquals(runOnArt(compileWithD8(input), main), runOnArt(output, main));
+ return inspector;
+ }
+
+ private String getProguardConfig(Path path, String additionalRules) throws IOException {
+ StringBuilder builder = new StringBuilder();
+ for (String line : Files.readAllLines(path)) {
+ builder.append(line);
+ builder.append(System.lineSeparator());
+ }
+ if (additionalRules != null) {
+ builder.append(additionalRules);
+ }
+ return builder.toString();
+ }
}
diff --git a/src/test/java/com/android/tools/r8/compatdexbuilder/CompatDexBuilderTests.java b/src/test/java/com/android/tools/r8/compatdexbuilder/CompatDexBuilderTests.java
index e49c740..341fb67 100644
--- a/src/test/java/com/android/tools/r8/compatdexbuilder/CompatDexBuilderTests.java
+++ b/src/test/java/com/android/tools/r8/compatdexbuilder/CompatDexBuilderTests.java
@@ -15,6 +15,7 @@
import com.android.tools.r8.ToolHelper.ArtCommandBuilder;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Enumeration;
import java.util.HashSet;
@@ -65,7 +66,7 @@
for (String className : CLASS_NAMES) {
expectedNames.add(SUBDIR + "/" + className + ".class.dex");
}
- try (ZipFile zipFile = new ZipFile(outputZip.toFile())) {
+ try (ZipFile zipFile = new ZipFile(outputZip.toFile(), StandardCharsets.UTF_8)) {
for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements(); ) {
ZipEntry ze = e.nextElement();
expectedNames.remove(ze.getName());
diff --git a/src/test/java/com/android/tools/r8/compatdx/CompatDxTests.java b/src/test/java/com/android/tools/r8/compatdx/CompatDxTests.java
index c55428f..7d181bc 100644
--- a/src/test/java/com/android/tools/r8/compatdx/CompatDxTests.java
+++ b/src/test/java/com/android/tools/r8/compatdx/CompatDxTests.java
@@ -17,6 +17,7 @@
import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.net.URI;
+import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
@@ -197,8 +198,8 @@
}
private void compareArchiveFiles(Path d8File, Path dxFile) throws IOException {
- ZipFile d8Zip = new ZipFile(d8File.toFile());
- ZipFile dxZip = new ZipFile(dxFile.toFile());
+ ZipFile d8Zip = new ZipFile(d8File.toFile(), StandardCharsets.UTF_8);
+ ZipFile dxZip = new ZipFile(dxFile.toFile(), StandardCharsets.UTF_8);
// TODO(zerny): This should test resource containment too once supported.
Set<String> d8Content = d8Zip.stream().map(ZipEntry::getName).collect(Collectors.toSet());
Set<String> dxContent = dxZip.stream().map(ZipEntry::getName).collect(Collectors.toSet());
diff --git a/src/test/java/com/android/tools/r8/naming/GenericSignatureParserTest.java b/src/test/java/com/android/tools/r8/naming/GenericSignatureParserTest.java
index 751ac83..1d1604b 100644
--- a/src/test/java/com/android/tools/r8/naming/GenericSignatureParserTest.java
+++ b/src/test/java/com/android/tools/r8/naming/GenericSignatureParserTest.java
@@ -82,7 +82,7 @@
parseSimpleError(
GenericSignatureParser::parseClassSignature,
e -> assertTrue(e.getMessage().startsWith("Expected L at position 1")));
- // TODO(sgjesse): The position 2 reported here is onr off.
+ // TODO(sgjesse): The position 2 reported here is one off.
parseSimpleError(
GenericSignatureParser::parseFieldSignature,
e -> assertTrue(e.getMessage().startsWith("Expected L, [ or T at position 2")));
@@ -104,7 +104,7 @@
fail("Succesfully parsed " + signature.substring(0, i) + " (position " + i +")");
}
} catch (GenericSignatureFormatError e) {
- assertTrue("" + i + " Was: " + e.getMessage(), e.getMessage().contains("at position " + (i + 1)));
+ assertTrue(e.getMessage().contains("at position " + (i + 1)));
}
}
}
diff --git a/src/test/java/com/android/tools/r8/naming/MinifierClassSignatureTest.java b/src/test/java/com/android/tools/r8/naming/MinifierClassSignatureTest.java
new file mode 100644
index 0000000..68cd36f
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/naming/MinifierClassSignatureTest.java
@@ -0,0 +1,496 @@
+// 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.naming;
+
+import static com.android.tools.r8.utils.DexInspectorMatchers.isPresent;
+import static com.android.tools.r8.utils.DexInspectorMatchers.isRenamed;
+import static org.hamcrest.CoreMatchers.not;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThat;
+import static org.objectweb.asm.Opcodes.ACC_FINAL;
+import static org.objectweb.asm.Opcodes.ACC_SUPER;
+import static org.objectweb.asm.Opcodes.ACC_SYNTHETIC;
+import static org.objectweb.asm.Opcodes.ALOAD;
+import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
+import static org.objectweb.asm.Opcodes.PUTFIELD;
+import static org.objectweb.asm.Opcodes.RETURN;
+import static org.objectweb.asm.Opcodes.V1_8;
+
+import com.android.tools.r8.DexIndexedConsumer;
+import com.android.tools.r8.DiagnosticsChecker;
+import com.android.tools.r8.R8Command;
+import com.android.tools.r8.StringConsumer;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.ToolHelper;
+import com.android.tools.r8.graph.invokesuper.Consumer;
+import com.android.tools.r8.origin.Origin;
+import com.android.tools.r8.utils.DexInspector;
+import com.android.tools.r8.utils.DexInspector.ClassSubject;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.junit.Test;
+import org.objectweb.asm.ClassWriter;
+import org.objectweb.asm.FieldVisitor;
+import org.objectweb.asm.MethodVisitor;
+
+public class MinifierClassSignatureTest extends TestBase {
+ /*
+
+ class Simple {
+ }
+ class Base<T> {
+ }
+ class Outer<T> {
+ class Inner {
+ class InnerInner {
+ }
+ class ExtendsInnerInner extends InnerInner {
+ }
+ }
+ class ExtendsInner extends Inner {
+ }
+ }
+
+ */
+
+ String baseSignature = "<T:Ljava/lang/Object;>Ljava/lang/Object;";
+ String outerSignature = "<T:Ljava/lang/Object;>Ljava/lang/Object;";
+ String extendsInnerSignature = "LOuter<TT;>.Inner;";
+ String extendsInnerInnerSignature = "LOuter<TT;>.Inner.InnerInner;";
+
+ private byte[] dumpSimple(String classSignature) throws Exception {
+
+ ClassWriter cw = new ClassWriter(0);
+ MethodVisitor mv;
+
+ String signature = classSignature;
+ cw.visit(V1_8, ACC_SUPER, "Simple", signature, "java/lang/Object", null);
+
+ {
+ mv = cw.visitMethod(0, "<init>", "()V", null, null);
+ mv.visitCode();
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
+ mv.visitInsn(RETURN);
+ mv.visitMaxs(1, 1);
+ mv.visitEnd();
+ }
+ cw.visitEnd();
+
+ return cw.toByteArray();
+ }
+
+ private byte[] dumpBase(String classSignature) throws Exception {
+
+ final String javacClassSignature = baseSignature;
+ ClassWriter cw = new ClassWriter(0);
+ MethodVisitor mv;
+
+ String signature = classSignature != null ? classSignature : javacClassSignature;
+ cw.visit(V1_8, ACC_SUPER, "Base", signature, "java/lang/Object", null);
+
+ {
+ mv = cw.visitMethod(0, "<init>", "()V", null, null);
+ mv.visitCode();
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
+ mv.visitInsn(RETURN);
+ mv.visitMaxs(1, 1);
+ mv.visitEnd();
+ }
+ cw.visitEnd();
+
+ return cw.toByteArray();
+ }
+
+
+ private byte[] dumpOuter(String classSignature) {
+
+ final String javacClassSignature = outerSignature;
+ ClassWriter cw = new ClassWriter(0);
+ MethodVisitor mv;
+
+ String signature = classSignature != null ? classSignature : javacClassSignature;
+ cw.visit(V1_8, ACC_SUPER, "Outer", signature, "java/lang/Object", null);
+
+ cw.visitInnerClass("Outer$ExtendsInner", "Outer", "ExtendsInner", 0);
+
+ cw.visitInnerClass("Outer$Inner", "Outer", "Inner", 0);
+
+ {
+ mv = cw.visitMethod(0, "<init>", "()V", null, null);
+ mv.visitCode();
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
+ mv.visitInsn(RETURN);
+ mv.visitMaxs(1, 1);
+ mv.visitEnd();
+ }
+ cw.visitEnd();
+
+ return cw.toByteArray();
+ }
+
+ private byte[] dumpInner(String classSignature) {
+
+ final String javacClassSignature = null;
+ ClassWriter cw = new ClassWriter(0);
+ FieldVisitor fv;
+ MethodVisitor mv;
+
+ String signature = classSignature != null ? classSignature : javacClassSignature;
+ cw.visit(V1_8, ACC_SUPER, "Outer$Inner", signature, "java/lang/Object", null);
+
+ cw.visitInnerClass("Outer$Inner", "Outer", "Inner", 0);
+
+ cw.visitInnerClass("Outer$Inner$ExtendsInnerInner", "Outer$Inner", "ExtendsInnerInner", 0);
+
+ cw.visitInnerClass("Outer$Inner$InnerInner", "Outer$Inner", "InnerInner", 0);
+
+ {
+ fv = cw.visitField(ACC_FINAL + ACC_SYNTHETIC, "this$0", "LOuter;", null, null);
+ fv.visitEnd();
+ }
+ {
+ mv = cw.visitMethod(0, "<init>", "(LOuter;)V", null, null);
+ mv.visitCode();
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitVarInsn(ALOAD, 1);
+ mv.visitFieldInsn(PUTFIELD, "Outer$Inner", "this$0", "LOuter;");
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
+ mv.visitInsn(RETURN);
+ mv.visitMaxs(2, 2);
+ mv.visitEnd();
+ }
+ cw.visitEnd();
+
+ return cw.toByteArray();
+ }
+
+ private byte[] dumpExtendsInner(String classSignature) throws Exception {
+
+ final String javacClassSignature = extendsInnerSignature;
+ ClassWriter cw = new ClassWriter(0);
+ FieldVisitor fv;
+ MethodVisitor mv;
+
+ String signature = classSignature != null ? classSignature : javacClassSignature;
+ cw.visit(V1_8, ACC_SUPER, "Outer$ExtendsInner", signature, "Outer$Inner", null);
+
+ cw.visitInnerClass("Outer$Inner", "Outer", "Inner", 0);
+
+ cw.visitInnerClass("Outer$ExtendsInner", "Outer", "ExtendsInner", 0);
+
+ {
+ fv = cw.visitField(ACC_FINAL + ACC_SYNTHETIC, "this$0", "LOuter;", null, null);
+ fv.visitEnd();
+ }
+ {
+ mv = cw.visitMethod(0, "<init>", "(LOuter;)V", null, null);
+ mv.visitCode();
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitVarInsn(ALOAD, 1);
+ mv.visitFieldInsn(PUTFIELD, "Outer$ExtendsInner", "this$0", "LOuter;");
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitVarInsn(ALOAD, 1);
+ mv.visitMethodInsn(INVOKESPECIAL, "Outer$Inner", "<init>", "(LOuter;)V", false);
+ mv.visitInsn(RETURN);
+ mv.visitMaxs(2, 2);
+ mv.visitEnd();
+ }
+ cw.visitEnd();
+
+ return cw.toByteArray();
+ }
+
+ private byte[] dumpInnerInner(String classSignature) throws Exception {
+
+ final String javacClassSignature = null;
+ ClassWriter cw = new ClassWriter(0);
+ FieldVisitor fv;
+ MethodVisitor mv;
+
+ String signature = classSignature != null ? classSignature : javacClassSignature;
+ cw.visit(V1_8, ACC_SUPER, "Outer$Inner$InnerInner", signature, "java/lang/Object", null);
+
+ cw.visitInnerClass("Outer$Inner", "Outer", "Inner", 0);
+
+ cw.visitInnerClass("Outer$Inner$InnerInner", "Outer$Inner", "InnerInner", 0);
+
+ {
+ fv = cw.visitField(ACC_FINAL + ACC_SYNTHETIC, "this$1", "LOuter$Inner;", null, null);
+ fv.visitEnd();
+ }
+ {
+ mv = cw.visitMethod(0, "<init>", "(LOuter$Inner;)V", null, null);
+ mv.visitCode();
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitVarInsn(ALOAD, 1);
+ mv.visitFieldInsn(PUTFIELD, "Outer$Inner$InnerInner", "this$1", "LOuter$Inner;");
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
+ mv.visitInsn(RETURN);
+ mv.visitMaxs(2, 2);
+ mv.visitEnd();
+ }
+ cw.visitEnd();
+
+ return cw.toByteArray();
+ }
+
+ private byte[] dumpExtendsInnerInner(String classSignature) throws Exception {
+
+ final String javacClassSignature = extendsInnerInnerSignature;
+ ClassWriter cw = new ClassWriter(0);
+ FieldVisitor fv;
+ MethodVisitor mv;
+
+ String signature = classSignature != null ? classSignature : javacClassSignature;
+ cw.visit(V1_8, ACC_SUPER, "Outer$Inner$ExtendsInnerInner", signature, "Outer$Inner$InnerInner",
+ null);
+
+ cw.visitInnerClass("Outer$Inner", "Outer", "Inner", 0);
+
+ cw.visitInnerClass("Outer$Inner$InnerInner", "Outer$Inner", "InnerInner", 0);
+
+ cw.visitInnerClass("Outer$Inner$ExtendsInnerInner", "Outer$Inner", "ExtendsInnerInner", 0);
+
+ {
+ fv = cw.visitField(ACC_FINAL + ACC_SYNTHETIC, "this$1", "LOuter$Inner;", null, null);
+ fv.visitEnd();
+ }
+ {
+ mv = cw.visitMethod(0, "<init>", "(LOuter$Inner;)V", null, null);
+ mv.visitCode();
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitVarInsn(ALOAD, 1);
+ mv.visitFieldInsn(PUTFIELD, "Outer$Inner$ExtendsInnerInner", "this$1", "LOuter$Inner;");
+ mv.visitVarInsn(ALOAD, 0);
+ mv.visitVarInsn(ALOAD, 1);
+ mv.visitMethodInsn(INVOKESPECIAL, "Outer$Inner$InnerInner", "<init>", "(LOuter$Inner;)V",
+ false);
+ mv.visitInsn(RETURN);
+ mv.visitMaxs(2, 2);
+ mv.visitEnd();
+ }
+ cw.visitEnd();
+
+ return cw.toByteArray();
+ }
+
+ public void runTest(
+ ImmutableMap<String, String> signatures,
+ Consumer<DiagnosticsChecker> diagnostics,
+ Consumer<DexInspector> inspect)
+ throws Exception {
+ DiagnosticsChecker checker = new DiagnosticsChecker();
+ DexInspector inspector = new DexInspector(
+ ToolHelper.runR8(R8Command.builder(checker)
+ .addClassProgramData(dumpSimple(signatures.get("Simple")), Origin.unknown())
+ .addClassProgramData(dumpBase(signatures.get("Base")), Origin.unknown())
+ .addClassProgramData(dumpOuter(signatures.get("Outer")), Origin.unknown())
+ .addClassProgramData(dumpInner(signatures.get("Outer$Inner")), Origin.unknown())
+ .addClassProgramData(
+ dumpExtendsInner(signatures.get("Outer$ExtendsInner")), Origin.unknown())
+ .addClassProgramData(
+ dumpInnerInner(signatures.get("Outer$Inner$InnerInner")), Origin.unknown())
+ .addClassProgramData(
+ dumpExtendsInnerInner(
+ signatures.get("Outer$Inner$ExtendsInnerInner")), Origin.unknown())
+ .addProguardConfiguration(ImmutableList.of(
+ "-keepattributes InnerClasses,EnclosingMethod,Signature",
+ "-keep,allowobfuscation class **"
+ ), Origin.unknown())
+ .setProgramConsumer(DexIndexedConsumer.emptyConsumer())
+ .setProguardMapConsumer(StringConsumer.emptyConsumer())
+ .build()));
+ // All classes are kept, and renamed.
+ assertThat(inspector.clazz("Simple"), isRenamed());
+ assertThat(inspector.clazz("Base"), isRenamed());
+ assertThat(inspector.clazz("Outer"), isRenamed());
+ assertThat(inspector.clazz("Outer$Inner"), isRenamed());
+ assertThat(inspector.clazz("Outer$ExtendsInner"), isRenamed());
+ assertThat(inspector.clazz("Outer$Inner$InnerInner"), isRenamed());
+ assertThat(inspector.clazz("Outer$Inner$ExtendsInnerInner"), isRenamed());
+
+ // Test that classes with have their original signature if the default was provided.
+ if (!signatures.containsKey("Simple")) {
+ assertNull(inspector.clazz("Simple").getOriginalSignatureAttribute());
+ }
+ if (!signatures.containsKey("Base")) {
+ assertEquals(baseSignature, inspector.clazz("Base").getOriginalSignatureAttribute());
+ }
+ if (!signatures.containsKey("Outer")) {
+ assertEquals(outerSignature, inspector.clazz("Outer").getOriginalSignatureAttribute());
+ }
+ if (!signatures.containsKey("Outer$Inner")) {
+ assertNull(inspector.clazz("Outer$Inner").getOriginalSignatureAttribute());
+ }
+ if (!signatures.containsKey("Outer$ExtendsInner")) {
+ assertEquals(extendsInnerSignature,
+ inspector.clazz("Outer$ExtendsInner").getOriginalSignatureAttribute());
+ }
+ if (!signatures.containsKey("Outer$Inner$InnerInner")) {
+ assertNull(inspector.clazz("Outer$Inner$InnerInner").getOriginalSignatureAttribute());
+ }
+ if (!signatures.containsKey("Outer$Inner$ExtendsInnerInner")) {
+ assertEquals(extendsInnerInnerSignature,
+ inspector.clazz("Outer$Inner$ExtendsInnerInner").getOriginalSignatureAttribute());
+ }
+
+ diagnostics.accept(checker);
+ inspect.accept(inspector);
+ }
+
+ private void testSingleClass(String name, String signature,
+ Consumer<DiagnosticsChecker> diagnostics,
+ Consumer<DexInspector> inspector)
+ throws Exception {
+ ImmutableMap<String, String> signatures = ImmutableMap.of(name, signature);
+ runTest(signatures, diagnostics, inspector);
+ }
+
+ private void isOriginUnknown(Origin origin) {
+ assertSame(Origin.unknown(), origin);
+ }
+
+ private void noWarnings(DiagnosticsChecker checker) {
+ assertEquals(0, checker.warnings.size());
+ }
+
+ private void noInspection(DexInspector inspector) {
+ }
+
+ private void noSignatureAttribute(ClassSubject clazz) {
+ assertNull(clazz.getFinalSignatureAttribute());
+ assertNull(clazz.getOriginalSignatureAttribute());
+ }
+
+ @Test
+ public void originalJavacSignatures() throws Exception {
+ // Test using the signatures generated by javac.
+ runTest(ImmutableMap.of(), this::noWarnings, this::noInspection);
+ }
+
+ @Test
+ public void classSignature_empty() throws Exception {
+ testSingleClass("Outer", "", this::noWarnings, inspector -> {
+ ClassSubject outer = inspector.clazz("Outer");
+ assertNull(outer.getFinalSignatureAttribute());
+ assertNull(outer.getOriginalSignatureAttribute());
+ });
+ }
+
+ @Test
+ public void classSignatureOuter_valid() throws Exception {
+ // class Outer<T extends Simple> extends Base<T>
+ String signature = "<T:LSimple;>LBase<TT;>;";
+ testSingleClass("Outer", signature, this::noWarnings, inspector -> {
+ ClassSubject outer = inspector.clazz("Outer");
+ ClassSubject simple = inspector.clazz("Simple");
+ ClassSubject base = inspector.clazz("Base");
+ String baseDescriptorWithoutSemicolon =
+ base.getFinalDescriptor().substring(0, base.getFinalDescriptor().length() - 1);
+ String minifiedSignature =
+ "<T:" + simple.getFinalDescriptor() + ">" + baseDescriptorWithoutSemicolon + "<TT;>;";
+ assertEquals(minifiedSignature, outer.getFinalSignatureAttribute());
+ assertEquals(signature, outer.getOriginalSignatureAttribute());
+ });
+ }
+
+ @Test
+ public void classSignatureExtendsInner_valid() throws Exception {
+ String signature = "LOuter<TT;>.Inner;";
+ testSingleClass("Outer$ExtendsInner", signature, this::noWarnings, inspector -> {
+ ClassSubject extendsInner = inspector.clazz("Outer$ExtendsInner");
+ ClassSubject outer = inspector.clazz("Outer");
+ ClassSubject inner = inspector.clazz("Outer$Inner");
+ String outerDescriptorWithoutSemicolon =
+ outer.getFinalDescriptor().substring(0, outer.getFinalDescriptor().length() - 1);
+ String innerFinalDescriptor = inner.getFinalDescriptor();
+ String innerLastPart =
+ innerFinalDescriptor.substring(innerFinalDescriptor.indexOf("$") + 1);
+ String minifiedSignature = outerDescriptorWithoutSemicolon + "<TT;>." + innerLastPart;
+ assertEquals(minifiedSignature, extendsInner.getFinalSignatureAttribute());
+ assertEquals(signature, extendsInner.getOriginalSignatureAttribute());
+ });
+ }
+
+ @Test
+ public void classSignatureOuter_classNotFound() throws Exception {
+ String signature = "<T:LNotFound;>LNotFound;";
+ testSingleClass("Outer", signature, this::noWarnings, inspector -> {
+ assertThat(inspector.clazz("NotFound"), not(isPresent()));
+ ClassSubject outer = inspector.clazz("Outer");
+ assertEquals(signature, outer.getOriginalSignatureAttribute());
+ });
+ }
+
+ @Test
+ public void classSignatureOuter_invalid() throws Exception {
+ testSingleClass("Outer", "X", diagnostics -> {
+ assertEquals(1, diagnostics.warnings.size());
+ DiagnosticsChecker.checkDiagnostic(diagnostics.warnings.get(0), this::isOriginUnknown,
+ "Invalid class signature for class Outer", "Expected L at position 1");
+ }, inspector -> noSignatureAttribute(inspector.clazz("Outer")));
+ }
+
+ @Test
+ public void classSignatureOuter_invalidEnd() throws Exception {
+ testSingleClass("Outer", "<L", diagnostics -> {
+ assertEquals(1, diagnostics.warnings.size());
+ DiagnosticsChecker.checkDiagnostic(diagnostics.warnings.get(0), this::isOriginUnknown,
+ "Invalid class signature for class Outer", "Unexpected end of signature at position 3");
+ }, inspector -> noSignatureAttribute(inspector.clazz("Outer")));
+ }
+
+ @Test
+ public void classSignatureExtendsInner_invalid() throws Exception {
+ testSingleClass("Outer$ExtendsInner", "X", diagnostics -> {
+ assertEquals(1, diagnostics.warnings.size());
+ DiagnosticsChecker.checkDiagnostic(diagnostics.warnings.get(0), this::isOriginUnknown,
+ "Invalid class signature for class Outer$ExtendsInner", "Expected L at position 1");
+ }, inspector -> noSignatureAttribute(inspector.clazz("Outer$ExtendsInner")));
+ }
+
+ @Test
+ public void classSignatureExtendsInnerInner_invalid() throws Exception {
+ testSingleClass("Outer$Inner$ExtendsInnerInner", "X", diagnostics -> {
+ assertEquals(1, diagnostics.warnings.size());
+ DiagnosticsChecker.checkDiagnostic(diagnostics.warnings.get(0), this::isOriginUnknown,
+ "Invalid class signature for class Outer$Inner$ExtendsInnerInner",
+ "Expected L at position 1");
+ }, inspector -> noSignatureAttribute(inspector.clazz("Outer$Inner$ExtendsInnerInner")));
+ }
+
+ @Test
+ public void multipleWarnings() throws Exception {
+ runTest(ImmutableMap.of(
+ "Outer", "X",
+ "Outer$ExtendsInner", "X",
+ "Outer$Inner$ExtendsInnerInner", "X"), diagnostics -> {
+ assertEquals(3, diagnostics.warnings.size());
+ }, inspector -> {
+ noSignatureAttribute(inspector.clazz("Outer"));
+ noSignatureAttribute(inspector.clazz("Outer$ExtendsInner"));
+ noSignatureAttribute(inspector.clazz("Outer$Inner$ExtendsInnerInner"));
+ });
+ }
+ @Test
+ public void regress80029761() throws Exception {
+ String signature = "LOuter<TT;>.com/example/Inner;";
+ testSingleClass("Outer$ExtendsInner", signature, diagnostics -> {
+ assertEquals(1, diagnostics.warnings.size());
+ DiagnosticsChecker.checkDiagnostic(diagnostics.warnings.get(0), this::isOriginUnknown,
+ "Invalid class signature for class Outer$ExtendsInner", "Expected ; at position 16");
+ }, inspector -> {
+ noSignatureAttribute(inspector.clazz("Outer$ExtendsInner"));
+ });
+ }
+}
diff --git a/src/test/java/com/android/tools/r8/naming/NamingTestBase.java b/src/test/java/com/android/tools/r8/naming/NamingTestBase.java
index d9808c0..f311c15 100644
--- a/src/test/java/com/android/tools/r8/naming/NamingTestBase.java
+++ b/src/test/java/com/android/tools/r8/naming/NamingTestBase.java
@@ -5,9 +5,9 @@
import com.android.tools.r8.ToolHelper;
import com.android.tools.r8.graph.AppInfoWithSubtyping;
-import com.android.tools.r8.graph.ClassAndMemberPublicizer;
import com.android.tools.r8.graph.DexApplication;
import com.android.tools.r8.graph.DexItemFactory;
+import com.android.tools.r8.optimize.ClassAndMemberPublicizer;
import com.android.tools.r8.shaking.Enqueuer;
import com.android.tools.r8.shaking.ProguardConfiguration;
import com.android.tools.r8.shaking.ProguardRuleParserException;
diff --git a/src/test/java/com/android/tools/r8/utils/DexInspector.java b/src/test/java/com/android/tools/r8/utils/DexInspector.java
index ace5c9b..e7e882a 100644
--- a/src/test/java/com/android/tools/r8/utils/DexInspector.java
+++ b/src/test/java/com/android/tools/r8/utils/DexInspector.java
@@ -3,6 +3,8 @@
// BSD-style license that can be found in the LICENSE file.
package com.android.tools.r8.utils;
+import static org.junit.Assert.assertTrue;
+
import com.android.tools.r8.StringResource;
import com.android.tools.r8.code.Const4;
import com.android.tools.r8.code.ConstString;
@@ -53,6 +55,8 @@
import com.android.tools.r8.code.Throw;
import com.android.tools.r8.dex.ApplicationReader;
import com.android.tools.r8.graph.DexAnnotation;
+import com.android.tools.r8.graph.DexAnnotationElement;
+import com.android.tools.r8.graph.DexAnnotationSet;
import com.android.tools.r8.graph.DexApplication;
import com.android.tools.r8.graph.DexClass;
import com.android.tools.r8.graph.DexCode;
@@ -65,6 +69,8 @@
import com.android.tools.r8.graph.DexProto;
import com.android.tools.r8.graph.DexType;
import com.android.tools.r8.graph.DexValue;
+import com.android.tools.r8.graph.DexValue.DexValueArray;
+import com.android.tools.r8.graph.DexValue.DexValueString;
import com.android.tools.r8.graph.InnerClassAttribute;
import com.android.tools.r8.naming.ClassNameMapper;
import com.android.tools.r8.naming.ClassNamingForNameMapper;
@@ -72,6 +78,8 @@
import com.android.tools.r8.naming.MemberNaming.FieldSignature;
import com.android.tools.r8.naming.MemberNaming.MethodSignature;
import com.android.tools.r8.naming.MemberNaming.Signature;
+import com.android.tools.r8.naming.signature.GenericSignatureAction;
+import com.android.tools.r8.naming.signature.GenericSignatureParser;
import com.android.tools.r8.smali.SmaliBuilder;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableList;
@@ -85,6 +93,7 @@
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.ExecutionException;
+import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
@@ -191,6 +200,50 @@
}
}
+ DexAnnotation findAnnotation(String name, DexAnnotationSet annotations) {
+ for (DexAnnotation annotation : annotations.annotations) {
+ DexType type = annotation.annotation.type;
+ String original = mapping == null ? type.toSourceString() : mapping.originalNameOf(type);
+ if (original.equals(name)) {
+ return annotation;
+ }
+ }
+ return null;
+ }
+
+ public String getFinalSignatureAttribute(DexAnnotationSet annotations) {
+ DexAnnotation annotation =
+ findAnnotation("dalvik.annotation.Signature", annotations);
+ if (annotation == null) {
+ return null;
+ }
+ assert annotation.annotation.elements.length == 1;
+ DexAnnotationElement element = annotation.annotation.elements[0];
+ assert element.value instanceof DexValueArray;
+ StringBuilder builder = new StringBuilder();
+ DexValueArray valueArray = (DexValueArray) element.value;
+ for (DexValue value : valueArray.getValues()) {
+ assertTrue(value instanceof DexValueString);
+ DexValueString s = (DexValueString) value;
+ builder.append(s.getValue());
+ }
+ return builder.toString();
+ }
+
+ public String getOriginalSignatureAttribute(
+ DexAnnotationSet annotations, BiConsumer<GenericSignatureParser, String> parse) {
+ String finalSignature = getFinalSignatureAttribute(annotations);
+ if (finalSignature == null || mapping == null) {
+ return finalSignature;
+ }
+
+ GenericSignatureGenerater rewriter = new GenericSignatureGenerater();
+ GenericSignatureParser<String> parser = new GenericSignatureParser<>(rewriter);
+ parse.accept(parser, finalSignature);
+ return rewriter.getSignature();
+ }
+
+
public ClassSubject clazz(Class clazz) {
return clazz(clazz.getTypeName());
}
@@ -367,6 +420,10 @@
public abstract boolean isLocalClass();
public abstract boolean isAnonymousClass();
+
+ public abstract String getOriginalSignatureAttribute();
+
+ public abstract String getFinalSignatureAttribute();
}
private class AbsentClassSubject extends ClassSubject {
@@ -448,6 +505,16 @@
public boolean isAnonymousClass() {
return false;
}
+
+ @Override
+ public String getOriginalSignatureAttribute() {
+ return null;
+ }
+
+ @Override
+ public String getFinalSignatureAttribute() {
+ return null;
+ }
}
public class FoundClassSubject extends ClassSubject {
@@ -561,23 +628,12 @@
assert !name.endsWith("EnclosingClass")
&& !name.endsWith("EnclosingMethod")
&& !name.endsWith("InnerClass");
- DexAnnotation annotation = findAnnotation(name);
+ DexAnnotation annotation = findAnnotation(name, dexClass.annotations);
return annotation == null
? new AbsentAnnotationSubject()
: new FoundAnnotationSubject(annotation);
}
- private DexAnnotation findAnnotation(String name) {
- for (DexAnnotation annotation : dexClass.annotations.annotations) {
- DexType type = annotation.annotation.type;
- String original = mapping == null ? type.toSourceString() : mapping.originalNameOf(type);
- if (original.equals(name)) {
- return annotation;
- }
- }
- return null;
- }
-
@Override
public String getOriginalName() {
if (naming != null) {
@@ -641,6 +697,17 @@
}
@Override
+ public String getOriginalSignatureAttribute() {
+ return DexInspector.this.getOriginalSignatureAttribute(
+ dexClass.annotations, GenericSignatureParser::parseClassSignature);
+ }
+
+ @Override
+ public String getFinalSignatureAttribute() {
+ return DexInspector.this.getFinalSignatureAttribute(dexClass.annotations);
+ }
+
+ @Override
public String toString() {
return dexClass.toSourceString();
}
@@ -677,6 +744,10 @@
public abstract boolean isClassInitializer();
+ public abstract String getOriginalSignatureAttribute();
+
+ public abstract String getFinalSignatureAttribute();
+
public abstract DexEncodedMethod getMethod();
public Iterator<InstructionSubject> iterateInstructions() {
@@ -745,6 +816,16 @@
public Signature getFinalSignature() {
return null;
}
+
+ @Override
+ public String getOriginalSignatureAttribute() {
+ return null;
+ }
+
+ @Override
+ public String getFinalSignatureAttribute() {
+ return null;
+ }
}
public class FoundMethodSubject extends MethodSubject {
@@ -817,6 +898,17 @@
}
@Override
+ public String getOriginalSignatureAttribute() {
+ return DexInspector.this.getOriginalSignatureAttribute(
+ dexMethod.annotations, GenericSignatureParser::parseMethodSignature);
+ }
+
+ @Override
+ public String getFinalSignatureAttribute() {
+ return DexInspector.this.getFinalSignatureAttribute(dexMethod.annotations);
+ }
+
+ @Override
public Iterator<InstructionSubject> iterateInstructions() {
return new InstructionIterator(this);
}
@@ -841,6 +933,10 @@
public abstract DexValue getStaticValue();
public abstract boolean isRenamed();
+
+ public abstract String getOriginalSignatureAttribute();
+
+ public abstract String getFinalSignatureAttribute();
}
public class AbsentFieldSubject extends FieldSubject {
@@ -889,6 +985,16 @@
public DexEncodedField getField() {
return null;
}
+
+ @Override
+ public String getOriginalSignatureAttribute() {
+ return null;
+ }
+
+ @Override
+ public String getFinalSignatureAttribute() {
+ return null;
+ }
}
public class FoundFieldSubject extends FieldSubject {
@@ -956,6 +1062,17 @@
}
@Override
+ public String getOriginalSignatureAttribute() {
+ return DexInspector.this.getOriginalSignatureAttribute(
+ dexField.annotations, GenericSignatureParser::parseFieldSignature);
+ }
+
+ @Override
+ public String getFinalSignatureAttribute() {
+ return DexInspector.this.getFinalSignatureAttribute(dexField.annotations);
+ }
+
+ @Override
public String toString() {
return dexField.toSourceString();
}
@@ -1410,4 +1527,63 @@
return result;
}
}
+
+ // Build the generic signature using the current mapping if any.
+ class GenericSignatureGenerater implements GenericSignatureAction<String> {
+
+ private StringBuilder signature;
+
+ public String getSignature() {
+ return signature.toString();
+ }
+
+ @Override
+ public void parsedSymbol(char symbol) {
+ signature.append(symbol);
+ }
+
+ @Override
+ public void parsedIdentifier(String identifier) {
+ signature.append(identifier);
+ }
+
+ @Override
+ public String parsedTypeName(String name) {
+ String type = name;
+ if (originalToObfuscatedMapping != null) {
+ String original = originalToObfuscatedMapping.inverse().get(name);
+ type = original != null ? original : name;
+ }
+ signature.append(type);
+ return type;
+ }
+
+ @Override
+ public String parsedInnerTypeName(String enclosingType, String name) {
+ String type;
+ if (originalToObfuscatedMapping != null) {
+ // The enclosingType has already been mapped if a mapping is present.
+ String minifiedEnclosing = originalToObfuscatedMapping.get(enclosingType);
+ type = originalToObfuscatedMapping.inverse().get(minifiedEnclosing + "$" + name);
+ if (type != null) {
+ assert type.startsWith(enclosingType + "$");
+ name = type.substring(enclosingType.length() + 1);
+ }
+ } else {
+ type = enclosingType + "$" + name;
+ }
+ signature.append(name);
+ return type;
+ }
+
+ @Override
+ public void start() {
+ signature = new StringBuilder();
+ }
+
+ @Override
+ public void stop() {
+ // nothing to do
+ }
+ }
}
diff --git a/tools/build_r8lib.py b/tools/build_r8lib.py
index c855bc5..4b7473e 100755
--- a/tools/build_r8lib.py
+++ b/tools/build_r8lib.py
@@ -25,16 +25,21 @@
ANDROID_JAR = 'third_party/android_jar/lib-v%s/android.jar' % API_LEVEL
-def build_r8lib():
+def build_r8lib(output_path=None, output_map=None, **kwargs):
+ if output_path is None:
+ output_path = R8LIB_JAR
+ if output_map is None:
+ output_map = R8LIB_MAP_FILE
toolhelper.run(
'r8',
('--release',
'--classfile',
'--lib', utils.RT_JAR,
utils.R8_JAR,
- '--output', R8LIB_JAR,
+ '--output', output_path,
'--pg-conf', utils.R8LIB_KEEP_RULES,
- '--pg-map-output', R8LIB_MAP_FILE))
+ '--pg-map-output', output_map),
+ **kwargs)
def test_d8sample():
diff --git a/tools/r8lib_size_compare.py b/tools/r8lib_size_compare.py
new file mode 100755
index 0000000..84dd1a2
--- /dev/null
+++ b/tools/r8lib_size_compare.py
@@ -0,0 +1,105 @@
+#!/usr/bin/env python
+# 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.
+
+'''
+Build r8lib.jar with both R8 and ProGuard and print a size comparison.
+
+By default, inlining is disabled in both R8 and ProGuard to make
+method-by-method comparison much easier. Pass --inlining to enable inlining.
+
+By default, only shows methods where R8's DEX output is 5 or more instructions
+larger than ProGuard+D8's output. Pass --threshold 0 to display all methods.
+'''
+
+import argparse
+import build_r8lib
+import os
+import subprocess
+import toolhelper
+import utils
+
+
+parser = argparse.ArgumentParser(description=__doc__.strip(),
+ formatter_class=argparse.RawTextHelpFormatter)
+parser.add_argument('-t', '--tmpdir',
+ help='Store auxiliary files in given directory')
+parser.add_argument('-i', '--inlining', action='store_true',
+ help='Enable inlining')
+parser.add_argument('--threshold')
+
+R8_RELOCATIONS = [
+ ('com.google.common', 'com.android.tools.r8.com.google.common'),
+ ('com.google.gson', 'com.android.tools.r8.com.google.gson'),
+ ('com.google.thirdparty', 'com.android.tools.r8.com.google.thirdparty'),
+ ('joptsimple', 'com.android.tools.r8.joptsimple'),
+ ('org.apache.commons', 'com.android.tools.r8.org.apache.commons'),
+ ('org.objectweb.asm', 'com.android.tools.r8.org.objectweb.asm'),
+ ('it.unimi.dsi.fastutil', 'com.android.tools.r8.it.unimi.dsi.fastutil'),
+]
+
+
+def is_output_newer(input, output):
+ if not os.path.exists(output):
+ return False
+ return os.stat(input).st_mtime < os.stat(output).st_mtime
+
+
+def check_call(args, **kwargs):
+ utils.PrintCmd(args)
+ return subprocess.check_call(args, **kwargs)
+
+
+def main(tmpdir=None, inlining=True,
+ run_jarsizecompare=True, threshold=None):
+ if tmpdir is None:
+ with utils.TempDir() as tmpdir:
+ return main(tmpdir, inlining)
+
+ inline_suffix = '-inline' if inlining else '-noinline'
+
+ pg_config = utils.R8LIB_KEEP_RULES
+ r8lib_jar = os.path.join(utils.LIBS, 'r8lib%s.jar' % inline_suffix)
+ r8lib_map = os.path.join(utils.LIBS, 'r8lib%s-map.txt' % inline_suffix)
+ r8lib_args = None
+ if not inlining:
+ r8lib_args = ['-Dcom.android.tools.r8.disableinlining=1']
+ pg_config = os.path.join(tmpdir, 'keep-noinline.txt')
+ with open(pg_config, 'w') as new_config:
+ with open(utils.R8LIB_KEEP_RULES) as old_config:
+ new_config.write(old_config.read().rstrip('\n') +
+ '\n-optimizations !method/inlining/*\n')
+
+ if not is_output_newer(utils.R8_JAR, r8lib_jar):
+ r8lib_memory = os.path.join(tmpdir, 'r8lib%s-memory.txt' % inline_suffix)
+ build_r8lib.build_r8lib(
+ output_path=r8lib_jar, output_map=r8lib_map,
+ extra_args=r8lib_args, track_memory_file=r8lib_memory)
+
+ pg_output = os.path.join(tmpdir, 'r8lib-pg%s.jar' % inline_suffix)
+ pg_memory = os.path.join(tmpdir, 'r8lib-pg%s-memory.txt' % inline_suffix)
+ pg_map = os.path.join(tmpdir, 'r8lib-pg%s-map.txt' % inline_suffix)
+ pg_args = ['tools/track_memory.sh', pg_memory,
+ 'third_party/proguard/proguard6.0.2/bin/proguard.sh',
+ '@' + pg_config,
+ '-lib', utils.RT_JAR,
+ '-injar', utils.R8_JAR,
+ '-printmapping', pg_map,
+ '-outjar', pg_output]
+ for library_name, relocated_package in R8_RELOCATIONS:
+ pg_args.extend(['-dontwarn', relocated_package + '.**',
+ '-dontnote', relocated_package + '.**'])
+ check_call(pg_args)
+ if threshold is None:
+ threshold = 5
+ toolhelper.run('jarsizecompare',
+ ['--threshold', str(threshold),
+ '--lib', utils.RT_JAR,
+ '--input', 'input', utils.R8_JAR,
+ '--input', 'r8', r8lib_jar, r8lib_map,
+ '--input', 'pg', pg_output, pg_map])
+
+
+if __name__ == '__main__':
+ main(**vars(parser.parse_args()))
diff --git a/tools/toolhelper.py b/tools/toolhelper.py
index a7f509c..82a2824 100644
--- a/tools/toolhelper.py
+++ b/tools/toolhelper.py
@@ -9,7 +9,7 @@
import utils
def run(tool, args, build=None, debug=True,
- profile=False, track_memory_file=None):
+ profile=False, track_memory_file=None, extra_args=None):
if build is None:
build, args = extract_build_from_args(args)
if build:
@@ -18,6 +18,8 @@
if track_memory_file:
cmd.extend(['tools/track_memory.sh', track_memory_file])
cmd.append('java')
+ if extra_args:
+ cmd.extend(extra_args)
if debug:
cmd.append('-ea')
if profile: