<commit_before>/**
 * Copyright (C) 2010 <NAME> <<EMAIL>>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * 
 */
package org.nnsoft.guice.autobind.annotations.features;

import static org.nnsoft.guice.autobind.annotations.To.Type.IMPLEMENTATION;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Named;
import javax.inject.Qualifier;
import javax.inject.Singleton;

import org.nnsoft.guice.autobind.annotations.Bind;
import org.nnsoft.guice.autobind.annotations.GuiceAnnotation;
import org.nnsoft.guice.autobind.install.BindingStage;
import org.nnsoft.guice.autobind.jsr330.Names;
import org.nnsoft.guice.autobind.scanner.features.BindingScannerFeature;

import com.google.inject.BindingAnnotation;


@Singleton
public class AutoBindingFeature extends BindingScannerFeature {
	protected Logger _logger = Logger.getLogger(AutoBindingFeature.class.getName());

	@Override
	public BindingStage accept(Class<Object> annotatedClass, Map<String, Annotation> annotations) {
		if (annotations.containsKey(Bind.class.getName())) {
			Bind annotation = (Bind) annotations.get(Bind.class.getName());
			if (!annotation.multiple() && !(annotation.to().value() == IMPLEMENTATION)) {
				return BindingStage.BINDING;
			}
		}
		return BindingStage.IGNORE;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void process(final Class<Object> annotatedClass,
			final Map<String, Annotation> annotations) {
		Bind annotation = (Bind) annotations.get(Bind.class.getName());
		Map<String, Annotation> filtered = filter(annotations);

		final boolean asSingleton = (annotations.containsKey(com.google.inject.Singleton.class
			.getName()) || annotations.containsKey(javax.inject.Singleton.class.getName()));

		if(annotation.value().value().length() > 0){
			filtered.put(Named.class.getName(), Names.named(resolver.resolve(annotation.value().value())));
		}

		Class<Object>[] interfaces;

		switch (annotation.to().value()) {
		case CUSTOM:
			interfaces = (Class<Object>[]) annotation.to().customs();
			break;
		case SUPER:
			Class<? super Object> superclass = annotatedClass.getSuperclass();
			if (Object.class.equals(superclass)) {
				interfaces = new Class[] { annotatedClass };
			} else {
				interfaces = new Class[] { superclass };
			}

			break;
		case INTERFACES:
		default:
			interfaces = (Class<Object>[]) annotatedClass.getInterfaces();
			if(interfaces.length == 0){
				List<Class<?>> interfaceCollection = new ArrayList<Class<?>>();
				Class<? super Object> parent = annotatedClass.getSuperclass();
				while(parent != null && !parent.equals(Object.class)){
					Collections.addAll(interfaceCollection, parent.getInterfaces());
					parent = parent.getSuperclass();
				}
				interfaces = interfaceCollection.toArray(new Class[interfaceCollection.size()]);
				if(interfaces.length == 0){
					interfaces = new Class[] { annotatedClass };
					//FIXME Guice doesn't allow a binding to itself
				}
			}
		}

		for (Class<Object> interf : interfaces) {
			if (_logger.isLoggable(Level.FINE)) {
				_logger.fine(String.format("Binding Class %s to Interface %s. Singleton? %s ",
					annotatedClass, interf, asSingleton));
			}

			if (filtered.size() > 0) {
				for (Annotation anno : filtered.values()) {
					bind(annotatedClass, interf, anno, (asSingleton ? Singleton.class : null));
				}
			} else {
				bind(annotatedClass, interf, null, (asSingleton ? Singleton.class : null));
			}
		}
	}

	@SuppressWarnings("unchecked")
	protected Map<String, Annotation> filter(final Map<String, Annotation> annotations) {
		Map<String, Annotation> filtered = new HashMap<String, Annotation>(annotations);
		
		for(Entry<String, Annotation> entry : annotations.entrySet()){
			String key = entry.getKey();
			if(qualifiers.contains(key)){
				continue;
			}
			if(others.contains(key)){
				filtered.remove(key);
				continue;
			}
			Class<? extends Annotation> annotation;
			try {
				annotation = (Class<? extends Annotation>) Class.forName(key);
				if(annotation.isAnnotationPresent(GuiceAnnotation.class)){
					filtered.remove(key);
					others.add(key);
					continue;
				}
				if(annotation.isAnnotationPresent(Qualifier.class)){
					qualifiers.add(key);
					continue;
				}
				if(annotation.isAnnotationPresent(BindingAnnotation.class)){
					qualifiers.add(key);
					continue;
				}
				filtered.remove(key);
				others.add(key);
			} catch (ClassNotFoundException e) {
				//TODO ignore
			}			
		}

		return filtered;
	}
}<commit_msg>code format<commit_after>package org.nnsoft.guice.autobind.annotations.features;

/*
 *    Copyright 2012 The 99 Software Foundation
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

import static org.nnsoft.guice.autobind.annotations.To.Type.IMPLEMENTATION;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Named;
import javax.inject.Qualifier;
import javax.inject.Singleton;

import org.nnsoft.guice.autobind.annotations.Bind;
import org.nnsoft.guice.autobind.annotations.GuiceAnnotation;
import org.nnsoft.guice.autobind.install.BindingStage;
import org.nnsoft.guice.autobind.jsr330.Names;
import org.nnsoft.guice.autobind.scanner.features.BindingScannerFeature;

import com.google.inject.BindingAnnotation;

@Singleton
public class AutoBindingFeature
    extends BindingScannerFeature
{

    protected Logger _logger = Logger.getLogger( AutoBindingFeature.class.getName() );

    @Override
    public BindingStage accept( Class<Object> annotatedClass, Map<String, Annotation> annotations )
    {
        if ( annotations.containsKey( Bind.class.getName() ) )
        {
            Bind annotation = (Bind) annotations.get( Bind.class.getName() );
            if ( !annotation.multiple() && !( annotation.to().value() == IMPLEMENTATION ) )
            {
                return BindingStage.BINDING;
            }
        }
        return BindingStage.IGNORE;
    }

    @SuppressWarnings( "unchecked" )
    @Override
    public void process( final Class<Object> annotatedClass, final Map<String, Annotation> annotations )
    {
        Bind annotation = (Bind) annotations.get( Bind.class.getName() );
        Map<String, Annotation> filtered = filter( annotations );

        final boolean asSingleton =
            ( annotations.containsKey( com.google.inject.Singleton.class.getName() ) || annotations.containsKey( javax.inject.Singleton.class.getName() ) );

        if ( annotation.value().value().length() > 0 )
        {
            filtered.put( Named.class.getName(), Names.named( resolver.resolve( annotation.value().value() ) ) );
        }

        Class<Object>[] interfaces;

        switch ( annotation.to().value() )
        {
            case CUSTOM:
                interfaces = (Class<Object>[]) annotation.to().customs();
                break;
            case SUPER:
                Class<? super Object> superclass = annotatedClass.getSuperclass();
                if ( Object.class.equals( superclass ) )
                {
                    interfaces = new Class[] { annotatedClass };
                }
                else
                {
                    interfaces = new Class[] { superclass };
                }

                break;
            case INTERFACES:
            default:
                interfaces = (Class<Object>[]) annotatedClass.getInterfaces();
                if ( interfaces.length == 0 )
                {
                    List<Class<?>> interfaceCollection = new ArrayList<Class<?>>();
                    Class<? super Object> parent = annotatedClass.getSuperclass();
                    while ( parent != null && !parent.equals( Object.class ) )
                    {
                        Collections.addAll( interfaceCollection, parent.getInterfaces() );
                        parent = parent.getSuperclass();
                    }
                    interfaces = interfaceCollection.toArray( new Class[interfaceCollection.size()] );
                    if ( interfaces.length == 0 )
                    {
                        interfaces = new Class[] { annotatedClass };
                        // FIXME Guice doesn't allow a binding to itself
                    }
                }
        }

        for ( Class<Object> interf : interfaces )
        {
            if ( _logger.isLoggable( Level.FINE ) )
            {
                _logger.fine( String.format( "Binding Class %s to Interface %s. Singleton? %s ", annotatedClass,
                                             interf, asSingleton ) );
            }

            if ( filtered.size() > 0 )
            {
                for ( Annotation anno : filtered.values() )
                {
                    bind( annotatedClass, interf, anno, ( asSingleton ? Singleton.class : null ) );
                }
            }
            else
            {
                bind( annotatedClass, interf, null, ( asSingleton ? Singleton.class : null ) );
            }
        }
    }

    @SuppressWarnings( "unchecked" )
    protected Map<String, Annotation> filter( final Map<String, Annotation> annotations )
    {
        Map<String, Annotation> filtered = new HashMap<String, Annotation>( annotations );

        for ( Entry<String, Annotation> entry : annotations.entrySet() )
        {
            String key = entry.getKey();
            if ( qualifiers.contains( key ) )
            {
                continue;
            }
            if ( others.contains( key ) )
            {
                filtered.remove( key );
                continue;
            }
            Class<? extends Annotation> annotation;
            try
            {
                annotation = (Class<? extends Annotation>) Class.forName( key );
                if ( annotation.isAnnotationPresent( GuiceAnnotation.class ) )
                {
                    filtered.remove( key );
                    others.add( key );
                    continue;
                }
                if ( annotation.isAnnotationPresent( Qualifier.class ) )
                {
                    qualifiers.add( key );
                    continue;
                }
                if ( annotation.isAnnotationPresent( BindingAnnotation.class ) )
                {
                    qualifiers.add( key );
                    continue;
                }
                filtered.remove( key );
                others.add( key );
            }
            catch ( ClassNotFoundException e )
            {
                // TODO ignore
            }
        }

        return filtered;
    }

}
{'fraction_non_alphanumeric': 0.11150029188558085, 'fraction_numerical': 0.002668668167792511, 'mean_word_length': 2.3866139508613387, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2566292', 'n_tokens_mistral': 3572, 'n_tokens_neox': 3165, 'n_words': 744}