Skip to content

Commit 0ea4f8e

Browse files
committed
Fixed resolveProxyTargetClass exception message
1 parent 139adba commit 0ea4f8e

File tree

1 file changed

+55
-42
lines changed

1 file changed

+55
-42
lines changed

spring-context/src/main/java/org/springframework/scripting/support/ScriptFactoryPostProcessor.java

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,15 +23,11 @@
2323
import org.apache.commons.logging.Log;
2424
import org.apache.commons.logging.LogFactory;
2525

26-
import org.springframework.asm.Type;
27-
28-
import org.springframework.cglib.core.Signature;
29-
import org.springframework.cglib.proxy.InterfaceMaker;
30-
3126
import org.springframework.aop.TargetSource;
3227
import org.springframework.aop.framework.AopInfrastructureBean;
3328
import org.springframework.aop.framework.ProxyFactory;
3429
import org.springframework.aop.support.DelegatingIntroductionInterceptor;
30+
import org.springframework.asm.Type;
3531
import org.springframework.beans.BeanUtils;
3632
import org.springframework.beans.PropertyValue;
3733
import org.springframework.beans.factory.BeanClassLoaderAware;
@@ -50,6 +46,8 @@
5046
import org.springframework.beans.factory.support.BeanDefinitionValidationException;
5147
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
5248
import org.springframework.beans.factory.support.GenericBeanDefinition;
49+
import org.springframework.cglib.core.Signature;
50+
import org.springframework.cglib.proxy.InterfaceMaker;
5351
import org.springframework.context.ResourceLoaderAware;
5452
import org.springframework.core.Conventions;
5553
import org.springframework.core.Ordered;
@@ -200,10 +198,12 @@ public void setDefaultProxyTargetClass(boolean defaultProxyTargetClass) {
200198
this.defaultProxyTargetClass = defaultProxyTargetClass;
201199
}
202200

201+
@Override
203202
public void setBeanClassLoader(ClassLoader classLoader) {
204203
this.beanClassLoader = classLoader;
205204
}
206205

206+
@Override
207207
public void setBeanFactory(BeanFactory beanFactory) {
208208
if (!(beanFactory instanceof ConfigurableBeanFactory)) {
209209
throw new IllegalStateException("ScriptFactoryPostProcessor doesn't work with a BeanFactory "
@@ -226,16 +226,18 @@ public void setBeanFactory(BeanFactory beanFactory) {
226226
}
227227
}
228228

229+
@Override
229230
public void setResourceLoader(ResourceLoader resourceLoader) {
230231
this.resourceLoader = resourceLoader;
231232
}
232233

234+
@Override
233235
public int getOrder() {
234236
return Integer.MIN_VALUE;
235237
}
236238

237239
@Override
238-
public Class predictBeanType(Class beanClass, String beanName) {
240+
public Class<?> predictBeanType(Class<?> beanClass, String beanName) {
239241
// We only apply special treatment to ScriptFactory implementations here.
240242
if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
241243
return null;
@@ -250,29 +252,33 @@ public Class predictBeanType(Class beanClass, String beanName) {
250252

251253
ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
252254
ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
253-
Class[] interfaces = scriptFactory.getScriptInterfaces();
255+
Class<?>[] interfaces = scriptFactory.getScriptInterfaces();
254256

255-
Class scriptedType = scriptFactory.getScriptedObjectType(scriptSource);
257+
Class<?> scriptedType = scriptFactory.getScriptedObjectType(scriptSource);
256258
if (scriptedType != null) {
257259
return scriptedType;
258-
} else if (!ObjectUtils.isEmpty(interfaces)) {
260+
}
261+
else if (!ObjectUtils.isEmpty(interfaces)) {
259262
return (interfaces.length == 1 ? interfaces[0] : createCompositeInterface(interfaces));
260-
} else {
263+
}
264+
else {
261265
if (bd.isSingleton()) {
262266
Object bean = this.scriptBeanFactory.getBean(scriptedObjectBeanName);
263267
if (bean != null) {
264268
return bean.getClass();
265269
}
266270
}
267271
}
268-
} catch (Exception ex) {
272+
}
273+
catch (Exception ex) {
269274
if (ex instanceof BeanCreationException
270275
&& ((BeanCreationException) ex).getMostSpecificCause() instanceof BeanCurrentlyInCreationException) {
271276
if (logger.isTraceEnabled()) {
272277
logger.trace("Could not determine scripted object type for bean '" + beanName + "': "
273278
+ ex.getMessage());
274279
}
275-
} else {
280+
}
281+
else {
276282
if (logger.isDebugEnabled()) {
277283
logger.debug("Could not determine scripted object type for bean '" + beanName + "'", ex);
278284
}
@@ -283,7 +289,7 @@ public Class predictBeanType(Class beanClass, String beanName) {
283289
}
284290

285291
@Override
286-
public Object postProcessBeforeInstantiation(Class beanClass, String beanName) {
292+
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
287293
// We only apply special treatment to ScriptFactory implementations here.
288294
if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
289295
return null;
@@ -298,27 +304,28 @@ public Object postProcessBeforeInstantiation(Class beanClass, String beanName) {
298304
ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
299305
boolean isFactoryBean = false;
300306
try {
301-
Class scriptedObjectType = scriptFactory.getScriptedObjectType(scriptSource);
307+
Class<?> scriptedObjectType = scriptFactory.getScriptedObjectType(scriptSource);
302308
// Returned type may be null if the factory is unable to determine the type.
303309
if (scriptedObjectType != null) {
304310
isFactoryBean = FactoryBean.class.isAssignableFrom(scriptedObjectType);
305311
}
306-
} catch (Exception ex) {
307-
throw new BeanCreationException(beanName, "Could not determine scripted object type for " + scriptFactory,
308-
ex);
312+
}
313+
catch (Exception ex) {
314+
throw new BeanCreationException(beanName,
315+
"Could not determine scripted object type for " + scriptFactory, ex);
309316
}
310317

311318
long refreshCheckDelay = resolveRefreshCheckDelay(bd);
312319
if (refreshCheckDelay >= 0) {
313-
Class[] interfaces = scriptFactory.getScriptInterfaces();
320+
Class<?>[] interfaces = scriptFactory.getScriptInterfaces();
314321
RefreshableScriptTargetSource ts = new RefreshableScriptTargetSource(this.scriptBeanFactory,
315322
scriptedObjectBeanName, scriptFactory, scriptSource, isFactoryBean);
316323
boolean proxyTargetClass = resolveProxyTargetClass(bd);
317324
String language = (String) bd.getAttribute(LANGUAGE_ATTRIBUTE);
318-
if (proxyTargetClass && (language==null || !language.equals("groovy"))) {
325+
if (proxyTargetClass && (language == null || !language.equals("groovy"))) {
319326
throw new BeanDefinitionValidationException(
320-
"Cannot use proxyTargetClass=true with script beans where language is not groovy (found "
321-
+ language + ")");
327+
"Cannot use proxyTargetClass=true with script beans where language is not 'groovy': '" +
328+
language + "'");
322329
}
323330
ts.setRefreshCheckDelay(refreshCheckDelay);
324331
return createRefreshableProxy(ts, interfaces, proxyTargetClass);
@@ -385,12 +392,14 @@ protected long resolveRefreshCheckDelay(BeanDefinition beanDefinition) {
385392
Object attributeValue = beanDefinition.getAttribute(REFRESH_CHECK_DELAY_ATTRIBUTE);
386393
if (attributeValue instanceof Number) {
387394
refreshCheckDelay = ((Number) attributeValue).longValue();
388-
} else if (attributeValue instanceof String) {
395+
}
396+
else if (attributeValue instanceof String) {
389397
refreshCheckDelay = Long.parseLong((String) attributeValue);
390-
} else if (attributeValue != null) {
391-
throw new BeanDefinitionStoreException("Invalid refresh check delay attribute ["
392-
+ REFRESH_CHECK_DELAY_ATTRIBUTE + "] with value [" + attributeValue
393-
+ "]: needs to be of type Number or String");
398+
}
399+
else if (attributeValue != null) {
400+
throw new BeanDefinitionStoreException("Invalid refresh check delay attribute [" +
401+
REFRESH_CHECK_DELAY_ATTRIBUTE + "] with value '" + attributeValue +
402+
"': needs to be of type Number or String");
394403
}
395404
return refreshCheckDelay;
396405
}
@@ -399,13 +408,15 @@ protected boolean resolveProxyTargetClass(BeanDefinition beanDefinition) {
399408
boolean proxyTargetClass = this.defaultProxyTargetClass;
400409
Object attributeValue = beanDefinition.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE);
401410
if (attributeValue instanceof Boolean) {
402-
proxyTargetClass = ((Boolean) attributeValue).booleanValue();
403-
} else if (attributeValue instanceof String) {
404-
proxyTargetClass = new Boolean((String) attributeValue);
405-
} else if (attributeValue != null) {
406-
throw new BeanDefinitionStoreException("Invalid refresh check delay attribute ["
407-
+ REFRESH_CHECK_DELAY_ATTRIBUTE + "] with value [" + attributeValue
408-
+ "]: needs to be of type Number or String");
411+
proxyTargetClass = (Boolean) attributeValue;
412+
}
413+
else if (attributeValue instanceof String) {
414+
proxyTargetClass = Boolean.valueOf((String) attributeValue);
415+
}
416+
else if (attributeValue != null) {
417+
throw new BeanDefinitionStoreException("Invalid proxy target class attribute [" +
418+
PROXY_TARGET_CLASS_ATTRIBUTE + "] with value '" + attributeValue +
419+
"': needs to be of type Boolean or String");
409420
}
410421
return proxyTargetClass;
411422
}
@@ -459,7 +470,8 @@ protected ScriptSource convertToScriptSource(String beanName, String scriptSourc
459470

460471
if (scriptSourceLocator.startsWith(INLINE_SCRIPT_PREFIX)) {
461472
return new StaticScriptSource(scriptSourceLocator.substring(INLINE_SCRIPT_PREFIX.length()), beanName);
462-
} else {
473+
}
474+
else {
463475
return new ResourceScriptSource(resourceLoader.getResource(scriptSourceLocator));
464476
}
465477
}
@@ -478,14 +490,14 @@ protected ScriptSource convertToScriptSource(String beanName, String scriptSourc
478490
* @see org.springframework.cglib.proxy.InterfaceMaker
479491
* @see org.springframework.beans.BeanUtils#findPropertyType
480492
*/
481-
protected Class createConfigInterface(BeanDefinition bd, Class[] interfaces) {
493+
protected Class<?> createConfigInterface(BeanDefinition bd, Class<?>[] interfaces) {
482494
InterfaceMaker maker = new InterfaceMaker();
483495
PropertyValue[] pvs = bd.getPropertyValues().getPropertyValues();
484496
for (PropertyValue pv : pvs) {
485497
String propertyName = pv.getName();
486-
Class propertyType = BeanUtils.findPropertyType(propertyName, interfaces);
498+
Class<?> propertyType = BeanUtils.findPropertyType(propertyName, interfaces);
487499
String setterName = "set" + StringUtils.capitalize(propertyName);
488-
Signature signature = new Signature(setterName, Type.VOID_TYPE, new Type[] { Type.getType(propertyType) });
500+
Signature signature = new Signature(setterName, Type.VOID_TYPE, new Type[] {Type.getType(propertyType)});
489501
maker.add(signature, new Type[0]);
490502
}
491503
if (bd instanceof AbstractBeanDefinition) {
@@ -511,7 +523,7 @@ protected Class createConfigInterface(BeanDefinition bd, Class[] interfaces) {
511523
* @return the merged interface as Class
512524
* @see java.lang.reflect.Proxy#getProxyClass
513525
*/
514-
protected Class createCompositeInterface(Class[] interfaces) {
526+
protected Class<?> createCompositeInterface(Class<?>[] interfaces) {
515527
return ClassUtils.createCompositeInterface(interfaces, this.beanClassLoader);
516528
}
517529

@@ -527,7 +539,7 @@ protected Class createCompositeInterface(Class[] interfaces) {
527539
* @see org.springframework.scripting.ScriptFactory#getScriptedObject
528540
*/
529541
protected BeanDefinition createScriptedObjectBeanDefinition(BeanDefinition bd, String scriptFactoryBeanName,
530-
ScriptSource scriptSource, Class[] interfaces) {
542+
ScriptSource scriptSource, Class<?>[] interfaces) {
531543

532544
GenericBeanDefinition objectBd = new GenericBeanDefinition(bd);
533545
objectBd.setFactoryBeanName(scriptFactoryBeanName);
@@ -546,7 +558,7 @@ protected BeanDefinition createScriptedObjectBeanDefinition(BeanDefinition bd, S
546558
* @return the generated proxy
547559
* @see RefreshableScriptTargetSource
548560
*/
549-
protected Object createRefreshableProxy(TargetSource ts, Class[] interfaces, boolean proxyTargetClass) {
561+
protected Object createRefreshableProxy(TargetSource ts, Class<?>[] interfaces, boolean proxyTargetClass) {
550562
ProxyFactory proxyFactory = new ProxyFactory();
551563
proxyFactory.setTargetSource(ts);
552564
ClassLoader classLoader = this.beanClassLoader;
@@ -556,7 +568,7 @@ protected Object createRefreshableProxy(TargetSource ts, Class[] interfaces, boo
556568
}
557569
proxyFactory.setInterfaces(interfaces);
558570
if (proxyTargetClass) {
559-
classLoader = null; // Force use of Class.getClassLoader()
571+
classLoader = null; // force use of Class.getClassLoader()
560572
proxyFactory.setProxyTargetClass(proxyTargetClass);
561573
}
562574

@@ -570,6 +582,7 @@ protected Object createRefreshableProxy(TargetSource ts, Class[] interfaces, boo
570582
/**
571583
* Destroy the inner bean factory (used for scripts) on shutdown.
572584
*/
585+
@Override
573586
public void destroy() {
574587
this.scriptBeanFactory.destroySingletons();
575588
}

0 commit comments

Comments
 (0)