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);
}
private static class ModuleMappingException extends RuntimeException {
public ModuleMappingException(String message) {
super(message);
}
}
@NotNull
@TestOnly
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();
for (OutputFile file : asList()) {
@@ -192,21 +201,27 @@ public class ClassFileFactory implements OutputFileCollection {
answer.append(file.asText());
break;
case "kotlin_module": {
ModuleMapping mapping = ModuleMappingUtilKt.loadModuleMapping(
ModuleMapping.Companion, file.asByteArray(), relativePath.getPath(),
CompilerDeserializationConfiguration.Default.INSTANCE, version -> {
throw new IllegalStateException("Version of the generated module cannot be incompatible: " + version);
}
);
for (Map.Entry<String, PackageParts> entry : mapping.getPackageFqName2Parts().entrySet()) {
FqName packageFqName = new FqName(entry.getKey());
PackageParts packageParts = entry.getValue();
answer.append("<package ").append(packageFqName).append(": ").append(packageParts.getParts()).append(">\n");
try {
ModuleMapping mapping = ModuleMappingUtilKt.loadModuleMapping(
ModuleMapping.Companion, file.asByteArray(), relativePath.getPath(),
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();
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:
throw new UnsupportedOperationException("Unknown OutputFile: " + file);
answer.append("Unknown output file: ").append(file);
}
}