Skip to content

Commit ee67f28

Browse files
Fix flipped type args for RelationInfo ToOne and ToMany getters, add type args.
1 parent a316cb4 commit ee67f28

File tree

6 files changed

+32
-34
lines changed

6 files changed

+32
-34
lines changed

objectbox-java/src/main/java/io/objectbox/query/EagerRelation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
import io.objectbox.relation.RelationInfo;
2020

21-
class EagerRelation {
21+
class EagerRelation<S, T> {
2222
public final int limit;
23-
public final RelationInfo relationInfo;
23+
public final RelationInfo<S, T> relationInfo;
2424

25-
EagerRelation(int limit, RelationInfo relationInfo) {
25+
EagerRelation(int limit, RelationInfo<S, T> relationInfo) {
2626
this.limit = limit;
2727
this.relationInfo = relationInfo;
2828
}

objectbox-java/src/main/java/io/objectbox/query/Query.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ native void nativeSetParameter(long handle, int entityId, int propertyId, @Nulla
9595
final Box<T> box;
9696
private final BoxStore store;
9797
private final QueryPublisher<T> publisher;
98-
@Nullable private final List<EagerRelation> eagerRelations;
98+
@Nullable private final List<EagerRelation<T, ?>> eagerRelations;
9999
@Nullable private final QueryFilter<T> filter;
100100
@Nullable private final Comparator<T> comparator;
101101
private final int queryAttempts;
102102
private static final int INITIAL_RETRY_BACK_OFF_IN_MS = 10;
103103

104104
long handle;
105105

106-
Query(Box<T> box, long queryHandle, @Nullable List<EagerRelation> eagerRelations, @Nullable QueryFilter<T> filter,
106+
Query(Box<T> box, long queryHandle, @Nullable List<EagerRelation<T, ?>> eagerRelations, @Nullable QueryFilter<T> filter,
107107
@Nullable Comparator<T> comparator) {
108108
this.box = box;
109109
store = box.getStore();
@@ -333,7 +333,7 @@ void resolveEagerRelations(List<T> entities) {
333333
/** Note: no null check on eagerRelations! */
334334
void resolveEagerRelationForNonNullEagerRelations(@Nonnull T entity, int entityIndex) {
335335
//noinspection ConstantConditions No null check.
336-
for (EagerRelation eagerRelation : eagerRelations) {
336+
for (EagerRelation<T, ?> eagerRelation : eagerRelations) {
337337
if (eagerRelation.limit == 0 || entityIndex < eagerRelation.limit) {
338338
resolveEagerRelation(entity, eagerRelation);
339339
}
@@ -342,27 +342,25 @@ void resolveEagerRelationForNonNullEagerRelations(@Nonnull T entity, int entityI
342342

343343
void resolveEagerRelation(@Nullable T entity) {
344344
if (eagerRelations != null && entity != null) {
345-
for (EagerRelation eagerRelation : eagerRelations) {
345+
for (EagerRelation<T, ?> eagerRelation : eagerRelations) {
346346
resolveEagerRelation(entity, eagerRelation);
347347
}
348348
}
349349
}
350350

351-
void resolveEagerRelation(@Nonnull T entity, EagerRelation eagerRelation) {
351+
void resolveEagerRelation(@Nonnull T entity, EagerRelation<T, ?> eagerRelation) {
352352
if (eagerRelations != null) {
353-
RelationInfo relationInfo = eagerRelation.relationInfo;
353+
RelationInfo<T, ?> relationInfo = eagerRelation.relationInfo;
354354
if (relationInfo.toOneGetter != null) {
355-
//noinspection unchecked Can't know target entity type.
356-
ToOne toOne = relationInfo.toOneGetter.getToOne(entity);
355+
ToOne<?> toOne = relationInfo.toOneGetter.getToOne(entity);
357356
if (toOne != null) {
358357
toOne.getTarget();
359358
}
360359
} else {
361360
if (relationInfo.toManyGetter == null) {
362361
throw new IllegalStateException("Relation info without relation getter: " + relationInfo);
363362
}
364-
//noinspection unchecked Can't know target entity type.
365-
List toMany = relationInfo.toManyGetter.getToMany(entity);
363+
List<?> toMany = relationInfo.toManyGetter.getToMany(entity);
366364
if (toMany != null) {
367365
//noinspection ResultOfMethodCallIgnored Triggers fetching target entities.
368366
toMany.size();

objectbox-java/src/main/java/io/objectbox/query/QueryBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ enum Operator {
9898
private Operator combineNextWith = Operator.NONE;
9999

100100
@Nullable
101-
private List<EagerRelation> eagerRelations;
101+
private List<EagerRelation<T, ?>> eagerRelations;
102102

103103
@Nullable
104104
private QueryFilter<T> filter;
@@ -382,10 +382,10 @@ public QueryBuilder<T> eager(int limit, RelationInfo relationInfo, @Nullable Rel
382382
if (eagerRelations == null) {
383383
eagerRelations = new ArrayList<>();
384384
}
385-
eagerRelations.add(new EagerRelation(limit, relationInfo));
385+
eagerRelations.add(new EagerRelation<>(limit, relationInfo));
386386
if (more != null) {
387387
for (RelationInfo info : more) {
388-
eagerRelations.add(new EagerRelation(limit, info));
388+
eagerRelations.add(new EagerRelation<>(limit, info));
389389
}
390390
}
391391
return this;

objectbox-java/src/main/java/io/objectbox/relation/RelationInfo.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ public class RelationInfo<SOURCE, TARGET> implements Serializable {
4444
public final int targetRelationId;
4545

4646
/** Only set for ToOne relations */
47-
public final ToOneGetter<TARGET> toOneGetter;
47+
public final ToOneGetter<SOURCE> toOneGetter;
4848

4949
/** Only set for ToMany relations */
50-
public final ToManyGetter<TARGET> toManyGetter;
50+
public final ToManyGetter<SOURCE> toManyGetter;
5151

5252
/** For ToMany relations based on ToOne backlinks (null otherwise). */
53-
public final ToOneGetter<SOURCE> backlinkToOneGetter;
53+
public final ToOneGetter<TARGET> backlinkToOneGetter;
5454

5555
/** For ToMany relations based on ToMany backlinks (null otherwise). */
56-
public final ToManyGetter<SOURCE> backlinkToManyGetter;
56+
public final ToManyGetter<TARGET> backlinkToManyGetter;
5757

5858
/** For stand-alone to-many relations (0 otherwise). */
5959
public final int relationId;
@@ -62,7 +62,7 @@ public class RelationInfo<SOURCE, TARGET> implements Serializable {
6262
* ToOne
6363
*/
6464
public RelationInfo(EntityInfo<SOURCE> sourceInfo, EntityInfo<TARGET> targetInfo, Property<SOURCE> targetIdProperty,
65-
ToOneGetter toOneGetter) {
65+
ToOneGetter<SOURCE> toOneGetter) {
6666
this.sourceInfo = sourceInfo;
6767
this.targetInfo = targetInfo;
6868
this.targetIdProperty = targetIdProperty;
@@ -77,8 +77,8 @@ public RelationInfo(EntityInfo<SOURCE> sourceInfo, EntityInfo<TARGET> targetInfo
7777
/**
7878
* ToMany as a ToOne backlink
7979
*/
80-
public RelationInfo(EntityInfo<SOURCE> sourceInfo, EntityInfo<TARGET> targetInfo, ToManyGetter toManyGetter,
81-
Property<TARGET> targetIdProperty, ToOneGetter backlinkToOneGetter) {
80+
public RelationInfo(EntityInfo<SOURCE> sourceInfo, EntityInfo<TARGET> targetInfo, ToManyGetter<SOURCE> toManyGetter,
81+
Property<TARGET> targetIdProperty, ToOneGetter<TARGET> backlinkToOneGetter) {
8282
this.sourceInfo = sourceInfo;
8383
this.targetInfo = targetInfo;
8484
this.targetIdProperty = targetIdProperty;
@@ -93,8 +93,8 @@ public RelationInfo(EntityInfo<SOURCE> sourceInfo, EntityInfo<TARGET> targetInfo
9393
/**
9494
* ToMany as a ToMany backlink
9595
*/
96-
public RelationInfo(EntityInfo<SOURCE> sourceInfo, EntityInfo<TARGET> targetInfo, ToManyGetter toManyGetter,
97-
ToManyGetter backlinkToManyGetter, int targetRelationId) {
96+
public RelationInfo(EntityInfo<SOURCE> sourceInfo, EntityInfo<TARGET> targetInfo, ToManyGetter<SOURCE> toManyGetter,
97+
ToManyGetter<TARGET> backlinkToManyGetter, int targetRelationId) {
9898
this.sourceInfo = sourceInfo;
9999
this.targetInfo = targetInfo;
100100
this.toManyGetter = toManyGetter;
@@ -109,7 +109,7 @@ public RelationInfo(EntityInfo<SOURCE> sourceInfo, EntityInfo<TARGET> targetInfo
109109
/**
110110
* Stand-alone ToMany.
111111
*/
112-
public RelationInfo(EntityInfo<SOURCE> sourceInfo, EntityInfo<TARGET> targetInfo, ToManyGetter toManyGetter,
112+
public RelationInfo(EntityInfo<SOURCE> sourceInfo, EntityInfo<TARGET> targetInfo, ToManyGetter<SOURCE> toManyGetter,
113113
int relationId) {
114114
this.sourceInfo = sourceInfo;
115115
this.targetInfo = targetInfo;

objectbox-java/src/main/java/io/objectbox/relation/ToMany.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class ToMany<TARGET> implements List<TARGET>, Serializable {
9090
transient private boolean removeFromTargetBox;
9191
transient private Comparator<TARGET> comparator;
9292

93-
public ToMany(Object sourceEntity, RelationInfo<? extends Object, TARGET> relationInfo) {
93+
public ToMany(Object sourceEntity, RelationInfo<?, TARGET> relationInfo) {
9494
//noinspection ConstantConditions Annotation does not enforce non-null.
9595
if (sourceEntity == null) {
9696
throw new IllegalArgumentException("No source entity given (null)");
@@ -690,7 +690,7 @@ public boolean internalCheckApplyToDbRequired() {
690690

691691
private boolean prepareToManyBacklinkEntitiesForDb(long entityId, IdGetter<TARGET> idGetter,
692692
@Nullable Map<TARGET, Boolean> setAdded, @Nullable Map<TARGET, Boolean> setRemoved) {
693-
ToManyGetter backlinkToManyGetter = relationInfo.backlinkToManyGetter;
693+
ToManyGetter<TARGET> backlinkToManyGetter = relationInfo.backlinkToManyGetter;
694694

695695
synchronized (this) {
696696
if (setAdded != null && !setAdded.isEmpty()) {
@@ -734,7 +734,7 @@ private boolean prepareToManyBacklinkEntitiesForDb(long entityId, IdGetter<TARGE
734734

735735
private boolean prepareToOneBacklinkEntitiesForDb(long entityId, IdGetter<TARGET> idGetter,
736736
@Nullable Map<TARGET, Boolean> setAdded, @Nullable Map<TARGET, Boolean> setRemoved) {
737-
ToOneGetter backlinkToOneGetter = relationInfo.backlinkToOneGetter;
737+
ToOneGetter<TARGET> backlinkToOneGetter = relationInfo.backlinkToOneGetter;
738738

739739
synchronized (this) {
740740
if (setAdded != null && !setAdded.isEmpty()) {

objectbox-java/src/main/java/io/objectbox/relation/ToOne.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public class ToOne<TARGET> implements Serializable {
4444
private static final long serialVersionUID = 5092547044335989281L;
4545

4646
private final Object entity;
47-
private final RelationInfo relationInfo;
47+
private final RelationInfo<Object, TARGET> relationInfo;
4848
private final boolean virtualProperty;
4949

5050
transient private BoxStore boxStore;
51-
transient private Box entityBox;
51+
transient private Box<Object> entityBox;
5252
transient private volatile Box<TARGET> targetBox;
5353
transient private Field targetIdField;
5454

@@ -71,15 +71,16 @@ public class ToOne<TARGET> implements Serializable {
7171
* @param sourceEntity The source entity that owns the to-one relation.
7272
* @param relationInfo Meta info as generated in the Entity_ (entity name plus underscore) classes.
7373
*/
74-
public ToOne(Object sourceEntity, RelationInfo relationInfo) {
74+
@SuppressWarnings("unchecked") // RelationInfo cast: ? is at least Object.
75+
public ToOne(Object sourceEntity, RelationInfo<?, TARGET> relationInfo) {
7576
if (sourceEntity == null) {
7677
throw new IllegalArgumentException("No source entity given (null)");
7778
}
7879
if (relationInfo == null) {
7980
throw new IllegalArgumentException("No relation info given (null)");
8081
}
8182
this.entity = sourceEntity;
82-
this.relationInfo = relationInfo;
83+
this.relationInfo = (RelationInfo<Object, TARGET>) relationInfo;
8384
virtualProperty = relationInfo.targetIdProperty.isVirtual;
8485
}
8586

@@ -128,7 +129,6 @@ private void ensureBoxes(@Nullable TARGET target) {
128129
throw new RuntimeException(e);
129130
}
130131
entityBox = boxStore.boxFor(relationInfo.sourceInfo.getEntityClass());
131-
//noinspection unchecked
132132
targetBox = boxStore.boxFor(relationInfo.targetInfo.getEntityClass());
133133
}
134134
}

0 commit comments

Comments
 (0)