Disable desugaring try-with-resources when --min_sdk_version is above 18.
RELNOTES: None
@@ -241,6 +241,7 @@ class Desugar {
private final boolean outputJava7;
private final boolean allowDefaultMethods;
+ private final boolean allowTryWithResources;
private final boolean allowCallsToObjectsNonNull;
private final boolean allowCallsToLongCompare;
/** An instance of Desugar is expected to be used ONLY ONCE */
@@ -254,6 +255,8 @@ class Desugar {
this.outputJava7 = options.minSdkVersion < 24;
this.allowDefaultMethods =
options.desugarInterfaceMethodBodiesIfNeeded || options.minSdkVersion >= 24;
+ this.allowTryWithResources =
+ !options.desugarTryWithResourcesIfNeeded || options.minSdkVersion >= 19;
this.allowCallsToObjectsNonNull = options.minSdkVersion >= 19;
this.allowCallsToLongCompare = options.minSdkVersion >= 19 && !options.alwaysRewriteLongCompare;
this.used = false;
@@ -350,9 +353,7 @@ class Desugar {
}
private void copyThrowableExtensionClass(OutputFileProvider outputFileProvider) {
- if (!outputJava7
- || !options.desugarTryWithResourcesIfNeeded
- || options.desugarTryWithResourcesOmitRuntimeClasses) {
+ if (allowTryWithResources || options.desugarTryWithResourcesOmitRuntimeClasses) {
// try-with-resources statements are okay in the output jar.
return;
}
@@ -485,15 +486,14 @@ class Desugar {
LambdaInfo lambdaClass,
UnprefixingClassWriter writer) {
ClassVisitor visitor = checkNotNull(writer);
-
- if (outputJava7) {
- // null ClassReaderFactory b/c we don't expect to need it for lambda classes
- visitor = new Java7Compatibility(visitor, (ClassReaderFactory) null);
- if (options.desugarTryWithResourcesIfNeeded) {
+ if (!allowTryWithResources) {
visitor =
new TryWithResourcesRewriter(
visitor, loader, visitedExceptionTypes, numOfTryWithResourcesInvoked);
}
+ if (outputJava7) {
+ // null ClassReaderFactory b/c we don't expect to need it for lambda classes
+ visitor = new Java7Compatibility(visitor, (ClassReaderFactory) null);
if (options.desugarInterfaceMethodBodiesIfNeeded) {
visitor =
new DefaultMethodClassFixer(visitor, classpathReader, bootclasspathReader, loader);
@@ -538,15 +538,14 @@ class Desugar {
UnprefixingClassWriter writer,
ImmutableSet<MethodInfo> lambdaMethodsUsedInInvokeDynamic) {
ClassVisitor visitor = checkNotNull(writer);
-
- if (!options.onlyDesugarJavac9ForLint) {
- if (outputJava7) {
- visitor = new Java7Compatibility(visitor, classpathReader);
- if (options.desugarTryWithResourcesIfNeeded) {
+ if (!allowTryWithResources) {
visitor =
new TryWithResourcesRewriter(
visitor, loader, visitedExceptionTypes, numOfTryWithResourcesInvoked);
}
+ if (!options.onlyDesugarJavac9ForLint) {
+ if (outputJava7) {
+ visitor = new Java7Compatibility(visitor, classpathReader);
if (options.desugarInterfaceMethodBodiesIfNeeded) {
visitor =
new DefaultMethodClassFixer(visitor, classpathReader, bootclasspathReader, loader);
@@ -562,7 +561,6 @@ class Desugar {
lambdaMethodsUsedInInvokeDynamic,
allowDefaultMethods);
}
-
if (!allowCallsToObjectsNonNull) {
visitor = new ObjectsRequireNonNullMethodRewriter(visitor);
}
