Skip to content

Commit

Permalink
Nullability refinements and related polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Mar 19, 2024
1 parent cd7ba18 commit c531a8a
Show file tree
Hide file tree
Showing 58 changed files with 327 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,18 @@ public static Object invokeJoinpointUsingReflection(@Nullable Object target, Met
}
}


/**
* Inner class to avoid a hard dependency on Kotlin at runtime.
*/
private static class KotlinDelegate {

public static Publisher<?> invokeSuspendingFunction(Method method, Object target, Object... args) {
public static Publisher<?> invokeSuspendingFunction(Method method, @Nullable Object target, Object... args) {
Continuation<?> continuation = (Continuation<?>) args[args.length -1];
Assert.state(continuation != null, "No Continuation available");
CoroutineContext context = continuation.getContext().minusKey(Job.Key);
return CoroutinesUtils.invokeSuspendingFunction(context, method, target, args);
}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -845,8 +845,10 @@ protected AbstractNestablePropertyAccessor getPropertyAccessorForPropertyPath(St
* @return the PropertyAccessor instance, either cached or newly created
*/
private AbstractNestablePropertyAccessor getNestedPropertyAccessor(String nestedProperty) {
if (this.nestedPropertyAccessors == null) {
this.nestedPropertyAccessors = new HashMap<>();
Map<String, AbstractNestablePropertyAccessor> nestedAccessors = this.nestedPropertyAccessors;
if (nestedAccessors == null) {
nestedAccessors = new HashMap<>();
this.nestedPropertyAccessors = nestedAccessors;
}
// Get value of bean property.
PropertyTokenHolder tokens = getPropertyNameTokens(nestedProperty);
Expand All @@ -862,7 +864,7 @@ private AbstractNestablePropertyAccessor getNestedPropertyAccessor(String nested
}

// Lookup cached sub-PropertyAccessor, create new one if not found.
AbstractNestablePropertyAccessor nestedPa = this.nestedPropertyAccessors.get(canonicalName);
AbstractNestablePropertyAccessor nestedPa = nestedAccessors.get(canonicalName);
if (nestedPa == null || nestedPa.getWrappedInstance() != ObjectUtils.unwrapOptional(value)) {
if (logger.isTraceEnabled()) {
logger.trace("Creating new nested " + getClass().getSimpleName() + " for property '" + canonicalName + "'");
Expand All @@ -871,7 +873,7 @@ private AbstractNestablePropertyAccessor getNestedPropertyAccessor(String nested
// Inherit all type-specific PropertyEditors.
copyDefaultEditorsTo(nestedPa);
copyCustomEditorsTo(nestedPa, canonicalName);
this.nestedPropertyAccessors.put(canonicalName, nestedPa);
nestedAccessors.put(canonicalName, nestedPa);
}
else {
if (logger.isTraceEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,6 +54,7 @@
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -250,6 +251,7 @@ public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefin
@SuppressWarnings("serial")
Closure<Object> beans = new Closure<>(this) {
@Override
@Nullable
public Object call(Object... args) {
invokeBeanDefiningClosure((Closure<?>) args[0]);
return null;
Expand Down Expand Up @@ -425,6 +427,7 @@ else if (args.length > 1 && args[args.length -1] instanceof Closure) {

private boolean addDeferredProperty(String property, Object newValue) {
if (newValue instanceof List || newValue instanceof Map) {
Assert.state(this.currentBeanDefinition != null, "No current bean definition set");
this.deferredProperties.put(this.currentBeanDefinition.getBeanName() + '.' + property,
new DeferredProperty(this.currentBeanDefinition, property, newValue));
return true;
Expand Down Expand Up @@ -640,6 +643,7 @@ else if (value instanceof Closure<?> callable) {
this.currentBeanDefinition = current;
}
}
Assert.state(this.currentBeanDefinition != null, "No current bean definition set");
this.currentBeanDefinition.addProperty(name, value);
}

Expand All @@ -654,6 +658,7 @@ else if (value instanceof Closure<?> callable) {
* </ul>
*/
@Override
@Nullable
public Object getProperty(String name) {
Binding binding = getBinding();
if (binding != null && binding.hasVariable(name)) {
Expand Down Expand Up @@ -727,9 +732,10 @@ private static class DeferredProperty {

private final String name;

@Nullable
public Object value;

public DeferredProperty(GroovyBeanDefinitionWrapper beanDefinition, String name, Object value) {
public DeferredProperty(GroovyBeanDefinitionWrapper beanDefinition, String name, @Nullable Object value) {
this.beanDefinition = beanDefinition;
this.name = name;
this.value = value;
Expand Down Expand Up @@ -762,20 +768,18 @@ public MetaClass getMetaClass() {
}

@Override
@Nullable
public Object getProperty(String property) {
if (property.equals("beanName")) {
return getBeanName();
}
else if (property.equals("source")) {
return getSource();
}
else if (this.beanDefinition != null) {
else {
return new GroovyPropertyValue(
property, this.beanDefinition.getBeanDefinition().getPropertyValues().get(property));
}
else {
return this.metaClass.getProperty(this, property);
}
}

@Override
Expand Down Expand Up @@ -804,9 +808,10 @@ private class GroovyPropertyValue extends GroovyObjectSupport {

private final String propertyName;

@Nullable
private final Object propertyValue;

public GroovyPropertyValue(String propertyName, Object propertyValue) {
public GroovyPropertyValue(String propertyName, @Nullable Object propertyValue) {
this.propertyName = propertyName;
this.propertyValue = propertyValue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -84,7 +84,7 @@ class GroovyBeanDefinitionWrapper extends GroovyObjectSupport {
this(beanName, clazz, null);
}

GroovyBeanDefinitionWrapper(@Nullable String beanName, Class<?> clazz, @Nullable Collection<?> constructorArgs) {
GroovyBeanDefinitionWrapper(@Nullable String beanName, @Nullable Class<?> clazz, @Nullable Collection<?> constructorArgs) {
this.beanName = beanName;
this.clazz = clazz;
this.constructorArgs = constructorArgs;
Expand Down Expand Up @@ -130,11 +130,12 @@ void setBeanDefinitionHolder(BeanDefinitionHolder holder) {
}

BeanDefinitionHolder getBeanDefinitionHolder() {
return new BeanDefinitionHolder(getBeanDefinition(), getBeanName());
Assert.state(this.beanName != null, "Bean name must be set");
return new BeanDefinitionHolder(getBeanDefinition(), this.beanName);
}

void setParent(Object obj) {
Assert.notNull(obj, "Parent bean cannot be set to a null runtime bean reference.");
void setParent(@Nullable Object obj) {
Assert.notNull(obj, "Parent bean cannot be set to a null runtime bean reference");
if (obj instanceof String name) {
this.parentName = name;
}
Expand All @@ -148,7 +149,7 @@ else if (obj instanceof GroovyBeanDefinitionWrapper wrapper) {
getBeanDefinition().setAbstract(false);
}

GroovyBeanDefinitionWrapper addProperty(String propertyName, Object propertyValue) {
GroovyBeanDefinitionWrapper addProperty(String propertyName, @Nullable Object propertyValue) {
if (propertyValue instanceof GroovyBeanDefinitionWrapper wrapper) {
propertyValue = wrapper.getBeanDefinition();
}
Expand All @@ -158,6 +159,7 @@ GroovyBeanDefinitionWrapper addProperty(String propertyName, Object propertyValu


@Override
@Nullable
public Object getProperty(String property) {
Assert.state(this.definitionWrapper != null, "BeanDefinition wrapper not initialized");
if (this.definitionWrapper.isReadableProperty(property)) {
Expand All @@ -170,7 +172,7 @@ else if (dynamicProperties.contains(property)) {
}

@Override
public void setProperty(String property, Object newValue) {
public void setProperty(String property, @Nullable Object newValue) {
if (PARENT.equals(property)) {
setParent(newValue);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -638,7 +638,7 @@ boolean hasAnyExternallyManagedDestroyMethod(String destroyMethod) {
}
}

private static boolean hasAnyExternallyManagedMethod(Set<String> candidates, String methodName) {
private static boolean hasAnyExternallyManagedMethod(@Nullable Set<String> candidates, String methodName) {
if (candidates != null) {
for (String candidate : candidates) {
int indexOfDot = candidate.lastIndexOf('.');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -126,9 +126,10 @@ protected void doRegisterBeanDefinitions(Element root) {
// then ultimately reset this.delegate back to its original (parent) reference.
// this behavior emulates a stack of delegates without actually necessitating one.
BeanDefinitionParserDelegate parent = this.delegate;
this.delegate = createDelegate(getReaderContext(), root, parent);
BeanDefinitionParserDelegate current = createDelegate(getReaderContext(), root, parent);
this.delegate = current;

if (this.delegate.isDefaultNamespace(root)) {
if (current.isDefaultNamespace(root)) {
String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
if (StringUtils.hasText(profileSpec)) {
String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
Expand All @@ -146,7 +147,7 @@ protected void doRegisterBeanDefinitions(Element root) {
}

preProcessXml(root);
parseBeanDefinitions(root, this.delegate);
parseBeanDefinitions(root, current);
postProcessXml(root);

this.delegate = parent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1096,7 +1096,13 @@ public Object executeSynchronized(CacheOperationInvoker invoker, Method method,
}
}
if (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isSuspendingFunction(method)) {
return Mono.fromFuture(cache.retrieve(key, () -> ((Mono<?>) invokeOperation(invoker)).toFuture()));
return Mono.fromFuture(cache.retrieve(key, () -> {
Mono<?> mono = ((Mono<?>) invokeOperation(invoker));
if (mono == null) {
mono = Mono.empty();
}
return mono.toFuture();
}));
}
return NOT_HANDLED;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -78,12 +78,13 @@ public Object invoke(final MethodInvocation invocation) throws Throwable {
}
}


/**
* Inner class to avoid a hard dependency on Kotlin at runtime.
*/
private static class KotlinDelegate {

public static Publisher<?> invokeSuspendingFunction(Method method, Object target, Object... args) {
public static Publisher<?> invokeSuspendingFunction(Method method, @Nullable Object target, Object... args) {
Continuation<?> continuation = (Continuation<?>) args[args.length - 1];
CoroutineContext coroutineContext = continuation.getContext().minusKey(Job.Key);
return CoroutinesUtils.invokeSuspendingFunction(coroutineContext, method, target, args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -257,6 +257,7 @@ public Builder classOutput(Path classOutput) {
* @return this builder for method chaining
*/
public Builder groupId(String groupId) {
Assert.hasText(groupId, "'groupId' must not be empty");
this.groupId = groupId;
return this;
}
Expand All @@ -268,6 +269,7 @@ public Builder groupId(String groupId) {
* @return this builder for method chaining
*/
public Builder artifactId(String artifactId) {
Assert.hasText(artifactId, "'artifactId' must not be empty");
this.artifactId = artifactId;
return this;
}
Expand All @@ -279,14 +281,12 @@ public Settings build() {
Assert.notNull(this.sourceOutput, "'sourceOutput' must not be null");
Assert.notNull(this.resourceOutput, "'resourceOutput' must not be null");
Assert.notNull(this.classOutput, "'classOutput' must not be null");
Assert.hasText(this.groupId, "'groupId' must not be null or empty");
Assert.hasText(this.artifactId, "'artifactId' must not be null or empty");
Assert.notNull(this.groupId, "'groupId' must not be null");
Assert.notNull(this.artifactId, "'artifactId' must not be null");
return new Settings(this.sourceOutput, this.resourceOutput, this.classOutput,
this.groupId, this.artifactId);
}

}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -318,8 +318,8 @@ else if (result instanceof org.springframework.util.concurrent.ListenableFuture<
}
}

private void publishEvents(Object result) {
if (result.getClass().isArray()) {
private void publishEvents(@Nullable Object result) {
if (result != null && result.getClass().isArray()) {
Object[] events = ObjectUtils.toObjectArray(result);
for (Object event : events) {
publishEvent(event);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -83,18 +83,21 @@ public void setBeanClassLoader(ClassLoader classLoader) {
public void afterPropertiesSet() throws MBeanServerNotFoundException, MBeanInfoRetrievalException {
super.afterPropertiesSet();

Class<?> interfaceToUse;
if (this.proxyInterface == null) {
this.proxyInterface = getManagementInterface();
if (this.proxyInterface == null) {
interfaceToUse = getManagementInterface();
if (interfaceToUse == null) {
throw new IllegalArgumentException("Property 'proxyInterface' or 'managementInterface' is required");
}
this.proxyInterface = interfaceToUse;
}
else {
interfaceToUse = this.proxyInterface;
if (getManagementInterface() == null) {
setManagementInterface(this.proxyInterface);
setManagementInterface(interfaceToUse);
}
}
this.mbeanProxy = new ProxyFactory(this.proxyInterface, this).getProxy(this.beanClassLoader);
this.mbeanProxy = new ProxyFactory(interfaceToUse, this).getProxy(this.beanClassLoader);
}


Expand Down
Loading

0 comments on commit c531a8a

Please sign in to comment.