Creating an empty FileCollection without a Project, and without injecting some object

How can I create an empty FileCollection without a Project, and without injecting any other objects? It can be immutable or mutable.

I just want a static method / field, or a constructor to do this, instead of having to pass around / inject some object.

Thanks.

You could probably extend AbstractFileCollection and implement getFiles() to return Collection.emptySet() but I’m not sure I’d recommend this as AbstractFileCollection is in an “internal” package and can change (or be removed) in future gradle versions without warning in a way that is not backwards compatible

Is there a reason why you can’t inject the ObjectFactory or the Project?

Injecting / passing an object just to clear a classpath property seems quite heavyweight for a simple operation.

Maybe I could use filter {false} to filter out all elements of the original classpath, but that seems like a hack, and it should be less performant.

An immutable empty FileCollection singleton seems like it might be useful…

Please note that you haven’t explained your use case yet

My use case is to clear a classpath.

I’m doing it in a helper function that does a few things, including using a higher order function parameter that is used to clear the classpath, then later set it.

An immutable empty FileCollection singleton seems like it might be useful…

I’m hoping you can see that my suggestion of extending AbstractFileCollection would allow you to achieve this

Yes. I just think that such a singleton should probably be provided by Gradle, so people don’t have to make their own. I wasn’t sure if such a singleton was.

I’ve created empty FileCollection via project.files() or empty FileTree via project.files().asFileTree. All of my plugins, tasks and extension objects etc hold a reference to the Project so I’ve never had issues with this

That’s what I’ve done, too. It’s just that, in my current context, the helper method doesn’t otherwise need access to a Project or anything else that can get me a Project, so I wanted to avoid passing or injecting a Project (or FileCollection, etc.) if possible.

Sure… and here you are :smile: