This is an automated email from the ASF dual-hosted git repository. borinquenkid pushed a commit to branch groovyism/grails-spring in repository https://gitbox.apache.org/repos/asf/grails-core.git
commit 7f825c77bd212313ce11b4c07e27a77a8f5c3cc6 Author: Walter Duque de Estrada <[email protected]> AuthorDate: Fri Oct 16 16:10:23 2026 -0500 refactor(grails-spring): idiomatic Groovy cleanup of converted bean-config sources Idiomatic Groovy rewrite of the sources converted in #16581 (grails-spring). No behaviour or API change. Applied - equals -> ==: 16 sites (DefaultBeanConfiguration getProperty/setProperty/setAutowire string and Boolean comparisons) - property syntax on plain Spring objects: ~35 sites (AbstractBeanDefinition setters -> property assignment in DefaultBeanConfiguration; BeanConfiguration/PropertyValue/GenericApplicationContext getters in both classes) - Arrays.asList -> list literal: 1 site (DYNAMIC_PROPS) - list.contains(x)/map.containsKey(k) -> `in`: 4 sites - map.get(k)/put(k,v) -> map[k] (reads only; writes kept as .put(), see below): 5 reads - new HashMap<>()/LinkedHashSet<>()/ArrayList<>() -> [:]/[] as Set/[]: 5 sites - list.add(x) -> `<<` (statement position, return value unused): 4 sites - for (Map.Entry e : map.entrySet()) -> map.each { k, v -> }: 3 loops (registerBeanAliasesWithRegistry, registerBeanDefinitionsWithRegistry, registerBeansWithConfig), removing the now-redundant entry.getKey()/getValue() locals and one dead `x == null ? null : x` ternary - instanceof + cast -> flow typing (drop the redundant cast): 4 sites - x != null ? x : y -> Elvis: 1 site (TypeSpecifyableTransactionProxyFactoryBean#getObjectType) - x == null ? return null : x.foo() -> safe navigation: 1 site (DefaultBeanConfiguration#getPropertyValue) - !list.isEmpty() with a preceding null check -> Groovy truth (null and empty already handled identically): 1 site (registerBeanAliasesWithRegistry) - private field + trivial getter -> final property: 1 site (GrailsContextEvent#eventType), preserving the getter-only public surface Deliberately left - BeanBuilder.groovy: left entirely unchanged. It is the runtime beans{} DSL builder (GroovyObjectSupport + hand-written invokeMethod/getProperty/setProperty, plus the nested ConfigurableRuntimeBeanReference/WrappedPropertyValue GroovyObject classes). Per the task's explicit caution, only Tier A null-safety/import idioms were in scope here, and an audit of the file found none that don't touch the dynamic-dispatch surface (every remaining `== null`/`!= null` either guards a throw or picks between two non-identical return values, not an `x?.foo()`/`x ?: y` shape). The one plausible Tier B win - converting the xmlns() Map.Entry loop to map.each - was skipped too, to stay inside the Tier-A-only instruction for this file. - GrailsApplicationContext.groovy: left entirely unchanged for the same reason - @POJO + hand-rolled GroovyObject with an explicit fragile-construction-order comment. No Tier A opportunities were found in it either. - BeanConfiguration.groovy / RuntimeSpringConfiguration.groovy: interfaces with no method bodies; nothing to idiomize. - DefaultBeanConfiguration's 7 constructor overloads: not collapsed into default parameters - they diverge in logic (some set `singleton = !prototype`, others set different defaults), which the skill explicitly excludes from that conversion. - map[k] = v (putAt) was tried for beanConfigs/beanDefinitions/aliases writes, then reverted to .put(k, v): javap showed `map[k] = v` compiling through ScriptBytecodeAdapter.invokeMethodN (a dynamic fallback) under this class's @CompileStatic, unlike `map[k]` reads and `list << x`, which bind statically to DefaultGroovyMethods.getAt/leftShift. Kept the explicit .put() call instead. - String concatenations going into commons-logging Log.debug/error(Object, ...) were left as `+` concatenation, not GString, since the parameter slot is Object, not String (GStringImpl would still work in practice via toString(), but the skill flags this as a real way the rewrite misbehaves, so it was not risked here). Verification grails-spring: compileGroovy/compileTestGroovy clean; codeStyle (checkstyle + CodeNarc) clean; test 21/0 (0 skipped); javap dynamic-dispatch audit clean on all four changed classes (only isIn/compareEqual/createList/initMetaClass ScriptBytecodeAdapter helpers and the pre-existing, untouched MetaObjectProtocol.respondsTo/invokeMethod reflection in removeBeanDefinition remain - no setGroovyObjectProperty/getGroovyObjectProperty/ invokeMethodN); downstream compiled: grails-core. Co-Authored-By: Claude Sonnet 5 <[email protected]> --- .../grails/spring/DefaultBeanConfiguration.groovy | 106 ++++++++++----------- .../DefaultRuntimeSpringConfiguration.groovy | 79 +++++++-------- .../org/grails/spring/GrailsContextEvent.groovy | 6 +- ...peSpecifyableTransactionProxyFactoryBean.groovy | 6 +- 4 files changed, 89 insertions(+), 108 deletions(-) diff --git a/grails-spring/src/main/groovy/org/grails/spring/DefaultBeanConfiguration.groovy b/grails-spring/src/main/groovy/org/grails/spring/DefaultBeanConfiguration.groovy index a41f25f533..07eeae6362 100644 --- a/grails-spring/src/main/groovy/org/grails/spring/DefaultBeanConfiguration.groovy +++ b/grails-spring/src/main/groovy/org/grails/spring/DefaultBeanConfiguration.groovy @@ -65,7 +65,7 @@ class DefaultBeanConfiguration extends GroovyObjectSupport implements BeanConfig private static final String PARENT = 'parent' private static final String BY_TYPE = 'byType' private static final String BY_CONSTRUCTOR = 'constructor' - private static final List<String> DYNAMIC_PROPS = Arrays.asList( + private static final List<String> DYNAMIC_PROPS = [ AUTOWIRE, CONSTRUCTOR_ARGS, DESTROY_METHOD, @@ -74,7 +74,7 @@ class DefaultBeanConfiguration extends GroovyObjectSupport implements BeanConfig INIT_METHOD, BY_NAME, BY_TYPE, - BY_CONSTRUCTOR) + BY_CONSTRUCTOR] private String parentName @@ -85,7 +85,7 @@ class DefaultBeanConfiguration extends GroovyObjectSupport implements BeanConfig if (wrapper.isReadableProperty(property)) { return wrapper.getPropertyValue(property) } - if (DYNAMIC_PROPS.contains(property)) { + if (property in DYNAMIC_PROPS) { return null } return super.getProperty(property) @@ -93,61 +93,61 @@ class DefaultBeanConfiguration extends GroovyObjectSupport implements BeanConfig @Override void setProperty(String property, Object newValue) { - if (PARENT.equals(property)) { - this.setParent(newValue) + if (property == PARENT) { + setParent(newValue) return } AbstractBeanDefinition bd = getBeanDefinition() - if (AUTOWIRE.equals(property)) { - if (BY_NAME.equals(newValue)) { - bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME) + if (property == AUTOWIRE) { + if (newValue == BY_NAME) { + bd.autowireMode = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME } - else if (BY_TYPE.equals(newValue)) { - bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE) + else if (newValue == BY_TYPE) { + bd.autowireMode = AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE } - else if (Boolean.TRUE.equals(newValue)) { - bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME) + else if (newValue == Boolean.TRUE) { + bd.autowireMode = AutowireCapableBeanFactory.AUTOWIRE_BY_NAME } - else if (BY_CONSTRUCTOR.equals(newValue)) { - bd.setAutowireMode(AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR) + else if (newValue == BY_CONSTRUCTOR) { + bd.autowireMode = AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR } } // constructorArgs - else if (CONSTRUCTOR_ARGS.equals(property) && newValue instanceof List<?>) { + else if (property == CONSTRUCTOR_ARGS && newValue instanceof List<?>) { ConstructorArgumentValues cav = new ConstructorArgumentValues() for (Object e in (List<?>) newValue) { cav.addGenericArgumentValue(e) } - bd.setConstructorArgumentValues(cav) + bd.constructorArgumentValues = cav } // destroyMethod - else if (DESTROY_METHOD.equals(property)) { + else if (property == DESTROY_METHOD) { if (newValue != null) { - bd.setDestroyMethodName(newValue.toString()) + bd.destroyMethodName = newValue.toString() } } // factoryBean - else if (FACTORY_BEAN.equals(property)) { + else if (property == FACTORY_BEAN) { if (newValue != null) { - bd.setFactoryBeanName(newValue.toString()) + bd.factoryBeanName = newValue.toString() } } // factoryMethod - else if (FACTORY_METHOD.equals(property)) { + else if (property == FACTORY_METHOD) { if (newValue != null) { - bd.setFactoryMethodName(newValue.toString()) + bd.factoryMethodName = newValue.toString() } } // initMethod - else if (INIT_METHOD.equals(property)) { + else if (property == INIT_METHOD) { if (newValue != null) { - bd.setInitMethodName(newValue.toString()) + bd.initMethodName = newValue.toString() } } // singleton property - else if (SINGLETON.equals(property)) { - bd.setScope(Boolean.TRUE.equals(newValue) ? BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE) + else if (property == SINGLETON) { + bd.scope = newValue == Boolean.TRUE ? BeanDefinition.SCOPE_SINGLETON : BeanDefinition.SCOPE_PROTOTYPE } else if (wrapper.isWritableProperty(property)) { wrapper.setPropertyValue(property, newValue) @@ -225,15 +225,15 @@ class DefaultBeanConfiguration extends GroovyObjectSupport implements BeanConfig for (Object constructorArg in constructorArgs) { cav.addGenericArgumentValue(constructorArg) } - bd.setConstructorArgumentValues(cav) + bd.constructorArgumentValues = cav } if (clazz != null) { - bd.setLazyInit(clazz.getAnnotation(Lazy) != null) - bd.setBeanClass(clazz) + bd.lazyInit = clazz.getAnnotation(Lazy) != null + bd.beanClass = clazz } - bd.setScope(singleton ? AbstractBeanDefinition.SCOPE_SINGLETON : AbstractBeanDefinition.SCOPE_PROTOTYPE) + bd.scope = singleton ? AbstractBeanDefinition.SCOPE_SINGLETON : AbstractBeanDefinition.SCOPE_PROTOTYPE if (parentName != null) { - bd.setParentName(parentName) + bd.parentName = parentName } wrapper = new BeanWrapperImpl(bd) return bd @@ -241,41 +241,41 @@ class DefaultBeanConfiguration extends GroovyObjectSupport implements BeanConfig BeanConfiguration addProperty(String propertyName, Object propertyValue) { if (propertyValue instanceof BeanConfiguration) { - propertyValue = ((BeanConfiguration) propertyValue).getBeanDefinition() + propertyValue = propertyValue.beanDefinition } getBeanDefinition() - .getPropertyValues() + .propertyValues .addPropertyValue(propertyName, propertyValue) return this } BeanConfiguration setDestroyMethod(String methodName) { - getBeanDefinition().setDestroyMethodName(methodName) + getBeanDefinition().destroyMethodName = methodName return this } BeanConfiguration setDependsOn(String[] dependsOn) { - getBeanDefinition().setDependsOn(dependsOn) + getBeanDefinition().dependsOn = dependsOn return this } BeanConfiguration setFactoryBean(String beanName) { - getBeanDefinition().setFactoryBeanName(beanName) + getBeanDefinition().factoryBeanName = beanName return this } BeanConfiguration setFactoryMethod(String methodName) { - getBeanDefinition().setFactoryMethodName(methodName) + getBeanDefinition().factoryMethodName = methodName return this } BeanConfiguration setAutowire(String type) { - if ('byName'.equals(type)) { - getBeanDefinition().setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_NAME) + if (type == 'byName') { + getBeanDefinition().autowireMode = AbstractBeanDefinition.AUTOWIRE_BY_NAME } - else if ('byType'.equals(type)) { - getBeanDefinition().setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE) + else if (type == 'byType') { + getBeanDefinition().autowireMode = AbstractBeanDefinition.AUTOWIRE_BY_TYPE } return this } @@ -285,26 +285,20 @@ class DefaultBeanConfiguration extends GroovyObjectSupport implements BeanConfig } Object getPropertyValue(String propName) { - PropertyValue propertyValue = getBeanDefinition() - .getPropertyValues() - .getPropertyValue(propName) - if (propertyValue == null) { - return null - } - - return propertyValue.getValue() + PropertyValue propertyValue = getBeanDefinition().propertyValues.getPropertyValue(propName) + return propertyValue?.value } boolean hasProperty(String propName) { - return getBeanDefinition().getPropertyValues().contains(propName) + return getBeanDefinition().propertyValues.contains(propName) } void setPropertyValue(String property, Object newValue) { - getBeanDefinition().getPropertyValues().addPropertyValue(property, newValue) + getBeanDefinition().propertyValues.addPropertyValue(property, newValue) } BeanConfiguration setAbstract(boolean isAbstract) { - getBeanDefinition().setAbstract(isAbstract) + getBeanDefinition().abstract = isAbstract return this } @@ -312,15 +306,15 @@ class DefaultBeanConfiguration extends GroovyObjectSupport implements BeanConfig Assert.notNull(obj, 'Parent bean cannot be set to a null runtime bean reference!') if (obj instanceof String) { - parentName = (String) obj + parentName = obj } else if (obj instanceof RuntimeBeanReference) { - parentName = ((RuntimeBeanReference) obj).getBeanName() + parentName = obj.beanName } else if (obj instanceof BeanConfiguration) { - parentName = ((BeanConfiguration) obj).getName() + parentName = obj.name } - getBeanDefinition().setParentName(parentName) + getBeanDefinition().parentName = parentName setAbstract(false) } diff --git a/grails-spring/src/main/groovy/org/grails/spring/DefaultRuntimeSpringConfiguration.groovy b/grails-spring/src/main/groovy/org/grails/spring/DefaultRuntimeSpringConfiguration.groovy index 38a960996f..8107ef07e9 100644 --- a/grails-spring/src/main/groovy/org/grails/spring/DefaultRuntimeSpringConfiguration.groovy +++ b/grails-spring/src/main/groovy/org/grails/spring/DefaultRuntimeSpringConfiguration.groovy @@ -48,12 +48,12 @@ class DefaultRuntimeSpringConfiguration implements RuntimeSpringConfiguration { private static final Log LOG = LogFactory.getLog(DefaultRuntimeSpringConfiguration) protected GenericApplicationContext context - private Map<String, BeanConfiguration> beanConfigs = new HashMap<>() - private Map<String, BeanDefinition> beanDefinitions = new HashMap<>() - private Set<String> beanNames = new LinkedHashSet<>() + private Map<String, BeanConfiguration> beanConfigs = [:] + private Map<String, BeanDefinition> beanDefinitions = [:] + private Set<String> beanNames = [] as Set protected ApplicationContext parent protected ClassLoader classLoader - protected Map<String, List<String>> aliases = new HashMap<>() + protected Map<String, List<String>> aliases = [:] protected ListableBeanFactory beanFactory /** @@ -102,14 +102,14 @@ class DefaultRuntimeSpringConfiguration implements RuntimeSpringConfiguration { if (parentCtx.containsBean('classLoader')) { Object cl = parentCtx.getBean('classLoader') if (cl instanceof ClassLoader) { - setClassLoaderOnContext((ClassLoader) cl) + setClassLoaderOnContext(cl) } } } private void setClassLoaderOnContext(ClassLoader cl) { - context.setClassLoader(cl) - context.getBeanFactory().setBeanClassLoader(cl) + context.classLoader = cl + context.beanFactory.beanClassLoader = cl } /** @@ -185,7 +185,7 @@ class DefaultRuntimeSpringConfiguration implements RuntimeSpringConfiguration { private void registerBeanConfiguration(String name, BeanConfiguration bc) { beanConfigs.put(name, bc) - beanNames.add(name) + beanNames << name } @SuppressWarnings('rawtypes') @@ -202,31 +202,31 @@ class DefaultRuntimeSpringConfiguration implements RuntimeSpringConfiguration { } void addBeanConfiguration(String beanName, BeanConfiguration beanConfiguration) { - beanConfiguration.setName(beanName) + beanConfiguration.name = beanName registerBeanConfiguration(beanName, beanConfiguration) } void addBeanDefinition(String name, BeanDefinition bd) { beanDefinitions.put(name, bd) beanConfigs.remove(name) - beanNames.add(name) + beanNames << name } boolean containsBean(String name) { - return beanNames.contains(name) + return name in beanNames } BeanConfiguration getBeanConfig(String name) { - return beanConfigs.get(name) + return beanConfigs[name] } AbstractBeanDefinition createBeanDefinition(String name) { if (containsBean(name)) { - if (beanDefinitions.containsKey(name)) { - return (AbstractBeanDefinition) beanDefinitions.get(name) + if (name in beanDefinitions) { + return (AbstractBeanDefinition) beanDefinitions[name] } - if (beanConfigs.containsKey(name)) { - return beanConfigs.get(name).getBeanDefinition() + if (name in beanConfigs) { + return beanConfigs[name].beanDefinition } } return null @@ -254,7 +254,7 @@ class DefaultRuntimeSpringConfiguration implements RuntimeSpringConfiguration { private void registerUnrefreshedBeansWithRegistry(BeanDefinitionRegistry registry) { if (context != null) { - for (String beanName in context.getBeanDefinitionNames()) { + for (String beanName in context.beanDefinitionNames) { register(registry, beanName, context.getBeanDefinition(beanName)) } } @@ -287,36 +287,34 @@ class DefaultRuntimeSpringConfiguration implements RuntimeSpringConfiguration { private void registerBeanConfigsWithRegistry(BeanDefinitionRegistry registry) { for (BeanConfiguration bc in beanConfigs.values()) { - String beanName = bc.getName() + String beanName = bc.name if (LOG.isDebugEnabled()) { LOG.debug('[RuntimeConfiguration] Registering bean [' + beanName + ']') if (LOG.isTraceEnabled()) { - PropertyValue[] pvs = bc.getBeanDefinition() - .getPropertyValues() + PropertyValue[] pvs = bc.beanDefinition + .propertyValues .getPropertyValues() for (PropertyValue pv in pvs) { - LOG.trace('[RuntimeConfiguration] With property [' + pv.getName() + '] set to [' + pv.getValue() + ']') + LOG.trace('[RuntimeConfiguration] With property [' + pv.name + '] set to [' + pv.value + ']') } } } - register(registry, beanName, bc.getBeanDefinition()) + register(registry, beanName, bc.beanDefinition) } } private void registerBeanDefinitionsWithRegistry(BeanDefinitionRegistry registry) { - for (Object key in beanDefinitions.keySet()) { - BeanDefinition bd = beanDefinitions.get(key) + beanDefinitions.each { String beanName, BeanDefinition bd -> if (LOG.isDebugEnabled()) { - LOG.debug('[RuntimeConfiguration] Registering bean [' + key + ']') + LOG.debug('[RuntimeConfiguration] Registering bean [' + beanName + ']') if (LOG.isTraceEnabled()) { - PropertyValue[] pvs = bd.getPropertyValues().getPropertyValues() + PropertyValue[] pvs = bd.propertyValues.getPropertyValues() for (PropertyValue pv in pvs) { - LOG.trace('[RuntimeConfiguration] With property [' + pv.getName() + '] set to [' + pv.getValue() + ']') + LOG.trace('[RuntimeConfiguration] With property [' + pv.name + '] set to [' + pv.value + ']') } } } - final String beanName = key.toString() register(registry, beanName, bd) } } @@ -326,22 +324,19 @@ class DefaultRuntimeSpringConfiguration implements RuntimeSpringConfiguration { return } - ApplicationContext ctx = targetSpringConfig.getUnrefreshedApplicationContext() + ApplicationContext ctx = targetSpringConfig.unrefreshedApplicationContext if (ctx instanceof BeanDefinitionRegistry) { - final BeanDefinitionRegistry registry = (BeanDefinitionRegistry) ctx - registerUnrefreshedBeansWithRegistry(registry) - registerBeansWithRegistry(registry) + registerUnrefreshedBeansWithRegistry(ctx) + registerBeansWithRegistry(ctx) } - for (Map.Entry<String, BeanConfiguration> beanEntry : beanConfigs.entrySet()) { - targetSpringConfig.addBeanConfiguration(beanEntry.getKey(), beanEntry.getValue()) + beanConfigs.each { String beanName, BeanConfiguration beanConfiguration -> + targetSpringConfig.addBeanConfiguration(beanName, beanConfiguration) } } private void registerBeanAliasesWithRegistry(BeanDefinitionRegistry beanDefinitionRegistry) { - for (Map.Entry<String, List<String>> entry : aliases.entrySet()) { - String beanName = entry.getKey() - List<String> beanAliases = entry.getValue() - if (beanAliases != null && !beanAliases.isEmpty()) { + aliases.each { String beanName, List<String> beanAliases -> + if (beanAliases) { for (String alias in beanAliases) { beanDefinitionRegistry.registerAlias(beanName, alias) } @@ -364,16 +359,16 @@ class DefaultRuntimeSpringConfiguration implements RuntimeSpringConfiguration { } void addAlias(String alias, String beanName) { - List<String> beanAliases = aliases.get(beanName) + List<String> beanAliases = aliases[beanName] if (beanAliases == null) { - beanAliases = new ArrayList<>() + beanAliases = [] aliases.put(beanName, beanAliases) } - beanAliases.add(alias) + beanAliases << alias } BeanDefinition getBeanDefinition(String beanName) { - return beanDefinitions.get(beanName) + return beanDefinitions[beanName] } void setBeanFactory(ListableBeanFactory beanFactory) { diff --git a/grails-spring/src/main/groovy/org/grails/spring/GrailsContextEvent.groovy b/grails-spring/src/main/groovy/org/grails/spring/GrailsContextEvent.groovy index 8c754ad022..ad36788d64 100644 --- a/grails-spring/src/main/groovy/org/grails/spring/GrailsContextEvent.groovy +++ b/grails-spring/src/main/groovy/org/grails/spring/GrailsContextEvent.groovy @@ -35,15 +35,11 @@ class GrailsContextEvent extends ApplicationEvent { public static final int DYNAMIC_METHODS_REGISTERED = 0 - private int eventType + final int eventType GrailsContextEvent(WebApplicationContext ctx, int eventType) { super(ctx) this.eventType = eventType } - int getEventType() { - return eventType - } - } diff --git a/grails-spring/src/main/groovy/org/grails/spring/TypeSpecifyableTransactionProxyFactoryBean.groovy b/grails-spring/src/main/groovy/org/grails/spring/TypeSpecifyableTransactionProxyFactoryBean.groovy index e4efee4f1a..ec4e7be04a 100644 --- a/grails-spring/src/main/groovy/org/grails/spring/TypeSpecifyableTransactionProxyFactoryBean.groovy +++ b/grails-spring/src/main/groovy/org/grails/spring/TypeSpecifyableTransactionProxyFactoryBean.groovy @@ -43,11 +43,7 @@ class TypeSpecifyableTransactionProxyFactoryBean extends TransactionProxyFactory @Override Class<?> getObjectType() { - if (type != null) { - return type - } - - return super.getObjectType() + return type ?: super.getObjectType() } }
