Skip to content

Update to the new element model #339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions reflectable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 4.0.13

* Update dependencies: `analyzer` 7.4, `build_runner` 2.4.15,
`dart_style` 3.0.0, `lints` 6.0.0.

## 4.0.12

* Revert analyzer dependency to 6.8.0 and lints to 5.0.0 due to macro
Expand Down
140 changes: 98 additions & 42 deletions reflectable/lib/capability.dart
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,10 @@ const subtypeQuantifyCapability = _SubtypeQuantifyCapability();
class SuperclassQuantifyCapability implements ReflecteeQuantifyCapability {
final Type upperBound;
final bool excludeUpperBound;
const SuperclassQuantifyCapability(this.upperBound,
{this.excludeUpperBound = false});
const SuperclassQuantifyCapability(
this.upperBound, {
this.excludeUpperBound = false,
});
}

/// Gives support for reflection on all superclasses of covered classes.
Expand Down Expand Up @@ -371,8 +373,9 @@ const typeAnnotationQuantifyCapability = TypeAnnotationQuantifyCapability();
/// the vocered classes, as well as the transitive closure thereof (that is,
/// including classes used as type annotations in classes used as type
/// annotations, etc.).
const typeAnnotationDeepQuantifyCapability =
TypeAnnotationQuantifyCapability(transitive: true);
const typeAnnotationDeepQuantifyCapability = TypeAnnotationQuantifyCapability(
transitive: true,
);

/// Quantifying capability instance specifying that the reflection support
/// for any given explicitly declared getter must also be given to its
Expand Down Expand Up @@ -427,7 +430,7 @@ class ImportAttachedCapability {
class GlobalQuantifyCapability extends ImportAttachedCapability {
final String classNamePattern;
const GlobalQuantifyCapability(this.classNamePattern, Reflectable reflector)
: super(reflector);
: super(reflector);
}

/// Gives reflection support in [reflector] for every class
Expand All @@ -443,7 +446,7 @@ class GlobalQuantifyCapability extends ImportAttachedCapability {
class GlobalQuantifyMetaCapability extends ImportAttachedCapability {
final Type metadataType;
const GlobalQuantifyMetaCapability(this.metadataType, Reflectable reflector)
: super(reflector);
: super(reflector);
}

// ---------- Private classes used to enable capability instances above.
Expand Down Expand Up @@ -509,8 +512,12 @@ class _StringInvocation extends StringInvocation {
@override
bool get isSetter => kind == StringInvocationKind.setter;

_StringInvocation(this.memberName, this.positionalArguments,
this.namedArguments, this.kind);
_StringInvocation(
this.memberName,
this.positionalArguments,
this.namedArguments,
this.kind,
);
}

/// Thrown when a method is invoked via a reflectable, but the reflectable
Expand All @@ -533,11 +540,20 @@ class ReflectableNoSuchMethodError extends Error

final StringInvocationKind kind;

ReflectableNoSuchMethodError(this.receiver, this.memberName,
this.positionalArguments, this.namedArguments, this.kind);
ReflectableNoSuchMethodError(
this.receiver,
this.memberName,
this.positionalArguments,
this.namedArguments,
this.kind,
);

StringInvocation get invocation => _StringInvocation(
memberName, positionalArguments, namedArguments ?? const {}, kind);
memberName,
positionalArguments,
namedArguments ?? const {},
kind,
);

@override
String toString() {
Expand All @@ -555,7 +571,8 @@ class ReflectableNoSuchMethodError extends Error
case StringInvocationKind.constructor:
kindName = 'constructor';
}
var description = 'NoSuchCapabilityError: no capability to invoke the '
var description =
'NoSuchCapabilityError: no capability to invoke the '
'$kindName "$memberName"\n'
'Receiver: $receiver\n'
'Arguments: $positionalArguments\n';
Expand All @@ -567,38 +584,77 @@ class ReflectableNoSuchMethodError extends Error
}

dynamic reflectableNoSuchInvokableError(
Object? receiver,
String memberName,
List positionalArguments,
Map<Symbol, dynamic>? namedArguments,
StringInvocationKind kind) {
Object? receiver,
String memberName,
List positionalArguments,
Map<Symbol, dynamic>? namedArguments,
StringInvocationKind kind,
) {
throw ReflectableNoSuchMethodError(
receiver, memberName, positionalArguments, namedArguments, kind);
}

dynamic reflectableNoSuchMethodError(Object? receiver, String memberName,
List positionalArguments, Map<Symbol, dynamic>? namedArguments) {
throw ReflectableNoSuchMethodError(receiver, memberName, positionalArguments,
namedArguments, StringInvocationKind.method);
}

dynamic reflectableNoSuchGetterError(Object? receiver, String memberName,
List positionalArguments, Map<Symbol, dynamic>? namedArguments) {
throw ReflectableNoSuchMethodError(receiver, memberName, positionalArguments,
namedArguments, StringInvocationKind.getter);
}

dynamic reflectableNoSuchSetterError(Object? receiver, String memberName,
List positionalArguments, Map<Symbol, dynamic>? namedArguments) {
throw ReflectableNoSuchMethodError(receiver, memberName, positionalArguments,
namedArguments, StringInvocationKind.setter);
receiver,
memberName,
positionalArguments,
namedArguments,
kind,
);
}

dynamic reflectableNoSuchMethodError(
Object? receiver,
String memberName,
List positionalArguments,
Map<Symbol, dynamic>? namedArguments,
) {
throw ReflectableNoSuchMethodError(
receiver,
memberName,
positionalArguments,
namedArguments,
StringInvocationKind.method,
);
}

dynamic reflectableNoSuchGetterError(
Object? receiver,
String memberName,
List positionalArguments,
Map<Symbol, dynamic>? namedArguments,
) {
throw ReflectableNoSuchMethodError(
receiver,
memberName,
positionalArguments,
namedArguments,
StringInvocationKind.getter,
);
}

dynamic reflectableNoSuchSetterError(
Object? receiver,
String memberName,
List positionalArguments,
Map<Symbol, dynamic>? namedArguments,
) {
throw ReflectableNoSuchMethodError(
receiver,
memberName,
positionalArguments,
namedArguments,
StringInvocationKind.setter,
);
}

dynamic reflectableNoSuchConstructorError(
Object? receiver,
String constructorName,
List positionalArguments,
Map<Symbol, dynamic>? namedArguments) {
throw ReflectableNoSuchMethodError(receiver, constructorName,
positionalArguments, namedArguments, StringInvocationKind.constructor);
Object? receiver,
String constructorName,
List positionalArguments,
Map<Symbol, dynamic>? namedArguments,
) {
throw ReflectableNoSuchMethodError(
receiver,
constructorName,
positionalArguments,
namedArguments,
StringInvocationKind.constructor,
);
}
20 changes: 14 additions & 6 deletions reflectable/lib/mirrors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,11 @@ abstract class ObjectMirror implements Mirror {
/// [StaticInvokeMetaCapability]; and [invoke] on a top-level function
/// requires a matching [TopLevelInvokeCapability] or
/// [TopLevelInvokeMetaCapability].
Object? invoke(String memberName, List positionalArguments,
[Map<Symbol, dynamic>? namedArguments]); // RET: InstanceMirror
Object? invoke(
String memberName,
List positionalArguments, [
Map<Symbol, dynamic>? namedArguments,
]); // RET: InstanceMirror

/// Invokes a getter and returns the result. The getter can be the
/// implicit getter for a field, or a user-defined getter method.
Expand Down Expand Up @@ -540,8 +543,10 @@ abstract class ClosureMirror implements InstanceMirror {
/// Required capabilities: [apply] requires a matching
/// [InstanceInvokeCapability] or [InstanceInvokeMetaCapability], targeting
/// the relevant `call` method.
Object? apply(List positionalArguments,
[Map<Symbol, dynamic>? namedArguments]); // RET: InstanceMirror
Object? apply(
List positionalArguments, [
Map<Symbol, dynamic>? namedArguments,
]); // RET: InstanceMirror
}

/// A [LibraryMirror] reflects a Dart language library, providing
Expand Down Expand Up @@ -965,8 +970,11 @@ abstract class ClassMirror implements TypeMirror, ObjectMirror {
///
/// Required capabilities: [newInstance] requires a matching
/// [NewInstanceCapability] or [NewInstanceMetaCapability].
Object newInstance(String constructorName, List positionalArguments,
[Map<Symbol, dynamic> namedArguments]); // RET: InstanceMirror
Object newInstance(
String constructorName,
List positionalArguments, [
Map<Symbol, dynamic> namedArguments,
]); // RET: InstanceMirror

/// Whether this mirror is equal to [other].
///
Expand Down
23 changes: 12 additions & 11 deletions reflectable/lib/reflectable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,18 @@ abstract class Reflectable extends implementation.ReflectableImpl

/// Const constructor, to enable usage as metadata, allowing for varargs
/// style invocation with up to ten arguments.
const Reflectable(
[super.cap0,
super.cap1,
super.cap2,
super.cap3,
super.cap4,
super.cap5,
super.cap6,
super.cap7,
super.cap8,
super.cap9]);
const Reflectable([
super.cap0,
super.cap1,
super.cap2,
super.cap3,
super.cap4,
super.cap5,
super.cap6,
super.cap7,
super.cap8,
super.cap9,
]);

const Reflectable.fromList(super.capabilities) : super.fromList();

Expand Down
30 changes: 21 additions & 9 deletions reflectable/lib/reflectable_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ class ReflectableBuilder implements Builder {
AssetId outputId = inputId.changeExtension('.reflectable.dart');
List<LibraryElement> visibleLibraries = await resolver.libraries.toList();
String generatedSource = await BuilderImplementation().buildMirrorLibrary(
resolver, inputId, outputId, inputLibrary, visibleLibraries, true, []);
resolver,
inputId,
outputId,
inputLibrary,
visibleLibraries,
true,
[],
);
await buildStep.writeAsString(outputId, generatedSource);
}

@override
Map<String, List<String>> get buildExtensions => const {
'.dart': ['.reflectable.dart']
};
'.dart': ['.reflectable.dart'],
};
}

ReflectableBuilder reflectableBuilder(BuilderOptions options) {
Expand All @@ -55,12 +62,13 @@ Future<BuildResult> reflectableBuild(List<String> arguments) async {
} else {
// TODO(eernst) feature: We should support some customization of
// the settings, e.g., specifying options like `suppress_warnings`.
var options = BuilderOptions(
<String, dynamic>{'entry_points': arguments, 'formatted': true},
isRoot: true);
var options = BuilderOptions(<String, dynamic>{
'entry_points': arguments,
'formatted': true,
}, isRoot: true);
final builder = ReflectableBuilder(options);
var builders = <BuilderApplication>[
applyToRoot(builder, generateFor: InputSet(include: arguments))
applyToRoot(builder, generateFor: InputSet(include: arguments)),
];
PackageGraph packageGraph = await PackageGraph.forThisPackage();
var environment = OverrideableEnvironment(IOEnvironment(packageGraph));
Expand All @@ -71,8 +79,12 @@ Future<BuildResult> reflectableBuild(List<String> arguments) async {
);
try {
BuildRunner build = await BuildRunner.create(
buildOptions, environment, builders, const {},
isReleaseBuild: false);
buildOptions,
environment,
builders,
const {},
isReleaseBuild: false,
);
BuildResult result = await build.run(const {});
await build.beforeExit();
return result;
Expand Down
Loading