blob: 2188bba7778375882ae185f106dc8cdae9a04a0d [file] [log] [blame]
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// 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.
import 'dart:collection';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/java_io.dart';
import 'package:analyzer/src/generated/source.dart';
export 'package:analyzer/src/generated/source.dart';
/// Instances of the class `FileBasedSource` implement a source that represents
/// a file.
class FileBasedSource extends Source {
/// A function that changes the way that files are read off of disk.
static Function fileReadMode = (String s) => s;
/// Map from encoded URI/filepath pair to a unique integer identifier. This
/// identifier is used for equality tests and hash codes.
/// The URI and filepath are joined into a pair by separating them with an '@'
/// character.
static final Map<String, int> _idTable = HashMap<String, int>();
/// The URI from which this source was originally derived.
final Uri uri;
/// The unique ID associated with this [FileBasedSource].
final int id;
/// The file represented by this source.
final JavaFile file;
/// The cached absolute path of this source.
String _absolutePath;
/// The cached encoding for this source.
String _encoding;
/// Initialize a newly created source object to represent the given [file]. If
/// a [uri] is given, then it will be used as the URI from which the source
/// was derived, otherwise a `file:` URI will be created based on the [file].
FileBasedSource(JavaFile file, [Uri uri])
: uri = uri ?? file.toURI(),
file = file,
id = _idTable.putIfAbsent(
'${uri ?? file.toURI()}@${file.getPath()}', () => _idTable.length);
TimestampedData<String> get contents {
return contentsFromFile;
/// Get the contents and timestamp of the underlying file.
/// 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.
/// @return the contents of the source paired with the modification stamp of
/// the source
/// @throws Exception if the contents of this source could not be accessed
/// See [contents].
TimestampedData<String> get contentsFromFile {
return TimestampedData<String>(
file.lastModified(), fileReadMode(file.readAsStringSync()));
String get encoding {
return _encoding ??= uri.toString();
String get fullName {
return _absolutePath ??= file.getAbsolutePath();
int get hashCode => uri.hashCode;
bool get isInSystemLibrary => uri.scheme == DartUriResolver.DART_SCHEME;
int get modificationStamp => file.lastModified();
String get shortName => file.getName();
UriKind get uriKind {
String scheme = uri.scheme;
return UriKind.fromScheme(scheme);
bool operator ==(Object object) {
if (object is FileBasedSource) {
return id ==;
} else if (object is Source) {
return uri == object.uri;
return false;
bool exists() => file.isFile();
String toString() {
if (file == null) {
return "<unknown source>";
return file.getAbsolutePath();