#75 Adapting to changed model types after rebase

This commit is contained in:
Gunnar Morling 2014-05-08 00:06:56 +02:00
parent 080689b345
commit 565cc3b8d5
3 changed files with 56 additions and 57 deletions

View File

@ -18,51 +18,49 @@
*/ */
package org.mapstruct.ap.conversion; package org.mapstruct.ap.conversion;
import org.mapstruct.ap.model.TypeConversion; import java.util.Locale;
import java.util.Set;
import org.mapstruct.ap.model.common.ConversionContext; import org.mapstruct.ap.model.common.ConversionContext;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
import org.mapstruct.ap.util.Strings; import org.mapstruct.ap.util.Strings;
import java.util.Collections;
import java.util.Locale;
import static org.mapstruct.ap.util.Collections.asSet; import static org.mapstruct.ap.util.Collections.asSet;
/** /**
* *
*/ */
public abstract class AbstractJodaTypeToStringConversion implements ConversionProvider { public abstract class AbstractJodaTypeToStringConversion extends SimpleConversion {
public TypeConversion to(String sourceReference, ConversionContext conversionContext) { @Override
return new TypeConversion( protected String getToExpression(ConversionContext conversionContext) {
asSet( return conversionString( conversionContext, "print" );
conversionContext.getTypeFactory().getType( dateTimeFormatClass() ), }
conversionContext.getTypeFactory().getType( Locale.class ) ),
Collections.<Type>emptyList(), @Override
conversionString( sourceReference, conversionContext, "print" ) protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) {
return asSet(
conversionContext.getTypeFactory().getType( dateTimeFormatClass() ),
conversionContext.getTypeFactory().getType( Locale.class )
); );
} }
public TypeConversion from(String targetReference, ConversionContext conversionContext) { @Override
return new TypeConversion( protected String getFromExpression(ConversionContext conversionContext) {
asSet( return conversionString( conversionContext, parseMethod() );
conversionContext.getTypeFactory().getType( dateTimeFormatClass() ) ),
Collections.<Type>emptyList(),
conversionString(
targetReference,
conversionContext,
parseMethod() )
);
} }
private String conversionString(String reference, ConversionContext conversionContext, String method) { @Override
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
return asSet( conversionContext.getTypeFactory().getType( dateTimeFormatClass() ) );
}
private String conversionString(ConversionContext conversionContext, String method) {
StringBuilder conversionString = new StringBuilder( "DateTimeFormat" ); StringBuilder conversionString = new StringBuilder( "DateTimeFormat" );
conversionString.append( dateFormatPattern( conversionContext ) ); conversionString.append( dateFormatPattern( conversionContext ) );
conversionString.append( "." ); conversionString.append( "." );
conversionString.append( method ); conversionString.append( method );
conversionString.append( "( " ); conversionString.append( "( <SOURCE> )" );
conversionString.append( reference );
conversionString.append( " )" );
return conversionString.toString(); return conversionString.toString();
} }
@ -98,7 +96,7 @@ public abstract class AbstractJodaTypeToStringConversion implements ConversionPr
return Class.forName( "org.joda.time.format.DateTimeFormat" ); return Class.forName( "org.joda.time.format.DateTimeFormat" );
} }
catch ( ClassNotFoundException e ) { catch ( ClassNotFoundException e ) {
throw new RuntimeException( "org.joda.time.format.DateTimeFormat not found on classpath" ); throw new RuntimeException( "org.joda.time.format.DateTimeFormat not found on classpath" );
} }
} }

View File

@ -18,34 +18,36 @@
*/ */
package org.mapstruct.ap.conversion; package org.mapstruct.ap.conversion;
import org.mapstruct.ap.model.TypeConversion; import java.util.Locale;
import java.util.Set;
import org.mapstruct.ap.model.common.ConversionContext; import org.mapstruct.ap.model.common.ConversionContext;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
import java.util.Collections;
import java.util.Locale;
import static org.mapstruct.ap.util.Collections.asSet; import static org.mapstruct.ap.util.Collections.asSet;
/** /**
* *
*/ */
public class JodaTimeToCalendarConversion implements ConversionProvider { public class JodaTimeToCalendarConversion extends SimpleConversion {
@Override @Override
public TypeConversion to(String sourceReference, ConversionContext conversionContext) { protected String getToExpression(ConversionContext conversionContext) {
return new TypeConversion( return "<SOURCE>.toCalendar( Locale.getDefault() )";
asSet( conversionContext.getTypeFactory().getType( Locale.class ) ),
Collections.<Type>emptyList(),
sourceReference + ".toCalendar( Locale.getDefault() )" );
} }
@Override @Override
public TypeConversion from(String targetReference, ConversionContext conversionContext) { protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) {
return new TypeConversion( return asSet( conversionContext.getTypeFactory().getType( Locale.class ) );
Collections.<Type>emptySet(), }
Collections.<Type>emptyList(),
"new " + conversionContext.getTargetType().getFullyQualifiedName() + "( " + targetReference @Override
+ " )" ); protected String getFromExpression(ConversionContext conversionContext) {
return "new " + conversionContext.getTargetType().getName() + "( <SOURCE> )";
}
@Override
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
return asSet( conversionContext.getTargetType() );
} }
} }

View File

@ -18,11 +18,12 @@
*/ */
package org.mapstruct.ap.conversion; package org.mapstruct.ap.conversion;
import org.mapstruct.ap.model.TypeConversion; import java.util.Set;
import org.mapstruct.ap.model.common.ConversionContext; import org.mapstruct.ap.model.common.ConversionContext;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
import java.util.Collections; import static org.mapstruct.ap.util.Collections.asSet;
/** /**
* Implementation of {@link org.mapstruct.ap.conversion.ConversionProvider} mapping Joda Types * Implementation of {@link org.mapstruct.ap.conversion.ConversionProvider} mapping Joda Types
@ -34,22 +35,20 @@ import java.util.Collections;
* to java.util.Date by invoking org.joda.time.base.AbstractInstant#toDate(). * to java.util.Date by invoking org.joda.time.base.AbstractInstant#toDate().
* Backward conversion is done. * Backward conversion is done.
*/ */
public class JodaTimeToDateConversion implements ConversionProvider { public class JodaTimeToDateConversion extends SimpleConversion {
@Override @Override
public TypeConversion to(String sourceReference, ConversionContext conversionContext) { protected String getToExpression(ConversionContext conversionContext) {
return new TypeConversion( return "<SOURCE>.toDate()";
Collections.<Type>emptySet(),
Collections.<Type>emptyList(),
sourceReference + ".toDate()" );
} }
@Override @Override
public TypeConversion from(String targetReference, ConversionContext conversionContext) { protected String getFromExpression(ConversionContext conversionContext) {
return new TypeConversion( return "new " + conversionContext.getTargetType().getName() + "( <SOURCE> )";
Collections.<Type>emptySet(), }
Collections.<Type>emptyList(),
"new " + conversionContext.getTargetType().getFullyQualifiedName() + "( " + targetReference @Override
+ " )" ); protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
return asSet( conversionContext.getTargetType() );
} }
} }