Skip to content

Commit c6086df

Browse files
committed
Refactor and simplify association linking logic
1 parent abf8336 commit c6086df

File tree

9 files changed

+70
-94
lines changed

9 files changed

+70
-94
lines changed

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
editorial revisions, annotations, elaborations, or other modifications
4444
represent, as a whole, an original work of authorship. For the purposes
4545
of this License, Derivative Works shall not include works that remain
46-
separable from, or merely link (or bind by name) to the interfaces of,
46+
separable from, or merely linkMeta (or bind by name) to the interfaces of,
4747
the Work and Derivative Works thereof.
4848

4949
"Contribution" shall mean any work of authorship, including

doma-processor/src/main/java/org/seasar/doma/internal/apt/cttype/EntityCtType.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public <R, P, TH extends Throwable> R accept(CtTypeVisitor<R, P, TH> visitor, P
5353
return visitor.visitEntityCtType(this, p);
5454
}
5555

56+
/**
57+
* Resolves an {@code EntityCtType} from the given {@code CtType}. This method traverses the type
58+
* hierarchy of the provided {@code CtType} and identifies if it corresponds to an {@code
59+
* EntityCtType}. It also handles nested and optional types.
60+
*
61+
* @param ctType the {@code CtType} instance to resolve into an {@code EntityCtType}.
62+
* @return the resolved {@code EntityCtType} if found; otherwise, null.
63+
*/
5664
public static EntityCtType resolveEntityCtType(CtType ctType) {
5765
class EntityCtTypeVisitor extends SimpleCtTypeVisitor<EntityCtType, Void, RuntimeException> {
5866
@Override

doma-processor/src/main/java/org/seasar/doma/internal/apt/generator/AggregateStrategyTypeGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import java.util.Iterator;
2121
import org.seasar.doma.internal.ClassName;
2222
import org.seasar.doma.internal.apt.Context;
23-
import org.seasar.doma.internal.apt.meta.entity.AggregateStrategyMeta;
24-
import org.seasar.doma.internal.apt.meta.entity.AssociationLinkerMeta;
23+
import org.seasar.doma.internal.apt.meta.aggregate.AggregateStrategyMeta;
24+
import org.seasar.doma.internal.apt.meta.aggregate.AssociationLinkerMeta;
2525
import org.seasar.doma.jdbc.aggregate.AbstractAggregateStrategyType;
2626
import org.seasar.doma.jdbc.aggregate.AssociationLinkerType;
2727

doma-processor/src/main/java/org/seasar/doma/internal/apt/generator/DaoImplQueryMethodGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545
import org.seasar.doma.internal.apt.cttype.OptionalLongCtType;
4646
import org.seasar.doma.internal.apt.cttype.SimpleCtTypeVisitor;
4747
import org.seasar.doma.internal.apt.cttype.StreamCtType;
48+
import org.seasar.doma.internal.apt.meta.aggregate.AggregateStrategyMeta;
49+
import org.seasar.doma.internal.apt.meta.aggregate.AssociationLinkerMeta;
4850
import org.seasar.doma.internal.apt.meta.dao.DaoMeta;
49-
import org.seasar.doma.internal.apt.meta.entity.AggregateStrategyMeta;
50-
import org.seasar.doma.internal.apt.meta.entity.AssociationLinkerMeta;
5151
import org.seasar.doma.internal.apt.meta.parameter.BasicInOutParameterMeta;
5252
import org.seasar.doma.internal.apt.meta.parameter.BasicInParameterMeta;
5353
import org.seasar.doma.internal.apt.meta.parameter.BasicListParameterMeta;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.seasar.doma.internal.apt.meta.entity;
16+
package org.seasar.doma.internal.apt.meta.aggregate;
1717

1818
import java.util.List;
1919
import java.util.Objects;
Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.seasar.doma.internal.apt.meta.entity;
16+
package org.seasar.doma.internal.apt.meta.aggregate;
1717

1818
import java.util.ArrayList;
1919
import java.util.Arrays;
2020
import java.util.Comparator;
2121
import java.util.HashSet;
22-
import java.util.LinkedList;
2322
import java.util.List;
2423
import java.util.Map;
2524
import java.util.Objects;
@@ -78,11 +77,10 @@ public AggregateStrategyMeta createTypeElementMeta(TypeElement typeElement) {
7877
if (root == null) {
7978
throw new AptException(Message.DOMA4478, typeElement, new Object[] {});
8079
}
81-
List<AssociationLinkerMeta> associationLinkerMetas =
82-
findAssociationLinkerMetas(typeElement, root);
80+
List<AssociationLinkerMeta> associationLinkerMetas = findAssociationLinkerMetas(typeElement);
8381
validateAllPropertyPaths(associationLinkerMetas);
8482
validateAllTableAliases(aggregateStrategyAnnot.getTableAliasValue(), associationLinkerMetas);
85-
validateAssociationNavigation(root, associationLinkerMetas);
83+
validateNavigation(root, associationLinkerMetas);
8684
return new AggregateStrategyMeta(
8785
root, aggregateStrategyAnnot.getTableAliasValue(), associationLinkerMetas);
8886
}
@@ -135,7 +133,7 @@ private void validateAllTableAliases(
135133
}
136134
}
137135

138-
private void validateAssociationNavigation(
136+
private void validateNavigation(
139137
EntityCtType root, List<AssociationLinkerMeta> associationLinkerMetas) {
140138
for (AssociationLinkerMeta linkerMeta : associationLinkerMetas) {
141139
TypeMirror source = root.getType();
@@ -146,24 +144,23 @@ private void validateAssociationNavigation(
146144
resolveEntity(
147145
source, segment, linkerMeta.filedElement(), linkerMeta.associationLinkerAnnot());
148146
}
149-
BiFunctionMeta biFunctionMeta = linkerMeta.biFunctionMeta();
150-
if (!ctx.getMoreTypes().isSameType(biFunctionMeta.source().getType(), source)) {
147+
if (!ctx.getMoreTypes().isSameType(linkerMeta.source().getType(), source)) {
151148
throw new AptException(
152149
Message.DOMA4475,
153150
linkerMeta.filedElement(),
154-
new Object[] {biFunctionMeta.source().getType(), source});
151+
new Object[] {linkerMeta.source().getType(), source});
155152
}
156-
if (!ctx.getMoreTypes().isSameType(biFunctionMeta.target().getType(), target)) {
153+
if (!ctx.getMoreTypes().isSameType(linkerMeta.target().getType(), target)) {
157154
throw new AptException(
158155
Message.DOMA4476,
159156
linkerMeta.filedElement(),
160-
new Object[] {biFunctionMeta.target().getType(), target});
157+
new Object[] {linkerMeta.target().getType(), target});
161158
}
162159
}
163160
}
164161

165162
private List<AssociationLinkerMeta> findAssociationLinkerMetas(
166-
TypeElement aggregateStrategyElement, EntityCtType root) {
163+
TypeElement aggregateStrategyElement) {
167164
List<AssociationLinkerMeta> associationLinkerMetas = new ArrayList<>();
168165

169166
for (VariableElement fieldElement :
@@ -173,10 +170,9 @@ private List<AssociationLinkerMeta> findAssociationLinkerMetas(
173170
continue;
174171
}
175172
validateModifiers(fieldElement);
176-
BiFunctionMeta biFunctionMeta = createBiFunctionMeta(fieldElement);
177173
AssociationLinkerMeta associationLinkerMeta =
178174
createAssociationLinkerMeta(
179-
fieldElement, associationLinkerAnnot, biFunctionMeta, aggregateStrategyElement);
175+
fieldElement, associationLinkerAnnot, aggregateStrategyElement);
180176
associationLinkerMetas.add(associationLinkerMeta);
181177
}
182178

@@ -233,37 +229,6 @@ private void validateModifiers(VariableElement linkerElement) {
233229
}
234230
}
235231

236-
private BiFunctionMeta createBiFunctionMeta(VariableElement linkerElement) {
237-
BiFunctionCtType ctType = ctx.getCtTypes().newBiFunctionCtType(linkerElement.asType());
238-
if (ctType == null) {
239-
throw new AptException(Message.DOMA4465, linkerElement, new Object[] {});
240-
}
241-
CtTypeVisitor<EntityCtType, String, RuntimeException> entityCtTypeVisitor =
242-
new SimpleCtTypeVisitor<>() {
243-
@Override
244-
protected EntityCtType defaultAction(CtType ctType, String ordinalNumber)
245-
throws RuntimeException {
246-
throw new AptException(Message.DOMA4466, linkerElement, new Object[] {ordinalNumber});
247-
}
248-
249-
@Override
250-
public EntityCtType visitEntityCtType(EntityCtType ctType, String ordinalNumber)
251-
throws RuntimeException {
252-
return ctType;
253-
}
254-
};
255-
256-
EntityCtType source = ctType.getFirstArgCtType().accept(entityCtTypeVisitor, "first");
257-
EntityCtType target = ctType.getSecondArgCtType().accept(entityCtTypeVisitor, "second");
258-
EntityCtType result = ctType.getResultCtType().accept(entityCtTypeVisitor, "third");
259-
260-
if (!ctx.getMoreTypes().isSameType(result.getType(), source.getType())) {
261-
throw new AptException(Message.DOMA4467, linkerElement, new Object[] {});
262-
}
263-
264-
return new BiFunctionMeta(source, target, result);
265-
}
266-
267232
private TypeMirror resolveEntity(
268233
TypeMirror typeMirror,
269234
String name,
@@ -295,7 +260,6 @@ private TypeMirror resolveEntity(
295260
}
296261

297262
private Optional<VariableElement> findAssociationField(TypeElement typeElement, String name) {
298-
List<VariableElement> results = new LinkedList<>();
299263
for (TypeElement t = typeElement;
300264
t != null && t.asType().getKind() != TypeKind.NONE;
301265
t = ctx.getMoreTypes().toTypeElement(t.getSuperclass())) {
@@ -321,8 +285,8 @@ private Optional<VariableElement> findAssociationField(TypeElement typeElement,
321285
private AssociationLinkerMeta createAssociationLinkerMeta(
322286
VariableElement fieldElement,
323287
AssociationLinkerAnnot associationLinkerAnnot,
324-
BiFunctionMeta biFunctionMeta,
325288
TypeElement aggregateStrategyElement) {
289+
326290
String propertyPath = associationLinkerAnnot.getPropertyPathValue();
327291
List<String> propertyPathSegments = Arrays.stream(propertyPath.split("\\.")).toList();
328292
int propertyPathDepth = propertyPathSegments.size();
@@ -334,15 +298,52 @@ private AssociationLinkerMeta createAssociationLinkerMeta(
334298
} else {
335299
ancestorPath = propertyPath.substring(0, propertyPath.lastIndexOf('.'));
336300
}
301+
302+
LinkMeta linkMeta = createLinkMeta(fieldElement);
303+
337304
return new AssociationLinkerMeta(
338305
associationLinkerAnnot,
339306
ancestorPath,
340307
associationLinkerAnnot.getPropertyPathValue(),
341308
propertyPathSegments,
342309
propertyPathDepth,
343310
associationLinkerAnnot.getTableAliasValue(),
344-
biFunctionMeta,
311+
linkMeta.source,
312+
linkMeta.target,
345313
aggregateStrategyElement,
346314
fieldElement);
347315
}
316+
317+
private LinkMeta createLinkMeta(VariableElement linkerElement) {
318+
BiFunctionCtType ctType = ctx.getCtTypes().newBiFunctionCtType(linkerElement.asType());
319+
if (ctType == null) {
320+
throw new AptException(Message.DOMA4465, linkerElement, new Object[] {});
321+
}
322+
CtTypeVisitor<EntityCtType, String, RuntimeException> entityCtTypeVisitor =
323+
new SimpleCtTypeVisitor<>() {
324+
@Override
325+
protected EntityCtType defaultAction(CtType ctType, String ordinalNumber)
326+
throws RuntimeException {
327+
throw new AptException(Message.DOMA4466, linkerElement, new Object[] {ordinalNumber});
328+
}
329+
330+
@Override
331+
public EntityCtType visitEntityCtType(EntityCtType ctType, String ordinalNumber)
332+
throws RuntimeException {
333+
return ctType;
334+
}
335+
};
336+
337+
EntityCtType source = ctType.getFirstArgCtType().accept(entityCtTypeVisitor, "first");
338+
EntityCtType target = ctType.getSecondArgCtType().accept(entityCtTypeVisitor, "second");
339+
EntityCtType result = ctType.getResultCtType().accept(entityCtTypeVisitor, "third");
340+
341+
if (!ctx.getMoreTypes().isSameType(result.getType(), source.getType())) {
342+
throw new AptException(Message.DOMA4467, linkerElement, new Object[] {});
343+
}
344+
345+
return new LinkMeta(source, target);
346+
}
347+
348+
private record LinkMeta(EntityCtType source, EntityCtType target) {}
348349
}
Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.seasar.doma.internal.apt.meta.entity;
16+
package org.seasar.doma.internal.apt.meta.aggregate;
1717

1818
import java.util.List;
1919
import java.util.Objects;
@@ -29,7 +29,8 @@ public record AssociationLinkerMeta(
2929
List<String> propertyPathSegments,
3030
int propertyPathDepth,
3131
String tableAlias,
32-
BiFunctionMeta biFunctionMeta,
32+
EntityCtType source,
33+
EntityCtType target,
3334
TypeElement classElement,
3435
VariableElement filedElement) {
3536

@@ -38,16 +39,9 @@ public record AssociationLinkerMeta(
3839
Objects.requireNonNull(propertyPath);
3940
Objects.requireNonNull(propertyPathSegments);
4041
Objects.requireNonNull(tableAlias);
41-
Objects.requireNonNull(biFunctionMeta);
42+
Objects.requireNonNull(source);
43+
Objects.requireNonNull(target);
4244
Objects.requireNonNull(classElement);
4345
Objects.requireNonNull(filedElement);
4446
}
45-
46-
public EntityCtType source() {
47-
return biFunctionMeta.source();
48-
}
49-
50-
public EntityCtType target() {
51-
return biFunctionMeta.target();
52-
}
5347
}

doma-processor/src/main/java/org/seasar/doma/internal/apt/meta/entity/BiFunctionMeta.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

doma-processor/src/main/java/org/seasar/doma/internal/apt/processor/AggregateStrategyProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
import org.seasar.doma.internal.apt.generator.Generator;
3030
import org.seasar.doma.internal.apt.generator.Printer;
3131
import org.seasar.doma.internal.apt.meta.TypeElementMetaFactory;
32-
import org.seasar.doma.internal.apt.meta.entity.AggregateStrategyMeta;
33-
import org.seasar.doma.internal.apt.meta.entity.AggregateStrategyMetaFactory;
32+
import org.seasar.doma.internal.apt.meta.aggregate.AggregateStrategyMeta;
33+
import org.seasar.doma.internal.apt.meta.aggregate.AggregateStrategyMetaFactory;
3434

3535
@SupportedAnnotationTypes({"org.seasar.doma.AggregateStrategy"})
3636
@SupportedOptions({

0 commit comments

Comments
 (0)