Skip to content

Commit 9286280

Browse files
Generator: use analyzer 8 element APIs #154
1 parent 949c4f3 commit 9286280

File tree

2 files changed

+37
-39
lines changed

2 files changed

+37
-39
lines changed

generator/lib/src/entity_resolver.dart

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'dart:async';
22
import 'dart:convert';
33

44
import 'package:analyzer/dart/constant/value.dart';
5-
import 'package:analyzer/dart/element/element2.dart';
5+
import 'package:analyzer/dart/element/element.dart';
66
import 'package:analyzer/dart/element/nullability_suffix.dart';
77
import 'package:analyzer/dart/element/type.dart';
88
import 'package:build/build.dart';
@@ -92,10 +92,10 @@ class EntityResolver extends Builder {
9292
}
9393

9494
ModelEntity generateForAnnotatedElement(
95-
Element2 classElement,
95+
Element classElement,
9696
ConstantReader annotation,
9797
) {
98-
if (classElement is! ClassElement2) {
98+
if (classElement is! ClassElement) {
9999
throw InvalidGenerationSourceError(
100100
"Entity '${classElement.displayName}': annotated element must be a class.",
101101
);
@@ -108,7 +108,7 @@ class EntityResolver extends Builder {
108108
IdUid(0, entityUid.isNull ? 0 : entityUid.intValue),
109109
entityRealClass.isNull
110110
? classElement.displayName
111-
: entityRealClass.typeValue.element3!.displayName,
111+
: entityRealClass.typeValue.element!.displayName,
112112
null,
113113
uidRequest: !entityUid.isNull && entityUid.intValue == 0,
114114
);
@@ -130,7 +130,7 @@ class EntityResolver extends Builder {
130130

131131
// Note: if there is no unnamed constructor this list will just be empty
132132
entity.constructorParams = constructorParams(
133-
classElement.unnamedConstructor2,
133+
classElement.unnamedConstructor,
134134
);
135135

136136
// Make sure all stored fields are writable when reading object from DB.
@@ -139,8 +139,8 @@ class EntityResolver extends Builder {
139139
// * don't have a corresponding argument in the constructor.
140140
// Note: the corresponding setter is also null for final fields.
141141
final readOnlyFields = <String>{};
142-
for (var f in classElement.getters2) {
143-
if (f.correspondingSetter2 == null &&
142+
for (var f in classElement.getters) {
143+
if (f.correspondingSetter == null &&
144144
!entity.constructorParams.any(
145145
(String param) => param.startsWith('${f.displayName} '),
146146
)) {
@@ -149,12 +149,12 @@ class EntityResolver extends Builder {
149149
}
150150

151151
// read all suitable annotated properties
152-
for (var f in classElement.fields2) {
152+
for (var f in classElement.fields) {
153153
// The field might be implicitly defined by a getter, aka it is synthetic
154154
// and does not exist in code. So always resolve the actual non-synthetic
155155
// element that exists in code (here a getter) as only it will have any
156156
// annotations.
157-
final annotated = f.nonSynthetic2;
157+
final annotated = f.nonSynthetic;
158158

159159
if (_transientChecker.hasAnnotationOfExact(annotated)) {
160160
log.info(
@@ -163,7 +163,7 @@ class EntityResolver extends Builder {
163163
continue;
164164
}
165165

166-
if (readOnlyFields.contains(f.name3) && !isRelationField(f)) {
166+
if (readOnlyFields.contains(f.name) && !isRelationField(f)) {
167167
log.info(
168168
" Skipping property '${f.displayName}': is read-only/getter.",
169169
);
@@ -225,10 +225,7 @@ class EntityResolver extends Builder {
225225
continue;
226226
}
227227
relTargetName =
228-
(f.type as ParameterizedType)
229-
.typeArguments[0]
230-
.element3!
231-
.displayName;
228+
(f.type as ParameterizedType).typeArguments[0].element!.displayName;
232229
}
233230

234231
final backlinkAnnotations = _backlinkChecker.annotationsOfExact(
@@ -339,7 +336,7 @@ class EntityResolver extends Builder {
339336

340337
// for code generation
341338
prop.dartFieldType =
342-
f.type.element3!.displayName + (isNullable(f.type) ? '?' : '');
339+
f.type.element!.displayName + (isNullable(f.type) ? '?' : '');
343340
entity.properties.add(prop);
344341
}
345342
}
@@ -350,10 +347,10 @@ class EntityResolver extends Builder {
350347
// for `setId()` won't compile. The only exception is when user uses
351348
// self-assigned IDs, then a different setter will be generated - one that
352349
// checks the ID being set is already the same, otherwise it must throw.
353-
final idField = classElement.fields2.singleWhere(
354-
(FieldElement2 f) => f.displayName == entity.idProperty.name,
350+
final idField = classElement.fields.singleWhere(
351+
(FieldElement f) => f.displayName == entity.idProperty.name,
355352
);
356-
if (idField.setter2 == null) {
353+
if (idField.setter == null) {
357354
if (!entity.idProperty.hasFlag(OBXPropertyFlags.ID_SELF_ASSIGNABLE)) {
358355
throw InvalidGenerationSourceError(
359356
"@Id field '${idField.displayName}' must be writable,"
@@ -384,7 +381,7 @@ class EntityResolver extends Builder {
384381
/// For fields that do not have a [Property.type] declared in their [Property]
385382
/// annotation tries to determine the ObjectBox database type based on the
386383
/// Dart type. May return null if no supported type is detected.
387-
int? detectObjectBoxType(FieldElement2 field, String classDisplayName) {
384+
int? detectObjectBoxType(FieldElement field, String classDisplayName) {
388385
final dartType = field.type;
389386

390387
if (dartType.isDartCoreInt) {
@@ -420,28 +417,28 @@ class EntityResolver extends Builder {
420417
} else if ([
421418
'Int8List',
422419
'Uint8List',
423-
].contains(dartType.element3!.displayName)) {
420+
].contains(dartType.element!.displayName)) {
424421
return OBXPropertyType.ByteVector;
425422
} else if ([
426423
'Int16List',
427424
'Uint16List',
428-
].contains(dartType.element3!.displayName)) {
425+
].contains(dartType.element!.displayName)) {
429426
return OBXPropertyType.ShortVector;
430427
} else if ([
431428
'Int32List',
432429
'Uint32List',
433-
].contains(dartType.element3!.displayName)) {
430+
].contains(dartType.element!.displayName)) {
434431
return OBXPropertyType.IntVector;
435432
} else if ([
436433
'Int64List',
437434
'Uint64List',
438-
].contains(dartType.element3!.displayName)) {
435+
].contains(dartType.element!.displayName)) {
439436
return OBXPropertyType.LongVector;
440-
} else if (dartType.element3!.displayName == 'Float32List') {
437+
} else if (dartType.element!.displayName == 'Float32List') {
441438
return OBXPropertyType.FloatVector;
442-
} else if (dartType.element3!.displayName == 'Float64List') {
439+
} else if (dartType.element!.displayName == 'Float64List') {
443440
return OBXPropertyType.DoubleVector;
444-
} else if (dartType.element3!.displayName == 'DateTime') {
441+
} else if (dartType.element!.displayName == 'DateTime') {
445442
log.warning(
446443
" DateTime property '${field.displayName}' in entity '$classDisplayName' is stored and read using millisecond precision. "
447444
'To silence this warning, add an explicit type using @Property(type: PropertyType.date) or @Property(type: PropertyType.dateNano) annotation.',
@@ -455,7 +452,7 @@ class EntityResolver extends Builder {
455452
return null;
456453
}
457454

458-
void processIdProperty(ModelEntity entity, ClassElement2 classElement) {
455+
void processIdProperty(ModelEntity entity, ClassElement classElement) {
459456
// check properties explicitly annotated with @Id()
460457
final annotated = entity.properties.where(
461458
(p) => p.hasFlag(OBXPropertyFlags.ID),
@@ -500,10 +497,10 @@ class EntityResolver extends Builder {
500497
}
501498

502499
void processAnnotationIndexUnique(
503-
FieldElement2 f,
504-
Element2 annotatedElement,
500+
FieldElement f,
501+
Element annotatedElement,
505502
int? fieldType,
506-
Element2 elementBare,
503+
Element elementBare,
507504
ModelProperty prop,
508505
) {
509506
IndexType? indexType;
@@ -598,7 +595,7 @@ class EntityResolver extends Builder {
598595

599596
void ensureSingleUniqueReplace(
600597
ModelEntity entity,
601-
ClassElement2 classElement,
598+
ClassElement classElement,
602599
) {
603600
final uniqueReplaceProps = entity.properties.where(
604601
(p) => p.hasFlag(OBXPropertyFlags.UNIQUE_ON_CONFLICT_REPLACE),
@@ -613,7 +610,7 @@ class EntityResolver extends Builder {
613610

614611
void ifSyncEnsureAllUniqueAreReplace(
615612
ModelEntity entity,
616-
ClassElement2 classElement,
613+
ClassElement classElement,
617614
) {
618615
if (!entity.hasFlag(OBXEntityFlags.SYNC_ENABLED)) return;
619616
final uniqueButNotReplaceProps = entity.properties.where((p) {
@@ -659,20 +656,19 @@ class EntityResolver extends Builder {
659656
return typeArgs.length == 1 ? typeArgs[0] : null;
660657
}
661658

662-
bool isRelationField(FieldElement2 f) =>
659+
bool isRelationField(FieldElement f) =>
663660
isToOneRelationField(f) || isToManyRelationField(f);
664661

665-
bool isToOneRelationField(FieldElement2 f) =>
666-
f.type.element3!.name3 == 'ToOne';
662+
bool isToOneRelationField(FieldElement f) => f.type.element!.name == 'ToOne';
667663

668-
bool isToManyRelationField(FieldElement2 f) =>
669-
f.type.element3!.name3 == 'ToMany';
664+
bool isToManyRelationField(FieldElement f) =>
665+
f.type.element!.name == 'ToMany';
670666

671667
bool isNullable(DartType type) =>
672668
type.nullabilitySuffix == NullabilitySuffix.star ||
673669
type.nullabilitySuffix == NullabilitySuffix.question;
674670

675-
List<String> constructorParams(ConstructorElement2? constructor) {
671+
List<String> constructorParams(ConstructorElement? constructor) {
676672
if (constructor == null) return List.empty();
677673
return constructor.formalParameters
678674
.map((param) {
@@ -739,7 +735,7 @@ class EntityResolver extends Builder {
739735
}
740736

741737
extension _TypeCheckerExtensions on TypeChecker {
742-
void runIfMatches(Element2 element, void Function(DartObject) fn) {
738+
void runIfMatches(Element element, void Function(DartObject) fn) {
743739
final annotations = annotationsOfExact(element);
744740
if (annotations.isNotEmpty) fn(annotations.first);
745741
}

objectbox/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## latest
22

3+
* Migrate generator to analyzer 8 APIs. Require at least analyzer 8.1.1 and source_gen 4.0.1.
4+
35
## 5.0.0 (2025-10-01)
46

57
**To upgrade to this major release** run `flutter pub upgrade objectbox --major-versions`

0 commit comments

Comments
 (0)