org.mapstruct.ap.internal.model.NestedPropertyMappingMethod Maven / Gradle / Ivy
Show all versions of mapstruct-processor Show documentation
/*
* 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.model;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import org.mapstruct.ap.internal.model.common.Parameter;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.model.beanmapping.PropertyEntry;
import org.mapstruct.ap.internal.util.Strings;
import org.mapstruct.ap.internal.util.ValueProvider;
/**
* This method is used to convert the nested properties as listed in propertyEntries into a method
* that creates a mapping from the start of this list to the end of the list.
*
* So, say that the start of the list is of TypeA and the end of the list is of TypeB than the forged method
* will create a forged mapping method: TypeB methodName( TypeA in ).
*
* @author Sjaak Derksen
*/
public class NestedPropertyMappingMethod extends MappingMethod {
private final List safePropertyEntries;
public static class Builder {
private MappingBuilderContext ctx;
private ForgedMethod method;
private List propertyEntries;
public Builder method( ForgedMethod sourceMethod ) {
this.method = sourceMethod;
return this;
}
public Builder propertyEntries( List propertyEntries ) {
this.propertyEntries = propertyEntries;
return this;
}
public Builder mappingContext(MappingBuilderContext mappingContext) {
this.ctx = mappingContext;
return this;
}
public NestedPropertyMappingMethod build() {
List existingVariableNames = new ArrayList<>();
for ( Parameter parameter : method.getSourceParameters() ) {
existingVariableNames.add( parameter.getName() );
}
final List thrownTypes = new ArrayList<>();
List safePropertyEntries = new ArrayList<>();
for ( PropertyEntry propertyEntry : propertyEntries ) {
String safeName = Strings.getSafeVariableName( propertyEntry.getName(), existingVariableNames );
safePropertyEntries.add( new SafePropertyEntry( propertyEntry, safeName ) );
existingVariableNames.add( safeName );
thrownTypes.addAll( ctx.getTypeFactory().getThrownTypes(
propertyEntry.getReadAccessor() ) );
}
method.addThrownTypes( thrownTypes );
return new NestedPropertyMappingMethod( method, safePropertyEntries );
}
}
private NestedPropertyMappingMethod( ForgedMethod method, List sourcePropertyEntries ) {
super( method );
this.safePropertyEntries = sourcePropertyEntries;
}
public Parameter getSourceParameter() {
for ( Parameter parameter : getParameters() ) {
if ( !parameter.isMappingTarget() && !parameter.isMappingContext() ) {
return parameter;
}
}
throw new IllegalStateException( "Method " + this + " has no source parameter." );
}
public List getPropertyEntries() {
return safePropertyEntries;
}
@Override
public Set getImportTypes() {
Set types = super.getImportTypes();
for ( SafePropertyEntry propertyEntry : safePropertyEntries) {
types.add( propertyEntry.getType() );
}
return types;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ( ( getReturnType() == null ) ? 0 : getReturnType().hashCode() );
return result;
}
@Override
public boolean equals( Object obj ) {
if ( this == obj ) {
return true;
}
if ( obj == null ) {
return false;
}
if ( getClass() != obj.getClass() ) {
return false;
}
NestedPropertyMappingMethod other = (NestedPropertyMappingMethod) obj;
if ( !super.equals( obj ) ) {
return false;
}
if ( getSourceParameters().size() != other.getSourceParameters().size() ) {
return false;
}
for ( int i = 0; i < getSourceParameters().size(); i++ ) {
if ( !getSourceParameters().get( i ).getType().equals( other.getSourceParameters().get( i ).getType() ) ) {
return false;
}
}
if ( !safePropertyEntries.equals( other.safePropertyEntries ) ) {
return false;
}
return true;
}
public static class SafePropertyEntry {
private final String safeName;
private final String readAccessorName;
private final String presenceCheckerName;
private final Type type;
public SafePropertyEntry(PropertyEntry entry, String safeName) {
this.safeName = safeName;
this.readAccessorName = ValueProvider.of( entry.getReadAccessor() ).getValue();
if ( entry.getPresenceChecker() != null ) {
this.presenceCheckerName = entry.getPresenceChecker().getSimpleName();
}
else {
this.presenceCheckerName = null;
}
this.type = entry.getType();
}
public String getName() {
return safeName;
}
public String getAccessorName() {
return readAccessorName;
}
public String getPresenceCheckerName() {
return presenceCheckerName;
}
public Type getType() {
return type;
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( !( o instanceof SafePropertyEntry ) ) {
return false;
}
SafePropertyEntry that = (SafePropertyEntry) o;
if ( !Objects.equals( readAccessorName, that.readAccessorName ) ) {
return false;
}
if ( !Objects.equals( presenceCheckerName, that.presenceCheckerName ) ) {
return false;
}
if ( !Objects.equals( type, that.type ) ) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = readAccessorName != null ? readAccessorName.hashCode() : 0;
result = 31 * result + ( presenceCheckerName != null ? presenceCheckerName.hashCode() : 0 );
result = 31 * result + ( type != null ? type.hashCode() : 0 );
return result;
}
}
}