| Index: packages/analyzer/lib/src/generated/source_io.dart
|
| diff --git a/packages/analyzer/lib/src/generated/source_io.dart b/packages/analyzer/lib/src/generated/source_io.dart
|
| index 4708aa0b97a905835f80482b8caae8182ceb9a38..43dfb42947a6e0977e916b053dddcd9cda5b57b6 100644
|
| --- a/packages/analyzer/lib/src/generated/source_io.dart
|
| +++ b/packages/analyzer/lib/src/generated/source_io.dart
|
| @@ -2,22 +2,23 @@
|
| // for details. All rights reserved. Use of this source code is governed by a
|
| // BSD-style license that can be found in the LICENSE file.
|
|
|
| -library engine.source.io;
|
| +library analyzer.src.generated.source_io;
|
|
|
| import 'dart:collection';
|
|
|
| -import 'engine.dart';
|
| -import 'java_core.dart';
|
| -import 'java_engine.dart';
|
| -import 'java_io.dart';
|
| -import 'source.dart';
|
| +import 'package:analyzer/exception/exception.dart';
|
| +import 'package:analyzer/src/generated/engine.dart';
|
| +import 'package:analyzer/src/generated/java_io.dart';
|
| +import 'package:analyzer/src/generated/source.dart';
|
| +import 'package:path/path.dart' as path;
|
|
|
| -export 'source.dart';
|
| +export 'package:analyzer/src/generated/source.dart';
|
|
|
| /**
|
| * Instances of the class [DirectoryBasedSourceContainer] represent a source container that
|
| * contains all sources within a given directory.
|
| */
|
| +@deprecated
|
| class DirectoryBasedSourceContainer implements SourceContainer {
|
| /**
|
| * The container's path (not `null`).
|
| @@ -81,6 +82,52 @@ class DirectoryBasedSourceContainer implements SourceContainer {
|
| }
|
| }
|
|
|
| +/**
|
| + * Instances of the class [ExplicitSourceResolver] map URIs to files on disk
|
| + * using a fixed mapping provided at construction time.
|
| + */
|
| +@deprecated
|
| +class ExplicitSourceResolver extends UriResolver {
|
| + final Map<Uri, JavaFile> uriToFileMap;
|
| + final Map<String, Uri> pathToUriMap;
|
| +
|
| + /**
|
| + * Construct an [ExplicitSourceResolver] based on the given [uriToFileMap].
|
| + */
|
| + ExplicitSourceResolver(Map<Uri, JavaFile> uriToFileMap)
|
| + : uriToFileMap = uriToFileMap,
|
| + pathToUriMap = _computePathToUriMap(uriToFileMap);
|
| +
|
| + @override
|
| + Source resolveAbsolute(Uri uri, [Uri actualUri]) {
|
| + JavaFile file = uriToFileMap[uri];
|
| + actualUri ??= uri;
|
| + if (file == null) {
|
| + return new NonExistingSource(
|
| + uri.toString(), actualUri, UriKind.fromScheme(actualUri.scheme));
|
| + } else {
|
| + return new FileBasedSource(file, actualUri);
|
| + }
|
| + }
|
| +
|
| + @override
|
| + Uri restoreAbsolute(Source source) {
|
| + return pathToUriMap[source.fullName];
|
| + }
|
| +
|
| + /**
|
| + * Build the inverse mapping of [uriToSourceMap].
|
| + */
|
| + static Map<String, Uri> _computePathToUriMap(
|
| + Map<Uri, JavaFile> uriToSourceMap) {
|
| + Map<String, Uri> pathToUriMap = <String, Uri>{};
|
| + uriToSourceMap.forEach((Uri uri, JavaFile file) {
|
| + pathToUriMap[file.getAbsolutePath()] = uri;
|
| + });
|
| + return pathToUriMap;
|
| + }
|
| +}
|
| +
|
| /**
|
| * Instances of the class `FileBasedSource` implement a source that represents a file.
|
| */
|
| @@ -102,6 +149,7 @@ class FileBasedSource extends Source {
|
| /**
|
| * The URI from which this source was originally derived.
|
| */
|
| + @override
|
| final Uri uri;
|
|
|
| /**
|
| @@ -130,32 +178,10 @@ class FileBasedSource extends Source {
|
| * derived, otherwise a `file:` URI will be created based on the [file].
|
| */
|
| FileBasedSource(JavaFile file, [Uri uri])
|
| - : this.uri = (uri == null ? file.toURI() : uri),
|
| + : this.uri = uri ?? file.toURI(),
|
| this.file = file,
|
| id = _idTable.putIfAbsent(
|
| - '${uri == null ? file.toURI() : uri}@${file.getPath()}',
|
| - () => _idTable.length);
|
| -
|
| - /**
|
| - * Initialize a newly created source object.
|
| - *
|
| - * @param file the file represented by this source
|
| - */
|
| - @deprecated // Use new FileBasedSource(file)
|
| - FileBasedSource.con1(JavaFile file) : this(file);
|
| -
|
| - /**
|
| - * Initialize a newly created source object.
|
| - *
|
| - * @param file the file represented by this source
|
| - * @param uri the URI from which this source was originally derived
|
| - */
|
| - @deprecated // Use new FileBasedSource(file, uri)
|
| - FileBasedSource.con2(Uri uri, JavaFile file)
|
| - : uri = uri,
|
| - file = file,
|
| - id = _idTable.putIfAbsent(
|
| - '$uri@${file.getPath()}', () => _idTable.length);
|
| + '${uri ?? file.toURI()}@${file.getPath()}', () => _idTable.length);
|
|
|
| @override
|
| TimestampedData<String> get contents {
|
| @@ -167,7 +193,7 @@ class FileBasedSource extends Source {
|
| /**
|
| * Get the contents and timestamp of the underlying file.
|
| *
|
| - * Clients should consider using the the method [AnalysisContext.getContents]
|
| + * Clients should consider using the method [AnalysisContext.getContents]
|
| * because contexts can have local overrides of the content of a source that the source is not
|
| * aware of.
|
| *
|
| @@ -197,7 +223,7 @@ class FileBasedSource extends Source {
|
| }
|
|
|
| @override
|
| - int get hashCode => id;
|
| + int get hashCode => uri.hashCode;
|
|
|
| @override
|
| bool get isInSystemLibrary => uri.scheme == DartUriResolver.DART_SCHEME;
|
| @@ -211,49 +237,22 @@ class FileBasedSource extends Source {
|
| @override
|
| UriKind get uriKind {
|
| String scheme = uri.scheme;
|
| - if (scheme == PackageUriResolver.PACKAGE_SCHEME) {
|
| - return UriKind.PACKAGE_URI;
|
| - } else if (scheme == DartUriResolver.DART_SCHEME) {
|
| - return UriKind.DART_URI;
|
| - } else if (scheme == FileUriResolver.FILE_SCHEME) {
|
| - return UriKind.FILE_URI;
|
| - }
|
| - return UriKind.FILE_URI;
|
| + return UriKind.fromScheme(scheme);
|
| }
|
|
|
| @override
|
| - bool operator ==(Object object) =>
|
| - object is FileBasedSource && id == object.id;
|
| + bool operator ==(Object object) {
|
| + if (object is FileBasedSource) {
|
| + return id == object.id;
|
| + } else if (object is Source) {
|
| + return uri == object.uri;
|
| + }
|
| + return false;
|
| + }
|
|
|
| @override
|
| bool exists() => file.isFile();
|
|
|
| - @override
|
| - Uri resolveRelativeUri(Uri containedUri) {
|
| - try {
|
| - Uri baseUri = uri;
|
| - bool isOpaque = uri.isAbsolute && !uri.path.startsWith('/');
|
| - if (isOpaque) {
|
| - String scheme = uri.scheme;
|
| - String part = uri.path;
|
| - if (scheme == DartUriResolver.DART_SCHEME && part.indexOf('/') < 0) {
|
| - part = "$part/$part.dart";
|
| - }
|
| - baseUri = parseUriWithException("$scheme:/$part");
|
| - }
|
| - Uri result = baseUri.resolveUri(containedUri);
|
| - if (isOpaque) {
|
| - result = parseUriWithException(
|
| - "${result.scheme}:${result.path.substring(1)}");
|
| - }
|
| - return result;
|
| - } catch (exception, stackTrace) {
|
| - throw new AnalysisException(
|
| - "Could not resolve URI ($containedUri) relative to source ($uri)",
|
| - new CaughtException(exception, stackTrace));
|
| - }
|
| - }
|
| -
|
| @override
|
| String toString() {
|
| if (file == null) {
|
| @@ -265,7 +264,11 @@ class FileBasedSource extends Source {
|
|
|
| /**
|
| * Instances of the class `FileUriResolver` resolve `file` URI's.
|
| + *
|
| + * This class is now deprecated, 'new FileUriResolver()' is equivalent to
|
| + * 'new ResourceUriResolver(PhysicalResourceProvider.INSTANCE)'.
|
| */
|
| +@deprecated
|
| class FileUriResolver extends UriResolver {
|
| /**
|
| * The name of the `file` scheme.
|
| @@ -277,8 +280,7 @@ class FileUriResolver extends UriResolver {
|
| if (!isFileUri(uri)) {
|
| return null;
|
| }
|
| - return new FileBasedSource(
|
| - new JavaFile.fromUri(uri), actualUri != null ? actualUri : uri);
|
| + return new FileBasedSource(new JavaFile.fromUri(uri), actualUri ?? uri);
|
| }
|
|
|
| @override
|
| @@ -349,6 +351,7 @@ class LocalSourcePredicate_TRUE implements LocalSourcePredicate {
|
| * should be canonicalized, but to preserve relative links within a package, the remainder of the
|
| * path from the package directory to the leaf should not.
|
| */
|
| +@deprecated
|
| class PackageUriResolver extends UriResolver {
|
| /**
|
| * The name of the `package` scheme.
|
| @@ -374,7 +377,7 @@ class PackageUriResolver extends UriResolver {
|
| */
|
| PackageUriResolver(this._packagesDirectories) {
|
| if (_packagesDirectories.length < 1) {
|
| - throw new IllegalArgumentException(
|
| + throw new ArgumentError(
|
| "At least one package directory must be provided");
|
| }
|
| }
|
| @@ -405,7 +408,7 @@ class PackageUriResolver extends UriResolver {
|
| JavaFile pkgDir = new JavaFile.relative(packagesDirectory, pkgName);
|
| try {
|
| pkgDir = pkgDir.getCanonicalFile();
|
| - } on JavaIOException catch (exception, stackTrace) {
|
| + } catch (exception, stackTrace) {
|
| if (!exception.toString().contains("Required key not available")) {
|
| AnalysisEngine.instance.logger.logError("Canonical failed: $pkgDir",
|
| new CaughtException(exception, stackTrace));
|
| @@ -453,31 +456,33 @@ class PackageUriResolver extends UriResolver {
|
| if (resolvedFile.exists()) {
|
| JavaFile canonicalFile =
|
| getCanonicalFile(packagesDirectory, pkgName, relPath);
|
| + if (actualUri != null) {
|
| + return new FileBasedSource(canonicalFile, actualUri);
|
| + }
|
| if (_isSelfReference(packagesDirectory, canonicalFile)) {
|
| uri = canonicalFile.toURI();
|
| }
|
| - return new FileBasedSource(
|
| - canonicalFile, actualUri != null ? actualUri : uri);
|
| + return new FileBasedSource(canonicalFile, uri);
|
| }
|
| }
|
| return new FileBasedSource(
|
| getCanonicalFile(_packagesDirectories[0], pkgName, relPath),
|
| - actualUri != null ? actualUri : uri);
|
| + actualUri ?? uri);
|
| }
|
|
|
| @override
|
| Uri restoreAbsolute(Source source) {
|
| - String sourcePath = source.fullName;
|
| + String sourceUri = _toFileUri(source.fullName);
|
| for (JavaFile packagesDirectory in _packagesDirectories) {
|
| List<JavaFile> pkgFolders = packagesDirectory.listFiles();
|
| if (pkgFolders != null) {
|
| for (JavaFile pkgFolder in pkgFolders) {
|
| try {
|
| - String pkgCanonicalPath = pkgFolder.getCanonicalPath();
|
| - if (sourcePath.startsWith(pkgCanonicalPath)) {
|
| - String relPath = sourcePath.substring(pkgCanonicalPath.length);
|
| - return parseUriWithException(
|
| - "$PACKAGE_SCHEME:${pkgFolder.getName()}$relPath");
|
| + String pkgCanonicalUri = _toFileUri(pkgFolder.getCanonicalPath());
|
| + if (sourceUri.startsWith(pkgCanonicalUri)) {
|
| + String relPath = sourceUri.substring(pkgCanonicalUri.length);
|
| + return Uri
|
| + .parse("$PACKAGE_SCHEME:${pkgFolder.getName()}$relPath");
|
| }
|
| } catch (e) {}
|
| }
|
| @@ -500,6 +505,12 @@ class PackageUriResolver extends UriResolver {
|
| return filePath.startsWith("$rootPath/lib");
|
| }
|
|
|
| + /**
|
| + * Convert the given file path to a "file:" URI. On Windows, this transforms
|
| + * backslashes to forward slashes.
|
| + */
|
| + String _toFileUri(String filePath) => path.context.toUri(filePath).toString();
|
| +
|
| /**
|
| * Return `true` if the given URI is a `package` URI.
|
| *
|
| @@ -511,7 +522,12 @@ class PackageUriResolver extends UriResolver {
|
|
|
| /**
|
| * Instances of the class `RelativeFileUriResolver` resolve `file` URI's.
|
| + *
|
| + * This class is now deprecated, file URI resolution should be done with
|
| + * ResourceUriResolver, i.e.
|
| + * 'new ResourceUriResolver(PhysicalResourceProvider.INSTANCE)'.
|
| */
|
| +@deprecated
|
| class RelativeFileUriResolver extends UriResolver {
|
| /**
|
| * The name of the `file` scheme.
|
| @@ -544,7 +560,7 @@ class RelativeFileUriResolver extends UriResolver {
|
| for (JavaFile dir in _relativeDirectories) {
|
| JavaFile file = new JavaFile.relative(dir, filePath);
|
| if (file.exists()) {
|
| - return new FileBasedSource(file, actualUri != null ? actualUri : uri);
|
| + return new FileBasedSource(file, actualUri ?? uri);
|
| }
|
| }
|
| }
|
|
|