From: Ales J. <ale...@ge...> - 2006-07-09 13:18:55
|
User: alesj Date: 06/07/09 09:18:49 Modified: src/main/org/jboss/spring/support SpringInjectionSupport.java Log: Reformat code + Spring-MC fix. Revision Changes Path 1.6 +68 -37 jboss-spring/src/main/org/jboss/spring/support/SpringInjectionSupport.java (In the diff below, changes in quantity of whitespace are not shown.) Index: SpringInjectionSupport.java =================================================================== RCS file: /cvsroot/jboss/jboss-spring/src/main/org/jboss/spring/support/SpringInjectionSupport.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -b -r1.5 -r1.6 --- SpringInjectionSupport.java 4 Nov 2005 15:41:45 -0000 1.5 +++ SpringInjectionSupport.java 9 Jul 2006 13:18:49 -0000 1.6 @@ -37,36 +37,44 @@ * Injects objects from bean factory located in JNDI at jndiName gained * from @Spring annotation's field jndiName. * It is applied to setter methods and fields annotated with @Spring annotation. - * @see MethodComparator Excludes overridden @Spring annotated methods - * Class type check is performed before actual setting. * * @author <a href="mailto:ale...@ge...">Ales Justin</a> + * @see MethodComparator Excludes overridden @Spring annotated methods + * Class type check is performed before actual setting. */ -public abstract class SpringInjectionSupport { +public abstract class SpringInjectionSupport +{ protected Logger log = Logger.getLogger(getClass()); private final Comparator<Method> METHOD_COMPARATOR = new MethodComparator(); - protected Object inject(Object target) throws Exception { + protected Object inject(Object target) throws Exception + { log.debug("Invoking Spring injection: " + target.getClass().getName()); Method[] methods = getAllMethods(target); - for (Method m : methods) { + for (Method m : methods) + { Spring spring = m.getAnnotation(Spring.class); - if (spring != null) { - if (isSetterMethod(m)) { + if (spring != null) + { + if (isSetterMethod(m)) + { injectToMethod(target, m, spring); - } else { + } else + { log.warn("Spring annotation only allowed on setter methods."); } } } Field[] fields = getAllFields(target); - for (Field f : fields) { + for (Field f : fields) + { Spring spring = f.getAnnotation(Spring.class); - if (spring != null) { + if (spring != null) + { injectToField(target, f, spring); } } @@ -74,36 +82,43 @@ return target; } - protected Method[] getAllMethods(Object bean) { + protected Method[] getAllMethods(Object bean) + { Class beanClass = bean.getClass(); Set<Method> methods = new TreeSet<Method>(METHOD_COMPARATOR); - while(beanClass != Object.class) { + while (beanClass != Object.class) + { methods.addAll(Arrays.asList(beanClass.getDeclaredMethods())); beanClass = beanClass.getSuperclass(); } return methods.toArray(new Method[methods.size()]); } - protected Field[] getAllFields(Object bean) { + protected Field[] getAllFields(Object bean) + { Class beanClass = bean.getClass(); List<Field> fields = new ArrayList<Field>(); - while(beanClass != Object.class) { + while (beanClass != Object.class) + { fields.addAll(Arrays.asList(beanClass.getDeclaredFields())); beanClass = beanClass.getSuperclass(); } return fields.toArray(new Field[fields.size()]); } - private boolean isSetterMethod(Method m) { + private boolean isSetterMethod(Method m) + { return m.getName().startsWith("set") && m.getParameterTypes().length == 1; } - private Object getObjectFromBeanFactory(Spring spring) throws Exception { - BeanFactory beanFactory = (BeanFactory)Util.lookup(spring.jndiName(), BeanFactory.class); + private Object getObjectFromBeanFactory(Spring spring) throws Exception + { + BeanFactory beanFactory = (BeanFactory) Util.lookup(spring.jndiName(), BeanFactory.class); return beanFactory.getBean(spring.bean()); } - private void injectToMethod(Object target, Method method, Spring spring) throws Exception { + private void injectToMethod(Object target, Method method, Spring spring) throws Exception + { Object bean = getObjectFromBeanFactory(spring); doAssert(bean, method.getParameterTypes()[0]); logInjection(spring, bean, target, method); @@ -111,7 +126,8 @@ method.invoke(target, bean); } - private void injectToField(Object target, Field field, Spring spring) throws Exception { + private void injectToField(Object target, Field field, Spring spring) throws Exception + { Object bean = getObjectFromBeanFactory(spring); doAssert(bean, field.getType()); logInjection(spring, bean, target, field); @@ -119,13 +135,15 @@ field.set(target, bean); } - private void doAssert(Object bean, Class expectedBeanClass) { + private void doAssert(Object bean, Class expectedBeanClass) + { Assert.isTrue(expectedBeanClass.isAssignableFrom(bean.getClass()), "Illegal bean class type - " + bean.getClass().getName() + " - cannot be assigned to: " + expectedBeanClass.getName()); } - private void logInjection(Spring spring, Object bean, Object target, Member m) { + private void logInjection(Spring spring, Object bean, Object target, Member m) + { log.debug("Injecting bean '" + spring.bean() + "' of class type " + bean.getClass().getName() + " into " + target + " via " + m); } @@ -134,43 +152,56 @@ * Equals on overridden methods. * Any other solution? */ - private class MethodComparator implements Comparator<Method> { + private class MethodComparator implements Comparator<Method> + { - public int compare(Method m1, Method m2) { + public int compare(Method m1, Method m2) + { String name1 = m1.getName(); String name2 = m2.getName(); - if (name1.equals(name2)) { + if (name1.equals(name2)) + { Class returnType1 = m1.getReturnType(); Class returnType2 = m2.getReturnType(); Class[] params1 = m1.getParameterTypes(); Class[] params2 = m1.getParameterTypes(); - if (params1.length == params2.length) { - if (returnType1.equals(returnType2)) { + if (params1.length == params2.length) + { + if (returnType1.equals(returnType2)) + { int i; int length = params1.length; - for (i = 0; i < length; i++) { - if (!params1[i].equals(params2[i])) { + for (i = 0; i < length; i++) + { + if (!params1[i].equals(params2[i])) + { break; } } //not equal - if (i < length) { + if (i < length) + { return params1[i].getName().compareTo(params2[i].getName()); - } else { + } else + { //overridden method - if (m1.getAnnotation(Spring.class) != null) { + if (m1.getAnnotation(Spring.class) != null) + { log.warn("Found overridden @Spring annotated method: " + m1); } return 0; } - } else { + } else + { return returnType1.getName().compareTo(returnType2.getName()); } - } else { + } else + { return params1.length - params2.length; } - } else { + } else + { return name1.compareTo(name2); } } |