Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7f8cd8c

Browse files
samebGuice Team
authored and
Guice Team
committedApr 14, 2023
Update LineNumbers to ignore all kinds of exceptions when reading classes with ASM, so Guice is less finicky about the precise .class version & ASM version. A while ago I had tried to do this by ignoring UnsupportedOperationException, but per #1654, ASM doesn't always throw UOE.
This change will log a single failure when Guice encounters an exception while reading classfiles and warn that ASM may be out of date. The consequences of this failing _all_ line number reading (due to, say, an accidental bug introduced while parsing classfiles) is that Guice won't emit line numbers for bind statements in modules (as binding source locations). A number of other tests would fail in that scenario, warning us that something is off. PiperOrigin-RevId: 524365509
1 parent d98a4de commit 7f8cd8c

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed
 

‎core/src/com/google/inject/internal/util/LineNumbers.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.lang.reflect.Member;
2828
import java.lang.reflect.Method;
2929
import java.util.Map;
30+
import java.util.logging.Level;
31+
import java.util.logging.Logger;
3032
import org.objectweb.asm.AnnotationVisitor;
3133
import org.objectweb.asm.ClassReader;
3234
import org.objectweb.asm.ClassVisitor;
@@ -42,6 +44,9 @@
4244
*/
4345
final class LineNumbers {
4446

47+
private static final Logger logger = Logger.getLogger(LineNumbers.class.getName());
48+
private static volatile boolean alreadyLoggedReadingFailure;
49+
4550
private static final int ASM_API_LEVEL = Opcodes.ASM9;
4651

4752
private final Class<?> type;
@@ -67,10 +72,20 @@ public LineNumbers(Class<?> type) throws IOException {
6772
if (in != null) {
6873
try {
6974
new ClassReader(in).accept(new LineNumberReader(), ClassReader.SKIP_FRAMES);
70-
} catch (UnsupportedOperationException ignored) {
75+
} catch (Exception ignored) {
7176
// We may be trying to inspect classes that were compiled with a more recent version
7277
// of javac than our ASM supports. If that happens, just ignore the class and don't
73-
// capture line numbers.
78+
// capture line numbers. But log the failure so folks know something's off.
79+
// (Only log it once, though, to avoid spam. It's OK if concurrent access makes this
80+
// happen more than once.)
81+
if (!alreadyLoggedReadingFailure) {
82+
alreadyLoggedReadingFailure = true;
83+
logger.log(
84+
Level.WARNING,
85+
"Failed loading line numbers. ASM is probably out of date. Further failures won't"
86+
+ " be logged.",
87+
ignored);
88+
}
7489
} finally {
7590
try {
7691
in.close();

0 commit comments

Comments
 (0)
Please sign in to comment.