From c42ba695329c0bee9ae37c15f98e88be1463c2c2 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 1 Jun 2017 08:43:25 +0100 Subject: [PATCH] Move @SuppressWarnings to private methods --- .../guice/module/SpringModule.java | 52 +++++++++++++------ 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/springframework/guice/module/SpringModule.java b/src/main/java/org/springframework/guice/module/SpringModule.java index 0d2769c..6561969 100644 --- a/src/main/java/org/springframework/guice/module/SpringModule.java +++ b/src/main/java/org/springframework/guice/module/SpringModule.java @@ -54,19 +54,22 @@ public class SpringModule implements Module { public SpringModule(DefaultListableBeanFactory beanFactory) { this.beanFactory = beanFactory; if (beanFactory.getBeanNamesForType(GuiceModuleMetadata.class).length > 0) { - this.matcher = new CompositeTypeMatcher(beanFactory.getBeansOfType(GuiceModuleMetadata.class).values()); + this.matcher = new CompositeTypeMatcher( + beanFactory.getBeansOfType(GuiceModuleMetadata.class).values()); } } - @SuppressWarnings({ "rawtypes", "unchecked" }) public void configure(Binder binder) { for (String name : this.beanFactory.getBeanDefinitionNames()) { BeanDefinition definition = this.beanFactory.getBeanDefinition(name); - if (definition.isAutowireCandidate() && definition.getRole() == AbstractBeanDefinition.ROLE_APPLICATION) { + if (definition.isAutowireCandidate() + && definition.getRole() == AbstractBeanDefinition.ROLE_APPLICATION) { Class type = this.beanFactory.getType(name); final String beanName = name; - Provider typeProvider = new BeanFactoryProvider(this.beanFactory, null, type); - Provider namedProvider = new BeanFactoryProvider(this.beanFactory, beanName, type); + Provider typeProvider = BeanFactoryProvider.typed(this.beanFactory, + type); + Provider namedProvider = BeanFactoryProvider.named(this.beanFactory, + beanName, type); if (!type.isInterface() && !ClassUtils.isCglibProxyClass(type)) { bindConditionally(binder, name, type, typeProvider, namedProvider); } @@ -80,9 +83,9 @@ public class SpringModule implements Module { } } - @SuppressWarnings({ "rawtypes", "unchecked" }) - private void bindConditionally(Binder binder, String name, Type type, Provider typeProvider, - Provider namedProvider) { + @SuppressWarnings({ "rawtypes", "unchecked" }) + private void bindConditionally(Binder binder, String name, Type type, + Provider typeProvider, Provider namedProvider) { if (!this.matcher.matches(name, type)) { return; } @@ -92,11 +95,13 @@ public class SpringModule implements Module { if (this.bound.get(type) == null) { // Only bind one provider for each type - binder.withSource("spring-guice").bind(Key.get(type)).toProvider(typeProvider); - this.bound.put(type, typeProvider); + binder.withSource("spring-guice").bind(Key.get(type)) + .toProvider(typeProvider); + this.bound.put(type, typeProvider); } // But allow binding to named beans - binder.withSource("spring-guice").bind(TypeLiteral.get(type)).annotatedWith(Names.named(name)).toProvider(namedProvider); + binder.withSource("spring-guice").bind(TypeLiteral.get(type)) + .annotatedWith(Names.named(name)).toProvider(namedProvider); } private static class BeanFactoryProvider implements Provider { @@ -109,21 +114,34 @@ public class SpringModule implements Module { private T result; - public BeanFactoryProvider(DefaultListableBeanFactory beanFactory, String name, Class type) { + private BeanFactoryProvider(DefaultListableBeanFactory beanFactory, String name, + Class type) { this.beanFactory = beanFactory; this.name = name; this.type = type; } + public static Provider named(DefaultListableBeanFactory beanFactory, + String name, Class type) { + return new BeanFactoryProvider(beanFactory, name, type); + } + + public static Provider typed(DefaultListableBeanFactory beanFactory, + Class type) { + return new BeanFactoryProvider(beanFactory, null, type); + } + @Override public T get() { if (this.result == null) { - String[] named = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.beanFactory, this.type); + String[] named = BeanFactoryUtils + .beanNamesForTypeIncludingAncestors(this.beanFactory, this.type); List names = new ArrayList(named.length); if (named.length == 1) { names.add(named[0]); - } else { + } + else { for (String name : named) { if (name.equals(this.name)) names.add(name); @@ -131,7 +149,8 @@ public class SpringModule implements Module { } if (names.size() == 1) { this.result = this.beanFactory.getBean(names.get(0), this.type); - } else { + } + else { for (String name : named) { if (this.beanFactory.getBeanDefinition(name).isPrimary()) { this.result = this.beanFactory.getBean(name, this.type); @@ -139,7 +158,8 @@ public class SpringModule implements Module { } } if (this.result == null) { - throw new ProvisionException("No primary bean definition for type: " + this.type); + throw new ProvisionException( + "No primary bean definition for type: " + this.type); } } }