JVM don't choke on exceptions in ClassFileFactory#createText

This commit is contained in:
Dmitry Petrov
2021-12-29 20:09:45 +03:00
committed by teamcity
parent 4ad6cfcf53
commit 9132c9b2d0
@@ -178,9 +178,18 @@ public class ClassFileFactory implements OutputFileCollection {
return createText(null); return createText(null);
} }
private static class ModuleMappingException extends RuntimeException {
public ModuleMappingException(String message) {
super(message);
}
}
@NotNull @NotNull
@TestOnly @TestOnly
public String createText(@Nullable String ignorePrefixPath) { public String createText(@Nullable String ignorePrefixPath) {
// NB this method is frequently used in JVM BE tests to display generated bytecode in case of test failure.
// It should be permissive, and should try to handle exceptions gracefully (otherwise you would make JVM BE devs unhappy).
StringBuilder answer = new StringBuilder(); StringBuilder answer = new StringBuilder();
for (OutputFile file : asList()) { for (OutputFile file : asList()) {
@@ -192,21 +201,27 @@ public class ClassFileFactory implements OutputFileCollection {
answer.append(file.asText()); answer.append(file.asText());
break; break;
case "kotlin_module": { case "kotlin_module": {
ModuleMapping mapping = ModuleMappingUtilKt.loadModuleMapping( try {
ModuleMapping.Companion, file.asByteArray(), relativePath.getPath(), ModuleMapping mapping = ModuleMappingUtilKt.loadModuleMapping(
CompilerDeserializationConfiguration.Default.INSTANCE, version -> { ModuleMapping.Companion, file.asByteArray(), relativePath.getPath(),
throw new IllegalStateException("Version of the generated module cannot be incompatible: " + version); CompilerDeserializationConfiguration.Default.INSTANCE,
} version -> {
); throw new ModuleMappingException("Generated module has incompatible JVM metadata version: " + version);
for (Map.Entry<String, PackageParts> entry : mapping.getPackageFqName2Parts().entrySet()) { }
FqName packageFqName = new FqName(entry.getKey()); );
PackageParts packageParts = entry.getValue(); for (Map.Entry<String, PackageParts> entry : mapping.getPackageFqName2Parts().entrySet()) {
answer.append("<package ").append(packageFqName).append(": ").append(packageParts.getParts()).append(">\n"); FqName packageFqName = new FqName(entry.getKey());
PackageParts packageParts = entry.getValue();
answer.append("<package ").append(packageFqName).append(": ").append(packageParts.getParts()).append(">\n");
}
break;
} catch (ModuleMappingException e) {
answer.append(relativePath).append(": ").append(e.getMessage()).append("\n");
break;
} }
break;
} }
default: default:
throw new UnsupportedOperationException("Unknown OutputFile: " + file); answer.append("Unknown output file: ").append(file);
} }
} }