Coverage Report - org.jtheque.core.spring.processors.ModulePostProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
ModulePostProcessor
0 %
0/30
0 %
0/18
2.143
 
 1  
 package org.jtheque.core.spring.processors;
 2  
 
 3  
 import org.jtheque.core.managers.module.annotations.Module;
 4  
 import org.jtheque.core.managers.module.annotations.Plug;
 5  
 import org.jtheque.core.managers.module.annotations.PrePlug;
 6  
 import org.jtheque.core.managers.module.annotations.UnPlug;
 7  
 import org.jtheque.core.managers.module.beans.EmptyBeanMethod;
 8  
 import org.jtheque.core.managers.module.beans.ModuleContainer;
 9  
 import org.jtheque.core.managers.module.beans.ReflectionBeanMethod;
 10  
 import org.jtheque.core.managers.module.loaders.ModuleLoader;
 11  
 import org.springframework.beans.factory.config.BeanPostProcessor;
 12  
 
 13  
 import java.lang.reflect.AnnotatedElement;
 14  
 import java.lang.reflect.Method;
 15  
 
 16  
 /*
 17  
  * This file is part of JTheque.
 18  
  *
 19  
  * JTheque is free software: you can redistribute it and/or modify
 20  
  * it under the terms of the GNU General Public License as published by
 21  
  * the Free Software Foundation, either version 3 of the License.
 22  
  *
 23  
  * JTheque is distributed in the hope that it will be useful,
 24  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 25  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 26  
  * GNU General Public License for more details.
 27  
  *
 28  
  * You should have received a copy of the GNU General Public License
 29  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 30  
  */
 31  
 
 32  
 /**
 33  
  * @author Baptiste Wicht
 34  
  */
 35  0
 public final class ModulePostProcessor implements BeanPostProcessor {
 36  
     @Override
 37  
     public Object postProcessBeforeInitialization(Object bean, String beanName) {
 38  0
         return bean;
 39  
     }
 40  
 
 41  
     @Override
 42  
     public Object postProcessAfterInitialization(Object bean, String beanName) {
 43  0
         Class<?> c = bean.getClass();
 44  
 
 45  0
         if (isModule(c)) {
 46  0
             registerModule(bean, beanName);
 47  
         }
 48  
 
 49  0
         return bean;
 50  
     }
 51  
 
 52  
     /**
 53  
      * Test if the annotated element is a module.
 54  
      *
 55  
      * @param c The annotated element.
 56  
      * @return true if the element is a module else false.
 57  
      */
 58  
     private static boolean isModule(AnnotatedElement c) {
 59  0
         return c.getAnnotation(Module.class) != null;
 60  
     }
 61  
 
 62  
     /**
 63  
      * Register a module.
 64  
      *
 65  
      * @param bean     The bean instance.
 66  
      * @param beanName The bean name.
 67  
      */
 68  
     private static void registerModule(Object bean, String beanName) {
 69  0
         ModuleContainer module = new ModuleContainer(
 70  
                 beanName,
 71  
                 bean.getClass().getAnnotation(Module.class)
 72  
         );
 73  
 
 74  0
         searchForAnnotatedMethods(bean, beanName, module);
 75  
 
 76  0
         completeMethods(module);
 77  
 
 78  0
         ModuleLoader.getModules().add(module);
 79  0
     }
 80  
 
 81  
     /**
 82  
      * Search for annotated methods.
 83  
      *
 84  
      * @param bean     The bean instance.
 85  
      * @param beanName The bean name.
 86  
      * @param module   The module container to fill.
 87  
      */
 88  
     private static void searchForAnnotatedMethods(Object bean, String beanName, ModuleContainer module) {
 89  0
         searchMethod(bean.getClass().getSuperclass(), beanName, module); //Proxy
 90  0
         searchMethod(bean.getClass(), beanName, module); //Not proxy
 91  0
     }
 92  
 
 93  
     /**
 94  
      * Search for module methods.
 95  
      *
 96  
      * @param beanClass The bean class.
 97  
      * @param beanName  The bean name.
 98  
      * @param module    The module container to fill.
 99  
      */
 100  
     private static void searchMethod(Class<?> beanClass, String beanName, ModuleContainer module) {
 101  0
         for (Method m : beanClass.getDeclaredMethods()) {
 102  0
             if (m.getAnnotation(UnPlug.class) != null) {
 103  0
                 module.setUnPlugMethod(new ReflectionBeanMethod(beanName, m.getName()));
 104  0
             } else if (m.getAnnotation(Plug.class) != null) {
 105  0
                 module.setPlugMethod(new ReflectionBeanMethod(beanName, m.getName()));
 106  0
             } else if (m.getAnnotation(PrePlug.class) != null) {
 107  0
                 module.setPrePlugMethod(new ReflectionBeanMethod(beanName, m.getName()));
 108  
             }
 109  
         }
 110  0
     }
 111  
 
 112  
     /**
 113  
      * Complete the un-setted methods.
 114  
      *
 115  
      * @param module The module container to fill.
 116  
      */
 117  
     private static void completeMethods(ModuleContainer module) {
 118  0
         if (module.getUnPlugMethod() == null) {
 119  0
             module.setUnPlugMethod(new EmptyBeanMethod());
 120  
         }
 121  
 
 122  0
         if (module.getPlugMethod() == null) {
 123  0
             module.setPlugMethod(new EmptyBeanMethod());
 124  
         }
 125  
 
 126  0
         if (module.getPrePlugMethod() == null) {
 127  0
             module.setPrePlugMethod(new EmptyBeanMethod());
 128  
         }
 129  0
     }
 130  
 }