ClasspathResources class abstract final

The registry behind classpath: locations: the files of lib/resources/ of the application and of its dependencies.

Dart has no classpath, so the generator embeds every file of a package's lib/resources/ into its generated module, and the files are registered here when the module is created. That is how application.yml, message bundles and templates are found on every platform. A path that is not embedded (a file added while developing without regenerating) is read from lib/resources/<path> on disk. Directories added with addDirectory come first, like src/test/resources before src/main/resources on a test classpath: a file there shadows the embedded file with the same path. A path whose .. segments climb above lib/resources/ (classpath:../../secret) finds nothing, as Java's class loader refuses such a name: resolve gives a resource that does not exist.

Applications only write locations (classpath:templates/mail.txt); resource loaders call this class. Code calls it directly in tests, to add a directory, or to list the embedded files.

// Reads lib/resources/banner.txt, embedded or from disk.
final resource =
    DefaultResourceLoader().getResource('classpath:banner.txt');
log.info(resource.readAsStringSync());

Spring (Java):

Resource resource = new ClassPathResource("banner.txt");

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Static Properties

directories → List<String>
The directories searched before the embedded files, in search order.
no setter
paths → Iterable<String>
The paths of all embedded files, relative to lib/resources/.
no setter

Static Methods

addDirectory(String directory) → void Function()
Makes classpath: look in directory on disk before the embedded files, until the returned function is called. The most recently added directory comes first. spring_boot_starter_test uses it for test/resources/, so test config files win over packaged ones.
migrate(String location) → String
location with the former prefix classpath: (or classpath*:) replaced by resources: (resources*:), after a warning naming the new prefix, once per location. Anything else, an optional: prefix included, is returned as it is.
migrateOptional(String location) → String
location after optional:-aware migrate: optional:classpath:x gives optional:resources:x.
normalize(String path) → String?
path relative to lib/resources/ (without classpath:): a leading / removed, \ read as /, . and empty segments dropped, .. resolved (/config/./app.properties gives config/app.properties). null when .. climbs above the root: nothing there is on the classpath.
registerAll(Map<String, EmbeddedResource> resources, {String? owner}) → void
Registers embedded files, keyed by their path under lib/resources/. Generated modules call it; applications do not.
resetLegacyWarnings() → void
Forgets the locations already warned about, for tests.
resolve(String path) → Resource
The resource for resources:<path> (or the former classpath:) or a plain path: a file of an added directory when it exists there, otherwise the embedded file, otherwise the file under directory (which may not exist). A path that climbs above the root with .. gives a resource that does not exist and cannot be read; so does createRelative of an embedded file. A file read from disk is a FileSystemResource, whose createRelative is a file's.
resolveEmbedded(String path) → Resource
The resource for path, ignoring the added directories: the embedded file, or else the file under directory; nothing for a path that climbs above the root, as in resolve.

Constants

allPrefix → const String
The prefix that searches every root, resources*: (Spring's classpath*:).
directory → const String
The directory read from disk for paths that are not embedded: lib/resources, relative to the working directory.
legacyAllPrefix → const String
The former prefix of the search over every root, classpath*:.
legacyPrefix → const String
The former prefix, classpath:, still read in 0.6.2 with a warning and refused in the next minor release.
prefix → const String
The location prefix, resources:.