Polishing

This commit is contained in:
Juergen Hoeller 2019-12-01 01:21:53 +01:00
parent 567c7695dd
commit 91b557eb4b
2 changed files with 28 additions and 38 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -41,8 +41,8 @@ import org.springframework.util.ObjectUtils;
/** /**
* Decorator for a standard {@link BeanInfo} object, e.g. as created by * Decorator for a standard {@link BeanInfo} object, e.g. as created by
* {@link Introspector#getBeanInfo(Class)}, designed to discover and register static * {@link Introspector#getBeanInfo(Class)}, designed to discover and register
* and/or non-void returning setter methods. For example: * static and/or non-void returning setter methods. For example:
* *
* <pre class="code"> * <pre class="code">
* public class Bean { * public class Bean {
@ -487,7 +487,7 @@ class ExtendedBeanInfo implements BeanInfo {
} }
/* /*
* See java.beans.IndexedPropertyDescriptor#equals(java.lang.Object) * See java.beans.IndexedPropertyDescriptor#equals
*/ */
@Override @Override
public boolean equals(@Nullable Object other) { public boolean equals(@Nullable Object other) {

View File

@ -232,7 +232,6 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
* @return the number of bean definitions found * @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors * @throws BeanDefinitionStoreException in case of loading or parsing errors
*/ */
@SuppressWarnings("rawtypes")
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException { public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
// Check for XML files and redirect them to the "standard" XmlBeanDefinitionReader // Check for XML files and redirect them to the "standard" XmlBeanDefinitionReader
String filename = encodedResource.getResource().getFilename(); String filename = encodedResource.getResource().getFilename();
@ -245,10 +244,10 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")
Closure beans = new Closure(this) { Closure<Object> beans = new Closure<Object>(this) {
@Override @Override
public Object call(Object... args) { public Object call(Object... args) {
invokeBeanDefiningClosure((Closure) args[0]); invokeBeanDefiningClosure((Closure<?>) args[0]);
return null; return null;
} }
}; };
@ -290,8 +289,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
* @param closure the block or closure * @param closure the block or closure
* @return this {@code GroovyBeanDefinitionReader} instance * @return this {@code GroovyBeanDefinitionReader} instance
*/ */
@SuppressWarnings("rawtypes") public GroovyBeanDefinitionReader beans(Closure<?> closure) {
public GroovyBeanDefinitionReader beans(Closure closure) {
return invokeBeanDefiningClosure(closure); return invokeBeanDefiningClosure(closure);
} }
@ -312,29 +310,25 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
* @param args the constructors arguments and closure configurer * @param args the constructors arguments and closure configurer
* @return the bean definition * @return the bean definition
*/ */
@SuppressWarnings("rawtypes")
public AbstractBeanDefinition bean(Class<?> type, Object...args) { public AbstractBeanDefinition bean(Class<?> type, Object...args) {
GroovyBeanDefinitionWrapper current = this.currentBeanDefinition; GroovyBeanDefinitionWrapper current = this.currentBeanDefinition;
try { try {
Closure callable = null; Closure<?> callable = null;
Collection constructorArgs = null; Collection<Object> constructorArgs = null;
if (!ObjectUtils.isEmpty(args)) { if (!ObjectUtils.isEmpty(args)) {
int index = args.length; int index = args.length;
Object lastArg = args[index - 1]; Object lastArg = args[index - 1];
if (lastArg instanceof Closure) { if (lastArg instanceof Closure<?>) {
callable = (Closure) lastArg; callable = (Closure<?>) lastArg;
index--; index--;
} }
if (index > -1) { constructorArgs = resolveConstructorArguments(args, 0, index);
constructorArgs = resolveConstructorArguments(args, 0, index);
}
} }
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(null, type, constructorArgs); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(null, type, constructorArgs);
if (callable != null) { if (callable != null) {
callable.call(this.currentBeanDefinition); callable.call(this.currentBeanDefinition);
} }
return this.currentBeanDefinition.getBeanDefinition(); return this.currentBeanDefinition.getBeanDefinition();
} }
finally { finally {
this.currentBeanDefinition = current; this.currentBeanDefinition = current;
@ -381,11 +375,10 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
* takes a class argument. * takes a class argument.
*/ */
@Override @Override
@SuppressWarnings("rawtypes")
public Object invokeMethod(String name, Object arg) { public Object invokeMethod(String name, Object arg) {
Object[] args = (Object[])arg; Object[] args = (Object[])arg;
if ("beans".equals(name) && args.length == 1 && args[0] instanceof Closure) { if ("beans".equals(name) && args.length == 1 && args[0] instanceof Closure) {
return beans((Closure) args[0]); return beans((Closure<?>) args[0]);
} }
else if ("ref".equals(name)) { else if ("ref".equals(name)) {
String refName; String refName;
@ -435,14 +428,13 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
return false; return false;
} }
@SuppressWarnings("rawtypes")
private void finalizeDeferredProperties() { private void finalizeDeferredProperties() {
for (DeferredProperty dp : this.deferredProperties.values()) { for (DeferredProperty dp : this.deferredProperties.values()) {
if (dp.value instanceof List) { if (dp.value instanceof List) {
dp.value = manageListIfNecessary((List) dp.value); dp.value = manageListIfNecessary((List<?>) dp.value);
} }
else if (dp.value instanceof Map) { else if (dp.value instanceof Map) {
dp.value = manageMapIfNecessary((Map) dp.value); dp.value = manageMapIfNecessary((Map<?, ?>) dp.value);
} }
dp.apply(); dp.apply();
} }
@ -454,8 +446,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
* @param callable the closure argument * @param callable the closure argument
* @return this {@code GroovyBeanDefinitionReader} instance * @return this {@code GroovyBeanDefinitionReader} instance
*/ */
@SuppressWarnings("rawtypes") protected GroovyBeanDefinitionReader invokeBeanDefiningClosure(Closure<?> callable) {
protected GroovyBeanDefinitionReader invokeBeanDefiningClosure(Closure callable) {
callable.setDelegate(this); callable.setDelegate(this);
callable.call(); callable.call();
finalizeDeferredProperties(); finalizeDeferredProperties();
@ -469,7 +460,6 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
* argument is sometimes a closure. All the arguments in between are constructor arguments. * argument is sometimes a closure. All the arguments in between are constructor arguments.
* @return the bean definition wrapper * @return the bean definition wrapper
*/ */
@SuppressWarnings("rawtypes")
private GroovyBeanDefinitionWrapper invokeBeanDefiningMethod(String beanName, Object[] args) { private GroovyBeanDefinitionWrapper invokeBeanDefiningMethod(String beanName, Object[] args) {
boolean hasClosureArgument = (args[args.length - 1] instanceof Closure); boolean hasClosureArgument = (args[args.length - 1] instanceof Closure);
if (args[0] instanceof Class) { if (args[0] instanceof Class) {
@ -495,9 +485,10 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
else if (args[0] instanceof Map) { else if (args[0] instanceof Map) {
// named constructor arguments // named constructor arguments
if (args.length > 1 && args[1] instanceof Class) { if (args.length > 1 && args[1] instanceof Class) {
List<Object> constructorArgs = resolveConstructorArguments(args, 2, hasClosureArgument ? args.length - 1 : args.length); List<Object> constructorArgs =
resolveConstructorArguments(args, 2, hasClosureArgument ? args.length - 1 : args.length);
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, (Class<?>) args[1], constructorArgs); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, (Class<?>) args[1], constructorArgs);
Map namedArgs = (Map) args[0]; Map<?, ?> namedArgs = (Map<?, ?>) args[0];
for (Object o : namedArgs.keySet()) { for (Object o : namedArgs.keySet()) {
String propName = (String) o; String propName = (String) o;
setProperty(propName, namedArgs.get(propName)); setProperty(propName, namedArgs.get(propName));
@ -507,7 +498,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
else { else {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName);
// First arg is the map containing factoryBean : factoryMethod // First arg is the map containing factoryBean : factoryMethod
Map.Entry factoryBeanEntry = (Map.Entry) ((Map) args[0]).entrySet().iterator().next(); Map.Entry<?, ?> factoryBeanEntry = ((Map<?, ?>) args[0]).entrySet().iterator().next();
// If we have a closure body, that will be the last argument. // If we have a closure body, that will be the last argument.
// In between are the constructor args // In between are the constructor args
int constructorArgsTest = (hasClosureArgument ? 2 : 1); int constructorArgsTest = (hasClosureArgument ? 2 : 1);
@ -531,12 +522,13 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
this.currentBeanDefinition.getBeanDefinition().setAbstract(true); this.currentBeanDefinition.getBeanDefinition().setAbstract(true);
} }
else { else {
List constructorArgs = resolveConstructorArguments(args, 0, hasClosureArgument ? args.length - 1 : args.length); List<Object> constructorArgs =
resolveConstructorArguments(args, 0, hasClosureArgument ? args.length - 1 : args.length);
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, null, constructorArgs); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(beanName, null, constructorArgs);
} }
if (hasClosureArgument) { if (hasClosureArgument) {
Closure callable = (Closure) args[args.length - 1]; Closure<?> callable = (Closure<?>) args[args.length - 1];
callable.setDelegate(this); callable.setDelegate(this);
callable.setResolveStrategy(Closure.DELEGATE_FIRST); callable.setResolveStrategy(Closure.DELEGATE_FIRST);
callable.call(this.currentBeanDefinition); callable.call(this.currentBeanDefinition);
@ -549,7 +541,6 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
return beanDefinition; return beanDefinition;
} }
@SuppressWarnings("rawtypes")
protected List<Object> resolveConstructorArguments(Object[] args, int start, int end) { protected List<Object> resolveConstructorArguments(Object[] args, int start, int end) {
Object[] constructorArgs = Arrays.copyOfRange(args, start, end); Object[] constructorArgs = Arrays.copyOfRange(args, start, end);
for (int i = 0; i < constructorArgs.length; i++) { for (int i = 0; i < constructorArgs.length; i++) {
@ -557,10 +548,10 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
constructorArgs[i] = constructorArgs[i].toString(); constructorArgs[i] = constructorArgs[i].toString();
} }
else if (constructorArgs[i] instanceof List) { else if (constructorArgs[i] instanceof List) {
constructorArgs[i] = manageListIfNecessary((List) constructorArgs[i]); constructorArgs[i] = manageListIfNecessary((List<?>) constructorArgs[i]);
} }
else if (constructorArgs[i] instanceof Map){ else if (constructorArgs[i] instanceof Map){
constructorArgs[i] = manageMapIfNecessary((Map) constructorArgs[i]); constructorArgs[i] = manageMapIfNecessary((Map<?, ?>) constructorArgs[i]);
} }
} }
return Arrays.asList(constructorArgs); return Arrays.asList(constructorArgs);
@ -621,7 +612,6 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
} }
} }
@SuppressWarnings("rawtypes")
protected void applyPropertyToBeanDefinition(String name, Object value) { protected void applyPropertyToBeanDefinition(String name, Object value) {
if (value instanceof GString) { if (value instanceof GString) {
value = value.toString(); value = value.toString();
@ -632,7 +622,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
else if (value instanceof Closure) { else if (value instanceof Closure) {
GroovyBeanDefinitionWrapper current = this.currentBeanDefinition; GroovyBeanDefinitionWrapper current = this.currentBeanDefinition;
try { try {
Closure callable = (Closure) value; Closure<?> callable = (Closure<?>) value;
Class<?> parameterType = callable.getParameterTypes()[0]; Class<?> parameterType = callable.getParameterTypes()[0];
if (Object.class == parameterType) { if (Object.class == parameterType) {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper(""); this.currentBeanDefinition = new GroovyBeanDefinitionWrapper("");
@ -833,8 +823,8 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
return retVal; return retVal;
} }
@SuppressWarnings({ "rawtypes", "unused" }) @SuppressWarnings("unused")
public boolean addAll(Collection values) { public boolean addAll(Collection<?> values) {
boolean retVal = (Boolean) InvokerHelper.invokeMethod(this.propertyValue, "addAll", values); boolean retVal = (Boolean) InvokerHelper.invokeMethod(this.propertyValue, "addAll", values);
for (Object value : values) { for (Object value : values) {
updateDeferredProperties(value); updateDeferredProperties(value);