Repository navigation
fix(properties): the class loader fallback of loadProperties(Class, ...) asks the given class (#38) - #54
Merged
Conversation
…s(Class, ...) finds nothing (#38) PropertiesFileExtensions.loadProperties(Class, String) falls back to ClassExtensions.getResourceAsStream(clazz.getClass(), name) when the class loader of the current thread does not find the file. clazz.getClass() is always java.lang.Class, so the fallback asks the boot loader and then the same loader as the first attempt again, never the loader of the given class. The test builds a class loader of its own over a temporary directory, with the platform class loader as its parent, loads ClassLoaderMarker through it once more and puts the properties file next to it, where the class loader of the tests does not see it. For the three ways to name the file (the file name relative to the package of the class, the absolute path with a leading slash, the package path and the file name of the three-argument overload) it pins today's result, null. The fix turns these rows.
…..) asks the given class (#38) loadProperties(Class, String) passed clazz.getClass(), which is always java.lang.Class, to the fallback, so the caller's class loader was never asked; it went unnoticed because the first attempt, the class loader of the current thread, usually finds the file. Found while moving lightblueseas/email-tails to 6.1, whose SendEmail loads its default mail.properties this way. The fallback now passes the given class, as the issue expects, so the name resolves as Class.getResourceAsStream does: relative to the package of the class, or from the root of its class loader with a leading slash. The stream of the fallback is closed after loading. The Javadoc of both overloads says which loader is asked when and that null comes back if neither finds the file, which the code did before as well. Closes #38
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #54 +/- ##
=============================================
+ Coverage 74.36% 74.84% +0.48%
- Complexity 204 205 +1
=============================================
Files 25 25
Lines 831 831
Branches 140 140
=============================================
+ Hits 618 622 +4
+ Misses 134 130 -4
Partials 79 79 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Closes #38
PropertiesFileExtensions.loadProperties(Class, String)fell back toClassExtensions.getResourceAsStream(clazz.getClass(), name).clazz.getClass()is alwaysjava.lang.Class, so the fallback asked the boot loader and then the class loader of the first attempt again, never the loader of the given class; the three-argument overload has the same behaviour. Found while moving lightblueseas/email-tails to 6.1.What changes
test(properties): pin that the class loader fallback of loadProperties(Class, ...) finds nothing (#38)(af7febe)PropertiesFileExtensionsClassLoaderTestbuilds a class loader of its own over a temporary directory (parent: the platform class loader), loads the new test helperClassLoaderMarkerthrough it once more and writes the properties file next to it, where the class loader of the tests does not see it (the setup asserts both). For three ways to name the file (file name relative to the package of the class, absolute path with a leading slash, package path and file name of the three-argument overload) it pins today's result:null.fix(properties): the class loader fallback of loadProperties(Class, ...) asks the given class (#38)(a56902b)ClassExtensions.getResourceAsStream(clazz, name), as the issue expects: the name resolves asClass.getResourceAsStreamdoes; the fallback's stream is closed (try-with-resources)nullcomes back if neither finds the file (as the code did before)found=next to the classEvidence
JDK 17.0.20 (Temurin; the workflow sets up
'17',projectSourceCompatibility=17),./gradlew spotlessApply build --console=plainthrough the build-slot script; counts summed overbuild/test-results/test/TEST-*.xml:The pinning test on af7febe is the counter-run of the fix: the same three rows against the old code give
null.