generateField method
Future<FieldDeclaration>
generateField(
- VariableMirror fieldMirror,
- Element? parentElement,
- Package package,
- String libraryUri,
- Uri sourceUri,
- String className,
- ClassDeclaration? parentClass,
- String? sourceCode,
Generate field declaration with analyzer support
Implementation
Future<FieldDeclaration> generateField(
mirrors.VariableMirror fieldMirror,
Element? parentElement,
Package package,
String libraryUri,
Uri sourceUri,
String className,
ClassDeclaration? parentClass,
String? sourceCode,
) async {
final fieldName = mirrors.MirrorSystem.getName(fieldMirror.simpleName);
// Get analyzer field element
FieldElement? fieldElement;
if (parentElement is InterfaceElement) {
fieldElement = parentElement.getField(fieldName);
}
final dartType = fieldElement?.type;
final mirrorType = fieldMirror.type;
Type runtimeType = mirrorType.hasReflectedType ? mirrorType.reflectedType : mirrorType.runtimeType;
// Extract annotations and resolve type
if(GenericTypeParser.shouldCheckGeneric(runtimeType)) {
final annotations = await _extractAnnotations(mirrorType.metadata, package);
final resolvedType = await _resolveTypeFromGenericAnnotation(annotations, fieldName);
if (resolvedType != null) {
runtimeType = resolvedType;
}
}
return StandardFieldDeclaration(
name: fieldName,
type: runtimeType,
element: fieldElement,
dartType: dartType,
libraryDeclaration: _libraryCache[libraryUri]!,
parentClass: parentClass != null ? StandardLinkDeclaration(
name: parentClass.getName(),
type: parentClass.getType(),
pointerType: parentClass.getType(),
qualifiedName: parentClass.getQualifiedName(),
isPublic: parentClass.getIsPublic(),
canonicalUri: Uri.parse(parentClass.getPackageUri()),
referenceUri: Uri.parse(parentClass.getPackageUri()),
isSynthetic: parentClass.getIsSynthetic(),
) : null,
linkDeclaration: await _getLinkDeclaration(fieldMirror.type, package, libraryUri, dartType),
annotations: await _extractAnnotations(fieldMirror.metadata, package),
sourceLocation: sourceUri,
isFinal: fieldMirror.isFinal,
isConst: fieldMirror.isConst,
isLate: _isLateField(sourceCode, fieldName),
isStatic: fieldMirror.isStatic,
isAbstract: false,
isPublic: !_isInternal(fieldName),
isSynthetic: _isSynthetic(fieldName),
isNullable: _isNullable(
fieldName: fieldName,
sourceCode: sourceCode ?? await _readSourceCode(sourceUri),
fieldElement: fieldElement
)
);
}