#75 Some re-organization in Conversions etc.

This commit is contained in:
Gunnar Morling 2014-05-08 00:53:39 +02:00
parent 534344a3df
commit ac01dda742
3 changed files with 63 additions and 84 deletions

View File

@ -40,7 +40,7 @@ public abstract class AbstractJodaTypeToStringConversion extends SimpleConversio
@Override @Override
protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) { protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) {
return asSet( return asSet(
conversionContext.getTypeFactory().getType( dateTimeFormatClass() ), conversionContext.getTypeFactory().getType( JodaTimeConstants.DATE_TIME_FORMAT_FQN ),
conversionContext.getTypeFactory().getType( Locale.class ) conversionContext.getTypeFactory().getType( Locale.class )
); );
} }
@ -52,7 +52,7 @@ public abstract class AbstractJodaTypeToStringConversion extends SimpleConversio
@Override @Override
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) { protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
return asSet( conversionContext.getTypeFactory().getType( dateTimeFormatClass() ) ); return asSet( conversionContext.getTypeFactory().getType( JodaTimeConstants.DATE_TIME_FORMAT_FQN ) );
} }
private String conversionString(ConversionContext conversionContext, String method) { private String conversionString(ConversionContext conversionContext, String method) {
@ -90,14 +90,4 @@ public abstract class AbstractJodaTypeToStringConversion extends SimpleConversio
protected abstract String formatStyle(); protected abstract String formatStyle();
protected abstract String parseMethod(); protected abstract String parseMethod();
private Class dateTimeFormatClass() {
try {
return Class.forName( "org.joda.time.format.DateTimeFormat" );
}
catch ( ClassNotFoundException e ) {
throw new RuntimeException( "org.joda.time.format.DateTimeFormat not found on classpath" );
}
}
} }

View File

@ -18,21 +18,20 @@
*/ */
package org.mapstruct.ap.conversion; package org.mapstruct.ap.conversion;
import static org.mapstruct.ap.conversion.ReverseConversion.reverse;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import javax.lang.model.util.Elements; import javax.lang.model.util.Elements;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.model.common.TypeFactory; import org.mapstruct.ap.model.common.TypeFactory;
import org.mapstruct.ap.util.NativeTypes; import org.mapstruct.ap.util.NativeTypes;
import static org.mapstruct.ap.conversion.ReverseConversion.reverse;
/** /**
* Holds built-in {@link ConversionProvider}s such as from {@code int} to {@code String}. * Holds built-in {@link ConversionProvider}s such as from {@code int} to {@code String}.
* *
@ -189,82 +188,61 @@ public class Conversions {
} }
private void registerJoda() { private void registerJoda() {
if ( isJodaTimeAvailable() ) { if ( !isJodaTimeAvailable() ) {
// joda to string return;
tryRegisterClassByName( }
JodaTimeConstants.DATE_TIME_FQN,
// Joda to String
register(
getClass( JodaTimeConstants.DATE_TIME_FQN ),
String.class, String.class,
new JodaDateTimeToStringConversion() new JodaDateTimeToStringConversion()
); );
tryRegisterClassByName( register(
JodaTimeConstants.LOCAL_DATE_FQN, getClass( JodaTimeConstants.LOCAL_DATE_FQN ),
String.class, String.class,
new JodaLocalDateToStringConversion() new JodaLocalDateToStringConversion()
); );
tryRegisterClassByName( register(
JodaTimeConstants.LOCAL_DATE_TIME_FQN, getClass( JodaTimeConstants.LOCAL_DATE_TIME_FQN ),
String.class, String.class,
new JodaLocalDateTimeToStringConversion() new JodaLocalDateTimeToStringConversion()
); );
tryRegisterClassByName( register(
JodaTimeConstants.LOCAL_TIME_FQN, getClass( JodaTimeConstants.LOCAL_TIME_FQN ),
String.class, String.class,
new JodaLocalTimeToStringConversion() new JodaLocalTimeToStringConversion()
); );
// joda to date // Joda to Date
tryRegisterClassByName( register(
JodaTimeConstants.DATE_TIME_FQN, getClass( JodaTimeConstants.DATE_TIME_FQN ),
Date.class, Date.class,
new JodaTimeToDateConversion() new JodaTimeToDateConversion()
); );
tryRegisterClassByName( register(
JodaTimeConstants.LOCAL_DATE_FQN, getClass( JodaTimeConstants.LOCAL_DATE_FQN ),
Date.class, Date.class,
new JodaTimeToDateConversion() new JodaTimeToDateConversion()
); );
tryRegisterClassByName( register(
JodaTimeConstants.LOCAL_DATE_TIME_FQN, getClass( JodaTimeConstants.LOCAL_DATE_TIME_FQN ),
Date.class, Date.class,
new JodaTimeToDateConversion() new JodaTimeToDateConversion()
); );
tryRegisterClassByName( // Joda to Calendar
JodaTimeConstants.DATE_TIME_FQN, register(
getClass( JodaTimeConstants.DATE_TIME_FQN ),
Calendar.class, Calendar.class,
new JodaTimeToCalendarConversion() new JodaTimeToCalendarConversion()
); );
} }
}
private static boolean isJodaTimeAvailable() { private static boolean isJodaTimeAvailable() {
return NativeTypes.isTypeAvailable( JodaTimeConstants.DATE_TIME_FQN ); return NativeTypes.isTypeAvailable( JodaTimeConstants.DATE_TIME_FQN );
} }
/**
* Invokes
* <ol>
* <li>{@link Class#forName(String)} </li>
* <li>{@link org.mapstruct.ap.conversion.Conversions#register(Class, Class, ConversionProvider)} </li>
* </ol>
* with the instance returned from 1.
*
* @param fullQualifiedClassName Name of type that should be reigstered
* @param target Target for {@link Conversions#register(Class, Class, ConversionProvider)}
* @param conversionProvider conversionProvider for
* {@link Conversions#register(Class, Class, ConversionProvider)}
*/
private void tryRegisterClassByName(String fullQualifiedClassName, Class<?> target,
ConversionProvider conversionProvider) {
try {
Class<?> classByName = Class.forName( fullQualifiedClassName );
register( classByName, target, conversionProvider );
}
catch ( ClassNotFoundException e ) {
throw new RuntimeException( e ); // rethrow exception?
}
}
private void registerNativeTypeConversion(Class<?> sourceType, Class<?> targetType) { private void registerNativeTypeConversion(Class<?> sourceType, Class<?> targetType) {
if ( sourceType.isPrimitive() && targetType.isPrimitive() ) { if ( sourceType.isPrimitive() && targetType.isPrimitive() ) {
register( sourceType, targetType, new PrimitiveToPrimitiveConversion( sourceType ) ); register( sourceType, targetType, new PrimitiveToPrimitiveConversion( sourceType ) );
@ -330,6 +308,15 @@ public class Conversions {
return new Key( sourceType, targetType ); return new Key( sourceType, targetType );
} }
private Class<?> getClass(String fullyQualifiedName) {
try {
return Conversions.class.getClassLoader().loadClass( fullyQualifiedName );
}
catch ( ClassNotFoundException e ) {
throw new RuntimeException( "Couldn't load class: " + fullyQualifiedName, e );
}
}
private static class Key { private static class Key {
private final Type sourceType; private final Type sourceType;
private final Type targetType; private final Type targetType;

View File

@ -34,5 +34,7 @@ public final class JodaTimeConstants {
public static final String LOCAL_TIME_FQN = "org.joda.time.LocalTime"; public static final String LOCAL_TIME_FQN = "org.joda.time.LocalTime";
public static final String DATE_TIME_FORMAT_FQN = "org.joda.time.format.DateTimeFormat";
public static final String DATE_TIME_FORMAT = "LL"; public static final String DATE_TIME_FORMAT = "LL";
} }