From dd6f97622e6c18ce0d8988da6a5bede45c94ca5d Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Wed, 6 Jan 2021 18:21:03 +0000 Subject: [PATCH] Prevent reflection errors crashing the game .getMethods() may throw if a method references classes which don't exist (such as client-only classes on a server). This is an Error, and so is unchecked - hence us not handling it before. Fixes #645 --- .../computercraft/core/asm/Generator.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/dan200/computercraft/core/asm/Generator.java b/src/main/java/dan200/computercraft/core/asm/Generator.java index a57e8e44e..c559c4112 100644 --- a/src/main/java/dan200/computercraft/core/asm/Generator.java +++ b/src/main/java/dan200/computercraft/core/asm/Generator.java @@ -56,11 +56,11 @@ public final class Generator private final LoadingCache, List>> classCache = CacheBuilder .newBuilder() - .build( CacheLoader.from( this::build ) ); + .build( CacheLoader.from( catching( this::build, Collections.emptyList() ) ) ); private final LoadingCache> methodCache = CacheBuilder .newBuilder() - .build( CacheLoader.from( this::build ) ); + .build( CacheLoader.from( catching( this::build, Optional.empty() ) ) ); Generator( Class base, List> context, Function wrap ) { @@ -358,4 +358,22 @@ public final class Generator arg.getName(), method.getDeclaringClass().getName(), method.getName() ); return null; } + + @SuppressWarnings( "Guava" ) + private static com.google.common.base.Function catching( Function function, U def ) + { + return x -> { + try + { + return function.apply( x ); + } + catch( Exception | LinkageError e ) + { + // LinkageError due to possible codegen bugs and NoClassDefFoundError. The latter occurs when fetching + // methods on a class which references non-existent (i.e. client-only) types. + ComputerCraft.log.error( "Error generating @LuaFunctions", e ); + return def; + } + }; + } }