Skip to content
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

Obtain getters and/or attribuites from ClassMirror #17361

Closed
DartBot opened this issue Mar 9, 2014 · 2 comments
Closed

Obtain getters and/or attribuites from ClassMirror #17361

DartBot opened this issue Mar 9, 2014 · 2 comments
Labels
area-library closed-not-planned Closed as we don't intend to take action on the reported issue library-mirrors type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@DartBot
Copy link

DartBot commented Mar 9, 2014

This issue was originally filed by luisvargastij...@gmail.com


Previous version of dart were able to get getters using

cm.getters.values

However actual version was removed that featuread and replaced by

cm.declarations

last code gets all attributes, getters, setters, methods and constructor. I would like to know if there is a way to get only "getters and attributes" without others method.

The code that I'm using right now is that one:

import "dart:mirrors";

class MyNestedClass {
  String name;
}
class MyClass {
  int i, j;
  MyNestedClass myNestedClass;
  int sum() => i + j;

  MyClass(this.i, this.j);
}

void main() {
  MyClass myClass = new MyClass(3, 5)
    ..myNestedClass = (new MyNestedClass()..name = "luis");
  print(myClass.toString());
  InstanceMirror im = reflect(myClass);

  ClassMirror cm = im.type;

  Map<Symbol, MethodMirror> instanceMembers = cm.instanceMembers;

  cm.declarations.forEach((name, declaration) {
      if(declaration.simpleName != cm.simpleName) // If is not te constructor
        print('${MirrorSystem.getName(name)}:${im.getField(name).reflectee}');
  });
}

As you can see in previous code to check if not the constructor I need to compare the delcaration.simpelName with cm.simpleName. Which until I understand this is ineficient since we are comparing strings.

In conclusion, I would like to know if there is or will be a better way to solve this problem.

@kasperl
Copy link

kasperl commented Mar 12, 2014

Added Area-Library, Library-Mirrors, Triaged labels.

@DartBot
Copy link
Author

DartBot commented Mar 13, 2014

This comment was originally written by @zoechi


I gave already an answer to this question on SO http://stackoverflow.com/questions/22317924.
Could be more straightforward though.

@kevmoo kevmoo added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) and removed priority-unassigned labels Feb 29, 2016
@a-siva a-siva added closed-as-intended Closed as the reported issue is expected behavior closed-not-planned Closed as we don't intend to take action on the reported issue and removed closed-as-intended Closed as the reported issue is expected behavior labels Aug 5, 2022
@a-siva a-siva closed this as completed Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-library closed-not-planned Closed as we don't intend to take action on the reported issue library-mirrors type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants