Dart: how to get metadata for anything other than classes and class members?
I am experimenting with metadata.
In this document :
https://www.dartlang.org/guides/language/language-tour#metadata
It is said that:
Metadata can appear before a library, class, typedef, type parameter,
constructor, factory, function, field, parameter, or variable
declaration and before an import or export directive. You can retrieve
metadata at runtime using reflection.
Thus... I try to get metadata...
// Resources:
// https://stackoverflow.com/questions/26826521/executing-bundle-of-functions-by-their-metadata-tag-in-dart-lang
import 'dart:mirrors';
/// This class represents the metadata "OnFailure".
class OnFailure {
final int criticalityLevel;
final String handlerName;
/// Constructor.
/// [criticalityLevel] represent the level of criticality.
/// [handlerName] represents the name of the function to execute.
const OnFailure(int this.criticalityLevel, String this.handlerName);
}
/// This class represents the metadata "Log".
class Log {
final String destination;
const Log(String this.destination);
}
/// This class represents the metadata "Doc".
class Doc {
final String path;
const Doc(String this.path);
}
@OnFailure(0, 'onFatalHandler')
class ClassProcessor {
@Doc('/var/doc/ClassProcessor')
bool status;
@Log('/var/log/ClassProcessor')
bool call(int value) {
return value > 0;
}
}
@OnFailure(0, 'onFatalHandler')
typedef bool TypeProcessor(int value);
main() {
ClassProcessor processorClass = ClassProcessor();
// Get the metadata for a class.
InstanceMirror instanceMirror = reflect(processorClass);
ClassMirror classMirror = instanceMirror.type;
print(instanceMirror.type.metadata); // => [InstanceMirror on Instance of 'OnFailure']
OnFailure metadata = classMirror.metadata[0].reflectee;
print("Critical level: ${metadata.criticalityLevel}"); // => Critical level: 0
print("Handler name: ${metadata.handlerName}"); // => Handler name: onFatalHandler
// Get the metadata for a method.
MethodMirror methodMirror = classMirror.declarations[Symbol('call')];
Log log = methodMirror.metadata[0].reflectee;
print("Log file is ${log.destination}");
// Get the metadata for a property.
VariableMirror variableMirror = classMirror.declarations[Symbol('status')];
Doc doc = variableMirror.metadata[0].reflectee;
print("Doc file is ${doc.path}");
// Get the metadata for a typedef.
// ???
TypeProcessor processorInstance = (int value) {
if (value > 0) {
print("That's OK.");
} else {
print("A fatal error occurred !");
}
};
instanceMirror = reflect(processorInstance);
instanceMirror.type.declarations.forEach((Symbol symbol, DeclarationMirror declarationMirror) {
print(declarationMirror.metadata);
});
// Get the metadata for a variable.
// ???
@Doc('/var/doc/data')
int data = 10;
instanceMirror = reflect(data);
print(instanceMirror.reflectee); // => 10
instanceMirror.type.declarations.forEach((Symbol symbol, DeclarationMirror declarationMirror) {
print(declarationMirror.metadata);
});
}
I can get metadata for classes, class properties and class methods, but I can't do so with "typedef" and for a variable.
Any idea on how to get metadata for anything other than a class or a class member ?
dart metadata dart-mirrors
add a comment |
I am experimenting with metadata.
In this document :
https://www.dartlang.org/guides/language/language-tour#metadata
It is said that:
Metadata can appear before a library, class, typedef, type parameter,
constructor, factory, function, field, parameter, or variable
declaration and before an import or export directive. You can retrieve
metadata at runtime using reflection.
Thus... I try to get metadata...
// Resources:
// https://stackoverflow.com/questions/26826521/executing-bundle-of-functions-by-their-metadata-tag-in-dart-lang
import 'dart:mirrors';
/// This class represents the metadata "OnFailure".
class OnFailure {
final int criticalityLevel;
final String handlerName;
/// Constructor.
/// [criticalityLevel] represent the level of criticality.
/// [handlerName] represents the name of the function to execute.
const OnFailure(int this.criticalityLevel, String this.handlerName);
}
/// This class represents the metadata "Log".
class Log {
final String destination;
const Log(String this.destination);
}
/// This class represents the metadata "Doc".
class Doc {
final String path;
const Doc(String this.path);
}
@OnFailure(0, 'onFatalHandler')
class ClassProcessor {
@Doc('/var/doc/ClassProcessor')
bool status;
@Log('/var/log/ClassProcessor')
bool call(int value) {
return value > 0;
}
}
@OnFailure(0, 'onFatalHandler')
typedef bool TypeProcessor(int value);
main() {
ClassProcessor processorClass = ClassProcessor();
// Get the metadata for a class.
InstanceMirror instanceMirror = reflect(processorClass);
ClassMirror classMirror = instanceMirror.type;
print(instanceMirror.type.metadata); // => [InstanceMirror on Instance of 'OnFailure']
OnFailure metadata = classMirror.metadata[0].reflectee;
print("Critical level: ${metadata.criticalityLevel}"); // => Critical level: 0
print("Handler name: ${metadata.handlerName}"); // => Handler name: onFatalHandler
// Get the metadata for a method.
MethodMirror methodMirror = classMirror.declarations[Symbol('call')];
Log log = methodMirror.metadata[0].reflectee;
print("Log file is ${log.destination}");
// Get the metadata for a property.
VariableMirror variableMirror = classMirror.declarations[Symbol('status')];
Doc doc = variableMirror.metadata[0].reflectee;
print("Doc file is ${doc.path}");
// Get the metadata for a typedef.
// ???
TypeProcessor processorInstance = (int value) {
if (value > 0) {
print("That's OK.");
} else {
print("A fatal error occurred !");
}
};
instanceMirror = reflect(processorInstance);
instanceMirror.type.declarations.forEach((Symbol symbol, DeclarationMirror declarationMirror) {
print(declarationMirror.metadata);
});
// Get the metadata for a variable.
// ???
@Doc('/var/doc/data')
int data = 10;
instanceMirror = reflect(data);
print(instanceMirror.reflectee); // => 10
instanceMirror.type.declarations.forEach((Symbol symbol, DeclarationMirror declarationMirror) {
print(declarationMirror.metadata);
});
}
I can get metadata for classes, class properties and class methods, but I can't do so with "typedef" and for a variable.
Any idea on how to get metadata for anything other than a class or a class member ?
dart metadata dart-mirrors
Click on the dart-mirrors tag I just added and you'll find a lot of examoles.
– Günter Zöchbauer
Nov 21 '18 at 20:50
Well, I had already read a lot of the material presented in this link. I took 8 hours to read more and experiment more. I did not find out how to get the metadata from atypedef
. My co-workers tried to find out also... with no result either. The documentation says that we can do it. However, what we understand by looking at all the responses is that it is not possible. That's too much trouble for us. We give up on Dart.
– Denis Beurive
Nov 22 '18 at 16:21
add a comment |
I am experimenting with metadata.
In this document :
https://www.dartlang.org/guides/language/language-tour#metadata
It is said that:
Metadata can appear before a library, class, typedef, type parameter,
constructor, factory, function, field, parameter, or variable
declaration and before an import or export directive. You can retrieve
metadata at runtime using reflection.
Thus... I try to get metadata...
// Resources:
// https://stackoverflow.com/questions/26826521/executing-bundle-of-functions-by-their-metadata-tag-in-dart-lang
import 'dart:mirrors';
/// This class represents the metadata "OnFailure".
class OnFailure {
final int criticalityLevel;
final String handlerName;
/// Constructor.
/// [criticalityLevel] represent the level of criticality.
/// [handlerName] represents the name of the function to execute.
const OnFailure(int this.criticalityLevel, String this.handlerName);
}
/// This class represents the metadata "Log".
class Log {
final String destination;
const Log(String this.destination);
}
/// This class represents the metadata "Doc".
class Doc {
final String path;
const Doc(String this.path);
}
@OnFailure(0, 'onFatalHandler')
class ClassProcessor {
@Doc('/var/doc/ClassProcessor')
bool status;
@Log('/var/log/ClassProcessor')
bool call(int value) {
return value > 0;
}
}
@OnFailure(0, 'onFatalHandler')
typedef bool TypeProcessor(int value);
main() {
ClassProcessor processorClass = ClassProcessor();
// Get the metadata for a class.
InstanceMirror instanceMirror = reflect(processorClass);
ClassMirror classMirror = instanceMirror.type;
print(instanceMirror.type.metadata); // => [InstanceMirror on Instance of 'OnFailure']
OnFailure metadata = classMirror.metadata[0].reflectee;
print("Critical level: ${metadata.criticalityLevel}"); // => Critical level: 0
print("Handler name: ${metadata.handlerName}"); // => Handler name: onFatalHandler
// Get the metadata for a method.
MethodMirror methodMirror = classMirror.declarations[Symbol('call')];
Log log = methodMirror.metadata[0].reflectee;
print("Log file is ${log.destination}");
// Get the metadata for a property.
VariableMirror variableMirror = classMirror.declarations[Symbol('status')];
Doc doc = variableMirror.metadata[0].reflectee;
print("Doc file is ${doc.path}");
// Get the metadata for a typedef.
// ???
TypeProcessor processorInstance = (int value) {
if (value > 0) {
print("That's OK.");
} else {
print("A fatal error occurred !");
}
};
instanceMirror = reflect(processorInstance);
instanceMirror.type.declarations.forEach((Symbol symbol, DeclarationMirror declarationMirror) {
print(declarationMirror.metadata);
});
// Get the metadata for a variable.
// ???
@Doc('/var/doc/data')
int data = 10;
instanceMirror = reflect(data);
print(instanceMirror.reflectee); // => 10
instanceMirror.type.declarations.forEach((Symbol symbol, DeclarationMirror declarationMirror) {
print(declarationMirror.metadata);
});
}
I can get metadata for classes, class properties and class methods, but I can't do so with "typedef" and for a variable.
Any idea on how to get metadata for anything other than a class or a class member ?
dart metadata dart-mirrors
I am experimenting with metadata.
In this document :
https://www.dartlang.org/guides/language/language-tour#metadata
It is said that:
Metadata can appear before a library, class, typedef, type parameter,
constructor, factory, function, field, parameter, or variable
declaration and before an import or export directive. You can retrieve
metadata at runtime using reflection.
Thus... I try to get metadata...
// Resources:
// https://stackoverflow.com/questions/26826521/executing-bundle-of-functions-by-their-metadata-tag-in-dart-lang
import 'dart:mirrors';
/// This class represents the metadata "OnFailure".
class OnFailure {
final int criticalityLevel;
final String handlerName;
/// Constructor.
/// [criticalityLevel] represent the level of criticality.
/// [handlerName] represents the name of the function to execute.
const OnFailure(int this.criticalityLevel, String this.handlerName);
}
/// This class represents the metadata "Log".
class Log {
final String destination;
const Log(String this.destination);
}
/// This class represents the metadata "Doc".
class Doc {
final String path;
const Doc(String this.path);
}
@OnFailure(0, 'onFatalHandler')
class ClassProcessor {
@Doc('/var/doc/ClassProcessor')
bool status;
@Log('/var/log/ClassProcessor')
bool call(int value) {
return value > 0;
}
}
@OnFailure(0, 'onFatalHandler')
typedef bool TypeProcessor(int value);
main() {
ClassProcessor processorClass = ClassProcessor();
// Get the metadata for a class.
InstanceMirror instanceMirror = reflect(processorClass);
ClassMirror classMirror = instanceMirror.type;
print(instanceMirror.type.metadata); // => [InstanceMirror on Instance of 'OnFailure']
OnFailure metadata = classMirror.metadata[0].reflectee;
print("Critical level: ${metadata.criticalityLevel}"); // => Critical level: 0
print("Handler name: ${metadata.handlerName}"); // => Handler name: onFatalHandler
// Get the metadata for a method.
MethodMirror methodMirror = classMirror.declarations[Symbol('call')];
Log log = methodMirror.metadata[0].reflectee;
print("Log file is ${log.destination}");
// Get the metadata for a property.
VariableMirror variableMirror = classMirror.declarations[Symbol('status')];
Doc doc = variableMirror.metadata[0].reflectee;
print("Doc file is ${doc.path}");
// Get the metadata for a typedef.
// ???
TypeProcessor processorInstance = (int value) {
if (value > 0) {
print("That's OK.");
} else {
print("A fatal error occurred !");
}
};
instanceMirror = reflect(processorInstance);
instanceMirror.type.declarations.forEach((Symbol symbol, DeclarationMirror declarationMirror) {
print(declarationMirror.metadata);
});
// Get the metadata for a variable.
// ???
@Doc('/var/doc/data')
int data = 10;
instanceMirror = reflect(data);
print(instanceMirror.reflectee); // => 10
instanceMirror.type.declarations.forEach((Symbol symbol, DeclarationMirror declarationMirror) {
print(declarationMirror.metadata);
});
}
I can get metadata for classes, class properties and class methods, but I can't do so with "typedef" and for a variable.
Any idea on how to get metadata for anything other than a class or a class member ?
dart metadata dart-mirrors
dart metadata dart-mirrors
edited Nov 21 '18 at 20:49
Günter Zöchbauer
316k66936882
316k66936882
asked Nov 21 '18 at 15:37
Denis BeuriveDenis Beurive
94
94
Click on the dart-mirrors tag I just added and you'll find a lot of examoles.
– Günter Zöchbauer
Nov 21 '18 at 20:50
Well, I had already read a lot of the material presented in this link. I took 8 hours to read more and experiment more. I did not find out how to get the metadata from atypedef
. My co-workers tried to find out also... with no result either. The documentation says that we can do it. However, what we understand by looking at all the responses is that it is not possible. That's too much trouble for us. We give up on Dart.
– Denis Beurive
Nov 22 '18 at 16:21
add a comment |
Click on the dart-mirrors tag I just added and you'll find a lot of examoles.
– Günter Zöchbauer
Nov 21 '18 at 20:50
Well, I had already read a lot of the material presented in this link. I took 8 hours to read more and experiment more. I did not find out how to get the metadata from atypedef
. My co-workers tried to find out also... with no result either. The documentation says that we can do it. However, what we understand by looking at all the responses is that it is not possible. That's too much trouble for us. We give up on Dart.
– Denis Beurive
Nov 22 '18 at 16:21
Click on the dart-mirrors tag I just added and you'll find a lot of examoles.
– Günter Zöchbauer
Nov 21 '18 at 20:50
Click on the dart-mirrors tag I just added and you'll find a lot of examoles.
– Günter Zöchbauer
Nov 21 '18 at 20:50
Well, I had already read a lot of the material presented in this link. I took 8 hours to read more and experiment more. I did not find out how to get the metadata from a
typedef
. My co-workers tried to find out also... with no result either. The documentation says that we can do it. However, what we understand by looking at all the responses is that it is not possible. That's too much trouble for us. We give up on Dart.– Denis Beurive
Nov 22 '18 at 16:21
Well, I had already read a lot of the material presented in this link. I took 8 hours to read more and experiment more. I did not find out how to get the metadata from a
typedef
. My co-workers tried to find out also... with no result either. The documentation says that we can do it. However, what we understand by looking at all the responses is that it is not possible. That's too much trouble for us. We give up on Dart.– Denis Beurive
Nov 22 '18 at 16:21
add a comment |
1 Answer
1
active
oldest
votes
This seems to be a Dart bug.
With
var mirrorSystem = currentMirrorSystem();
var libraryMirror = mirrorSystem.libraries.keys
.where((k) => k.path.endsWith('/bin/main.dart'))
.map((k) => mirrorSystem.libraries[k])
.first;
libraryMirror.declarations
should include typedef
s, but it doesn't.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415552%2fdart-how-to-get-metadata-for-anything-other-than-classes-and-class-members%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This seems to be a Dart bug.
With
var mirrorSystem = currentMirrorSystem();
var libraryMirror = mirrorSystem.libraries.keys
.where((k) => k.path.endsWith('/bin/main.dart'))
.map((k) => mirrorSystem.libraries[k])
.first;
libraryMirror.declarations
should include typedef
s, but it doesn't.
add a comment |
This seems to be a Dart bug.
With
var mirrorSystem = currentMirrorSystem();
var libraryMirror = mirrorSystem.libraries.keys
.where((k) => k.path.endsWith('/bin/main.dart'))
.map((k) => mirrorSystem.libraries[k])
.first;
libraryMirror.declarations
should include typedef
s, but it doesn't.
add a comment |
This seems to be a Dart bug.
With
var mirrorSystem = currentMirrorSystem();
var libraryMirror = mirrorSystem.libraries.keys
.where((k) => k.path.endsWith('/bin/main.dart'))
.map((k) => mirrorSystem.libraries[k])
.first;
libraryMirror.declarations
should include typedef
s, but it doesn't.
This seems to be a Dart bug.
With
var mirrorSystem = currentMirrorSystem();
var libraryMirror = mirrorSystem.libraries.keys
.where((k) => k.path.endsWith('/bin/main.dart'))
.map((k) => mirrorSystem.libraries[k])
.first;
libraryMirror.declarations
should include typedef
s, but it doesn't.
answered Nov 22 '18 at 16:51
Günter ZöchbauerGünter Zöchbauer
316k66936882
316k66936882
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415552%2fdart-how-to-get-metadata-for-anything-other-than-classes-and-class-members%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Click on the dart-mirrors tag I just added and you'll find a lot of examoles.
– Günter Zöchbauer
Nov 21 '18 at 20:50
Well, I had already read a lot of the material presented in this link. I took 8 hours to read more and experiment more. I did not find out how to get the metadata from a
typedef
. My co-workers tried to find out also... with no result either. The documentation says that we can do it. However, what we understand by looking at all the responses is that it is not possible. That's too much trouble for us. We give up on Dart.– Denis Beurive
Nov 22 '18 at 16:21