Skip to content

Conversation

@sagacity
Copy link
Contributor

@sagacity sagacity commented Oct 10, 2018

Hi! Based on a brief discussion on Gitter with @snicoll I've added support for filtering resources in
FilteredClassLoader. This can be used when testing things like @ConditionalOnResource.

The current class is a bit tricky to augment since it relies on varargs lists of Predicate<String>. This is why I've chosen to follow the same approach and allow filtering ClassPathResources, but this means it's not possible to filter both classes and resources simultaneously. If this is desired the loader should probably be refactored to use a builder or something, but I didn't want to make this a breaking change.

I've based the pull request on the 2.0.x branch because the master branch doesn't build right now.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Oct 10, 2018
@philwebb philwebb added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Oct 11, 2018
@philwebb philwebb added this to the 2.1.x milestone Oct 11, 2018
@snicoll snicoll self-requested a review October 11, 2018 15:56
Copy link
Member

@snicoll snicoll left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I am not sure that reusing Predicate<String> is a good idea considering that we apply them on both resources and classes. I'll give it some more thoughts and perhaps someone else on the team will have some idea in the meantime.

Feel free to chime in as well!


@Override
public URL getResource(String name) {
for (Predicate<String> filter : this.filters) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be overkill but classes and resources predicates are being applied to both use cases with no way for an implementation to know what type of "resource" needs to be filtered.

Copy link
Contributor Author

@sagacity sagacity Oct 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No exactly, this is why I found the current design a bit unfortunate. If there's no objection to changing FilteredClassLoader a bit more (and adjusting the tests currently using it), I could take a stab at refactoring into a builder idea, similar to ApplicationContextRunner.

Is this something that is desired/acceptable?

@snicoll snicoll self-assigned this Oct 12, 2018
@snicoll snicoll modified the milestones: 2.1.x, 2.1.0.RC1 Oct 12, 2018
@snicoll snicoll closed this in baf83ae Oct 12, 2018
snicoll added a commit that referenced this pull request Oct 12, 2018
* pr/14774:
  Polish "Allow ClassPathResources to be filtered by FilteredClassLoader"
  Allow ClassPathResources to be filtered by FilteredClassLoader
@snicoll
Copy link
Member

snicoll commented Oct 12, 2018

@RoyJacobs thank you for making your first contribution to Spring Boot. I've merged it with a polish commit. I've decided in the end to split the internals with two filter lists so that only the case where raw filters are specified are affected by that side effect we've discussed.

@sagacity
Copy link
Contributor Author

Fair enough :)

@sagacity sagacity deleted the filtered-resources branch October 12, 2018 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants