#200 Simplifying definition of conversions

This commit is contained in:
Gunnar Morling 2014-04-27 19:45:40 +02:00
parent f58b26b519
commit 0656d74b7e
20 changed files with 116 additions and 178 deletions

View File

@ -35,13 +35,13 @@ import static org.mapstruct.ap.util.Collections.asSet;
public class BigDecimalToBigIntegerConversion extends SimpleConversion { public class BigDecimalToBigIntegerConversion extends SimpleConversion {
@Override @Override
public ConversionExpression getToExpression(ConversionContext conversionContext) { public String getToExpression(ConversionContext conversionContext) {
return new ConversionExpression( "", ".toBigInteger()" ); return "<SOURCE>.toBigInteger()";
} }
@Override @Override
public ConversionExpression getFromExpression(ConversionContext conversionContext) { public String getFromExpression(ConversionContext conversionContext) {
return new ConversionExpression("new BigDecimal( ", " )"); return "new BigDecimal( <SOURCE> )";
} }
@Override @Override

View File

@ -44,13 +44,13 @@ public class BigDecimalToPrimitiveConversion extends SimpleConversion {
} }
@Override @Override
public ConversionExpression getToExpression(ConversionContext conversionContext) { public String getToExpression(ConversionContext conversionContext) {
return new ConversionExpression( "", "." + targetType.getName() + "Value()" ); return "<SOURCE>." + targetType.getName() + "Value()";
} }
@Override @Override
public ConversionExpression getFromExpression(ConversionContext conversionContext) { public String getFromExpression(ConversionContext conversionContext) {
return new ConversionExpression( "BigDecimal.valueOf( ", " )" ); return "BigDecimal.valueOf( <SOURCE> )";
} }
@Override @Override

View File

@ -34,13 +34,13 @@ import static org.mapstruct.ap.util.Collections.asSet;
public class BigDecimalToStringConversion extends SimpleConversion { public class BigDecimalToStringConversion extends SimpleConversion {
@Override @Override
public ConversionExpression getToExpression(ConversionContext conversionContext) { public String getToExpression(ConversionContext conversionContext) {
return new ConversionExpression( "", ".toString()" ); return "<SOURCE>.toString()";
} }
@Override @Override
public ConversionExpression getFromExpression(ConversionContext conversionContext) { public String getFromExpression(ConversionContext conversionContext) {
return new ConversionExpression( "new BigDecimal( ", " )" ); return "new BigDecimal( <SOURCE> )";
} }
@Override @Override

View File

@ -45,13 +45,13 @@ public class BigDecimalToWrapperConversion extends SimpleConversion {
} }
@Override @Override
public ConversionExpression getToExpression(ConversionContext conversionContext) { public String getToExpression(ConversionContext conversionContext) {
return new ConversionExpression( "", "." + targetType.getName() + "Value()" ); return "<SOURCE>." + targetType.getName() + "Value()";
} }
@Override @Override
public ConversionExpression getFromExpression(ConversionContext conversionContext) { public String getFromExpression(ConversionContext conversionContext) {
return new ConversionExpression( "BigDecimal.valueOf( ", " )" ); return "BigDecimal.valueOf( <SOURCE> )";
} }
@Override @Override

View File

@ -44,17 +44,17 @@ public class BigIntegerToPrimitiveConversion extends SimpleConversion {
} }
@Override @Override
public ConversionExpression getToExpression(ConversionContext conversionContext) { public String getToExpression(ConversionContext conversionContext) {
return new ConversionExpression( "", "." + targetType.getName() + "Value()" ); return "<SOURCE>." + targetType.getName() + "Value()";
} }
@Override @Override
public ConversionExpression getFromExpression(ConversionContext conversionContext) { public String getFromExpression(ConversionContext conversionContext) {
String castString = ""; String castString = "";
if ( targetType == float.class || targetType == double.class ) { if ( targetType == float.class || targetType == double.class ) {
castString = "(long) "; castString = "(long) ";
} }
return new ConversionExpression( "BigInteger.valueOf( " + castString, " )" ); return "BigInteger.valueOf( " + castString + "<SOURCE> )";
} }
@Override @Override

View File

@ -34,13 +34,13 @@ import static org.mapstruct.ap.util.Collections.asSet;
public class BigIntegerToStringConversion extends SimpleConversion { public class BigIntegerToStringConversion extends SimpleConversion {
@Override @Override
public ConversionExpression getToExpression(ConversionContext conversionContext) { public String getToExpression(ConversionContext conversionContext) {
return new ConversionExpression( "", ".toString()" ); return "<SOURCE>.toString()";
} }
@Override @Override
public ConversionExpression getFromExpression(ConversionContext conversionContext) { public String getFromExpression(ConversionContext conversionContext) {
return new ConversionExpression( "new BigInteger( ", " )" ); return "new BigInteger( <SOURCE> )";
} }
@Override @Override

View File

@ -45,19 +45,18 @@ public class BigIntegerToWrapperConversion extends SimpleConversion {
} }
@Override @Override
public ConversionExpression getToExpression(ConversionContext conversionContext) { public String getToExpression(ConversionContext conversionContext) {
return new ConversionExpression( "", "." + targetType.getName() + "Value()" ); return "<SOURCE>." + targetType.getName() + "Value()";
} }
@Override @Override
public ConversionExpression getFromExpression(ConversionContext conversionContext) { public String getFromExpression(ConversionContext conversionContext) {
String toLongValueStr = ""; String toLongValueStr = "";
if ( targetType == float.class || targetType == double.class ) { if ( targetType == float.class || targetType == double.class ) {
toLongValueStr = ".longValue()"; toLongValueStr = ".longValue()";
} }
return new ConversionExpression( "BigInteger.valueOf( ", toLongValueStr + " )" ); return "BigInteger.valueOf( <SOURCE>" + toLongValueStr + " )";
} }
@Override @Override

View File

@ -28,12 +28,12 @@ import org.mapstruct.ap.model.common.ConversionContext;
public class CharToStringConversion extends SimpleConversion { public class CharToStringConversion extends SimpleConversion {
@Override @Override
public ConversionExpression getToExpression(ConversionContext conversionContext) { public String getToExpression(ConversionContext conversionContext) {
return new ConversionExpression( "String.valueOf( ", " )" ); return "String.valueOf( <SOURCE> )";
} }
@Override @Override
public ConversionExpression getFromExpression(ConversionContext conversionContext) { public String getFromExpression(ConversionContext conversionContext) {
return new ConversionExpression( "", ".charAt( 0 )" ); return "<SOURCE>.charAt( 0 )";
} }
} }

View File

@ -28,12 +28,12 @@ import org.mapstruct.ap.model.common.ConversionContext;
public class CharWrapperToStringConversion extends SimpleConversion { public class CharWrapperToStringConversion extends SimpleConversion {
@Override @Override
public ConversionExpression getToExpression(ConversionContext conversionContext) { public String getToExpression(ConversionContext conversionContext) {
return new ConversionExpression( "", ".toString()" ); return "<SOURCE>.toString()";
} }
@Override @Override
public ConversionExpression getFromExpression(ConversionContext conversionContext) { public String getFromExpression(ConversionContext conversionContext) {
return new ConversionExpression( "", ".charAt( 0 )" ); return "<SOURCE>.charAt( 0 )";
} }
} }

View File

@ -1,56 +0,0 @@
/**
* Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.conversion;
/**
*
* @author Sjaak Derksen
*/
public class ConversionExpression {
private final String openExpression;
private final String closeExpression;
private String expression;
public static ConversionExpression empty() {
return new ConversionExpression( "", "" );
}
public ConversionExpression( String openExpression, String closeExpression ) {
this.openExpression = openExpression;
this.closeExpression = closeExpression;
}
public String getOpenExpression() {
return openExpression;
}
public String getCloseExpression() {
return closeExpression;
}
public String getExpression() {
return expression;
}
public void setExpression( String expression ) {
this.expression = expression;
}
}

View File

@ -22,13 +22,14 @@ import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import org.mapstruct.ap.model.Assignment; import org.mapstruct.ap.model.Assignment;
import org.mapstruct.ap.model.assignment.AssignmentFactory; import org.mapstruct.ap.model.assignment.AssignmentFactory;
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 static org.mapstruct.ap.util.Collections.asSet;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.mapstruct.ap.util.Collections.asSet;
/** /**
* Conversion between {@link String} and {@link Date}. * Conversion between {@link String} and {@link Date}.
@ -42,9 +43,8 @@ public class DateToStringConversion implements ConversionProvider {
return AssignmentFactory.createTypeConversion( return AssignmentFactory.createTypeConversion(
asSet( conversionContext.getTypeFactory().getType( SimpleDateFormat.class ) ), asSet( conversionContext.getTypeFactory().getType( SimpleDateFormat.class ) ),
Collections.<Type>emptyList(), Collections.<Type>emptyList(),
getOpenExpression( conversionContext, "format" ), getConversionExpression( conversionContext, "format" )
getCloseExpression() ); );
} }
@Override @Override
@ -52,12 +52,11 @@ public class DateToStringConversion implements ConversionProvider {
return AssignmentFactory.createTypeConversion( return AssignmentFactory.createTypeConversion(
asSet( conversionContext.getTypeFactory().getType( SimpleDateFormat.class ) ), asSet( conversionContext.getTypeFactory().getType( SimpleDateFormat.class ) ),
asList( conversionContext.getTypeFactory().getType( ParseException.class ) ), asList( conversionContext.getTypeFactory().getType( ParseException.class ) ),
getOpenExpression( conversionContext, "parse" ), getConversionExpression( conversionContext, "parse" )
getCloseExpression()
); );
} }
private String getOpenExpression(ConversionContext conversionContext, String method) { private String getConversionExpression(ConversionContext conversionContext, String method) {
StringBuilder conversionString = new StringBuilder( "new SimpleDateFormat(" ); StringBuilder conversionString = new StringBuilder( "new SimpleDateFormat(" );
if ( conversionContext.getDateFormat() != null ) { if ( conversionContext.getDateFormat() != null ) {
@ -68,12 +67,8 @@ public class DateToStringConversion implements ConversionProvider {
conversionString.append( ")." ); conversionString.append( ")." );
conversionString.append( method ); conversionString.append( method );
conversionString.append( "( " ); conversionString.append( "( <SOURCE> )" );
return conversionString.toString(); return conversionString.toString();
} }
private String getCloseExpression() {
return " )";
}
} }

View File

@ -33,15 +33,13 @@ import static org.mapstruct.ap.util.Collections.asSet;
public class EnumStringConversion extends SimpleConversion { public class EnumStringConversion extends SimpleConversion {
@Override @Override
public ConversionExpression getToExpression(ConversionContext conversionContext) { public String getToExpression(ConversionContext conversionContext) {
return new ConversionExpression("", ".toString()" ); return "<SOURCE>.toString()";
} }
@Override @Override
public ConversionExpression getFromExpression(ConversionContext conversionContext) { public String getFromExpression(ConversionContext conversionContext) {
return new ConversionExpression( return "Enum.valueOf( " + conversionContext.getTargetType().getName() + ".class, <SOURCE> )";
"Enum.valueOf( " + conversionContext.getTargetType().getName() + ".class, ",
" )" );
} }
@Override @Override

View File

@ -38,12 +38,12 @@ public class PrimitiveToPrimitiveConversion extends SimpleConversion {
} }
@Override @Override
public ConversionExpression getToExpression(ConversionContext conversionContext) { public String getToExpression(ConversionContext conversionContext) {
return ConversionExpression.empty(); return "<SOURCE>";
} }
@Override @Override
public ConversionExpression getFromExpression(ConversionContext conversionContext) { public String getFromExpression(ConversionContext conversionContext) {
return new ConversionExpression( "(" + sourceType + ") ", "" ); return "(" + sourceType + ")<SOURCE>";
} }
} }

View File

@ -43,13 +43,13 @@ public class PrimitiveToStringConversion extends SimpleConversion {
} }
@Override @Override
public ConversionExpression getToExpression(ConversionContext conversionContext) { public String getToExpression(ConversionContext conversionContext) {
return new ConversionExpression( "String.valueOf( ", " )" ); return "String.valueOf( <SOURCE> )";
} }
@Override @Override
public ConversionExpression getFromExpression(ConversionContext conversionContext) { public String getFromExpression(ConversionContext conversionContext) {
return new ConversionExpression( wrapperType.getSimpleName() + ".parse" + return wrapperType.getSimpleName() + ".parse" +
Strings.capitalize( sourceType.getSimpleName() ) + "( ", " )" ); Strings.capitalize( sourceType.getSimpleName() ) + "( <SOURCE> )";
} }
} }

View File

@ -45,17 +45,17 @@ public class PrimitiveToWrapperConversion extends SimpleConversion {
} }
@Override @Override
public ConversionExpression getToExpression(ConversionContext conversionContext) { public String getToExpression(ConversionContext conversionContext) {
if ( sourceType == targetType ) { if ( sourceType == targetType ) {
return ConversionExpression.empty(); return "<SOURCE>";
} }
else { else {
return new ConversionExpression( "(" + targetType.getName() + ") ", "" ); return "(" + targetType.getName() + ")<SOURCE>";
} }
} }
@Override @Override
public ConversionExpression getFromExpression(ConversionContext conversionContext) { public String getFromExpression(ConversionContext conversionContext) {
return new ConversionExpression( "", "." + sourceType.getName() + "Value()" ); return "<SOURCE>." + sourceType.getName() + "Value()";
} }
} }

View File

@ -20,10 +20,11 @@ package org.mapstruct.ap.conversion;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
import org.mapstruct.ap.model.Assignment; import org.mapstruct.ap.model.Assignment;
import org.mapstruct.ap.model.assignment.AssignmentFactory; import org.mapstruct.ap.model.assignment.AssignmentFactory;
import org.mapstruct.ap.model.common.ConversionContext;
import org.mapstruct.ap.model.assignment.TypeConversion; import org.mapstruct.ap.model.assignment.TypeConversion;
import org.mapstruct.ap.model.common.ConversionContext;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
/** /**
@ -35,48 +36,50 @@ public abstract class SimpleConversion implements ConversionProvider {
@Override @Override
public Assignment to(ConversionContext conversionContext) { public Assignment to(ConversionContext conversionContext) {
ConversionExpression toExpressions = getToExpression( conversionContext ); String toExpression = getToExpression( conversionContext );
return AssignmentFactory.createTypeConversion( return AssignmentFactory.createTypeConversion(
getToConversionImportTypes( conversionContext ), getToConversionImportTypes( conversionContext ),
Collections.<Type>emptyList(), Collections.<Type>emptyList(),
toExpressions.getOpenExpression(), toExpression
toExpressions.getCloseExpression() ); );
} }
@Override @Override
public Assignment from(ConversionContext conversionContext) { public Assignment from(ConversionContext conversionContext) {
ConversionExpression fromExpressions = getFromExpression( conversionContext ); String fromExpression = getFromExpression( conversionContext );
return AssignmentFactory.createTypeConversion( return AssignmentFactory.createTypeConversion(
getFromConversionImportTypes( conversionContext ), getFromConversionImportTypes( conversionContext ),
Collections.<Type>emptyList(), Collections.<Type>emptyList(),
fromExpressions.getOpenExpression(), fromExpression
fromExpressions.getCloseExpression() ); );
} }
/** /**
* Returns the conversion string (opening and closing part) from source to target. * Returns the conversion string from source to target. The placeholder {@code <SOURCE>} can be used to represent a
* reference to the source value.
*
* @param conversionContext A context providing optional information required for creating the conversion.
*
* @return The conversion string from source to target
*/
protected abstract String getToExpression(ConversionContext conversionContext);
/**
* Returns the conversion string from target to source. The placeholder {@code <SOURCE>} can be used to represent a
* reference to the target value.
* *
* @param conversionContext ConversionContext providing optional information required for creating the conversion. * @param conversionContext ConversionContext providing optional information required for creating the conversion.
* *
* @return The open- and close parts of the conversion expression * @return The conversion string from target to source
*/ */
protected abstract ConversionExpression getToExpression( ConversionContext conversionContext ); protected abstract String getFromExpression(ConversionContext conversionContext);
/**
* Creates the conversion string (opening and closing part) from target to source.
*
* @param conversionContext ConversionContext providing optional information required for creating
* the conversion.
*
* @return The open- and close parts of the conversion expression
*/
protected abstract ConversionExpression getFromExpression( ConversionContext conversionContext );
/** /**
* Returns a set with imported types of the "from" conversion. Defaults to an empty set; can be overridden in * Returns a set with imported types of the "from" conversion. Defaults to an empty set; can be overridden in
* sub-classes to return the required types. * sub-classes to return the required types.
* *
* @param conversionContext the conversion context * @param conversionContext the conversion context
*
* @return conversion types required in the "from" conversion * @return conversion types required in the "from" conversion
*/ */
protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) { protected Set<Type> getFromConversionImportTypes(ConversionContext conversionContext) {
@ -88,10 +91,10 @@ public abstract class SimpleConversion implements ConversionProvider {
* sub-classes to return the required types. * sub-classes to return the required types.
* *
* @param conversionContext the conversion context * @param conversionContext the conversion context
*
* @return conversion types required in the "to" conversion * @return conversion types required in the "to" conversion
*/ */
protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) { protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) {
return Collections.<Type>emptySet(); return Collections.<Type>emptySet();
} }
} }

View File

@ -42,13 +42,13 @@ public class WrapperToStringConversion extends SimpleConversion {
} }
@Override @Override
public ConversionExpression getToExpression(ConversionContext conversionContext) { public String getToExpression(ConversionContext conversionContext) {
return new ConversionExpression( "String.valueOf( ", " )" ); return "String.valueOf( <SOURCE> )";
} }
@Override @Override
public ConversionExpression getFromExpression(ConversionContext conversionContext) { public String getFromExpression(ConversionContext conversionContext) {
return new ConversionExpression( sourceType.getSimpleName() + ".parse" + return sourceType.getSimpleName() + ".parse" +
Strings.capitalize( primitiveType.getSimpleName() ) + "( ", " )" ); Strings.capitalize( primitiveType.getSimpleName() ) + "( <SOURCE> )";
} }
} }

View File

@ -44,22 +44,22 @@ public class WrapperToWrapperConversion extends SimpleConversion {
} }
@Override @Override
public ConversionExpression getToExpression(ConversionContext conversionContext) { public String getToExpression(ConversionContext conversionContext) {
if ( sourceType == targetType ) { if ( sourceType == targetType ) {
return ConversionExpression.empty(); return "<SOURCE>";
} }
else { else {
return new ConversionExpression( "", "." + targetType.getName() + "Value()" ); return "<SOURCE>." + targetType.getName() + "Value()";
} }
} }
@Override @Override
public ConversionExpression getFromExpression(ConversionContext conversionContext) { public String getFromExpression(ConversionContext conversionContext) {
if ( sourceType == targetType ) { if ( sourceType == targetType ) {
return ConversionExpression.empty(); return "<SOURCE>";
} }
else { else {
return new ConversionExpression( "", "." + sourceType.getName() + "Value()" ); return "<SOURCE>." + sourceType.getName() + "Value()";
} }
} }
} }

View File

@ -20,6 +20,7 @@ package org.mapstruct.ap.model.assignment;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import org.mapstruct.ap.model.Assignment; import org.mapstruct.ap.model.Assignment;
import org.mapstruct.ap.model.FactoryMethod; import org.mapstruct.ap.model.FactoryMethod;
import org.mapstruct.ap.model.MapperReference; import org.mapstruct.ap.model.MapperReference;
@ -38,28 +39,25 @@ public class AssignmentFactory {
private AssignmentFactory() { private AssignmentFactory() {
} }
public static Assignment createTypeConversion( Set<Type> importTypes, public static Assignment createTypeConversion(Set<Type> importTypes, List<Type> exceptionTypes, String expression) {
List<Type> exceptionTypes, return new TypeConversion( importTypes, exceptionTypes, expression );
String openExpression,
String closeExpression ) {
return new TypeConversion( importTypes, exceptionTypes, openExpression, closeExpression );
} }
public static FactoryMethod createFactory(SourceMethod method, MapperReference declaringMapper) { public static FactoryMethod createFactory(SourceMethod method, MapperReference declaringMapper) {
return new MethodReference( method, declaringMapper, null ); return new MethodReference( method, declaringMapper, null );
} }
public static Assignment createMethodReference(SourceMethod method, MapperReference declaringMapper, public static Assignment createMethodReference(SourceMethod method, MapperReference declaringMapper,
Type targetType) { Type targetType) {
return new MethodReference(method, declaringMapper, targetType); return new MethodReference( method, declaringMapper, targetType );
} }
public static Assignment createMethodReference( BuiltInMethod method, ConversionContext contextParam ) { public static Assignment createMethodReference(BuiltInMethod method, ConversionContext contextParam) {
return new MethodReference( method, contextParam ); return new MethodReference( method, contextParam );
} }
public static Simple createSimple( String sourceRef ) { public static Simple createSimple(String sourceRef) {
return new Simple(sourceRef ); return new Simple( sourceRef );
} }
} }

View File

@ -21,8 +21,8 @@ package org.mapstruct.ap.model.assignment;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import org.mapstruct.ap.model.Assignment;
import org.mapstruct.ap.model.Assignment;
import org.mapstruct.ap.model.common.ModelElement; import org.mapstruct.ap.model.common.ModelElement;
import org.mapstruct.ap.model.common.Type; import org.mapstruct.ap.model.common.Type;
@ -33,6 +33,7 @@ import org.mapstruct.ap.model.common.Type;
*/ */
public class TypeConversion extends ModelElement implements Assignment { public class TypeConversion extends ModelElement implements Assignment {
private static final String SOURCE_REFERENCE_PATTERN = "<SOURCE>";
private final Set<Type> importTypes; private final Set<Type> importTypes;
private final List<Type> exceptionTypes; private final List<Type> exceptionTypes;
@ -46,16 +47,16 @@ public class TypeConversion extends ModelElement implements Assignment {
*/ */
private Assignment assignment; private Assignment assignment;
TypeConversion( Set<Type> importTypes, TypeConversion( Set<Type> importTypes,
List<Type> exceptionTypes, List<Type> exceptionTypes,
String openExpression, String expression ) {
String closeExpression ) {
this.importTypes = new HashSet<Type>( importTypes ); this.importTypes = new HashSet<Type>( importTypes );
this.importTypes.addAll( exceptionTypes ); this.importTypes.addAll( exceptionTypes );
this.exceptionTypes = exceptionTypes; this.exceptionTypes = exceptionTypes;
this.openExpression = openExpression;
this.closeExpression = closeExpression; int patternIndex = expression.indexOf( SOURCE_REFERENCE_PATTERN );
this.openExpression = expression.substring( 0, patternIndex );
this.closeExpression = expression.substring( patternIndex + 8 );
} }
@Override @Override