13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- package org .springframework .data .repository .config ;
16
+ package org .springframework .data .repository .aot . generate ;
17
17
18
18
import java .util .LinkedHashMap ;
19
19
import java .util .Map ;
20
20
import java .util .Map .Entry ;
21
21
import java .util .function .Supplier ;
22
22
23
+ import javax .lang .model .element .Modifier ;
24
+
23
25
import org .springframework .beans .factory .BeanFactory ;
24
26
import org .springframework .core .ResolvableType ;
25
- import org .springframework .data .repository .aot .generate .RepositoryContributor ;
26
27
import org .springframework .data .repository .core .support .RepositoryComposition ;
27
28
import org .springframework .data .repository .core .support .RepositoryFactoryBeanSupport ;
28
29
import org .springframework .javapoet .CodeBlock ;
30
+ import org .springframework .javapoet .MethodSpec ;
29
31
import org .springframework .javapoet .TypeName ;
32
+ import org .springframework .javapoet .TypeSpec ;
30
33
import org .springframework .util .Assert ;
31
34
import org .springframework .util .StringUtils ;
32
35
39
42
* @author Christoph Strobl
40
43
* @since 4.0
41
44
*/
42
- class AotRepositoryBeanDefinitionPropertiesDecorator {
45
+ public class AotRepositoryBeanDefinitionPropertiesDecorator {
43
46
44
47
private static final Map <ResolvableType , String > RESERVED_TYPES ;
45
48
@@ -57,10 +60,13 @@ class AotRepositoryBeanDefinitionPropertiesDecorator {
57
60
* @param inheritedProperties bean definition code (containing properties and such) already added via another
58
61
* component.
59
62
* @param repositoryContributor the contributor providing the actual AOT repository implementation.
63
+ * @throws IllegalArgumentException if {@link RepositoryContributor#getContributedTypeName()} is not set.
60
64
*/
61
65
public AotRepositoryBeanDefinitionPropertiesDecorator (Supplier <CodeBlock > inheritedProperties ,
62
66
RepositoryContributor repositoryContributor ) {
63
67
68
+ Assert .notNull (repositoryContributor .getContributedTypeName (), "Contributed type name must not be null" );
69
+
64
70
this .inheritedProperties = inheritedProperties ;
65
71
this .repositoryContributor = repositoryContributor ;
66
72
}
@@ -73,54 +79,58 @@ public AotRepositoryBeanDefinitionPropertiesDecorator(Supplier<CodeBlock> inheri
73
79
* needs to have potential constructor arguments resolved.
74
80
*
75
81
* @return the decorated code block.
76
- * @throws IllegalArgumentException if {@link RepositoryContributor#getContributedTypeName()} is not set.
77
82
*/
78
83
public CodeBlock decorate () {
79
84
80
- Assert .notNull (repositoryContributor .getContributedTypeName (), "Contributed type name must not be null" );
81
-
82
85
CodeBlock .Builder builder = CodeBlock .builder ();
86
+
83
87
// bring in properties as usual
84
88
builder .add (inheritedProperties .get ());
85
89
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 );
89
92
90
- builder .add ("public $T getRepositoryFragments(" , RepositoryComposition .RepositoryFragments .class );
91
- int counter = 0 ;
92
93
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 ());
97
95
}
98
- builder .add (") {\n " );
99
96
100
- builder .indent ();
97
+ callbackMethod .addCode (buildCallbackBody ());
98
+
99
+ TypeSpec repositoryFragmentsFunction = TypeSpec .anonymousClassBuilder ("" )
100
+ .superclass (RepositoryFactoryBeanSupport .RepositoryFragmentsFunction .class ).addMethod (callbackMethod .build ())
101
+ .build ();
101
102
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 ()) {
103
114
104
115
TypeName argumentType = TypeName .get (entry .getValue ().getType ());
105
116
String reservedArgumentName = RESERVED_TYPES .get (entry .getValue ());
106
117
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 ());
108
119
} 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 ;
111
123
}
124
+
125
+ callback .addStatement ("$T $L = $L" , argumentType , entry .getKey (), reservedArgumentName );
112
126
}
113
127
}
114
128
115
- builder .addStatement ("return RepositoryComposition.RepositoryFragments. just(new $L($L))" ,
129
+ callback .addStatement ("return $T. just(new $L($L))" , RepositoryComposition . RepositoryFragments . class ,
116
130
repositoryContributor .getContributedTypeName ().getCanonicalName (),
117
131
StringUtils .collectionToDelimitedString (repositoryContributor .requiredArgs ().keySet (), ", " ));
118
- builder .unindent ();
119
- builder .add ("}\n " );
120
- builder .unindent ();
121
- builder .add ("});\n " );
122
132
123
- return builder .build ();
133
+ return callback .build ();
124
134
}
125
135
126
136
}
0 commit comments