1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16- package org .springframework .data .repository .config ;
16+ package org .springframework .data .repository .aot . generate ;
1717
1818import java .util .LinkedHashMap ;
1919import java .util .Map ;
2020import java .util .Map .Entry ;
2121import java .util .function .Supplier ;
2222
23+ import javax .lang .model .element .Modifier ;
24+
2325import org .springframework .beans .factory .BeanFactory ;
2426import org .springframework .core .ResolvableType ;
25- import org .springframework .data .repository .aot .generate .RepositoryContributor ;
2627import org .springframework .data .repository .core .support .RepositoryComposition ;
2728import org .springframework .data .repository .core .support .RepositoryFactoryBeanSupport ;
2829import org .springframework .javapoet .CodeBlock ;
30+ import org .springframework .javapoet .MethodSpec ;
2931import org .springframework .javapoet .TypeName ;
32+ import org .springframework .javapoet .TypeSpec ;
3033import org .springframework .util .Assert ;
3134import org .springframework .util .StringUtils ;
3235
3942 * @author Christoph Strobl
4043 * @since 4.0
4144 */
42- class AotRepositoryBeanDefinitionPropertiesDecorator {
45+ public class AotRepositoryBeanDefinitionPropertiesDecorator {
4346
4447 private static final Map <ResolvableType , String > RESERVED_TYPES ;
4548
@@ -57,10 +60,13 @@ class AotRepositoryBeanDefinitionPropertiesDecorator {
5760 * @param inheritedProperties bean definition code (containing properties and such) already added via another
5861 * component.
5962 * @param repositoryContributor the contributor providing the actual AOT repository implementation.
63+ * @throws IllegalArgumentException if {@link RepositoryContributor#getContributedTypeName()} is not set.
6064 */
6165 public AotRepositoryBeanDefinitionPropertiesDecorator (Supplier <CodeBlock > inheritedProperties ,
6266 RepositoryContributor repositoryContributor ) {
6367
68+ Assert .notNull (repositoryContributor .getContributedTypeName (), "Contributed type name must not be null" );
69+
6470 this .inheritedProperties = inheritedProperties ;
6571 this .repositoryContributor = repositoryContributor ;
6672 }
@@ -73,54 +79,58 @@ public AotRepositoryBeanDefinitionPropertiesDecorator(Supplier<CodeBlock> inheri
7379 * needs to have potential constructor arguments resolved.
7480 *
7581 * @return the decorated code block.
76- * @throws IllegalArgumentException if {@link RepositoryContributor#getContributedTypeName()} is not set.
7782 */
7883 public CodeBlock decorate () {
7984
80- Assert .notNull (repositoryContributor .getContributedTypeName (), "Contributed type name must not be null" );
81-
8285 CodeBlock .Builder builder = CodeBlock .builder ();
86+
8387 // bring in properties as usual
8488 builder .add (inheritedProperties .get ());
8589
86- builder .add ("beanDefinition.getPropertyValues().addPropertyValue(\" repositoryFragmentsFunction\" , new $T() {\n " ,
87- RepositoryFactoryBeanSupport .RepositoryFragmentsFunction .class );
88- builder .indent ();
90+ MethodSpec .Builder callbackMethod = MethodSpec .methodBuilder ("getRepositoryFragments" ).addModifiers (Modifier .PUBLIC )
91+ .returns (RepositoryComposition .RepositoryFragments .class );
8992
90- builder .add ("public $T getRepositoryFragments(" , RepositoryComposition .RepositoryFragments .class );
91- int counter = 0 ;
9293 for (Entry <ResolvableType , String > entry : RESERVED_TYPES .entrySet ()) {
93- builder .add ("$T $L" , entry .getKey ().toClass (), entry .getValue ());
94- if (++counter < RESERVED_TYPES .size ()) {
95- builder .add (", " );
96- }
94+ callbackMethod .addParameter (entry .getKey ().toClass (), entry .getValue ());
9795 }
98- builder .add (") {\n " );
9996
100- builder .indent ();
97+ callbackMethod .addCode (buildCallbackBody ());
98+
99+ TypeSpec repositoryFragmentsFunction = TypeSpec .anonymousClassBuilder ("" )
100+ .superclass (RepositoryFactoryBeanSupport .RepositoryFragmentsFunction .class ).addMethod (callbackMethod .build ())
101+ .build ();
101102
102- for (Map .Entry <String , ResolvableType > entry : repositoryContributor .requiredArgs ().entrySet ()) {
103+ builder .addStatement ("beanDefinition.getPropertyValues().addPropertyValue($S, $L)" , "repositoryFragmentsFunction" ,
104+ repositoryFragmentsFunction );
105+
106+ return builder .build ();
107+ }
108+
109+ private CodeBlock buildCallbackBody () {
110+
111+ CodeBlock .Builder callback = CodeBlock .builder ();
112+
113+ for (Entry <String , ResolvableType > entry : repositoryContributor .requiredArgs ().entrySet ()) {
103114
104115 TypeName argumentType = TypeName .get (entry .getValue ().getType ());
105116 String reservedArgumentName = RESERVED_TYPES .get (entry .getValue ());
106117 if (reservedArgumentName == null ) {
107- builder .addStatement ("$1T $2L = beanFactory.getBean($1T.class)" , argumentType , entry .getKey ());
118+ callback .addStatement ("$1T $2L = beanFactory.getBean($1T.class)" , argumentType , entry .getKey ());
108119 } else {
109- if (!reservedArgumentName .equals (entry .getKey ())) {
110- builder .addStatement ("$T $L = $L" , argumentType , entry .getKey (), reservedArgumentName );
120+
121+ if (reservedArgumentName .equals (entry .getKey ())) {
122+ continue ;
111123 }
124+
125+ callback .addStatement ("$T $L = $L" , argumentType , entry .getKey (), reservedArgumentName );
112126 }
113127 }
114128
115- builder .addStatement ("return RepositoryComposition.RepositoryFragments. just(new $L($L))" ,
129+ callback .addStatement ("return $T. just(new $L($L))" , RepositoryComposition . RepositoryFragments . class ,
116130 repositoryContributor .getContributedTypeName ().getCanonicalName (),
117131 StringUtils .collectionToDelimitedString (repositoryContributor .requiredArgs ().keySet (), ", " ));
118- builder .unindent ();
119- builder .add ("}\n " );
120- builder .unindent ();
121- builder .add ("});\n " );
122132
123- return builder .build ();
133+ return callback .build ();
124134 }
125135
126136}
0 commit comments