2013-11-26 2 views
0

Darts Mirrors은 현재 문서화가 잘되어 있지 않으며 실험하기 매우 어렵습니다. 코드는 콘솔 내에서와 다르게 동작합니다.다트에서 수업 트리를하는 사람이 있습니까?

내 자신의 사용

, 내가 노드가 같은 것을 함께하는 나무로 클래스 (Type들)을 처리 할 수 ​​싶어요 : 내 인생

class Node { 
    type ... <== Type itself 
    name ... <== name of current class 
    super ... <== super of this class, eg, extends super 
    mixins ... <== mixins used to build this Type 
    extendChildren ... <== Types for which this type is super 
    mixinChildren ... <== Types for which this type is a mixin 
} 

를, 내가 뭔가를 얻을 수 없다 이 기본 전류는 Mirrors입니다. 나보다 더 똑똑한 누군가가 그 기회를 갖기를 바란다 !!

답변

1
import 'dart:collection'; 
import 'package:reflection/reflection.dart'; 

void main() { 
    var libraryName = const Symbol('dart.core'); 
    var libraries = MirrorSystemInfo.current.isolate.libraries; 
    var libraryCore = libraries.values.singleOrDefault((l) => l.simpleName == libraryName); 
    if(libraryCore != null) { 
    var classMap = libraryCore.getClass(#Map); 
    var subclasses = findAllSubclasses(classMap); 
    for(var subclass in subclasses) { 
     print(SymbolHelper.getName(subclass.simpleName)); 
    } 
    } 
} 

List<String> findAllSubclasses(TypeInfo type) { 
    var typeOrig = type.originalDeclaration; 
    var childs = MirrorSystemInfo.current.isolate.libraries.values 
    .select((library) => library.getClasses(BindingFlags.PRIVATE | BindingFlags.PUBLIC).values) 
    .selectMany((clazz) => clazz) 
    .where((clazz) => clazz.isA(type) && clazz.originalDeclaration != typeOrig) 
    .orderBy((clazz) => clazz.simpleName, compareSymbols); 
    return childs.toList(); 
} 

int compareSymbols(Symbol a, Symbol b) { 
    return Comparable.compare(SymbolHelper.getName(a), SymbolHelper.getName(b)); 
} 
DelegatingMap 
HashMap 
HttpSession 
ImmutableMap 
LinkedHashMap 
ListMapView 
SplayTreeMap 
UnmodifiableMapMixin 
UnmodifiableMapView 
_CaseInsensitiveStringMap 
_CustomHashMap 
_HashMap 
_HttpSession 
_IdentityHashMap 
_LinkedCustomHashMap 
_LinkedHashMap 
_LinkedHashMapMixin 
_LinkedIdentityHashMap 
_UnmodifiableMap 
_UnmodifiableMap 
_UnmodifiableMapView 
2

다음은 수퍼 클래스의 이름과 Foo 멤버의 이름을 인쇄하는 간단한 예제입니다.

API는 문자열이 아닌 기호를 사용합니다. 이것들은 dart2js가 미러를 사용하는 코드를 축소 할 수 있도록하기 위해 필요합니다. 조금이라도 힘들지만, 코드가 크로스 브라우저로 실행되고 컴팩트해야합니다.

기호와 문자열을 변환하려면 MirrorSystem.getName()MirrorSystem.getSymbol()을 참조하십시오. 실제로 (실제로는 새로운 기호 ('foo')를 사용할 수 있습니다).

또한 기호에 대한 특수 리터럴 구문을 제공하는 새로운 기능이 최근 추가되었습니다. 최근까지는 const 기호 ('foo')를 입력해야했지만 지금은 #foo 만 입력해야 예제를 볼 때 이전 글꼴과 새 글꼴이 혼합되어 나타날 수 있습니다.

거울에 대한 자세한 내용은 article을 참조하십시오.

경고 - 예에서 약간의 오타가있을 수 있습니다.

import 'dart:mirrors'; 

class Bob { 
} 

class Foo extends Bob { 
    String bar = 'jim'; 
} 

main() { 
    var classMirror = reflectClass(Foo); 

    print(MirrorSystem.getName(classMirror.superClass.simpleName)); 

    classMirror.declarations.values.forEach((d) => print(MirrorSystem.getName(d.simpleName))); 
} 

업데이트 : 앨런은 아래의 말씀을 바탕으로 (또한 검증되지 않은) :

예 출처 :

library foo; 

class B extends A { 
} 

class A { 
} 

정의 :

List<ClassMirror> findSubClasses(Symbol libraryName, ClassMirror superClass) => 
    currentMirrorSystem().findLibrary(libraryName).declarations.values 
     .where((d) => d is ClassMirror 
      && d.superClass.simpleName == superClass.simpleName); 

사용법 :

var cm = reflectClass(A); 
var subclasses = findSubClasses(#foo, cm); 

js로 컴파일하는 데 관심이 있다면 실험 할 수도있는 @MirrorsUsed 속성이 있습니다. 아직 실험적이어서 변화가 예상됩니다.

+0

감사합니다. 이걸 멀리 할 수있었습니다. 내가 우연히 만났던 곳은'super/extend'와'mixin/with'에서 아이들을 낳는 것이 었습니다. 어떤 생각이 어떻게 할 수 있습니까? –

+0

이것을 의미합니까? classMirror.superClass.declarations.values.forEach (....) 나는 믹스와 함께 거울을 사용하려하지 않았다. 이게 무엇을 출력합니까? classMirror.mixin.declarations.values.forEach (...) –

+0

클래스 자체에서 하위 클래스를 가져올 수 있다고 생각하지 않으면 모든 클래스를 반복하고 수퍼 클래스를 요청해야합니다. 믹스 인에 대해서도 마찬가지입니다. 짜증나지만, 모든 클래스에서 하위 클래스에 도달 할 수 있다면 어떤 클래스에서든지 어디서나 볼 수 있으므로 볼 수있는 것을 알 수있는 방법이 없다는 트리 흔들림의 근거가 있다고 생각합니다. –