<commit_before>package trip.spi.helpers;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

import javax.annotation.PostConstruct;

import lombok.RequiredArgsConstructor;
import trip.spi.GeneratedFromStatelessService;
import trip.spi.ServiceProvider;
import trip.spi.ServiceProviderException;

@RequiredArgsConstructor
public class ProvidableClass<T> {

	final Class<T> targetClazz;
	final Iterable<ProvidableField> fields;
	final Consumer<Object> postConstructor;

	public void provide( Object instance, ServiceProvider provider )
			throws ServiceProviderException, IllegalArgumentException, IllegalAccessException {
		postConstructor.accept(instance);
		for ( final ProvidableField field : fields )
			field.provide( instance, provider );
	}

	public static <T> ProvidableClass<T> wrap( QualifierExtractor extractor, Class<T> targetClazz ) {
		return new ProvidableClass<T>(
				targetClazz,
				readClassProvidableFields( extractor, targetClazz ),
				readPostConstructor(targetClazz));
	}

	static Consumer<Object> readPostConstructor( Class<?> targetClazz ){
		Method postConstructor = null;
		for ( final Method method : targetClazz.getMethods() )
			if ( method.isAnnotationPresent(PostConstruct.class) ){
				postConstructor = method;
				break;
			}
		return postConstructor != null
			? new PostConstructorMethod(postConstructor)
			: EmptyMethod.INSTANCE;
	}

	static Iterable<ProvidableField> readClassProvidableFields( QualifierExtractor extractor, Class<?> targetClazz ) {
		final List<ProvidableField> providableFields = new ArrayList<ProvidableField>();
		Class<? extends Object> clazz = targetClazz;
		while ( !Object.class.equals( clazz ) ) {
			populateWithProvidableFields( extractor, clazz, providableFields );
			if ( clazz.isAnnotationPresent(GeneratedFromStatelessService.class) )
				break;
			clazz = clazz.getSuperclass();
		}
		return providableFields;
	}

	static void populateWithProvidableFields( QualifierExtractor extractor, Class<?> targetClazz, List<ProvidableField> providableFields ) {
		for ( final Field field : targetClazz.getDeclaredFields() ){
			final Collection<Class<? extends Annotation>> qualifiers = extractQualifiersFromAvoidingNPEWhenCreatingQualifierExtractor(extractor, field);
			if ( extractor.isASingleElementProvider( field ) )
				providableFields.add( SingleElementProvidableField.from( qualifiers, field ) );
			else if ( extractor.isAManyElementsProvider( field ) )
				providableFields.add( ManyElementsProvidableField.from( qualifiers, field ) );
		}
	}

	private static Collection<Class<? extends Annotation>> extractQualifiersFromAvoidingNPEWhenCreatingQualifierExtractor(
			final QualifierExtractor extractor, final Field field)
	{
		if ( null == extractor )
			return Collections.emptyList();
		return extractor.extractQualifiersFrom(field);
	}
}

class EmptyMethod implements Consumer<Object> {

	static final Consumer<Object> INSTANCE = new EmptyMethod();

	@Override
	public void accept(Object t) {}
}

@RequiredArgsConstructor
class PostConstructorMethod implements Consumer<Object> {

	final Method method;

	@Override
	public void accept(Object target) {
		try {
			method.invoke(target);
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			throw new ServiceProviderException(e);
		}
	}
}<commit_msg>#34: post constructors should be called after injection process<commit_after>package trip.spi.helpers;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

import javax.annotation.PostConstruct;

import lombok.RequiredArgsConstructor;
import trip.spi.GeneratedFromStatelessService;
import trip.spi.ServiceProvider;
import trip.spi.ServiceProviderException;

@RequiredArgsConstructor
public class ProvidableClass<T> {

	final Class<T> targetClazz;
	final Iterable<ProvidableField> fields;
	final Consumer<Object> postConstructor;

	public void provide( Object instance, ServiceProvider provider )
			throws ServiceProviderException, IllegalArgumentException, IllegalAccessException {
		for ( final ProvidableField field : fields )
			field.provide( instance, provider );
		postConstructor.accept(instance);
	}

	public static <T> ProvidableClass<T> wrap( QualifierExtractor extractor, Class<T> targetClazz ) {
		return new ProvidableClass<T>(
				targetClazz,
				readClassProvidableFields( extractor, targetClazz ),
				readPostConstructor(targetClazz));
	}

	static Consumer<Object> readPostConstructor( Class<?> targetClazz ){
		Method postConstructor = null;
		for ( final Method method : targetClazz.getMethods() )
			if ( method.isAnnotationPresent(PostConstruct.class) ){
				postConstructor = method;
				break;
			}
		return postConstructor != null
			? new PostConstructorMethod(postConstructor)
			: EmptyMethod.INSTANCE;
	}

	static Iterable<ProvidableField> readClassProvidableFields( QualifierExtractor extractor, Class<?> targetClazz ) {
		final List<ProvidableField> providableFields = new ArrayList<ProvidableField>();
		Class<? extends Object> clazz = targetClazz;
		while ( !Object.class.equals( clazz ) ) {
			populateWithProvidableFields( extractor, clazz, providableFields );
			if ( clazz.isAnnotationPresent(GeneratedFromStatelessService.class) )
				break;
			clazz = clazz.getSuperclass();
		}
		return providableFields;
	}

	static void populateWithProvidableFields( QualifierExtractor extractor, Class<?> targetClazz, List<ProvidableField> providableFields ) {
		for ( final Field field : targetClazz.getDeclaredFields() ){
			final Collection<Class<? extends Annotation>> qualifiers = extractQualifiersFromAvoidingNPEWhenCreatingQualifierExtractor(extractor, field);
			if ( extractor.isASingleElementProvider( field ) )
				providableFields.add( SingleElementProvidableField.from( qualifiers, field ) );
			else if ( extractor.isAManyElementsProvider( field ) )
				providableFields.add( ManyElementsProvidableField.from( qualifiers, field ) );
		}
	}

	private static Collection<Class<? extends Annotation>> extractQualifiersFromAvoidingNPEWhenCreatingQualifierExtractor(
			final QualifierExtractor extractor, final Field field)
	{
		if ( null == extractor )
			return Collections.emptyList();
		return extractor.extractQualifiersFrom(field);
	}
}

class EmptyMethod implements Consumer<Object> {

	static final Consumer<Object> INSTANCE = new EmptyMethod();

	@Override
	public void accept(Object t) {}
}

@RequiredArgsConstructor
class PostConstructorMethod implements Consumer<Object> {

	final Method method;

	@Override
	public void accept(Object target) {
		try {
			method.invoke(target);
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			throw new ServiceProviderException(e);
		}
	}
}
{'fraction_non_alphanumeric': 0.08699277376320178, 'fraction_numerical': 0.00027793218454697053, 'mean_word_length': 5.9268527430221365, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2491879', 'n_tokens_mistral': 2181, 'n_tokens_neox': 1952, 'n_words': 464}