Skip to content

Commit 2af6081

Browse files
authored
Remove mirrors, release 4.0.0. (#783)
1 parent 9475e65 commit 2af6081

File tree

5 files changed

+11
-73
lines changed

5 files changed

+11
-73
lines changed

source_gen/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
## 3.1.1-wip
1+
## 4.0.0
22

3+
- **Breaking Change**: remove `TypeChecker.fromRuntime`, use
4+
`TypeChecker.typeNamed` instead. This removes all use of `dart:mirror`, so
5+
builders using `source_gen` can be AOT compiled for better performance.
36
- Keep `// GENERATED FILE` comments on the first line.
47

58
## 3.1.0

source_gen/lib/src/generator_for_annotation.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,19 @@ abstract class GeneratorForAnnotation<T> extends Generator {
5858
/// annotations. You can override this by setting [throwOnUnresolved] to
5959
/// `false`.
6060
///
61-
/// With `source_gen` 4.0.0 this class will stop using mirrors for matching
62-
/// annotations and will fall back to comparing the name of `T`. Pass
63-
/// [inPackage] and [inSdk] to tighten the check; see [TypeChecker.typeNamed].
61+
/// [TypeChecker.typeNamed] on `T` is used to match the annotation. By default
62+
/// it matches any annotation with the same name. Pass [inPackage] and [inSdk]
63+
/// to tighten the check; see [TypeChecker.typeNamed] for details.
64+
///
6465
/// To use a custom annotation check, override [typeChecker].
6566
const GeneratorForAnnotation({
6667
this.throwOnUnresolved = true,
6768
this.inPackage,
6869
this.inSdk,
6970
});
7071

71-
// This will switch to `typeNamed` in 4.0.0.
72-
// ignore: deprecated_member_use_from_same_package
73-
TypeChecker get typeChecker => TypeChecker.fromRuntime(T);
72+
TypeChecker get typeChecker =>
73+
TypeChecker.typeNamed(T, inPackage: inPackage, inSdk: inSdk);
7474

7575
@override
7676
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) async {

source_gen/lib/src/type_checker.dart

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
// ignore_for_file: deprecated_member_use until analyzer 7 support is dropped.
66

7-
import 'dart:mirrors' hide SourceLocation;
8-
97
import 'package:analyzer/dart/analysis/results.dart';
108
import 'package:analyzer/dart/ast/ast.dart';
119
import 'package:analyzer/dart/constant/value.dart';
@@ -34,20 +32,6 @@ abstract class TypeChecker {
3432
/// ```
3533
const factory TypeChecker.any(Iterable<TypeChecker> checkers) = _AnyChecker;
3634

37-
/// Create a new [TypeChecker] backed by a runtime [type].
38-
///
39-
/// This implementation uses `dart:mirrors` (runtime reflection).
40-
@Deprecated('''
41-
Will be removed in 4.0.0 to drop `dart:mirrors` dependency.
42-
43-
Recommended: replace `fromRuntime(Foo)` with
44-
`typeNamed(Foo, inPackage: 'foo_package')`. This is a slighly weaker check than
45-
`fromRuntime(Foo)` as it matches any annotation named `Foo` in
46-
`package:foo_package`.
47-
48-
If you need an exact match, use `fromUrl`.''')
49-
const factory TypeChecker.fromRuntime(Type type) = _MirrorTypeChecker;
50-
5135
/// Create a new [TypeChecker] for types matching the name of [type].
5236
///
5337
/// Optionally, also pass [inPackage] to restrict to a specific package by
@@ -267,29 +251,6 @@ class _LibraryTypeChecker extends TypeChecker {
267251
String toString() => urlOfElement(_type.element3!);
268252
}
269253

270-
// Checks a runtime type against a static type.
271-
class _MirrorTypeChecker extends TypeChecker {
272-
static Uri _uriOf(ClassMirror mirror) => normalizeUrl(
273-
(mirror.owner as LibraryMirror).uri,
274-
).replace(fragment: MirrorSystem.getName(mirror.simpleName));
275-
276-
// Precomputed type checker for types that already have been used.
277-
static final _cache = Expando<TypeChecker>();
278-
279-
final Type _type;
280-
281-
const _MirrorTypeChecker(this._type) : super._();
282-
283-
TypeChecker get _computed =>
284-
_cache[this] ??= TypeChecker.fromUrl(_uriOf(reflectClass(_type)));
285-
286-
@override
287-
bool isExactly(Element2 element) => _computed.isExactly(element);
288-
289-
@override
290-
String toString() => _computed.toString();
291-
}
292-
293254
// Checks a runtime type name and optional package against a static type.
294255
class _NameTypeChecker extends TypeChecker {
295256
final Type _type;

source_gen/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: source_gen
2-
version: 3.1.1-wip
2+
version: 4.0.0
33
description: >-
44
Source code generation builders and utilities for the Dart build system
55
repository: https://github.com/dart-lang/source_gen/tree/master/source_gen

source_gen/test/type_checker_test.dart

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -334,32 +334,6 @@ void main() {
334334
});
335335
}
336336

337-
group('TypeChecker.forRuntime', () {
338-
commonTests(
339-
// ignore: deprecated_member_use_from_same_package
340-
checkIterable: () => const TypeChecker.fromRuntime(Iterable),
341-
// ignore: deprecated_member_use_from_same_package
342-
checkEnum: () => const TypeChecker.fromRuntime(Enum),
343-
// ignore: deprecated_member_use_from_same_package
344-
checkEnumMixin: () => const TypeChecker.fromRuntime(MyEnumMixin),
345-
// ignore: deprecated_member_use_from_same_package
346-
checkMap: () => const TypeChecker.fromRuntime(Map),
347-
// ignore: deprecated_member_use_from_same_package
348-
checkMapMixin: () => const TypeChecker.fromRuntime(MyMapMixin),
349-
// ignore: deprecated_member_use_from_same_package
350-
checkHashMap: () => const TypeChecker.fromRuntime(HashMap),
351-
// ignore: deprecated_member_use_from_same_package
352-
checkGenerator: () => const TypeChecker.fromRuntime(Generator),
353-
354-
checkGeneratorForAnnotation:
355-
// ignore: deprecated_member_use_from_same_packages
356-
() => const TypeChecker.typeNamed(
357-
GeneratorForAnnotation,
358-
inPackage: 'source_gen',
359-
),
360-
);
361-
});
362-
363337
group('TypeChecker.typeNamed without package', () {
364338
commonTests(
365339
checkIterable: () => const TypeChecker.typeNamed(Iterable),

0 commit comments

Comments
 (0)