Created
July 27, 2011 10:12
-
-
Save nschlimm/1109076 to your computer and use it in GitHub Desktop.
Receive delegate for decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String getRegisteredDelegate(ConfigurableListableBeanFactory beanFactory, DecoratorInfo decoratorInfo) { | |
DelegateField arbitraryDelegateField = decoratorInfo.getDelegateFields().get(0); | |
DependencyDescriptor delegateDependencyDescriptor = DecoratorModuleUtils.createRuleBasedDescriptor(arbitraryDelegateField.getDeclaredField(), new Class[] { IgnoreDecoratorAutowiringLogic.class }); | |
List<String> registeredDelegates = new ArrayList<String>(); | |
String[] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, arbitraryDelegateField.getDeclaredField().getType(), true, false); | |
for (String candidate : candidateNames) { | |
BeanDefinition bd = beanFactory.getBeanDefinition(candidate); | |
// Annotated Bean scanned in the classpath | |
if (bd instanceof AnnotatedBeanDefinition) { | |
AnnotatedBeanDefinition abd = (AnnotatedBeanDefinition) bd; | |
// No @Decorator | |
if (!DecoratorInfo.isDecorator(abd)) { | |
Class decoratorClass = null; | |
try { | |
decoratorClass = ClassUtils.forName(abd.getBeanClassName(), this.getClass().getClassLoader()); | |
} catch (Exception e) { | |
throw new DecoratorAwareBeanFactoryPostProcessorException("Could not find decorator class: " + abd.getBeanClassName(), e); | |
} | |
// Consider scoped proxies | |
if (candidate.startsWith("scopedTarget.")) { | |
candidate = candidate.replace("scopedTarget.", ""); | |
} | |
// Bean must match delegate dependency descriptor (that describes the delegate field of the given decorator) | |
if ((((DefaultListableBeanFactory) beanFactory).isAutowireCandidate(candidate, delegateDependencyDescriptor))) { | |
registeredDelegates.add(candidate); | |
} | |
} | |
} | |
} | |
if (registeredDelegates.size() > 1) { | |
throw new DecoratorAwareBeanFactoryPostProcessorException("Could not find unique delegate for decorator info: " + decoratorInfo.toString()); | |
} | |
return registeredDelegates.get(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment