public <T extends ConfigBeanProxy> T setConfigBean(T finalConfigBean, ConfigBeanDefaultValue configBeanDefaultValue, ConfigBeanProxy parent) {
Class owningClassForLocation = getOwningClassForLocation(configBeanDefaultValue.getLocation());
Class configBeanClass = getClassForFullName(configBeanDefaultValue.getConfigBeanClassName());
try {
ConfigBeanProxy configBeanInstance = null;
if (getNameForConfigBean(finalConfigBean, configBeanClass) == null) {
List<ConfigBeanProxy> extensions = getExtensions(parent);
for (ConfigBeanProxy extension : extensions) {
try {
configBeanInstance = (ConfigBeanProxy) configBeanClass.cast(extension);
break;
} catch (Exception e) {
// ignore, not the right type.
}
}
if (!configBeanDefaultValue.replaceCurrentIfExists() || !stackPositionHigher(finalConfigBean, configBeanInstance)) {
if (configBeanInstance != null) return (T) configBeanInstance;
}
if (configBeanInstance != null) {
extensions.remove(configBeanInstance);
}
}
} catch (InvocationTargetException e) {
LOG.log(Level.INFO, cannotSetConfigBean, e);
} catch (IllegalAccessException e) {
LOG.log(Level.INFO, cannotSetConfigBean, e);
}
Method m = getMatchingSetterMethod(owningClassForLocation, configBeanClass);
if (m != null) {
try {
if (configBeanClass.getAnnotation(HasCustomizationTokens.class) != null) {
applyCustomTokens(configBeanDefaultValue, finalConfigBean, parent);
}
m.invoke(parent, finalConfigBean);
} catch (Exception e) {
LogHelper.log(LOG, Level.INFO, cannotSetConfigBeanFor, e, finalConfigBean.getClass().getName());
}
return finalConfigBean;
}
m = findSuitableCollectionGetter(owningClassForLocation, configBeanClass);
if (m != null) {
try {
Collection col = (Collection) m.invoke(parent);
String name = getNameForConfigBean(finalConfigBean, configBeanClass);
ConfigBeanProxy itemToRemove = getNamedConfigBeanFromCollection(col, name, configBeanClass);
if (configBeanDefaultValue.replaceCurrentIfExists()) {
try {
if (itemToRemove != null) {
if (stackPositionHigher(finalConfigBean, itemToRemove)) {
col.remove(itemToRemove);