dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.23k stars 1.57k forks source link

source_gen: need access to constantInitializers in ConstructorElement #26381

Open kevmoo opened 8 years ago

kevmoo commented 8 years ago

Right now, pkg/source_gen is using ConstructorElementImpl.constantInitializers. It'd be nice if this was defined as an abstract getter on ConstructorElement.

This is the only private usage remaining in pkg/source_gen

bwilkerson commented 8 years ago

What is source_gen using the initializers for?

kevmoo commented 8 years ago

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.

bwilkerson commented 8 years ago

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.

kevmoo commented 8 years ago

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).

bwilkerson commented 8 years ago

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.

rlch commented 3 years ago

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?

rlch commented 3 years ago

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?)

toshiossada commented 1 year ago

has It any update for this issue?

toshiossada commented 1 year ago

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?