Package io.github.classgraph
Class ResourceList
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- java.util.ArrayList<T>
-
- io.github.classgraph.PotentiallyUnmodifiableList<Resource>
-
- io.github.classgraph.ResourceList
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.AutoCloseable
,java.lang.Cloneable
,java.lang.Iterable<Resource>
,java.util.Collection<Resource>
,java.util.List<Resource>
,java.util.RandomAccess
public class ResourceList extends PotentiallyUnmodifiableList<Resource> implements java.lang.AutoCloseable
An AutoCloseable list of AutoCloseableResource
objects.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ResourceList.ByteArrayConsumer
AFunctionalInterface
for consuming the contents of aResource
as a byte array.static interface
ResourceList.ByteArrayConsumerThrowsIOException
AFunctionalInterface
for consuming the contents of aResource
as a byte array, throwingIOException
to the caller if an IO exception occurs.static interface
ResourceList.ByteBufferConsumer
static interface
ResourceList.ByteBufferConsumerThrowsIOException
AFunctionalInterface
for consuming the contents of aResource
as aByteBuffer
, throwingIOException
to the caller if an IO exception occurs.static interface
ResourceList.InputStreamConsumer
static interface
ResourceList.InputStreamConsumerThrowsIOException
AFunctionalInterface
for consuming the contents of aResource
as anInputStream
, throwingIOException
to the caller if an IO exception occurs.static interface
ResourceList.ResourceFilter
Filter aResourceList
using a predicate mapping aResource
object to a boolean, producing anotherResourceList
for all items in the list for which the predicate is true.
-
Field Summary
Fields Modifier and Type Field Description private static ResourceList.ResourceFilter
CLASSFILE_FILTER
Returns true if a Resource has a path ending in ".class".(package private) static ResourceList
EMPTY_LIST
An unmodifiable emptyResourceList
.(package private) static long
serialVersionUID
serialVersionUID.-
Fields inherited from class io.github.classgraph.PotentiallyUnmodifiableList
modifiable
-
-
Constructor Summary
Constructors Constructor Description ResourceList()
Create a new modifiable empty list ofResource
objects.ResourceList(int sizeHint)
Create a new modifiable empty list ofResource
objects, given a size hint.ResourceList(java.util.Collection<Resource> resourceCollection)
Create a new modifiable emptyResourceList
, given an initial collection ofResource
objects.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.util.Map<java.lang.String,ResourceList>
asMap()
Return thisResourceList
as a map from resource path (obtained fromResource.getPath()
) to aResourceList
ofResource
objects that have that path.ResourceList
classFilesOnly()
Return a newResourceList
consisting of only the resources with the filename extension ".class".void
close()
Close all theResource
objects in thisResourceList
.static ResourceList
emptyList()
Return an unmodifiable emptyResourceList
.ResourceList
filter(ResourceList.ResourceFilter filter)
Find the subset of theResource
objects in this list for which the given filter predicate is true.java.util.List<java.util.Map.Entry<java.lang.String,ResourceList>>
findDuplicatePaths()
Find duplicate resource paths within thisResourceList
.void
forEachByteArray(ResourceList.ByteArrayConsumer byteArrayConsumer)
Deprecated.void
forEachByteArray(ResourceList.ByteArrayConsumer byteArrayConsumer, boolean ignoreIOExceptions)
void
forEachByteArrayIgnoringIOException(ResourceList.ByteArrayConsumer byteArrayConsumer)
Fetch the content of eachResource
in thisResourceList
as a byte array, pass the byte array to the givenResourceList.ByteArrayConsumer
, then close the underlying InputStream or release the underlying ByteBuffer by callingResource.close()
for eachResource
.void
forEachByteArrayThrowingIOException(ResourceList.ByteArrayConsumerThrowsIOException byteArrayConsumerThrowsIOException)
Fetch the content of eachResource
in thisResourceList
as a byte array, pass the byte array to the givenResourceList.ByteArrayConsumer
, then close the underlying InputStream or release the underlying ByteBuffer by callingResource.close()
.void
forEachByteBuffer(ResourceList.ByteBufferConsumer byteBufferConsumer)
Deprecated.void
forEachByteBuffer(ResourceList.ByteBufferConsumer byteBufferConsumer, boolean ignoreIOExceptions)
void
forEachByteBufferIgnoringIOException(ResourceList.ByteBufferConsumer byteBufferConsumer)
Read eachResource
in thisResourceList
as aByteBuffer
, pass theByteBuffer
to the givenResourceList.InputStreamConsumer
, then release theByteBuffer
after theResourceList.ByteBufferConsumer
returns, by callingResource.close()
for eachResource
.void
forEachByteBufferThrowingIOException(ResourceList.ByteBufferConsumerThrowsIOException byteBufferConsumerThrowsIOException)
Read eachResource
in thisResourceList
as aByteBuffer
, pass theByteBuffer
to the givenResourceList.InputStreamConsumer
, then release theByteBuffer
after theResourceList.ByteBufferConsumer
returns, by callingResource.close()
.void
forEachInputStream(ResourceList.InputStreamConsumer inputStreamConsumer)
Deprecated.void
forEachInputStream(ResourceList.InputStreamConsumer inputStreamConsumer, boolean ignoreIOExceptions)
void
forEachInputStreamIgnoringIOException(ResourceList.InputStreamConsumer inputStreamConsumer)
Fetch anInputStream
for eachResource
in thisResourceList
, pass theInputStream
to the givenResourceList.InputStreamConsumer
, then close theInputStream
after theResourceList.InputStreamConsumer
returns, by callingResource.close()
for eachResource
.void
forEachInputStreamThrowingIOException(ResourceList.InputStreamConsumerThrowsIOException inputStreamConsumerThrowsIOException)
Fetch anInputStream
for eachResource
in thisResourceList
, pass theInputStream
to the givenResourceList.InputStreamConsumer
, then close theInputStream
after theResourceList.InputStreamConsumer
returns, by callingResource.close()
.ResourceList
get(java.lang.String resourcePath)
Returns a list of all resources with the requested path.java.util.List<java.lang.String>
getPaths()
Get the paths of all resources in this list relative to the package root.java.util.List<java.lang.String>
getPathsRelativeToClasspathElement()
Get the paths of all resources in this list relative to the root of the classpath element.java.util.List<java.net.URI>
getURIs()
Get the URIs of all resources in this list, by callingResource.getURI()
for each item in the list.java.util.List<java.net.URL>
getURLs()
Get the URLs of all resources in this list, by callingResource.getURL()
for each item in the list.ResourceList
nonClassFilesOnly()
Return a newResourceList
consisting of non-classfile resources only.-
Methods inherited from class io.github.classgraph.PotentiallyUnmodifiableList
add, add, addAll, addAll, clear, equals, hashCode, iterator, listIterator, makeUnmodifiable, remove, remove, removeAll, retainAll, set
-
Methods inherited from class java.util.ArrayList
clone, contains, ensureCapacity, forEach, get, indexOf, isEmpty, lastIndexOf, listIterator, removeIf, removeRange, replaceAll, size, sort, spliterator, subList, toArray, toArray, trimToSize
-
-
-
-
Field Detail
-
serialVersionUID
static final long serialVersionUID
serialVersionUID.- See Also:
- Constant Field Values
-
EMPTY_LIST
static final ResourceList EMPTY_LIST
An unmodifiable emptyResourceList
.
-
CLASSFILE_FILTER
private static final ResourceList.ResourceFilter CLASSFILE_FILTER
Returns true if a Resource has a path ending in ".class".
-
-
Constructor Detail
-
ResourceList
public ResourceList()
Create a new modifiable empty list ofResource
objects.
-
ResourceList
public ResourceList(int sizeHint)
Create a new modifiable empty list ofResource
objects, given a size hint.- Parameters:
sizeHint
- the size hint
-
ResourceList
public ResourceList(java.util.Collection<Resource> resourceCollection)
Create a new modifiable emptyResourceList
, given an initial collection ofResource
objects.- Parameters:
resourceCollection
- the collection ofResource
objects.
-
-
Method Detail
-
emptyList
public static ResourceList emptyList()
Return an unmodifiable emptyResourceList
.- Returns:
- the unmodifiable empty
ResourceList
.
-
get
public ResourceList get(java.lang.String resourcePath)
Returns a list of all resources with the requested path. (There may be more than one resource with a given path, from different classpath elements or modules, so this returns aResourceList
rather than a singleResource
.)- Parameters:
resourcePath
- The path of a resource- Returns:
- A
ResourceList
ofResource
objects in this list that have the given path (there may be more than one resource with a given path, from different classpath elements or modules, so this returns aResourceList
rather than a singleResource
.) Returns the empty list if no resource with is found with a matching path.
-
getPaths
public java.util.List<java.lang.String> getPaths()
Get the paths of all resources in this list relative to the package root.- Returns:
- The paths of all resources in this list relative to the package root, by calling
Resource.getPath()
for each item in the list.
-
getPathsRelativeToClasspathElement
public java.util.List<java.lang.String> getPathsRelativeToClasspathElement()
Get the paths of all resources in this list relative to the root of the classpath element.- Returns:
- The paths of all resources in this list relative to the root of the classpath element, by calling
Resource.getPathRelativeToClasspathElement()
for each item in the list.
-
getURLs
public java.util.List<java.net.URL> getURLs()
Get the URLs of all resources in this list, by callingResource.getURL()
for each item in the list. Note that any resource with ajrt:
URI (e.g. a system resource, or a resource from a jlink'd image) will causeIllegalArgumentException
to be thrown, sinceURL
does not support this scheme, sogetURIs()
is strongly preferred overgetURLs()
.- Returns:
- The URLs of all resources in this list.
-
getURIs
public java.util.List<java.net.URI> getURIs()
Get the URIs of all resources in this list, by callingResource.getURI()
for each item in the list.- Returns:
- The URIs of all resources in this list.
-
classFilesOnly
public ResourceList classFilesOnly()
Return a newResourceList
consisting of only the resources with the filename extension ".class".- Returns:
- A new
ResourceList
consisting of only the resources with the filename extension ".class".
-
nonClassFilesOnly
public ResourceList nonClassFilesOnly()
Return a newResourceList
consisting of non-classfile resources only.- Returns:
- A new
ResourceList
consisting of only the resources that do not have the filename extension ".class".
-
asMap
public java.util.Map<java.lang.String,ResourceList> asMap()
Return thisResourceList
as a map from resource path (obtained fromResource.getPath()
) to aResourceList
ofResource
objects that have that path.- Returns:
- This
ResourceList
as a map from resource path (obtained fromResource.getPath()
) to aResourceList
ofResource
objects that have that path.
-
findDuplicatePaths
public java.util.List<java.util.Map.Entry<java.lang.String,ResourceList>> findDuplicatePaths()
Find duplicate resource paths within thisResourceList
.- Returns:
- A
List
ofMap.Entry
objects for all resources in the classpath and/or module path that have a non-unique path (i.e. where there are at least two resources with the same path). The key of each returnedMap.Entry
is the path (obtained fromResource.getPath()
), and the value is aResourceList
of at least two uniqueResource
objects that have that path.
-
filter
public ResourceList filter(ResourceList.ResourceFilter filter)
Find the subset of theResource
objects in this list for which the given filter predicate is true.- Parameters:
filter
- TheResourceList.ResourceFilter
to apply.- Returns:
- The subset of the
Resource
objects in this list for which the given filter predicate is true.
-
forEachByteArray
@Deprecated public void forEachByteArray(ResourceList.ByteArrayConsumer byteArrayConsumer, boolean ignoreIOExceptions)
Deprecated.Fetch the content of eachResource
in thisResourceList
as a byte array, pass the byte array to the givenResourceList.ByteArrayConsumer
, then close the underlying InputStream or release the underlying ByteBuffer by callingResource.close()
.- Parameters:
byteArrayConsumer
- TheResourceList.ByteArrayConsumer
.ignoreIOExceptions
- if true, anyIOException
thrown while trying to load any of the resources will be silently ignored.- Throws:
java.lang.IllegalArgumentException
- if ignoreExceptions is false, and anIOException
is thrown while trying to load any of the resources.
-
forEachByteArray
@Deprecated public void forEachByteArray(ResourceList.ByteArrayConsumer byteArrayConsumer)
Deprecated.Fetch the content of eachResource
in thisResourceList
as a byte array, pass the byte array to the givenResourceList.ByteArrayConsumer
, then close the underlying InputStream or release the underlying ByteBuffer by callingResource.close()
.- Parameters:
byteArrayConsumer
- TheResourceList.ByteArrayConsumer
.- Throws:
java.lang.IllegalArgumentException
- if anIOException
is thrown while trying to load any of the resources.
-
forEachByteArrayIgnoringIOException
public void forEachByteArrayIgnoringIOException(ResourceList.ByteArrayConsumer byteArrayConsumer)
Fetch the content of eachResource
in thisResourceList
as a byte array, pass the byte array to the givenResourceList.ByteArrayConsumer
, then close the underlying InputStream or release the underlying ByteBuffer by callingResource.close()
for eachResource
. If anIOException
occurs while opening or reading from any resource, the resource is silently skipped.- Parameters:
byteArrayConsumer
- TheResourceList.ByteArrayConsumer
.
-
forEachByteArrayThrowingIOException
public void forEachByteArrayThrowingIOException(ResourceList.ByteArrayConsumerThrowsIOException byteArrayConsumerThrowsIOException) throws java.io.IOException
Fetch the content of eachResource
in thisResourceList
as a byte array, pass the byte array to the givenResourceList.ByteArrayConsumer
, then close the underlying InputStream or release the underlying ByteBuffer by callingResource.close()
.- Parameters:
byteArrayConsumerThrowsIOException
- TheResourceList.ByteArrayConsumerThrowsIOException
.- Throws:
java.io.IOException
- if trying to load any of the resources results in anIOException
being thrown.
-
forEachInputStream
@Deprecated public void forEachInputStream(ResourceList.InputStreamConsumer inputStreamConsumer, boolean ignoreIOExceptions)
Deprecated.Fetch anInputStream
for eachResource
in thisResourceList
, pass theInputStream
to the givenResourceList.InputStreamConsumer
, then close theInputStream
after theResourceList.InputStreamConsumer
returns, by callingResource.close()
for eachResource
.- Parameters:
inputStreamConsumer
- TheResourceList.InputStreamConsumer
.ignoreIOExceptions
- if true, anyIOException
thrown while trying to load any of the resources will be silently ignored.- Throws:
java.lang.IllegalArgumentException
- if ignoreExceptions is false, and anIOException
is thrown while trying to open any of the resources.
-
forEachInputStream
@Deprecated public void forEachInputStream(ResourceList.InputStreamConsumer inputStreamConsumer)
Deprecated.Fetch anInputStream
for eachResource
in thisResourceList
, pass theInputStream
to the givenResourceList.InputStreamConsumer
, then close theInputStream
after theResourceList.InputStreamConsumer
returns, by callingResource.close()
for eachResource
.- Parameters:
inputStreamConsumer
- TheResourceList.InputStreamConsumer
.- Throws:
java.lang.IllegalArgumentException
- anIOException
is thrown while trying to open any of the resources.
-
forEachInputStreamIgnoringIOException
public void forEachInputStreamIgnoringIOException(ResourceList.InputStreamConsumer inputStreamConsumer)
Fetch anInputStream
for eachResource
in thisResourceList
, pass theInputStream
to the givenResourceList.InputStreamConsumer
, then close theInputStream
after theResourceList.InputStreamConsumer
returns, by callingResource.close()
for eachResource
. If anIOException
occurs while opening or reading from any resource, the resource is silently skipped.- Parameters:
inputStreamConsumer
- TheResourceList.InputStreamConsumer
.
-
forEachInputStreamThrowingIOException
public void forEachInputStreamThrowingIOException(ResourceList.InputStreamConsumerThrowsIOException inputStreamConsumerThrowsIOException) throws java.io.IOException
Fetch anInputStream
for eachResource
in thisResourceList
, pass theInputStream
to the givenResourceList.InputStreamConsumer
, then close theInputStream
after theResourceList.InputStreamConsumer
returns, by callingResource.close()
.- Parameters:
inputStreamConsumerThrowsIOException
- TheResourceList.InputStreamConsumerThrowsIOException
.- Throws:
java.io.IOException
- if trying to open or read from any of the resources results in anIOException
being thrown.
-
forEachByteBuffer
@Deprecated public void forEachByteBuffer(ResourceList.ByteBufferConsumer byteBufferConsumer, boolean ignoreIOExceptions)
Deprecated.Read eachResource
in thisResourceList
as aByteBuffer
, pass theByteBuffer
to the givenResourceList.InputStreamConsumer
, then release theByteBuffer
after theResourceList.ByteBufferConsumer
returns, by callingResource.close()
for eachResource
.- Parameters:
byteBufferConsumer
- TheResourceList.ByteBufferConsumer
.ignoreIOExceptions
- if true, anyIOException
thrown while trying to load any of the resources will be silently ignored.- Throws:
java.lang.IllegalArgumentException
- if ignoreExceptions is false, and anIOException
is thrown while trying to load any of the resources.
-
forEachByteBuffer
@Deprecated public void forEachByteBuffer(ResourceList.ByteBufferConsumer byteBufferConsumer)
Deprecated.Read eachResource
in thisResourceList
as aByteBuffer
, pass theByteBuffer
to the givenResourceList.InputStreamConsumer
, then release theByteBuffer
after theResourceList.ByteBufferConsumer
returns, by callingResource.close()
for eachResource
.- Parameters:
byteBufferConsumer
- TheResourceList.ByteBufferConsumer
.- Throws:
java.lang.IllegalArgumentException
- if anIOException
is thrown while trying to load any of the resources.
-
forEachByteBufferIgnoringIOException
public void forEachByteBufferIgnoringIOException(ResourceList.ByteBufferConsumer byteBufferConsumer)
Read eachResource
in thisResourceList
as aByteBuffer
, pass theByteBuffer
to the givenResourceList.InputStreamConsumer
, then release theByteBuffer
after theResourceList.ByteBufferConsumer
returns, by callingResource.close()
for eachResource
. If anIOException
occurs while opening or reading from any resource, the resource is silently skipped.- Parameters:
byteBufferConsumer
- TheResourceList.ByteBufferConsumer
.
-
forEachByteBufferThrowingIOException
public void forEachByteBufferThrowingIOException(ResourceList.ByteBufferConsumerThrowsIOException byteBufferConsumerThrowsIOException) throws java.io.IOException
Read eachResource
in thisResourceList
as aByteBuffer
, pass theByteBuffer
to the givenResourceList.InputStreamConsumer
, then release theByteBuffer
after theResourceList.ByteBufferConsumer
returns, by callingResource.close()
.- Parameters:
byteBufferConsumerThrowsIOException
- TheResourceList.ByteBufferConsumerThrowsIOException
.- Throws:
java.io.IOException
- if trying to load any of the resources results in anIOException
being thrown.
-
close
public void close()
Close all theResource
objects in thisResourceList
.- Specified by:
close
in interfacejava.lang.AutoCloseable
-
-