#3015 Fix annotations to the forged methods

This commit is contained in:
Orange Add 2022-11-04 06:22:35 +08:00 committed by GitHub
parent 6a394ad466
commit 93f7c3b8ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 209 additions and 6 deletions

View File

@ -14,6 +14,7 @@ import org.mapstruct.ap.internal.model.source.Method;
import org.mapstruct.ap.internal.util.Strings;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
@ -121,6 +122,9 @@ public abstract class AbstractMappingMethodBuilder<B extends AbstractMappingMeth
}
public List<Annotation> getMethodAnnotations() {
if ( method instanceof ForgedMethod ) {
return Collections.emptyList();
}
AdditionalAnnotationsBuilder additionalAnnotationsBuilder =
new AdditionalAnnotationsBuilder(
ctx.getElementUtils(),

View File

@ -6,6 +6,7 @@
package org.mapstruct.ap.internal.model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
@ -117,13 +118,20 @@ public class ValueMappingMethod extends MappingMethod {
LifecycleMethodResolver.beforeMappingMethods( method, selectionParameters, ctx, existingVariables );
List<LifecycleCallbackMethodReference> afterMappingMethods =
LifecycleMethodResolver.afterMappingMethods( method, selectionParameters, ctx, existingVariables );
AdditionalAnnotationsBuilder additionalAnnotationsBuilder =
List<Annotation> annotations;
if ( method instanceof ForgedMethod ) {
annotations = Collections.emptyList();
}
else {
annotations = new ArrayList<>();
AdditionalAnnotationsBuilder additionalAnnotationsBuilder =
new AdditionalAnnotationsBuilder(
ctx.getElementUtils(),
ctx.getTypeFactory(),
ctx.getMessager() );
List<Annotation> annotations = new ArrayList<>();
annotations.addAll( additionalAnnotationsBuilder.getProcessedAnnotations( method.getExecutable() ) );
ctx.getElementUtils(),
ctx.getTypeFactory(),
ctx.getMessager() );
annotations.addAll( additionalAnnotationsBuilder.getProcessedAnnotations( method.getExecutable() ) );
}
// finally return a mapping
return new ValueMappingMethod( method,
annotations,

View File

@ -0,0 +1,153 @@
/*
* 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._3015;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import org.mapstruct.AnnotateWith;
import org.mapstruct.Mapper;
import org.mapstruct.ap.test.annotatewith.CustomMethodOnlyAnnotation;
/**
* @author orange add
*/
@Mapper
public interface Issue3015Mapper {
@AnnotateWith( CustomMethodOnlyAnnotation.class )
Target map(Source source);
class Source {
private NestedSource nested;
private List<String> list;
private Stream<String> stream;
private AnnotateSourceEnum annotateWithEnum;
private Map<String, Integer> map;
public NestedSource getNested() {
return nested;
}
public void setNested(NestedSource nested) {
this.nested = nested;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Stream<String> getStream() {
return stream;
}
public void setStream(Stream<String> stream) {
this.stream = stream;
}
public AnnotateSourceEnum getAnnotateWithEnum() {
return annotateWithEnum;
}
public void setAnnotateWithEnum(AnnotateSourceEnum annotateWithEnum) {
this.annotateWithEnum = annotateWithEnum;
}
public Map<String, Integer> getMap() {
return map;
}
public void setMap(Map<String, Integer> map) {
this.map = map;
}
}
class Target {
private NestedTarget nested;
private List<Integer> list;
private Stream<Integer> stream;
private AnnotateTargetEnum annotateWithEnum;
private Map<String, String> map;
public NestedTarget getNested() {
return nested;
}
public void setNested(NestedTarget nested) {
this.nested = nested;
}
public List<Integer> getList() {
return list;
}
public void setList(List<Integer> list) {
this.list = list;
}
public Stream<Integer> getStream() {
return stream;
}
public void setStream(Stream<Integer> stream) {
this.stream = stream;
}
public AnnotateTargetEnum getAnnotateWithEnum() {
return annotateWithEnum;
}
public void setAnnotateWithEnum(AnnotateTargetEnum annotateWithEnum) {
this.annotateWithEnum = annotateWithEnum;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}
enum AnnotateSourceEnum {
EXISTING;
}
enum AnnotateTargetEnum {
EXISTING;
}
class NestedSource {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
class NestedTarget {
private Integer value;
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
}
}

View File

@ -0,0 +1,38 @@
/*
* 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._3015;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.mapstruct.ap.test.annotatewith.CustomMethodOnlyAnnotation;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.factory.Mappers;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author orange add
*/
@WithClasses({
Issue3015Mapper.class,
CustomMethodOnlyAnnotation.class
})
class Issue3015Test {
@ProcessorTest
void noNeedPassAnnotationToForgeMethod() {
Issue3015Mapper mapper = Mappers.getMapper( Issue3015Mapper.class );
Method[] declaredMethods = mapper.getClass().getDeclaredMethods();
List<Method> annotationMethods = Arrays.stream( declaredMethods )
.filter( method -> method.getAnnotation( CustomMethodOnlyAnnotation.class ) != null )
.collect( Collectors.toList() );
assertThat( annotationMethods ).hasSize( 1 );
}
}