
org.cyclopsgroup.caff.conversion.AnnotatedConverter Maven / Gradle / Ivy
package org.cyclopsgroup.caff.conversion;
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
/**
* Converter that converts based on rules defined in annotation
*
* @author Jiaqi Guo
* @param Type of value to converter from/to
*/
public class AnnotatedConverter
implements Converter
{
private static class Builder
{
private Annotation annotation;
private Converter toConverter( Class type )
{
if ( annotation == null )
{
throw new NullPointerException( "Input annotation can't be NULL" );
}
ConversionSupport support = annotation.annotationType().getAnnotation( ConversionSupport.class );
if ( support == null )
{
throw new IllegalArgumentException( "Annotation " + annotation + " is not annotated with "
+ ConversionSupport.class );
}
try
{
return support.factoryType().newInstance().getConverterFor( type, annotation );
}
catch ( InstantiationException e )
{
throw new RuntimeException( "Can't create converter for " + annotation, e );
}
catch ( IllegalAccessException e )
{
throw new RuntimeException( "Can't create converter for " + annotation, e );
}
}
private Builder withAccess( AccessibleObject access )
{
for ( Annotation a : access.getAnnotations() )
{
if ( a.annotationType().isAnnotationPresent( ConversionSupport.class ) )
{
annotation = a;
return this;
}
}
throw new IllegalArgumentException( "Access " + access + " isn't annotated with any conversion annotation" );
}
private Builder withAnnotation( Annotation annotation )
{
this.annotation = annotation;
return this;
}
}
/**
* Verify if an access point marked as convertable
*
* @param access Accessible object to verify
* @return True if it's marked
*/
public static boolean isConversionSupported( AccessibleObject access )
{
for ( Annotation a : access.getAnnotations() )
{
if ( a.annotationType().isAnnotationPresent( ConversionSupport.class ) )
{
return true;
}
}
return false;
}
private final Converter proxy;
/**
* @param type Type to convert from/to
* @param access Access object where conversion annotation is placed
*/
public AnnotatedConverter( Class type, AccessibleObject access )
{
this.proxy = new Builder().withAccess( access ).toConverter( type );
}
/**
* @param type Value type to convert from/to
* @param annotation Annotation that defines conversion rule
*/
public AnnotatedConverter( Class type, Annotation annotation )
{
this.proxy = new Builder().withAnnotation( annotation ).toConverter( type );
}
/**
* @inheritDoc
*/
public T fromCharacters( CharSequence text )
{
return proxy.fromCharacters( text );
}
/**
* @inheritDoc
*/
public CharSequence toCharacters( T value )
{
return proxy.toCharacters( value );
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy