mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-31 13:42:59 +00:00 
			
		
		
		
	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
This commit is contained in:
		| @@ -56,11 +56,11 @@ public final class Generator<T> | |||||||
|  |  | ||||||
|     private final LoadingCache<Class<?>, List<NamedMethod<T>>> classCache = CacheBuilder |     private final LoadingCache<Class<?>, List<NamedMethod<T>>> classCache = CacheBuilder | ||||||
|         .newBuilder() |         .newBuilder() | ||||||
|         .build( CacheLoader.from( this::build ) ); |         .build( CacheLoader.from( catching( this::build, Collections.emptyList() ) ) ); | ||||||
|  |  | ||||||
|     private final LoadingCache<Method, Optional<T>> methodCache = CacheBuilder |     private final LoadingCache<Method, Optional<T>> methodCache = CacheBuilder | ||||||
|         .newBuilder() |         .newBuilder() | ||||||
|         .build( CacheLoader.from( this::build ) ); |         .build( CacheLoader.from( catching( this::build, Optional.empty() ) ) ); | ||||||
|  |  | ||||||
|     Generator( Class<T> base, List<Class<?>> context, Function<T, T> wrap ) |     Generator( Class<T> base, List<Class<?>> context, Function<T, T> wrap ) | ||||||
|     { |     { | ||||||
| @@ -358,4 +358,22 @@ public final class Generator<T> | |||||||
|             arg.getName(), method.getDeclaringClass().getName(), method.getName() ); |             arg.getName(), method.getDeclaringClass().getName(), method.getName() ); | ||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     @SuppressWarnings( "Guava" ) | ||||||
|  |     private static <T, U> com.google.common.base.Function<T, U> catching( Function<T, U> 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; | ||||||
|  |             } | ||||||
|  |         }; | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jonathan Coates
					Jonathan Coates