Find function/method body explicit dependency types using Dart analyzer package - dart

I would like to understand how can I analyze methods / functions body to find types that are explicitly referenced from it. I have success analyzing method declaration (return type, parameter types, etc..), however I have no idea how to do that for body.
Assuming following function:
String someFunction(int param) {
final list = <String>['a', 'b', 'c']; // -> DartTypes: String, List<String>
final myClass = MyClass<Arg>(); // -> DartTypes: Arg, MyClass<Arg>
final functionCall = anotherFunction<FunctionArg<Arg>>(); // -> DartTypes: Arg, FunctionArg<Arg>
return 'result';
}
// At is point I would like to know that my function depends on
// String, List<String>, Arg, MyClass<Arg>, FunctionArg<Arg>
// in term of DartType instances with proper typeArguments.
I tried getting AstNode for method element described here: https://stackoverflow.com/a/57043177/2033394
However I could not get elements from nodes to figure out their types. Their declaredElement values are always null. So I can not get back to Element API from AST API.

If you've used the exact snippet from the answer you've referenced, the problem is likely in getParsedLibraryByElement(). This method only parses the referenced library - meaning that you'll get an AST that doesn't necessarily have semantic references (like the declaredElement of AST nodes) set.
Instead, you'll want to use getResolvedLibraryByElement. The AST returned by that method will have its types and references fully resolved.
With the resolved AST, you could visit the body of the method with a custom visitor to find type references. Your definition of "referenced types" isn't really exact - but perhaps you can collect types in visitNamedType for type references and visitVariableDeclaration to collect the types of variables.

Related

Clang AST matching method call on class, derived class or typedef to either

I have a matcher that works perfectly for matching operator() calls on instances of a class or classes derived from that class. For example, it matches the final line of:
class MyBase { void operator()(...) {} };
MyBase b;
b(parameters);
using a matcher like:
const auto MyBaseExpr =
expr(hasType(cxxRecordDecl(isSameOrDerivedFrom("::MyBase"))));
Finder->addMatcher(traverse(
TK_AsIs, cxxOperatorCallExpr(
hasOverloadedOperatorName("()"),
hasArgument(0, anyOf(MyBaseExpr, MyOtherBaseExpr)),
hasAnyArgument(...),
this);
But I'd also like to be able to match such calls on instances of typedefs for the base or derived types like in the last line below:
typedef MyBase MyTypedef;
MyTypedef t;
t(parameters);
and I can't seem to fathom the correct way to specify this match. Attempting to use hasUnqualifiedDesugaredType rather than hasType doesn't work since it works on a type rather than a Decl and if I try to do more matching with the type then I can't use isSameOrDerived which returns a Matcher<CXXRecordDecl>. A similar problem occurs when trying to use hasCanonicalType:
.../RedundantStringCStrCheck.cpp:193:40: error: invalid initialization of reference of type ‘const clang::ast_matchers:
:internal::Matcher<clang::QualType>&’ from expression of type ‘clang::ast_matchers::internal::BindableMatcher<clang::Decl>’
193 | expr(hasCanonicalType(cxxRecordDecl(isSameOrDerivedFrom("::MyBase"))));
MyTypedef is defined from MyBase so its Canonical Type should be MyBase. More information about canonical type: https://clang.llvm.org/docs/InternalsManual.html#canonical-types
This is the example from LibASTMatchersReference , it uses hasType().
Thien Tran provided the pointer which led me to the right answer. Here's my original expression
const auto MyBaseExpr =
expr(hasType(cxxRecordDecl(isSameOrDerivedFrom("::MyBase"))));
I was trying to use:
const auto MyBaseExpr =
expr(hasCanonicalType(cxxRecordDecl(isSameOrDerivedFrom("::MyBase"))));
but the description of hasCanonicalType in LibASTMatchersReference shows that it takes and returns Matcher<QualType> yet cxxRecordDecl has type Matcher<Decl>, so this did not compile.
The mismatch of types can be corrected by inserting a call to hasDeclaration. It's then also necessary to keep the call to hasType in order to turn the Matcher<QualType> result of hasCanonicalType back into something that can be passed to expr.
After all that I ended up with:
const auto MyBaseExpr =
expr(hasType(hasCanonicalType(hasDeclaration(cxxRecordDecl(isSameOrDerivedFrom("::MyBase"))))));
which seems to work perfectly.

A few questions about Dart generics and type safety

I have the following Dart 2 code with null-safety.
extension Foo<T> on List<T> {
List<U> bar<U>({
U Function(T)? transform,
}) {
final t = transform ?? _identityTransform;
return map(t).toList();
}
}
U _identityTransform<T, U>(T t) => t as U; // #1, #2
void main() {
final strings = ['a', 'b', 'c'].bar<String>(); // #3
final ints = ['1', '2', '3'].bar(transform: int.parse);
print(strings);
print(ints);
}
It is an extension method on List<T> with a custom method that is basically a map with the
difference that it can return a new list of the same type if no transform is specified. (My real code is more complex than this, but this example is enough to present my questions.)
I want to be able to call bar() on a List with transform or without; if called without it, _identityTransform should be used.
The code above works, but I have a few reservations as to its quality, and questions, as I haven't really come to terms with Dart generics yet:
In the line marked #1 - the _identityTransform takes two generic parameters as I need access to them, but when the function is used the generic types are not used because I don't think it is possible to write something like _identityTransform<T, U> there. Is there a better way of defining _identityTransform? Am I losing any type safety with my current code?
In the line marked #2 I need a cast as U for the code to compile, I haven't managed to make the code work without it. Is there a way to do it without the cast?
In the line marked #3, when I call the extension method without any transform (i.e. I want the identity transform to kick in) I need to explicitly pass the generic type, otherwise the compiler complains about missing generic type (in strong mode) or infers strings to be List<dynamic> (strong mode turned off). Is some generics magic possible to be able to call .bar() and still have strings be inferred to List<String>?
I would make _identityTransform a nested function of bar so that you can remove its type arguments and instead use the same T and U as bar:
extension Foo<T> on List<T> {
List<U> bar<U>({
U Function(T)? transform,
}) {
U _identityTransform(T t) => t as U;
final t = transform ?? _identityTransform;
return map(t).toList();
}
}
Alternatively if you want to explicitly use _identityTransform<T, U>, then you could use a closure: t = transform ?? (arg) => _identityTransform<T, U>(arg), but that seems like overkill.
You need the cast. T and U are independent/unrelated types. Since you don't know that you want T and U to be the same until bar checks its argument at runtime, you will need the explicit cast to satisfy static type checking.
If the caller passes nothing for the transform argument, there is nothing to infer U from, so it will be dynamic. I can't think of any magical way make U default to T in such a case (again, that would be known only at runtime, but generics must satisfy static analysis).

What is #dynamicCallable in Swift?

From Apple's documentation:
The #dynamicCallable attribute lets you call named types like you call
functions using a simple syntactic sugar. The primary use case is
dynamic language interoperability.
Why would you want to use an #dynamicCallable instead of direct approch?
#dynamicCallable is a new feature of Swift 5. From Paul Hudson's article on "How to use #dynamicCallable in Swift" (emphasis mine):
SE-0216 adds a new #dynamicCallable attribute to Swift, which
brings with it the ability to mark a type as being directly callable.
It’s syntactic sugar rather than any sort of compiler magic,
effectively transforming this code:
let result = random(numberOfZeroes: 3)
Into this:
let result = random.dynamicallyCall(withKeywordArguments: ["numberOfZeroes": 3])
[...] #dynamicCallable is the natural extension of
#dynamicMemberLookup [SE-0195], and serves the same purpose: to
make it easier for Swift code to work alongside dynamic languages such
as Python and JavaScript. [...] #dynamicCallable is really flexible about which data
types its methods accept and return, allowing you to benefit from all
of Swift’s type safety while still having some wriggle room for
advanced usage.
Introduce user-defined dynamically "callable" types
Proposal: SE-0216
Authors: Chris Lattner, Dan Zheng
Review Manager: John McCall
Implementation: apple/swift#20305
Decision Notes: Rationale
Status: Implemented (Swift 5)
Introduction
This proposal is a follow-up to SE-0195 - Introduce User-defined "Dynamic Member
Lookup" Types,
which shipped in Swift 4.2. It introduces a new #dynamicCallable attribute, which marks
a type as being "callable" with normal syntax. It is simple syntactic sugar
which allows the user to write:
a = someValue(keyword1: 42, "foo", keyword2: 19)
and have it be rewritten by the compiler as:
a = someValue.dynamicallyCall(withKeywordArguments: [
"keyword1": 42, "": "foo", "keyword2": 19
])
Many other languages have analogous features (e.g. Python "callables", C++ operator(), and
functors in many other languages), but the
primary motivation of this proposal is to allow elegant and natural interoperation with
dynamic languages in Swift.
Swift-evolution threads:
- Pitch: Introduce user-defined dynamically "callable"
types.
- Pitch #2: Introduce user-defined dynamically “callable”
types.
- Current pitch thread: Pitch #3: Introduce user-defined dynamically “callable”
types
Motivation and context
Swift is exceptional at interworking with existing C and Objective-C APIs and
we would like to extend this interoperability to dynamic languages like Python,
JavaScript, Perl, and Ruby. We explored this overall goal in a long design
process wherein the Swift evolution community evaluated multiple different
implementation approaches. The conclusion was that the best approach was to put
most of the complexity into dynamic language specific bindings written as
pure-Swift libraries, but add small hooks in Swift to allow these bindings to
provide a natural experience to their clients.
SE-0195
was the first step in this process, which introduced a binding to naturally
express member lookup rules in dynamic languages.
What does interoperability with Python mean? Let's explain this by looking at
an example. Here's some simple Python code:
class Dog:
def __init__(self, name):
self.name = name
self.tricks = [] # creates a new empty list for each `Dog`
def add_trick(self, trick):
self.tricks.append(trick)
With the SE-0195 #dynamicMemberLookup feature
introduced in Swift 4.2, it is possible to implement a Python interoperability
layer
written in Swift. It interoperates with the Python runtime, and project all
Python values into a single PythonObject type. It allows us to call into the
Dog class like this:
// import DogModule.Dog as Dog
let Dog = Python.import.call(with: "DogModule.Dog")
// dog = Dog("Brianna")
let dog = Dog.call(with: "Brianna")
// dog.add_trick("Roll over")
dog.add_trick.call(with: "Roll over")
// dog2 = Dog("Kaylee").add_trick("snore")
let dog2 = Dog.call(with: "Kaylee").add_trick.call(with: "snore")
This also works with arbitrary other APIs as well. Here is an example working
with the Python pickle API and the builtin Python function open. Note that
we choose to put builtin Python functions like import and open into a
Python namespace to avoid polluting the global namespace, but other designs
are possible:
// import pickle
let pickle = Python.import.call(with: "pickle")
// file = open(filename)
let file = Python.open.call(with: filename)
// blob = file.read()
let blob = file.read.call()
// result = pickle.loads(blob)
let result = pickle.loads.call(with: blob)
This capability works well, but the syntactic burden of having to use
foo.call(with: bar, baz) instead of foo(bar, baz) is significant. Beyond
the syntactic weight, it directly harms code clarity by making code hard to
read and understand, cutting against a core value of Swift.
The proposed #dynamicCallable attribute directly solves this problem.
With it, these examples become more natural and clear, effectively matching the
original Python code in expressiveness:
// import DogModule.Dog as Dog
let Dog = Python.import("DogModule.Dog")
// dog = Dog("Brianna")
let dog = Dog("Brianna")
// dog.add_trick("Roll over")
dog.add_trick("Roll over")
// dog2 = Dog("Kaylee").add_trick("snore")
let dog2 = Dog("Kaylee").add_trick("snore")
Python builtins:
// import pickle
let pickle = Python.import("pickle")
// file = open(filename)
let file = Python.open(filename)
// blob = file.read()
let blob = file.read()
// result = pickle.loads(blob)
let result = pickle.loads(blob)
This proposal merely introduces a syntactic sugar - it does not add any new
semantic model to Swift. We believe that interoperability with scripting
languages is an important and rising need in the Swift community, particularly
as Swift makes inroads into the server development and machine learning
communities. This feature is also precedented in other languages (e.g. Scala's
Dynamic trait), and
can be used for other purposes besides language interoperability (e.g.
implementing dynamic proxy objects).
Proposed solution
We propose introducing a new #dynamicCallable attribute to the Swift language
which may be applied to structs, classes, enums, and protocols. This follows
the precedent of
SE-0195.
Before this proposal, values of these types are not valid in a call expression:
the only existing callable values in Swift are those with function types
(functions, methods, closures, etc) and metatypes (which are initializer
expressions like String(42)). Thus, it is always an error to "call" an
instance of a nominal type (like a struct, for instance).
With this proposal, types with the #dynamicCallable attribute on their
primary type declaration become "callable". They are required to implement at
least one of the following two methods for handling the call behavior:
func dynamicallyCall(withArguments: <#Arguments#>) -> <#R1#>
// `<#Arguments#>` can be any type that conforms to `ExpressibleByArrayLiteral`.
// `<#Arguments#>.ArrayLiteralElement` and the result type `<#R1#>` can be arbitrary.
func dynamicallyCall(withKeywordArguments: <#KeywordArguments#>) -> <#R2#>
// `<#KeywordArguments#>` can be any type that conforms to `ExpressibleByDictionaryLiteral`.
// `<#KeywordArguments#>.Key` must be a type that conforms to `ExpressibleByStringLiteral`.
// `<#KeywordArguments#>.Value` and the result type `<#R2#>` can be arbitrary.
// Note: in these type signatures, bracketed types like <#Arguments#> and <#KeywordArguments#>
// are not actual types, but rather any actual type that meets the specified conditions.
As stated above, <#Arguments#> and <#KeywordArguments#> can be any types
that conform to the
ExpressibleByArrayLiteral
and
ExpressibleByDictionaryLiteral
protocols, respectively. The latter is inclusive of
KeyValuePairs,
which supports duplicate keys, unlike Dictionary.
Thus, using KeyValuePairs is recommended to support duplicate keywords and
positional arguments (because positional arguments are desugared as keyword
arguments with the empty string "" as the key).
If a type implements the withKeywordArguments: method, it may be dynamically
called with both positional and keyword arguments: positional arguments have
the empty string "" as the key. If a type only implements the
withArguments: method but is called with keyword arguments, a compile-time
error is emitted.
Since dynamic calls are syntactic sugar for direct calls to dynamicallyCall
methods, additional behavior of the dynamicallyCall methods is directly
forwarded. For example, if a dynamicallyCall method is marked with throws
or #discardableResult, then the corresponding sugared dynamic call will
forward that behavior.
Ambiguity resolution: most specific match
Since there are two #dynamicCallable methods, there may be multiple ways to
handle some dynamic calls. What happens if a type specifies both the
withArguments: and withKeywordArguments: methods?
We propose that the type checker resolve this ambiguity towards the tightest
match based on syntactic form of the expression. The exact rules are:
If a #dynamicCallable type implements the withArguments: method and it is
called with no keyword arguments, use the withArguments: method.
In all other cases, attempt to use the withKeywordArguments: method.
This includes the case where a #dynamicCallable type implements the
withKeywordArguments: method and it is called with at least one keyword
argument.
This also includes the case where a #dynamicCallable type implements only
the withKeywordArguments: method (not the withArguments: method) and
it is called with no keyword arguments.
If #dynamicCallable type does not implement the withKeywordArguments:
method but the call site has keyword arguments, an error is emitted.
Here are some toy illustrative examples:
#dynamicCallable
struct Callable {
func dynamicallyCall(withArguments args: [Int]) -> Int { return args.count }
}
let c1 = Callable()
c1() // desugars to `c1.dynamicallyCall(withArguments: [])`
c1(1, 2) // desugars to `c1.dynamicallyCall(withArguments: [1, 2])`
c1(a: 1, 2) // error: `Callable` does not define the 'withKeywordArguments:' method
#dynamicCallable
struct KeywordCallable {
func dynamicallyCall(withKeywordArguments args: KeyValuePairs<String, Int>) -> Int {
return args.count
}
}
let c2 = KeywordCallable()
c2() // desugars to `c2.dynamicallyCall(withKeywordArguments: [:])`
c2(1, 2) // desugars to `c2.dynamicallyCall(withKeywordArguments: ["": 1, "": 2])`
c2(a: 1, 2) // desugars to `c2.dynamicallyCall(withKeywordArguments: ["a": 1, "": 2])`
#dynamicCallable
struct BothCallable {
func dynamicallyCall(withArguments args: [Int]) -> Int { return args.count }
func dynamicallyCall(withKeywordArguments args: KeyValuePairs<String, Int>) -> Int {
return args.count
}
}
let c3 = BothCallable()
c3() // desugars to `c3.dynamicallyCall(withArguments: [])`
c3(1, 2) // desugars to `c3.dynamicallyCall(withArguments: [1, 2])`
c3(a: 1, 2) // desugars to `c3.dynamicallyCall(withKeywordArguments: ["a": 1, "": 2])`
This ambiguity resolution rule works out naturally given the behavior of the
Swift type checker, because it only resolves call expressions when the type
of the base expression is known. At that point, it knows whether the base is a
function type, metatype, or a valid #dynamicCallable type, and it knows the
syntactic form of the call.
This proposal does not require massive or invasive changes to the constraint
solver. Please look at the implementation for more details.
Example usage
Here, we sketch some example bindings to show how this could be used in
practice. Note that there are lots of design decisions that are orthogonal to
this proposal (e.g. how to handle exceptions) that we aren't going into here.
This is just to show how this feature provides an underlying facility that
language bindings authors can use to achieve their desired result. These
examples also show #dynamicMemberLookup to illustrate how they work together,
but elides other implementation details.
JavaScript supports callable objects but does not have keyword arguments.
Here is a sample JavaScript binding:
#dynamicCallable #dynamicMemberLookup
struct JSValue {
// JavaScript doesn't have keyword arguments.
#discardableResult
func dynamicallyCall(withArguments: [JSValue]) -> JSValue { ... }
// This is a `#dynamicMemberLookup` requirement.
subscript(dynamicMember member: JSValue) -> JSValue {...}
// ... other stuff ...
}
On the other hand, a common JavaScript pattern is to take a dictionary of
values as a stand-in for argument labels (called like
example({first: 1, second: 2, third: 3}) in JavaScript). A JavaScript bridge
in Swift could choose to implement keyword argument support to allow this to be
called as example(first: 1, second: 2, third: 3) from Swift code (kudos to
Ben Rimmington for this
observation).
Python does support keyword arguments. While a Python binding could implement
only the withKeywordArguments: method, it is be better to implement both the
non-keyword and keyword forms to make the non-keyword case slightly more
efficient (avoid allocating temporary storage) and to make direct calls with
positional arguments nicer (x.dynamicallyCall(withArguments: 1, 2) instead of
x.dynamicallyCall(withKeywordArguments: ["": 1, "": 2])).
Here is a sample Python binding:
#dynamicCallable #dynamicMemberLookup
struct PythonObject {
// Python supports arbitrary mixes of keyword arguments and non-keyword
// arguments.
#discardableResult
func dynamicallyCall(
withKeywordArguments: KeyValuePairs<String, PythonObject>
) -> PythonObject { ... }
// An implementation of a Python binding could choose to implement this
// method as well, avoiding allocation of a temporary array.
#discardableResult
func dynamicallyCall(withArguments: [PythonObject]) -> PythonObject { ... }
// This is a `#dynamicMemberLookup` requirement.
subscript(dynamicMember member: String) -> PythonObject {...}
// ... other stuff ...
}
Limitations
Following the precedent of SE-0195, this attribute must be placed on the
primary definition of a type, not on an extension.
This proposal does not introduce the ability to provide dynamically callable
static/class members. We don't believe this is important given the goal of
supporting dynamic languages like Python, but it could be explored if a use
case is discovered in the future. Such future work should keep in mind that
call syntax on metatypes is already meaningful, and that ambiguity would have
to be resolved somehow (e.g. through the most specific rule).
This proposal supports direct calls of values and methods, but subsets out
support for currying methods in Smalltalk family languages. This is just an
implementation limitation given the current state of currying in the Swift
compiler. Support can be added in the future if there is a specific need.
Source compatibility
This is a strictly additive proposal with no source breaking changes.
Effect on ABI stability
This is a strictly additive proposal with no ABI breaking changes.
Effect on API resilience
This has no impact on API resilience which is not already captured by other
language features.
Future directions
Dynamic member calling (for Smalltalk family languages)
In addition to supporting languages like Python and JavaScript, we would also
like to grow to support Smalltalk derived languages like Ruby and Squeak. These
languages resolve method calls using both the base name as well as the
keyword arguments at the same time. For example, consider this Ruby code:
time = Time.zone.parse(user_time)
The Time.zone reference is a member lookup, but zone.parse(user_time) is a
method call, and needs to be handled differently than a lookup of zone.parse
followed by a direct function call.
This can be handled by adding a new #dynamicMemberCallable attribute, which
acts similarly to #dynamicCallable but enables dynamic member calls (instead
of dynamic calls of self).
#dynamicMemberCallable would have the following requirements:
func dynamicallyCallMethod(named: S1, withArguments: [T5]) -> T6
func dynamicallyCallMethod(named: S2, withKeywordArguments: [S3 : T7]) -> T8
Here is a sample Ruby binding:
#dynamicMemberCallable #dynamicMemberLookup
struct RubyObject {
#discardableResult
func dynamicallyCallMethod(
named: String, withKeywordArguments: KeyValuePairs<String, RubyObject>
) -> RubyObject { ... }
// This is a `#dynamicMemberLookup` requirement.
subscript(dynamicMember member: String) -> RubyObject {...}
// ... other stuff ...
}
General callable behavior
This proposal is mainly directed at dynamic language interoperability. For this
use case, it makes sense for the dynamicallyCall method to take a
variable-sized list of arguments where each argument has the same type.
However, it may be useful to support general callable behavior (akin to
operator() in C++) where the desugared "callable" method can have a fixed
number of arguments and arguments of different types.
For example, consider something like:
struct BinaryFunction<T1, T2, U> {
func call(_ argument1: T1, _ argument1: T2) -> U { ... }
}
It is not unreasonable to look ahead to a day where sugaring such things is
supported, particularly when/if Swift gets variadic
generics.
This could allow typesafe n-ary smart function pointer types.
We feel that the approach outlined in this proposal supports this direction.
When/if a motivating use case for general callable behavior comes up, we can
simply add a new form to represent it and enhance the type checker to prefer
that during ambiguity resolution. If this is a likely direction, then it may be
better to name the attribute #callable instead of #dynamicCallable in
anticipation of that future growth.
We believe that general callable behavior and #dynamicCallable are orthogonal
features and should be evaluated separately.
Alternatives considered
Many alternatives were considered and discussed. Most of them are captured in
the "Alternatives Considered" section of
SE-0195.
Here are a few points raised in the discussion:
It was suggested that we use subscripts to represent the call
implementations instead of a function call, aligning with
#dynamicMemberLookup. We think that functions are a better fit here: the
reason #dynamicMemberLookup uses subscripts is to allow the members to be
l-values, but call results are not l-values.
It was requested that we design and implement the 'static callable' version
of this proposal in conjunction with the dynamic version proposed here. In
the author's opinion, it is important to consider static callable support as
a likely future direction to make sure that the two features sit well next
to each other and have a consistent design (something we believe this
proposal has done) but it doesn't make sense to join the two proposals. So
far, there have been no strong motivating use case presented for the static
callable version, and Swift lacks certain generics features (e.g. variadics)
that would be necessary to make static callables general. We feel that static
callable should stand alone on its own merits.

How to access the methods for a higher rule?

During writing validation rules a came across the problem that I need some content from a rule in my grammar which is hierarchically higher than the one I pass to my validationMethod.
I know that I can refer to a "higher" rule with .eContainer but then I don't have a clue how to access the values I want to.
For example I have the following grammar snippet:
rule1:
name=ID content=rule2
;
rule2:
<<whatever content>>
;
If I have a normal validationMethod with the argument rule1 then I can access the name via .name but when I give rule2 as an argument and then referring to rule via .eConatiner the .name method does not exist.
Greetings Krzmbrzl
EObject is the root class of all AST node classes. It comes from the EMF Ecore framework which is used by Xtext to generate the AST implementation. Therefore, the EObject class contains a lot of the tree structure features, e.g., iterating through a tree. The EObject.eContainer() method returns an element of type EObject which actually is the super type of the type of the actual returned object. To get access to methods of the next higher element, you have to cast the eContainers methods result like this:
#Check
public void check(rule2 r2) {
EObject o = r2.eContainer();
rule1 r1 = (rule1) o;
String r1Name = r1.getName();
}
If the type of the parent object is ambigous, you should test whether the actual type is the expected with an instanceof expression:
#Check
public void check(rule2 r2) {
EObject o = r2.eContainer();
if (o instanceof rule1) {
rule1 r1 = (rule1) o;
String r1Name = r1.getName();
}
}
Xtend provide the same instanceof-expression like Java. But if the object to be checked definetly can have more then a few types, you can use Xtends really powerful switch expression. It supports so called type guards. You can switch over any object and instead of case value: guards simply write the a concrete type:
switch (anyAbstractTypeObject) {
ConcreteSubtypeA: {...}
ConcreteSubtypeB: {...}
}
This is an elegant shorthand if-instanceof-eleseif-... in Xtend.

How to getting in Dart the type mirror with the specified type arguments through reflection?

Sample code that explains problem.
import "dart:mirrors";
void main() {
var type = getTypeFromDeclaration();
var typeArguments = getAnotherTypeArguments();
var myType = reflectType(type, typeArguments);
}
How to getting in Dart the type mirror with the specified type arguments through reflection?
P.S.
I think that I don't need to explain "Why this need?" because we all know that this functionality required for the data hydration.
Also this is very useful in data codecs that used reflection for the better data consistency.
As you noticed, I will not explain why.
First, I agree that it would be best if the mirror system allowed creating a type mirror of Map from the type mirrors of Foo and Bar and the class mirror of Map. That is currently not the case.
Without that, I don't think you can solve the problem as written.
There is no way to create a parameterized type with a type argument that is not known as a type at compile-time.
For completeness, I'll include a way to reflect a parameterized type if you know the type at compile time. If the type arguments can be represented as something else than a Type object or a TypeMirror object, you can build your own representation that allows operations like this.
If you can't use reflectType(Map<Foo,Baz>) because Map<Foo,Baz> is not a valid type literal, there is a small workaround to get a Type for any type: Have a class with a type parameter and a way to get the Type value of that parameter.
import "dart:mirrors";
class Typer<T> { Type get type => T; }
main() {
var mapStringInt = reflectType(new Typer<Map<String,int>>().type);
print(mapStringInt); // ClassMirror on 'Map'
print(mapStringInt.typeArguments); // [ClassMirror on 'String', ClassMirror on 'int']
// That is: it's a TypeMirror on Map<String,int>.
}

Resources