Browse Source

Make MethodName package-private

Closes gh-28832
pull/28834/head
Phillip Webb 3 years ago
parent
commit
87b83e8291
  1. 4
      spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGenerator.java
  2. 11
      spring-core/src/main/java/org/springframework/aot/generate/GeneratedMethods.java
  3. 8
      spring-core/src/main/java/org/springframework/aot/generate/MethodName.java
  4. 14
      spring-core/src/test/java/org/springframework/aot/generate/GeneratedMethodsTests.java
  5. 5
      spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java

4
spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGenerator.java

@ -25,12 +25,12 @@ import org.springframework.aot.generate.GeneratedClass;
import org.springframework.aot.generate.GeneratedMethod; import org.springframework.aot.generate.GeneratedMethod;
import org.springframework.aot.generate.GeneratedMethods; import org.springframework.aot.generate.GeneratedMethods;
import org.springframework.aot.generate.GenerationContext; import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.MethodName;
import org.springframework.aot.generate.MethodReference; import org.springframework.aot.generate.MethodReference;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RegisteredBean; import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.javapoet.ClassName; import org.springframework.javapoet.ClassName;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
/** /**
* Generates a method that returns a {@link BeanDefinition} to be registered. * Generates a method that returns a {@link BeanDefinition} to be registered.
@ -153,7 +153,7 @@ class BeanDefinitionMethodGenerator {
nonGeneratedParent = nonGeneratedParent.getParent(); nonGeneratedParent = nonGeneratedParent.getParent();
} }
if (nonGeneratedParent != null) { if (nonGeneratedParent != null) {
return MethodName.of(getSimpleBeanName(nonGeneratedParent.getBeanName()), "innerBean").toString(); return StringUtils.uncapitalize(getSimpleBeanName(nonGeneratedParent.getBeanName()) + "InnerBean");
} }
return "innerBean"; return "innerBean";
} }

11
spring-core/src/main/java/org/springframework/aot/generate/GeneratedMethods.java

@ -68,17 +68,6 @@ public class GeneratedMethods {
* @return the newly added {@link GeneratedMethod} * @return the newly added {@link GeneratedMethod}
*/ */
public GeneratedMethod add(String suggestedName, Consumer<Builder> method) { public GeneratedMethod add(String suggestedName, Consumer<Builder> method) {
Assert.notNull(suggestedName, "'suggestedName' must not be null");
return add(MethodName.of(suggestedName), method);
}
/**
* Add a new {@link GeneratedMethod}.
* @param suggestedName the suggested name for the method
* @param method a {@link Consumer} used to build the method
* @return the newly added {@link GeneratedMethod}
*/
public GeneratedMethod add(MethodName suggestedName, Consumer<Builder> method) {
Assert.notNull(suggestedName, "'suggestedName' must not be null"); Assert.notNull(suggestedName, "'suggestedName' must not be null");
Assert.notNull(method, "'method' must not be null"); Assert.notNull(method, "'method' must not be null");
String generatedName = this.methodNameGenerator.apply(this.prefix.and(suggestedName)); String generatedName = this.methodNameGenerator.apply(this.prefix.and(suggestedName));

8
spring-core/src/main/java/org/springframework/aot/generate/MethodName.java

@ -28,7 +28,7 @@ import org.springframework.util.StringUtils;
* @author Phillip Webb * @author Phillip Webb
* @since 6.0 * @since 6.0
*/ */
public final class MethodName { final class MethodName {
private static final String[] PREFIXES = { "get", "set", "is" }; private static final String[] PREFIXES = { "get", "set", "is" };
@ -51,7 +51,7 @@ public final class MethodName {
* @param parts the parts the form the name * @param parts the parts the form the name
* @return a method name instance * @return a method name instance
*/ */
public static MethodName of(String... parts) { static MethodName of(String... parts) {
Assert.notNull(parts, "'parts' must not be null"); Assert.notNull(parts, "'parts' must not be null");
return new MethodName(join(parts)); return new MethodName(join(parts));
} }
@ -61,7 +61,7 @@ public final class MethodName {
* @param name the name to concatenate * @param name the name to concatenate
* @return a new method name instance * @return a new method name instance
*/ */
public MethodName and(MethodName name) { MethodName and(MethodName name) {
Assert.notNull(name, "'name' must not be null"); Assert.notNull(name, "'name' must not be null");
return and(name.value); return and(name.value);
} }
@ -71,7 +71,7 @@ public final class MethodName {
* @param parts the parts to concatenate * @param parts the parts to concatenate
* @return a new method name instance * @return a new method name instance
*/ */
public MethodName and(String... parts) { MethodName and(String... parts) {
Assert.notNull(parts, "'parts' must not be null"); Assert.notNull(parts, "'parts' must not be null");
String joined = join(parts); String joined = join(parts);
String prefix = getPrefix(joined); String prefix = getPrefix(joined);

14
spring-core/src/test/java/org/springframework/aot/generate/GeneratedMethodsTests.java

@ -52,20 +52,6 @@ class GeneratedMethodsTests {
assertThat(methods.add("test", methodSpecCustomizer).getName()).hasToString("__test"); assertThat(methods.add("test", methodSpecCustomizer).getName()).hasToString("__test");
} }
@Test
void addWithMethodNameWhenSuggestedMethodIsNullThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() ->
this.methods.add((MethodName) null, methodSpecCustomizer))
.withMessage("'suggestedName' must not be null");
}
@Test
void addWithMethodNameWhenMethodIsNullThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() ->
this.methods.add(MethodName.of("test"), null))
.withMessage("'method' must not be null");
}
@Test @Test
void addWithStringNameWhenSuggestedMethodIsNullThrowsException() { void addWithStringNameWhenSuggestedMethodIsNullThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException().isThrownBy(() ->

5
spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java

@ -43,7 +43,6 @@ import org.springframework.aot.generate.GeneratedClass;
import org.springframework.aot.generate.GeneratedMethod; import org.springframework.aot.generate.GeneratedMethod;
import org.springframework.aot.generate.GeneratedMethods; import org.springframework.aot.generate.GeneratedMethods;
import org.springframework.aot.generate.GenerationContext; import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.generate.MethodName;
import org.springframework.aot.generate.MethodReference; import org.springframework.aot.generate.MethodReference;
import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.RuntimeHints;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
@ -826,8 +825,8 @@ public class PersistenceAnnotationBeanPostProcessor implements InstantiationAwar
EntityManagerFactoryUtils.class, ListableBeanFactory.class, EntityManagerFactoryUtils.class, ListableBeanFactory.class,
REGISTERED_BEAN_PARAMETER, unitName); REGISTERED_BEAN_PARAMETER, unitName);
} }
GeneratedMethod generatedMethod = generatedMethods String methodName = "get" + StringUtils.capitalize(unitName) + "EntityManager";
.add(MethodName.of("get", unitName, "EntityManager"), method -> GeneratedMethod generatedMethod = generatedMethods.add(methodName, method ->
generateGetEntityManagerMethod(method, injectedElement)); generateGetEntityManagerMethod(method, injectedElement));
return CodeBlock.of("$L($L)", generatedMethod.getName(), REGISTERED_BEAN_PARAMETER); return CodeBlock.of("$L($L)", generatedMethod.getName(), REGISTERED_BEAN_PARAMETER);
} }

Loading…
Cancel
Save