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
1818import java .util .ArrayList ;
1919import java .util .Arrays ;
2020import java .util .Comparator ;
2121import java .util .HashSet ;
22- import java .util .LinkedList ;
2322import java .util .List ;
2423import java .util .Map ;
2524import 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}
0 commit comments