Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
package org.mapstruct.ap.internal.conversion;

import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Set;

import org.mapstruct.ap.internal.model.common.ConversionContext;
import org.mapstruct.ap.internal.model.common.FieldReference;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.util.Collections;
import org.mapstruct.ap.internal.util.Strings;
Expand Down Expand Up @@ -38,10 +40,8 @@ protected String getToExpression(ConversionContext conversionContext) {
}

private String dateTimeFormatter(ConversionContext conversionContext) {
if ( !Strings.isEmpty( conversionContext.getDateFormat() ) ) {
return ConversionUtils.dateTimeFormatter( conversionContext )
+ ".ofPattern( \"" + conversionContext.getDateFormat()
+ "\" )";
if ( Strings.isNotEmpty( conversionContext.getDateFormat() ) ) {
return GetDateTimeFormatterField.getDateTimeFormatterFieldName( conversionContext.getDateFormat() );
}
else {
return ConversionUtils.dateTimeFormatter( conversionContext ) + "." + defaultFormatterSuffix();
Expand Down Expand Up @@ -88,4 +88,15 @@ protected Set<Type> getFromConversionImportTypes(ConversionContext conversionCon
return Collections.asSet( conversionContext.getTargetType() );
}

@Override
public List<FieldReference> getRequiredHelperFields(ConversionContext conversionContext) {
if ( Strings.isNotEmpty( conversionContext.getDateFormat() ) ) {
return java.util.Collections.singletonList(
new GetDateTimeFormatterField(
conversionContext.getTypeFactory(),
conversionContext.getDateFormat() ) );
}

return super.getRequiredHelperFields( conversionContext );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
*/
package org.mapstruct.ap.internal.conversion;

import java.util.Collections;
import java.util.List;

import org.mapstruct.ap.internal.model.HelperMethod;
import org.mapstruct.ap.internal.model.TypeConversion;
import org.mapstruct.ap.internal.model.common.Assignment;
import org.mapstruct.ap.internal.model.common.ConversionContext;
import org.mapstruct.ap.internal.model.HelperMethod;
import org.mapstruct.ap.internal.model.common.FieldReference;

/**
* Implementations create inline {@link TypeConversion}s such as
Expand Down Expand Up @@ -49,4 +52,13 @@ public interface ConversionProvider {
*/
List<HelperMethod> getRequiredHelperMethods(ConversionContext conversionContext);

/**
* @param conversionContext ConversionContext providing optional information required for creating the conversion.
*
* @return any fields when required.
*/
default List<FieldReference> getRequiredHelperFields(ConversionContext conversionContext) {
return Collections.emptyList();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.internal.conversion;

import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import org.mapstruct.ap.internal.model.common.FieldReference;
import org.mapstruct.ap.internal.model.common.FinalField;
import org.mapstruct.ap.internal.model.common.TypeFactory;

public class GetDateTimeFormatterField extends FinalField implements FieldReference {

private final String dateFormat;

public GetDateTimeFormatterField(TypeFactory typeFactory, String dateFormat) {
super( typeFactory.getType( DateTimeFormatter.class ), getDateTimeFormatterFieldName( dateFormat ) );
this.dateFormat = dateFormat;
}

@Override
public Map<String, Object> getTemplateParameter() {
Map<String, Object> parameter = new HashMap<>();
parameter.put( "dateFormat", dateFormat );
return parameter;
}

public static String getDateTimeFormatterFieldName(String dateFormat) {
StringBuilder sb = new StringBuilder();
sb.append( "dateTimeFormatter_" );

dateFormat.codePoints().forEach( cp -> {
if ( Character.isJavaIdentifierPart( cp ) ) {
// safe to character to method name as is
sb.append( Character.toChars( cp ) );
}
else {
// could not be used in method name
sb.append( "_" );
}
} );

sb.append( "_" );

int hashCode = dateFormat.hashCode();
sb.append( hashCode < 0 ? "0" : "1" );
sb.append( Math.abs( hashCode ) );

return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.lang.model.element.ExecutableElement;

import org.mapstruct.ap.internal.model.common.Accessibility;
import org.mapstruct.ap.internal.model.common.ConstructorFragment;
import org.mapstruct.ap.internal.model.common.ConversionContext;
import org.mapstruct.ap.internal.model.common.FieldReference;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.source.Method;
Expand Down Expand Up @@ -216,7 +220,7 @@ public List<String> getParameterNames() {

@Override
public boolean overridesMethod() {
return false;
return false;
}

@Override
Expand Down Expand Up @@ -248,4 +252,19 @@ public boolean isLifecycleCallbackMethod() {
public boolean isUpdateMethod() {
return false; // irrelevant
}

public FieldReference getFieldReference() {
return null;
}

public ConstructorFragment getConstructorFragment() {
return null;
}

/**
* @return additional template parameters
*/
public Map<String, Object> getTemplateParameter() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.TypeElement;
import org.mapstruct.ap.internal.util.ElementUtils;
import org.mapstruct.ap.internal.util.TypeUtils;

import org.mapstruct.ap.internal.model.common.Assignment;
import org.mapstruct.ap.internal.model.common.FormattingParameters;
Expand All @@ -28,8 +26,10 @@
import org.mapstruct.ap.internal.model.source.selector.SelectionCriteria;
import org.mapstruct.ap.internal.option.Options;
import org.mapstruct.ap.internal.util.AccessorNamingUtils;
import org.mapstruct.ap.internal.util.ElementUtils;
import org.mapstruct.ap.internal.util.FormattingMessager;
import org.mapstruct.ap.internal.util.Services;
import org.mapstruct.ap.internal.util.TypeUtils;
import org.mapstruct.ap.spi.EnumMappingStrategy;
import org.mapstruct.ap.spi.EnumTransformationStrategy;
import org.mapstruct.ap.spi.MappingExclusionProvider;
Expand Down Expand Up @@ -101,6 +101,8 @@ Assignment getTargetAssignment(Method mappingMethod, ForgedMethodHistory descrip
Supplier<Assignment> forger);

Set<SupportingMappingMethod> getUsedSupportedMappings();

Set<Field> getUsedSupportedFields();
}

private final TypeFactory typeFactory;
Expand Down Expand Up @@ -241,6 +243,10 @@ public Set<SupportingMappingMethod> getUsedSupportedMappings() {
return mappingResolver.getUsedSupportedMappings();
}

public Set<Field> getUsedSupportedFields() {
return mappingResolver.getUsedSupportedFields();
}

/**
* @param sourceType from which an automatic sub-mapping needs to be generated
* @param targetType to which an automatic sub-mapping needs to be generated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import java.util.Objects;
import java.util.Set;

import org.mapstruct.ap.internal.model.common.ConstructorFragment;
import org.mapstruct.ap.internal.model.common.ModelElement;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.source.builtin.BuiltInConstructorFragment;

/**
* A mapper instance field, initialized as null
Expand All @@ -20,13 +20,15 @@
*/
public class SupportingConstructorFragment extends ModelElement {

private final String variableName;
private final String templateName;
private final SupportingMappingMethod definingMethod;

public SupportingConstructorFragment(SupportingMappingMethod definingMethod,
BuiltInConstructorFragment constructorFragment) {
ConstructorFragment constructorFragment, String variableName) {
this.templateName = getTemplateNameForClass( constructorFragment.getClass() );
this.definingMethod = definingMethod;
this.variableName = variableName;
}

@Override
Expand All @@ -43,10 +45,15 @@ public SupportingMappingMethod getDefiningMethod() {
return definingMethod;
}

public String getVariableName() {
return variableName;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ( ( variableName == null ) ? 0 : variableName.hashCode() );
result = prime * result + ( ( templateName == null ) ? 0 : templateName.hashCode() );
return result;
}
Expand All @@ -64,10 +71,12 @@ public boolean equals(Object obj) {
}
SupportingConstructorFragment other = (SupportingConstructorFragment) obj;

if ( !Objects.equals( variableName, other.variableName ) ) {
return false;
}
if ( !Objects.equals( templateName, other.templateName ) ) {
return false;
}

return true;
}

Expand All @@ -80,4 +89,17 @@ public static void addAllFragmentsIn(Set<SupportingMappingMethod> supportingMapp
}
}
}

public static SupportingConstructorFragment getSafeConstructorFragment(SupportingMappingMethod method,
ConstructorFragment fragment,
Field supportingField) {
if ( fragment == null ) {
return null;
}

return new SupportingConstructorFragment(
method,
fragment,
supportingField != null ? supportingField.getVariableName() : null );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
*/
package org.mapstruct.ap.internal.model;

import java.util.Objects;
import java.util.Map;
import java.util.Set;

import org.mapstruct.ap.internal.model.source.builtin.BuiltInFieldReference;
import org.mapstruct.ap.internal.model.common.FieldReference;
import org.mapstruct.ap.internal.util.Strings;

/**
* supports the
Expand All @@ -18,11 +19,14 @@
public class SupportingField extends Field {

private final String templateName;
private final Map<String, Object> templateParameter;

private final SupportingMappingMethod definingMethod;

public SupportingField(SupportingMappingMethod definingMethod, BuiltInFieldReference fieldReference, String name) {
public SupportingField(SupportingMappingMethod definingMethod, FieldReference fieldReference, String name) {
super( fieldReference.getType(), name, true );
this.templateName = getTemplateNameForClass( fieldReference.getClass() );
this.templateParameter = fieldReference.getTemplateParameter();
this.definingMethod = definingMethod;
}

Expand All @@ -31,36 +35,12 @@ public String getTemplateName() {
return templateName;
}

public SupportingMappingMethod getDefiningMethod() {
return definingMethod;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ( ( templateName == null ) ? 0 : templateName.hashCode() );
return result;
public Map<String, Object> getTemplateParameter() {
return templateParameter;
}

@Override
public boolean equals(Object obj) {
if ( this == obj ) {
return true;
}
if ( obj == null ) {
return false;
}
if ( getClass() != obj.getClass() ) {
return false;
}
SupportingField other = (SupportingField) obj;

if ( !Objects.equals( templateName, other.templateName ) ) {
return false;
}

return true;
public SupportingMappingMethod getDefiningMethod() {
return definingMethod;
}

public static void addAllFieldsIn(Set<SupportingMappingMethod> supportingMappingMethods, Set<Field> targets) {
Expand All @@ -71,4 +51,21 @@ public static void addAllFieldsIn(Set<SupportingMappingMethod> supportingMapping
}
}
}

public static Field getSafeField(SupportingMappingMethod method, FieldReference ref, Set<Field> existingFields) {
if ( ref == null ) {
return null;
}

String name = ref.getVariableName();
for ( Field existingField : existingFields ) {
if ( existingField.getVariableName().equals( ref.getVariableName() )
&& existingField.getType().equals( ref.getType() ) ) {
// field already exists, use that one
return existingField;
}
}
name = Strings.getSafeVariableName( name, Field.getFieldNames( existingFields ) );
return new SupportingField( method, ref, name );
}
}
Loading