Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ffa2993
feat: Copy the method annotation to the implementation method
chenzijia12300 Aug 16, 2022
6eeaff8
feat: Add method annotation copy supporting IterableMapping,MapMappin…
chenzijia12300 Aug 17, 2022
0fb8856
feat: Copy the method annotation to the implementation method
chenzijia12300 Aug 16, 2022
e1d19a5
feat: Add method annotation copy supporting IterableMapping,MapMappin…
chenzijia12300 Aug 17, 2022
9dc0f16
feat: Reimplement #2733
chenzijia12300 Aug 20, 2022
bb0f488
Merge remote-tracking branch 'origin/keep-method-annotations' into ke…
chenzijia12300 Aug 20, 2022
042a034
fix bug
chenzijia12300 Aug 20, 2022
e4d7125
fix: Adjust if statements to avoid printing useless newlines
chenzijia12300 Aug 21, 2022
fc5495a
adjust unit test
chenzijia12300 Aug 21, 2022
92288c4
docs: add license
chenzijia12300 Aug 21, 2022
2db350d
docs: add header license
chenzijia12300 Aug 21, 2022
73d6e0c
docs: checkstyle
chenzijia12300 Aug 21, 2022
13e95c7
fix bug
chenzijia12300 Aug 21, 2022
138ee37
feat: Support for implementing classes carrying the @Deprecated annot…
chenzijia12300 Aug 22, 2022
3ce4501
Merge branch 'master' into feat2773
chenzijia12300 Aug 24, 2022
d5b66ad
Merge branch 'mapstruct:main' into feat2773
chenzijia12300 Aug 24, 2022
b98dc9c
Merge remote-tracking branch 'origin/feat2773' into feat2773
chenzijia12300 Aug 25, 2022
e7a637c
Merge branch 'mapstruct:main' into feat2773
chenzijia12300 Aug 25, 2022
cd2baf9
Merge remote-tracking branch 'origin/feat2773' into feat2773
chenzijia12300 Aug 25, 2022
54d0444
feat: Remove redundant judgments and revert BeanMappingMethod change
chenzijia12300 Aug 25, 2022
2174db2
feat: revert BeanMappingMethod change
chenzijia12300 Aug 25, 2022
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 @@ -328,6 +328,11 @@ public boolean overridesMethod() {
return false;
}

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

@Override
public ExecutableElement getExecutable() {
return basedOn.getExecutable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ public boolean overridesMethod() {
return false;
}

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

@Override
public ExecutableElement getExecutable() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ private Mapper(TypeFactory typeFactory, String packageName, String name,
this.customPackage = customPackage;
this.customImplName = customImplName;
customAnnotations.forEach( this::addAnnotation );

TypeElement typeElement = this.getMapperDefinitionType().getTypeElement();
if ( typeElement.getAnnotation( Deprecated.class ) != null) {
addAnnotation( new Annotation(typeFactory.getType( Deprecated.class )) );
}
this.decorator = decorator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
public abstract class NormalTypeMappingMethod extends MappingMethod {
private final MethodReference factoryMethod;
private final boolean overridden;

private final boolean deprecated;

private final boolean mapNullToDefault;

NormalTypeMappingMethod(Method method, Collection<String> existingVariableNames, MethodReference factoryMethod,
Expand All @@ -31,6 +34,7 @@ public abstract class NormalTypeMappingMethod extends MappingMethod {
super( method, existingVariableNames, beforeMappingReferences, afterMappingReferences );
this.factoryMethod = factoryMethod;
this.overridden = method.overridesMethod();
this.deprecated = method.deprecatedMethod();
this.mapNullToDefault = mapNullToDefault;
}

Expand All @@ -56,6 +60,10 @@ public boolean isOverridden() {
return overridden;
}

public boolean isDeprecated() {
return deprecated;
}

public MethodReference getFactoryMethod() {
return this.factoryMethod;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ default boolean isPresenceCheck() {
*/
boolean overridesMethod();


/**
*
* @return Returns true when the method is modified by @Deprecated
*/
boolean deprecatedMethod();

ExecutableElement getExecutable();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.type.TypeKind;

import org.mapstruct.ap.internal.gem.ConditionGem;
import org.mapstruct.ap.internal.gem.ObjectFactoryGem;
Expand Down Expand Up @@ -349,6 +351,10 @@ public Accessibility getAccessibility() {
return accessibility;
}

public TypeFactory getTypeFactory() {
return typeFactory;
}

public boolean inverses(SourceMethod method) {
return method.getDeclaringMapper() == null
&& method.isAbstract()
Expand Down Expand Up @@ -515,6 +521,18 @@ public boolean overridesMethod() {
return declaringMapper == null && executable.getModifiers().contains( Modifier.ABSTRACT );
}

@Override
public boolean deprecatedMethod() {
Type deprecatedType = typeFactory.getType( Deprecated.class );
return executable != null && executable.getAnnotationMirrors()
.stream()
.map( AnnotationMirror::getAnnotationType )
.filter( annotationType -> TypeKind.DECLARED == annotationType.getKind() )
.map( typeFactory::getType )
.anyMatch( annotationType -> deprecatedType.equals( annotationType ) );

}

@Override
public boolean matches(List<Type> sourceTypes, Type targetType) {
MethodMatcher matcher = new MethodMatcher( typeUtils, typeFactory, this );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ public boolean overridesMethod() {
return false;
}

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

@Override
public ExecutableElement getExecutable() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<#nt><@includeModel object=annotation/>
</#list>
<#if overridden>@Override</#if>
<#if deprecated>
@Deprecated
</#if>
<#lt>${accessibility.keyword} <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, </#if></#list>)<@throws/> {
<#assign targetType = resultType />
<#if !existingInstanceMapping>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
-->
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.IterableMappingMethod" -->
<#if overridden>@Override</#if>
<#if deprecated>
@Deprecated
</#if>
<#lt>${accessibility.keyword} <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, </#if></#list>)<@throws/> {
<#list beforeMappingReferencesWithoutMappingTarget as callback>
<@includeModel object=callback targetBeanName=resultName targetType=resultType/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
-->
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.MapMappingMethod" -->
<#if overridden>@Override</#if>
<#if deprecated>
@Deprecated
</#if>
<#lt>${accessibility.keyword} <@includeModel object=returnType /> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, </#if></#list>)<@throws/> {
<#list beforeMappingReferencesWithoutMappingTarget as callback>
<@includeModel object=callback targetBeanName=resultName targetType=resultType/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
-->
<#-- @ftlvariable name="" type="org.mapstruct.ap.internal.model.StreamMappingMethod" -->
<#if overridden>@Override</#if>
<#if deprecated>
@Deprecated
</#if>
<#lt>${accessibility.keyword} <@includeModel object=returnType/> ${name}(<#list parameters as param><@includeModel object=param/><#if param_has_next>, </#if></#list>)<@throws/> {
<#--TODO does it even make sense to do a callback if the result is a Stream, as they are immutable-->
<#list beforeMappingReferencesWithoutMappingTarget as callback>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.bugs._2773;

/**
* @author orange add
*/
public class ChartEntry {

private String chartName;
private String songTitle;
private String artistName;
private String recordedAt;
private String city;
private int position;

public String getChartName() {
return chartName;
}

public void setChartName(String chartName) {
this.chartName = chartName;
}

public String getSongTitle() {
return songTitle;
}

public void setSongTitle(String songTitle) {
this.songTitle = songTitle;
}

public String getArtistName() {
return artistName;
}

public void setArtistName(String artistName) {
this.artistName = artistName;
}

public String getRecordedAt() {
return recordedAt;
}

public void setRecordedAt(String recordedAt) {
this.recordedAt = recordedAt;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public int getPosition() {
return position;
}

public void setPosition(int position) {
this.position = position;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.bugs._2773;

import org.mapstruct.IterableMapping;
import org.mapstruct.MapMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

/**
* @author orange add
*/

@Mapper
@Deprecated
public interface Issue2773Mapper {

Issue2773Mapper INSTANCE = Mappers.getMapper( Issue2773Mapper.class );

@Deprecated
@TestAnnotation(name = "hello", id = 1, address = {"shenzhen", "guangzhou"})
@Mapping(target = "name", source = "chartEntry1.recordedAt")
@Mapping(target = "city", source = "chartEntry2.city")
Studio toStudio(ChartEntry chartEntry1, ChartEntry chartEntry2);

@Mappings({@Mapping(target = "city", source = "city"), @Mapping(target = "name", source = "recordedAt")})
Studio map(ChartEntry chartEntry);

@Deprecated
@IterableMapping(numberFormat = "$#.00")
List<String> prices(List<Integer> prices);

@Deprecated
Set<String> integerStreamToStringSet(Stream<Integer> integers);

@Deprecated
@MapMapping(valueDateFormat = "dd.MM.yyyy")
Map<String, String> longDateMapToStringStringMap(Map<Long, Date> source);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.bugs._2773;

import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author orange add
*/
@IssueKey("2773")
public class Issue2773Test {

@ProcessorTest
@WithClasses({ChartEntry.class, Issue2773Mapper.class, Studio.class, TestAnnotation.class})
public void shouldContainMethodAnnotations() throws NoSuchMethodException {
Issue2773Mapper issue2773Mapper = Issue2773Mapper.INSTANCE;
Class<? extends Issue2773Mapper> mapperClass = issue2773Mapper.getClass();
Method toStudio = mapperClass.getMethod( "toStudio", ChartEntry.class, ChartEntry.class );
Method prices = mapperClass.getMethod( "prices", List.class );
Method integerStreamToStringSet = mapperClass.getMethod( "integerStreamToStringSet", Stream.class );
Method longDateMapToStringStringMap = mapperClass.getMethod( "longDateMapToStringStringMap", Map.class );
assertThat( mapperClass.getAnnotation( Deprecated.class ) ).isNotNull();
assertThat( toStudio.getAnnotation( Deprecated.class ) ).isNotNull();
assertThat( prices.getAnnotation( Deprecated.class ) ).isNotNull();
assertThat( integerStreamToStringSet.getAnnotation( Deprecated.class ) ).isNotNull();
assertThat( longDateMapToStringStringMap.getAnnotation( Deprecated.class ) ).isNotNull();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.bugs._2773;

/**
* @author orange add
*/
public class Studio {

private String city;
private String name;

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.bugs._2773;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author orange add
*/

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {

int id();

String name();

String[] address();
}