Open kevmoo opened 8 years ago
What is source_gen using the initializers for?
To work backwards from the field values in a constant to the const constructor call that generated it. This allows me to use Mirrors to create the instance.
I'm not sure why you want to use Mirrors to create the object when you already have a representation of the object. In the two cases I can see the annotation object has either a single String or two bool valued fields. Seems like you could just ask the DartObject representing the instance for the values of the fields and use toBoolValue() or toStringValue() to get the value. I think this would be both easier to implement and more performant.
Code flow:
User defines customer annotation class: @JsonSerializable(includeCtor: true, includeToJsonMethod: true)
I parse this with the analyzer, create the instance, and let the user write code that just consume the instance in the generator
class JsonSerializableGenerator {
Future<Something> generate(JsonSerializable annotationValue, SomeAnalyzerElement element) { ... }
My fallback is ugly – making folks attach map literals as annotations (or something).
Note that this approach only works if the code generator built on top of your package always has all classes referenced in the annotations available at runtime. That's probably true 95% of the time, but there will be corner cases where it isn't possible for you to create instances of their annotations.
You could cover all of the cases if you let users understand that they are statically analyzing the code and don't actually have the code being analyzed loaded, and let them use a DartObject representation of the value of the annotation.
I'm currently trying to solve a problem that requires access to constantInitializers
.
Is there a workaround / could anyone guide me in the right direction to make a PR to solve this?
I'm currently trying to solve a problem that requires access to
constantInitializers
. Is there a workaround / could anyone guide me in the right direction to make a PR to solve this?
Just ended up walking the AST to get them. Although I still think this issue is worth resolving for the sake of efficiency (unless I'm missing something obvious?)
has It any update for this issue?
I'm currently trying to solve a problem that requires access to
constantInitializers
. Is there a workaround / could anyone guide me in the right direction to make a PR to solve this?Just ended up walking the AST to get them. Although I still think this issue is worth resolving for the sake of efficiency (unless I'm missing something obvious?)
how did you do this? could you give anexample?
Right now,
pkg/source_gen
is usingConstructorElementImpl.constantInitializers
. It'd be nice if this was defined as an abstract getter onConstructorElement
.This is the only private usage remaining in
pkg/source_gen