Coverage Report - org.apache.onami.autobind.annotations.features.AutoBindingFeature
 
Classes in this File Line Coverage Branch Coverage Complexity
AutoBindingFeature
0%
0/63
0%
0/50
10
AutoBindingFeature$1
0%
0/1
N/A
10
 
 1  
 package org.apache.onami.autobind.annotations.features;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 5  
  * contributor license agreements.  See the NOTICE file distributed with
 6  
  * this work for additional information regarding copyright ownership.
 7  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 8  
  * (the "License"); you may not use this file except in compliance with
 9  
  * the License.  You may obtain a copy of the License at
 10  
  *
 11  
  *  http://www.apache.org/licenses/LICENSE-2.0
 12  
  *
 13  
  * Unless required by applicable law or agreed to in writing, software
 14  
  * distributed under the License is distributed on an "AS IS" BASIS,
 15  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 16  
  * See the License for the specific language governing permissions and
 17  
  * limitations under the License.
 18  
  */
 19  
 
 20  
 import static java.lang.String.format;
 21  
 import static java.util.Collections.addAll;
 22  
 import static java.util.logging.Logger.getLogger;
 23  
 import static org.apache.onami.autobind.annotations.To.Type.IMPLEMENTATION;
 24  
 import static org.apache.onami.autobind.install.BindingStage.BINDING;
 25  
 import static org.apache.onami.autobind.install.BindingStage.IGNORE;
 26  
 import static org.apache.onami.autobind.jsr330.Names.named;
 27  
 
 28  
 import java.lang.annotation.Annotation;
 29  
 import java.util.ArrayList;
 30  
 import java.util.HashMap;
 31  
 import java.util.List;
 32  
 import java.util.Map;
 33  
 import java.util.Map.Entry;
 34  
 import java.util.logging.Level;
 35  
 import java.util.logging.Logger;
 36  
 
 37  
 import javax.inject.Named;
 38  
 import javax.inject.Qualifier;
 39  
 import javax.inject.Singleton;
 40  
 
 41  
 import org.apache.onami.autobind.annotations.Bind;
 42  
 import org.apache.onami.autobind.annotations.GuiceAnnotation;
 43  
 import org.apache.onami.autobind.install.BindingStage;
 44  
 import org.apache.onami.autobind.scanner.features.BindingScannerFeature;
 45  
 
 46  
 import com.google.inject.BindingAnnotation;
 47  
 import org.apache.onami.autobind.utils.ClassLoadingUtils;
 48  
 
 49  
 @Singleton
 50  0
 public class AutoBindingFeature
 51  
     extends BindingScannerFeature
 52  
 {
 53  
 
 54  0
     protected final Logger _logger = getLogger( getClass().getName() );
 55  
 
 56  
     @Override
 57  
     public BindingStage accept( Class<Object> annotatedClass, Map<String, Annotation> annotations )
 58  
     {
 59  0
         if ( annotations.containsKey( Bind.class.getName() ) )
 60  
         {
 61  0
             Bind annotation = (Bind) annotations.get( Bind.class.getName() );
 62  0
             if ( !annotation.multiple() && !( IMPLEMENTATION == annotation.to().value() ) )
 63  
             {
 64  0
                 return BINDING;
 65  
             }
 66  
         }
 67  0
         return IGNORE;
 68  
     }
 69  
 
 70  
     @SuppressWarnings( "unchecked" )
 71  
     @Override
 72  
     public void process( final Class<Object> annotatedClass, final Map<String, Annotation> annotations )
 73  
     {
 74  0
         Bind annotation = (Bind) annotations.get( Bind.class.getName() );
 75  0
         Map<String, Annotation> filtered = filter( annotations );
 76  
 
 77  0
         final boolean asSingleton =
 78  
             ( annotations.containsKey( com.google.inject.Singleton.class.getName() ) || annotations.containsKey( javax.inject.Singleton.class.getName() ) );
 79  
 
 80  0
         if ( annotation.value().value().length() > 0 )
 81  
         {
 82  0
             filtered.put( Named.class.getName(), named( resolver.resolve( annotation.value().value() ) ) );
 83  
         }
 84  
 
 85  
         Class<Object>[] interfaces;
 86  
 
 87  0
         switch ( annotation.to().value() )
 88  
         {
 89  
             case CUSTOM:
 90  0
                 interfaces = (Class<Object>[]) annotation.to().customs();
 91  0
                 break;
 92  
 
 93  
             case SUPER:
 94  0
                 Class<? super Object> superclass = annotatedClass.getSuperclass();
 95  0
                 if ( Object.class.equals( superclass ) )
 96  
                 {
 97  0
                     interfaces = new Class[] { annotatedClass };
 98  
                 }
 99  
                 else
 100  
                 {
 101  0
                     interfaces = new Class[] { superclass };
 102  
                 }
 103  
 
 104  0
                 break;
 105  
 
 106  
             case INTERFACES:
 107  
             default:
 108  0
                 interfaces = (Class<Object>[]) annotatedClass.getInterfaces();
 109  0
                 if ( interfaces.length == 0 )
 110  
                 {
 111  0
                     List<Class<?>> interfaceCollection = new ArrayList<Class<?>>();
 112  0
                     Class<? super Object> parent = annotatedClass.getSuperclass();
 113  0
                     while ( parent != null && !parent.equals( Object.class ) )
 114  
                     {
 115  0
                         addAll( interfaceCollection, parent.getInterfaces() );
 116  0
                         parent = parent.getSuperclass();
 117  
                     }
 118  0
                     interfaces = interfaceCollection.toArray( new Class[interfaceCollection.size()] );
 119  0
                     if ( interfaces.length == 0 )
 120  
                     {
 121  0
                         interfaces = new Class[] { annotatedClass };
 122  
                         // FIXME Guice doesn't allow a binding to itself
 123  
                     }
 124  
                 }
 125  
         }
 126  
 
 127  0
         for ( Class<Object> interf : interfaces )
 128  
         {
 129  0
             if ( _logger.isLoggable( Level.FINE ) )
 130  
             {
 131  0
                 _logger.fine( format( "Binding Class %s to Interface %s. Singleton? %s ",
 132  
                                       annotatedClass, interf, asSingleton ) );
 133  
             }
 134  
 
 135  0
             if ( filtered.size() > 0 )
 136  
             {
 137  0
                 for ( Annotation anno : filtered.values() )
 138  
                 {
 139  0
                     bind( annotatedClass, interf, anno, ( asSingleton ? Singleton.class : null ) );
 140  
                 }
 141  
             }
 142  
             else
 143  
             {
 144  0
                 bind( annotatedClass, interf, null, ( asSingleton ? Singleton.class : null ) );
 145  
             }
 146  
         }
 147  0
     }
 148  
 
 149  
     @SuppressWarnings( "unchecked" )
 150  
     protected Map<String, Annotation> filter( final Map<String, Annotation> annotations )
 151  
     {
 152  0
         Map<String, Annotation> filtered = new HashMap<String, Annotation>( annotations );
 153  
 
 154  0
         for ( Entry<String, Annotation> entry : annotations.entrySet() )
 155  
         {
 156  0
             String key = entry.getKey();
 157  0
             if ( qualifiers.contains( key ) )
 158  
             {
 159  0
                 continue;
 160  
             }
 161  0
             if ( others.contains( key ) )
 162  
             {
 163  0
                 filtered.remove( key );
 164  0
                 continue;
 165  
             }
 166  
             Class<? extends Annotation> annotation;
 167  
             try
 168  
             {
 169  0
                 annotation = (Class<? extends Annotation>) ClassLoadingUtils.loadClass(key);
 170  0
                 if ( annotation.isAnnotationPresent( GuiceAnnotation.class ) )
 171  
                 {
 172  0
                     filtered.remove( key );
 173  0
                     others.add( key );
 174  0
                     continue;
 175  
                 }
 176  0
                 if ( annotation.isAnnotationPresent( Qualifier.class ) )
 177  
                 {
 178  0
                     qualifiers.add( key );
 179  0
                     continue;
 180  
                 }
 181  0
                 if ( annotation.isAnnotationPresent( BindingAnnotation.class ) )
 182  
                 {
 183  0
                     qualifiers.add( key );
 184  0
                     continue;
 185  
                 }
 186  0
                 filtered.remove( key );
 187  0
                 others.add( key );
 188  
             }
 189  0
             catch ( ClassNotFoundException e )
 190  
             {
 191  
                 // TODO ignore
 192  0
             }
 193  0
         }
 194  
 
 195  0
         return filtered;
 196  
     }
 197  
 
 198  
 }