Throw ClassNotFoundException for missing class resource in ThrowawayClassLoader#36938
Open
junhyeong9812 wants to merge 1 commit into
Open
Conversation
ThrowawayClassLoader.loadClass fell back to loadClassFromResource, which returns null when no class resource is available. Returning null from loadClass violates the ClassLoader contract and leads to a NullPointerException in callers such as PreComputeFieldFeature. Re-throw the original ClassNotFoundException when the resource fallback yields no class. Signed-off-by: junhyeong9812 <[email protected]>
ClassNotFoundException for missing class resource in ThrowawayClassLoader
Member
|
Please rebase on |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
ThrowawayClassLoader.loadClasscan currently returnnull, which violatesthe
ClassLoader.loadClasscontract (it must return a non-nullClassorthrow
ClassNotFoundException). This change makes the fallback path honor thecontract.
Problem
loadClass(name, resolve)delegates tosuper.loadClass(name, true)and, onClassNotFoundException, falls back toloadClassFromResource(name):loadClassFromResourcereturnsnullwhen the.classresource is notavailable:
So
loadClassreturnsnullwhen both the standard delegation and theresource fallback fail to find the class. The caller
PreComputeFieldFeature.provideFieldValueimmediately dereferences the result:Fix
Re-throw the original
ClassNotFoundExceptionwhen the resource fallback yieldsno class, so
loadClassnever returnsnull:The original exception is preserved because it carries the most accurate
delegation-failure detail; the resource fallback only confirms there is no
class-byte resource to define.
Note on impact
This is primarily a contract-correctness and diagnostics fix, not a crash fix.
The
NullPointerExceptioninPreComputeFieldFeatureis caught by a broadcatch (Throwable)that falls back to runtime evaluation of the field, so thebuild does not fail today. The observable improvement is twofold:
loadClasshonors the documentedClassLoadercontract.ClassNotFoundException(with the class name) instead of a confusingNullPointerException.The trigger is narrow: it requires a declaring class whose
.classresource isnot resolvable (e.g. a runtime-generated class) that also cannot be loaded via
delegation.
Tests
Added
ThrowawayClassLoaderTests.loadClassThrowsClassNotFoundExceptionWhenClassResourceIsMissing,which drives
loadClassthrough the fallback with a resource loader thatreturns no class bytes and asserts that
ClassNotFoundExceptionis thrown(previously
loadClassreturnednull)../gradlew :spring-core:testandcheckstyleMain/checkstyleTestpass.