Coverage Report - nl.beesting.beangenerator.locators.MethodClassLocator
 
Classes in this File Line Coverage Branch Coverage Complexity
MethodClassLocator
100%
15/15
80%
8/10
0
 
 1  
 package nl.beesting.beangenerator.locators;
 2  
 
 3  
 import java.util.regex.Matcher;
 4  
 import java.util.regex.Pattern;
 5  
 
 6  
 import nl.beesting.beangenerator.locators.methodlocators.AbstractClassPathResolver;
 7  
 
 8  
 import org.aopalliance.intercept.MethodInvocation;
 9  
 import org.apache.commons.lang.StringUtils;
 10  
 
 11  
 
 12  
 /**
 13  
  * This class locator tries to find a classes by method name. It will parse the name of a given
 14  
  * method and try to find a class that that matches it. For example is the method is named
 15  
  * findCatsByIds it will try to load a class named Cat. Finding the classes is the work of the
 16  
  * {@link AbstractClassPathResolver}
 17  
  */
 18  2
 public class MethodClassLocator implements ClassLocator {
 19  
     private AbstractClassPathResolver classResolverDelegate;
 20  
 
 21  
     public Class<?> findClass(MethodInvocation invocation) {
 22  3
         Class<?> result = null;
 23  3
         String methodName = invocation.getMethod().getName();
 24  
 
 25  3
         Pattern pattern = Pattern.compile("[A-Z][a-z]*");
 26  3
         Matcher matcher = pattern.matcher(methodName);
 27  
 
 28  6
         while (matcher.find() && result == null) {
 29  3
             String possibleClassName = matcher.group();
 30  
 
 31  9
             for (int i = 0; i < 3 && result == null; i++) {
 32  6
                 String name = possibleClassName.substring(0, possibleClassName.length() - i);
 33  6
                 if (StringUtils.isNotBlank(name)) {
 34  6
                     result = classResolverDelegate.findClass(name);
 35  
                 }
 36  
             }
 37  3
         }
 38  
 
 39  3
         return result;
 40  
     }
 41  
 
 42  
     /**
 43  
      * @param classResolverDelegate the classResolverDelegate to set
 44  
      */
 45  
     public void setClassResolverDelegate(AbstractClassPathResolver classResolverDelegate) {
 46  2
         this.classResolverDelegate = classResolverDelegate;
 47  2
     }
 48  
 }