summaryrefslogtreecommitdiff
path: root/java/com/google/devtools/build/android/desugar/TryWithResourcesRewriter.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/com/google/devtools/build/android/desugar/TryWithResourcesRewriter.java')
-rw-r--r--java/com/google/devtools/build/android/desugar/TryWithResourcesRewriter.java59
1 files changed, 40 insertions, 19 deletions
diff --git a/java/com/google/devtools/build/android/desugar/TryWithResourcesRewriter.java b/java/com/google/devtools/build/android/desugar/TryWithResourcesRewriter.java
index 8e6d6d5..98eef45 100644
--- a/java/com/google/devtools/build/android/desugar/TryWithResourcesRewriter.java
+++ b/java/com/google/devtools/build/android/desugar/TryWithResourcesRewriter.java
@@ -29,6 +29,7 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.android.desugar.BytecodeTypeInference.InferredType;
+import com.google.devtools.build.android.desugar.io.BitFlags;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Optional;
@@ -165,14 +166,12 @@ public class TryWithResourcesRewriter extends ClassVisitor {
new CloseResourceMethodSpecializer(cv, resourceInternalName, isInterface));
}
} else {
+ // It is possible that all calls to $closeResources(...) are in dead code regions, and the
+ // calls are eliminated, which leaving the method $closeResources() unused. (b/78030676).
+ // In this case, we just discard the method body.
checkState(
- closeResourceMethod == null,
- "The field resourceTypeInternalNames is empty. "
- + "But the class has the $closeResource method.");
- checkState(
- !hasCloseResourceMethod,
- "The class %s has close resource method, but resourceTypeInternalNames is empty.",
- internalName);
+ !hasCloseResourceMethod || closeResourceMethod != null,
+ "There should be $closeResources(...) in the class file.");
}
super.visitEnd();
}
@@ -311,18 +310,20 @@ public class TryWithResourcesRewriter extends ClassVisitor {
InferredType resourceType = typeInference.getTypeOfOperandFromTop(0);
Optional<String> resourceClassInternalName = resourceType.getInternalName();
- checkState(
- resourceClassInternalName.isPresent(),
- "The resource class %s is not a reference type in %s.%s",
- resourceType,
- internalName,
- methodSignature);
- checkState(
- isAssignableFrom(
- "java.lang.AutoCloseable", resourceClassInternalName.get().replace('/', '.')),
- "The resource type should be a subclass of java.lang.AutoCloseable: %s",
- resourceClassInternalName);
-
+ {
+ // Check the resource type.
+ checkState(
+ resourceClassInternalName.isPresent(),
+ "The resource class %s is not a reference type in %s.%s",
+ resourceType,
+ internalName,
+ methodSignature);
+ String resourceClassName = resourceClassInternalName.get().replace('/', '.');
+ checkState(
+ hasCloseMethod(resourceClassName),
+ "The resource class %s should have a close() method.",
+ resourceClassName);
+ }
resourceTypeInternalNames.add(resourceClassInternalName.get());
super.visitMethodInsn(
opcode,
@@ -356,6 +357,26 @@ public class TryWithResourcesRewriter extends ClassVisitor {
return isAssignableFrom("java.lang.Throwable", owner.replace('/', '.'));
}
+ private boolean hasCloseMethod(String resourceClassName) {
+ try {
+ Class<?> klass = classLoader.loadClass(resourceClassName);
+ klass.getMethod("close");
+ return true;
+ } catch (ClassNotFoundException e) {
+ throw new AssertionError(
+ "Failed to load class "
+ + resourceClassName
+ + " when desugaring method "
+ + internalName
+ + "."
+ + methodSignature,
+ e);
+ } catch (NoSuchMethodException e) {
+ // There is no close() method in the class, so return false.
+ return false;
+ }
+ }
+
private boolean isAssignableFrom(String baseClassName, String subClassName) {
try {
Class<?> baseClass = classLoader.loadClass(baseClassName);