forked from mapstruct/mapstruct
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mapstruct#674 Support Java 8 java.util.Optional type
- Loading branch information
1 parent
d7c0d15
commit cbec95f
Showing
11 changed files
with
348 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/OptionalToValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* 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.source.builtin; | ||
|
||
import java.util.Optional; | ||
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.common.TypeFactory; | ||
|
||
import static org.mapstruct.ap.internal.util.Collections.asSet; | ||
|
||
/** | ||
* @author Iaroslav Bogdanchikov | ||
*/ | ||
public class OptionalToValue extends BuiltInMethod { | ||
|
||
private final Parameter parameter; | ||
private final Type returnType; | ||
private final Set<Type> importTypes; | ||
|
||
public OptionalToValue(TypeFactory typeFactory) { | ||
Type type = typeFactory.getType( Optional.class ); | ||
this.parameter = new Parameter( "optional", type ); | ||
this.returnType = type.getTypeParameters().get( 0 ); | ||
this.importTypes = asSet( parameter.getType() ); | ||
} | ||
|
||
@Override | ||
public boolean doTypeVarsMatch(Type sourceType, Type targetType) { | ||
boolean match = false; | ||
if ( sourceType.getTypeParameters().size() == 1 ) { | ||
match = sourceType.getTypeParameters().get( 0 ).isAssignableTo( targetType ); | ||
} | ||
return match; | ||
} | ||
|
||
@Override | ||
public Parameter getParameter() { | ||
return parameter; | ||
} | ||
|
||
@Override | ||
public Type getReturnType() { | ||
return returnType; | ||
} | ||
|
||
@Override | ||
public Set<Type> getImportTypes() { | ||
return importTypes; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
processor/src/main/java/org/mapstruct/ap/internal/model/source/builtin/ValueToOptional.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* 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.source.builtin; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
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.common.TypeFactory; | ||
|
||
import static org.mapstruct.ap.internal.util.Collections.asSet; | ||
import static org.mapstruct.ap.internal.util.Collections.first; | ||
|
||
/** | ||
* @author Iaroslav Bogdanchikov | ||
*/ | ||
public class ValueToOptional extends BuiltInMethod { | ||
|
||
private final Parameter parameter; | ||
private final Type returnType; | ||
private final Set<Type> importTypes; | ||
|
||
public ValueToOptional(TypeFactory typeFactory) { | ||
Type type = typeFactory.getType( Optional.class ); | ||
this.parameter = new Parameter( "value", type.getTypeParameters().get(0) ); | ||
this.returnType = type; | ||
this.importTypes = asSet( parameter.getType() ); | ||
} | ||
|
||
@Override | ||
public boolean matches(final List<Type> sourceTypes, final Type targetType) { | ||
if ( sourceTypes.size() != 1 ) { | ||
return false; | ||
} | ||
|
||
Type sourceType = first( sourceTypes ); | ||
|
||
Type returnType = getReturnType().resolveParameterToType( sourceType, getParameter().getType() ).getMatch(); | ||
if ( returnType == null ) { | ||
return false; | ||
} | ||
|
||
return returnType.erasure().isAssignableTo( targetType ) && returnType != sourceType; | ||
} | ||
|
||
@Override | ||
public Parameter getParameter() { | ||
return parameter; | ||
} | ||
|
||
@Override | ||
public Type getReturnType() { | ||
return returnType; | ||
} | ||
|
||
@Override | ||
public Set<Type> getImportTypes() { | ||
return importTypes; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
processor/src/main/java/org/mapstruct/ap/internal/util/OptionalConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* 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.util; | ||
|
||
/** | ||
* Helper holding Optional fully qualified class name for conversion registration. | ||
*/ | ||
public final class OptionalConstants { | ||
|
||
public static final String OPTIONAL_FQN = "java.util.Optional"; | ||
|
||
private OptionalConstants() { | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
...sor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/OptionalToValue.ftl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<#-- | ||
Copyright MapStruct Authors. | ||
Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
--> | ||
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> | ||
private <T> T ${name}( <@includeModel object=findType("Optional") raw=true/><T> optional ) { | ||
if ( optional == null ) { | ||
return null; | ||
} | ||
|
||
return optional.orElse( null ); | ||
} |
11 changes: 11 additions & 0 deletions
11
...sor/src/main/resources/org/mapstruct/ap/internal/model/source/builtin/ValueToOptional.ftl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<#-- | ||
Copyright MapStruct Authors. | ||
Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 | ||
--> | ||
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.SupportingMappingMethod" --> | ||
private <T> Optional<T> ${name}( T value ) { | ||
return Optional.ofNullable( value ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
processor/src/test/java/org/mapstruct/ap/test/builtin/bean/SourceOptionalProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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.test.builtin.bean; | ||
|
||
import java.util.Optional; | ||
|
||
public class SourceOptionalProperty { | ||
|
||
// CHECKSTYLE:OFF | ||
public Optional<String> fromOptionalPubProp; | ||
// CHECKSTYLE:ON | ||
|
||
// CHECKSTYLE:OFF | ||
public String toOptionalPubProp; | ||
// CHECKSTYLE:ON | ||
|
||
private Optional<String> fromOptionalProp; | ||
|
||
private String toOptionalProp; | ||
|
||
private Optional<String> optional; | ||
|
||
public Optional<String> getFromOptionalProp() { | ||
return fromOptionalProp; | ||
} | ||
|
||
public void setFromOptionalProp(Optional<String> fromOptionalProp) { | ||
this.fromOptionalProp = fromOptionalProp; | ||
} | ||
|
||
public String getToOptionalProp() { | ||
return toOptionalProp; | ||
} | ||
|
||
public void setToOptionalProp(String toOptionalProp) { | ||
this.toOptionalProp = toOptionalProp; | ||
} | ||
|
||
public Optional<String> getOptional() { | ||
return optional; | ||
} | ||
|
||
public void setOptional(Optional<String> optional) { | ||
this.optional = optional; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
processor/src/test/java/org/mapstruct/ap/test/builtin/bean/TargetOptionalProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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.test.builtin.bean; | ||
|
||
import java.util.Optional; | ||
|
||
public class TargetOptionalProperty { | ||
|
||
// CHECKSTYLE:OFF | ||
public Optional<String> toOptionalPubProp; | ||
// CHECKSTYLE:ON | ||
|
||
// CHECKSTYLE:OFF | ||
public String fromOptionalPubProp; | ||
// CHECKSTYLE:ON | ||
|
||
private Optional<String> toOptionalProp; | ||
|
||
private String fromOptionalProp; | ||
|
||
private Optional<String> optional; | ||
|
||
public Optional<String> getToOptionalProp() { | ||
return toOptionalProp; | ||
} | ||
|
||
public void setToOptionalProp(Optional<String> toOptionalProp) { | ||
this.toOptionalProp = toOptionalProp; | ||
} | ||
|
||
public String getFromOptionalProp() { | ||
return fromOptionalProp; | ||
} | ||
|
||
public void setFromOptionalProp(String fromOptionalProp) { | ||
this.fromOptionalProp = fromOptionalProp; | ||
} | ||
|
||
public Optional<String> getOptional() { | ||
return optional; | ||
} | ||
|
||
public void setOptional(Optional<String> optional) { | ||
this.optional = optional; | ||
} | ||
} |
Oops, something went wrong.