mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#75 Some re-organization in Conversions etc.
This commit is contained in:
parent
534344a3df
commit
ac01dda742
@ -40,7 +40,7 @@ public abstract class AbstractJodaTypeToStringConversion extends SimpleConversio
|
||||
@Override
|
||||
protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) {
|
||||
return asSet(
|
||||
conversionContext.getTypeFactory().getType( dateTimeFormatClass() ),
|
||||
conversionContext.getTypeFactory().getType( JodaTimeConstants.DATE_TIME_FORMAT_FQN ),
|
||||
conversionContext.getTypeFactory().getType( Locale.class )
|
||||
);
|
||||
}
|
||||
@ -52,7 +52,7 @@ public abstract class AbstractJodaTypeToStringConversion extends SimpleConversio
|
||||
|
||||
@Override
|
||||
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) {
|
||||
@ -90,14 +90,4 @@ public abstract class AbstractJodaTypeToStringConversion extends SimpleConversio
|
||||
protected abstract String formatStyle();
|
||||
|
||||
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" );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,21 +18,20 @@
|
||||
*/
|
||||
package org.mapstruct.ap.conversion;
|
||||
|
||||
import static org.mapstruct.ap.conversion.ReverseConversion.reverse;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.lang.model.util.Elements;
|
||||
|
||||
import org.mapstruct.ap.model.common.Type;
|
||||
import org.mapstruct.ap.model.common.TypeFactory;
|
||||
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}.
|
||||
*
|
||||
@ -189,82 +188,61 @@ public class Conversions {
|
||||
}
|
||||
|
||||
private void registerJoda() {
|
||||
if ( isJodaTimeAvailable() ) {
|
||||
// joda to string
|
||||
tryRegisterClassByName(
|
||||
JodaTimeConstants.DATE_TIME_FQN,
|
||||
String.class,
|
||||
new JodaDateTimeToStringConversion()
|
||||
);
|
||||
tryRegisterClassByName(
|
||||
JodaTimeConstants.LOCAL_DATE_FQN,
|
||||
String.class,
|
||||
new JodaLocalDateToStringConversion()
|
||||
);
|
||||
tryRegisterClassByName(
|
||||
JodaTimeConstants.LOCAL_DATE_TIME_FQN,
|
||||
String.class,
|
||||
new JodaLocalDateTimeToStringConversion()
|
||||
);
|
||||
tryRegisterClassByName(
|
||||
JodaTimeConstants.LOCAL_TIME_FQN,
|
||||
String.class,
|
||||
new JodaLocalTimeToStringConversion()
|
||||
);
|
||||
|
||||
// joda to date
|
||||
tryRegisterClassByName(
|
||||
JodaTimeConstants.DATE_TIME_FQN,
|
||||
Date.class,
|
||||
new JodaTimeToDateConversion()
|
||||
);
|
||||
tryRegisterClassByName(
|
||||
JodaTimeConstants.LOCAL_DATE_FQN,
|
||||
Date.class,
|
||||
new JodaTimeToDateConversion()
|
||||
);
|
||||
tryRegisterClassByName(
|
||||
JodaTimeConstants.LOCAL_DATE_TIME_FQN,
|
||||
Date.class,
|
||||
new JodaTimeToDateConversion()
|
||||
);
|
||||
|
||||
tryRegisterClassByName(
|
||||
JodaTimeConstants.DATE_TIME_FQN,
|
||||
Calendar.class,
|
||||
new JodaTimeToCalendarConversion()
|
||||
);
|
||||
if ( !isJodaTimeAvailable() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Joda to String
|
||||
register(
|
||||
getClass( JodaTimeConstants.DATE_TIME_FQN ),
|
||||
String.class,
|
||||
new JodaDateTimeToStringConversion()
|
||||
);
|
||||
register(
|
||||
getClass( JodaTimeConstants.LOCAL_DATE_FQN ),
|
||||
String.class,
|
||||
new JodaLocalDateToStringConversion()
|
||||
);
|
||||
register(
|
||||
getClass( JodaTimeConstants.LOCAL_DATE_TIME_FQN ),
|
||||
String.class,
|
||||
new JodaLocalDateTimeToStringConversion()
|
||||
);
|
||||
register(
|
||||
getClass( JodaTimeConstants.LOCAL_TIME_FQN ),
|
||||
String.class,
|
||||
new JodaLocalTimeToStringConversion()
|
||||
);
|
||||
|
||||
// Joda to Date
|
||||
register(
|
||||
getClass( JodaTimeConstants.DATE_TIME_FQN ),
|
||||
Date.class,
|
||||
new JodaTimeToDateConversion()
|
||||
);
|
||||
register(
|
||||
getClass( JodaTimeConstants.LOCAL_DATE_FQN ),
|
||||
Date.class,
|
||||
new JodaTimeToDateConversion()
|
||||
);
|
||||
register(
|
||||
getClass( JodaTimeConstants.LOCAL_DATE_TIME_FQN ),
|
||||
Date.class,
|
||||
new JodaTimeToDateConversion()
|
||||
);
|
||||
|
||||
// Joda to Calendar
|
||||
register(
|
||||
getClass( JodaTimeConstants.DATE_TIME_FQN ),
|
||||
Calendar.class,
|
||||
new JodaTimeToCalendarConversion()
|
||||
);
|
||||
}
|
||||
|
||||
private static boolean isJodaTimeAvailable() {
|
||||
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) {
|
||||
if ( sourceType.isPrimitive() && targetType.isPrimitive() ) {
|
||||
register( sourceType, targetType, new PrimitiveToPrimitiveConversion( sourceType ) );
|
||||
@ -330,6 +308,15 @@ public class Conversions {
|
||||
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 final Type sourceType;
|
||||
private final Type targetType;
|
||||
|
@ -34,5 +34,7 @@ public final class JodaTimeConstants {
|
||||
|
||||
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";
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user