All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kikaha.core.cdi.helpers.ProvidableClass Maven / Gradle / Ivy

package kikaha.core.cdi.helpers;

import java.lang.annotation.Annotation;
import java.lang.reflect.*;
import java.util.*;
import java.util.function.Consumer;
import javax.annotation.PostConstruct;
import kikaha.core.cdi.*;
import lombok.*;
import lombok.experimental.Accessors;

@Getter
@Accessors( fluent = true )
@RequiredArgsConstructor
public class ProvidableClass {

	final Class targetClazz;
	final Iterable fields;
	final Consumer postConstructor;

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

	static Consumer 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 readClassProvidableFields( QualifierExtractor extractor, Class targetClazz ) {
		final List providableFields = new TinyList<>();
		Class 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 providableFields ) {
		for ( final Field field : targetClazz.getDeclaredFields() ) {
			final Collection> qualifiers = extractQualifiersFromAvoidingNPEWhenCreatingQualifierExtractor( extractor, field );
			if ( extractor.isAManyElementsProvider( field ) )
				providableFields.add( ManyElementsProvidableField.from( qualifiers, field ) );
			else if ( extractor.isASingleElementProvider( field ) )
				providableFields.add( SingleElementProvidableField.from( qualifiers, field ) );
		}
	}

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

class EmptyMethod implements Consumer {

	static final Consumer INSTANCE = new EmptyMethod();

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

@RequiredArgsConstructor
class PostConstructorMethod implements Consumer {

	final Method method;

	@Override
	public void accept( Object target ) {
		try {
			method.invoke( target );
		} catch ( IllegalAccessException | IllegalArgumentException | InvocationTargetException e ) {
			throw new ServiceProviderException( "Can't call " + method.toString(), e );
		}
	}
}