mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#1427 Add support for custom name in Spring stereotype annotations
This commit is contained in:
parent
af1eab0ece
commit
90a487ac06
@ -8,7 +8,9 @@ package org.mapstruct.ap.internal.processor;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.mapstruct.ap.internal.gem.MappingConstantsGem;
|
import org.mapstruct.ap.internal.gem.MappingConstantsGem;
|
||||||
import org.mapstruct.ap.internal.model.Annotation;
|
import org.mapstruct.ap.internal.model.Annotation;
|
||||||
@ -16,6 +18,13 @@ import org.mapstruct.ap.internal.model.Mapper;
|
|||||||
import org.mapstruct.ap.internal.model.annotation.AnnotationElement;
|
import org.mapstruct.ap.internal.model.annotation.AnnotationElement;
|
||||||
import org.mapstruct.ap.internal.model.annotation.AnnotationElement.AnnotationElementType;
|
import org.mapstruct.ap.internal.model.annotation.AnnotationElement.AnnotationElementType;
|
||||||
|
|
||||||
|
import javax.lang.model.element.AnnotationMirror;
|
||||||
|
import javax.lang.model.element.Element;
|
||||||
|
import javax.lang.model.element.PackageElement;
|
||||||
|
import javax.lang.model.element.TypeElement;
|
||||||
|
|
||||||
|
import static javax.lang.model.element.ElementKind.PACKAGE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link ModelElementProcessor} which converts the given {@link Mapper}
|
* A {@link ModelElementProcessor} which converts the given {@link Mapper}
|
||||||
* object into a Spring bean in case Spring is configured as the
|
* object into a Spring bean in case Spring is configured as the
|
||||||
@ -25,6 +34,7 @@ import org.mapstruct.ap.internal.model.annotation.AnnotationElement.AnnotationEl
|
|||||||
* @author Andreas Gudian
|
* @author Andreas Gudian
|
||||||
*/
|
*/
|
||||||
public class SpringComponentProcessor extends AnnotationBasedComponentModelProcessor {
|
public class SpringComponentProcessor extends AnnotationBasedComponentModelProcessor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getComponentModelIdentifier() {
|
protected String getComponentModelIdentifier() {
|
||||||
return MappingConstantsGem.ComponentModelGem.SPRING;
|
return MappingConstantsGem.ComponentModelGem.SPRING;
|
||||||
@ -33,7 +43,9 @@ public class SpringComponentProcessor extends AnnotationBasedComponentModelProce
|
|||||||
@Override
|
@Override
|
||||||
protected List<Annotation> getTypeAnnotations(Mapper mapper) {
|
protected List<Annotation> getTypeAnnotations(Mapper mapper) {
|
||||||
List<Annotation> typeAnnotations = new ArrayList<>();
|
List<Annotation> typeAnnotations = new ArrayList<>();
|
||||||
|
if ( !isAlreadyAnnotatedAsSpringStereotype( mapper ) ) {
|
||||||
typeAnnotations.add( component() );
|
typeAnnotations.add( component() );
|
||||||
|
}
|
||||||
|
|
||||||
if ( mapper.getDecorator() != null ) {
|
if ( mapper.getDecorator() != null ) {
|
||||||
typeAnnotations.add( qualifierDelegate() );
|
typeAnnotations.add( qualifierDelegate() );
|
||||||
@ -91,4 +103,57 @@ public class SpringComponentProcessor extends AnnotationBasedComponentModelProce
|
|||||||
private Annotation component() {
|
private Annotation component() {
|
||||||
return new Annotation( getTypeFactory().getType( "org.springframework.stereotype.Component" ) );
|
return new Annotation( getTypeFactory().getType( "org.springframework.stereotype.Component" ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isAlreadyAnnotatedAsSpringStereotype(Mapper mapper) {
|
||||||
|
Set<Element> handledElements = new HashSet<>();
|
||||||
|
return mapper.getAnnotations()
|
||||||
|
.stream()
|
||||||
|
.anyMatch(
|
||||||
|
annotation -> isOrIncludesComponentAnnotation( annotation, handledElements )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isOrIncludesComponentAnnotation(Annotation annotation, Set<Element> handledElements) {
|
||||||
|
return isOrIncludesComponentAnnotation(
|
||||||
|
annotation.getType().getTypeElement(), handledElements
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isOrIncludesComponentAnnotation(Element element, Set<Element> handledElements) {
|
||||||
|
if ( "org.springframework.stereotype.Component".equals(
|
||||||
|
( (TypeElement) element ).getQualifiedName().toString()
|
||||||
|
)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( AnnotationMirror annotationMirror : element.getAnnotationMirrors() ) {
|
||||||
|
Element annotationMirrorElement = annotationMirror.getAnnotationType().asElement();
|
||||||
|
// Bypass java lang annotations to improve performance avoiding unnecessary checks
|
||||||
|
if ( !isAnnotationInPackage( annotationMirrorElement, "java.lang.annotation" ) &&
|
||||||
|
!handledElements.contains( annotationMirrorElement ) ) {
|
||||||
|
handledElements.add( annotationMirrorElement );
|
||||||
|
boolean isOrIncludesComponentAnnotation = isOrIncludesComponentAnnotation(
|
||||||
|
annotationMirrorElement, handledElements
|
||||||
|
);
|
||||||
|
|
||||||
|
if ( isOrIncludesComponentAnnotation ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private PackageElement getPackageOf( Element element ) {
|
||||||
|
while ( element.getKind() != PACKAGE ) {
|
||||||
|
element = element.getEnclosingElement();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (PackageElement) element;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isAnnotationInPackage(Element element, String packageFQN) {
|
||||||
|
return packageFQN.equals( getPackageOf( element ).getQualifiedName().toString() );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* 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.injectionstrategy.spring.annotateWith;
|
||||||
|
|
||||||
|
import java.lang.annotation.Documented;
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Target({ElementType.TYPE})
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
@Component
|
||||||
|
public @interface CustomStereotype {
|
||||||
|
String value() default "";
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* 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.injectionstrategy.spring.annotateWith;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import org.mapstruct.AnnotateWith;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingConstants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Ben Zegveld
|
||||||
|
*/
|
||||||
|
@AnnotateWith( value = Component.class, elements = @AnnotateWith.Element( strings = "AnnotateWithComponent" ) )
|
||||||
|
@Mapper( componentModel = MappingConstants.ComponentModel.SPRING )
|
||||||
|
public interface CustomerSpringComponentQualifiedMapper {
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* 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.injectionstrategy.spring.annotateWith;
|
||||||
|
|
||||||
|
import org.mapstruct.AnnotateWith;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingConstants;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Jose Carlos Campanero Ortiz
|
||||||
|
*/
|
||||||
|
@AnnotateWith( value = Controller.class, elements = @AnnotateWith.Element( strings = "AnnotateWithController" ) )
|
||||||
|
@Mapper( componentModel = MappingConstants.ComponentModel.SPRING )
|
||||||
|
public interface CustomerSpringControllerQualifiedMapper {
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* 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.injectionstrategy.spring.annotateWith;
|
||||||
|
|
||||||
|
import org.mapstruct.AnnotateWith;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingConstants;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Jose Carlos Campanero Ortiz
|
||||||
|
*/
|
||||||
|
@AnnotateWith(
|
||||||
|
value = CustomStereotype.class,
|
||||||
|
elements = @AnnotateWith.Element( strings = "AnnotateWithCustomStereotype" )
|
||||||
|
)
|
||||||
|
@Mapper( componentModel = MappingConstants.ComponentModel.SPRING )
|
||||||
|
public interface CustomerSpringCustomStereotypeQualifiedMapper {
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* 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.injectionstrategy.spring.annotateWith;
|
||||||
|
|
||||||
|
import org.mapstruct.AnnotateWith;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingConstants;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Jose Carlos Campanero Ortiz
|
||||||
|
*/
|
||||||
|
@AnnotateWith( value = Repository.class, elements = @AnnotateWith.Element( strings = "AnnotateWithRepository" ) )
|
||||||
|
@Mapper( componentModel = MappingConstants.ComponentModel.SPRING )
|
||||||
|
public interface CustomerSpringRepositoryQualifiedMapper {
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
/*
|
||||||
|
* 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.injectionstrategy.spring.annotateWith;
|
||||||
|
|
||||||
|
import org.mapstruct.AnnotateWith;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingConstants;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Jose Carlos Campanero Ortiz
|
||||||
|
*/
|
||||||
|
@AnnotateWith( value = Service.class, elements = @AnnotateWith.Element( strings = "AnnotateWithService" ) )
|
||||||
|
@Mapper( componentModel = MappingConstants.ComponentModel.SPRING )
|
||||||
|
public interface CustomerSpringServiceQualifiedMapper {
|
||||||
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* 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.injectionstrategy.spring.annotateWith;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.ProcessorTest;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
import org.mapstruct.ap.testutil.WithSpring;
|
||||||
|
import org.mapstruct.ap.testutil.runner.GeneratedSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test field injection for component model spring.
|
||||||
|
*
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
* @author Jose Carlos Campanero Ortiz
|
||||||
|
*/
|
||||||
|
@WithClasses({
|
||||||
|
CustomerSpringComponentQualifiedMapper.class,
|
||||||
|
CustomerSpringControllerQualifiedMapper.class,
|
||||||
|
CustomerSpringServiceQualifiedMapper.class,
|
||||||
|
CustomerSpringRepositoryQualifiedMapper.class,
|
||||||
|
CustomStereotype.class,
|
||||||
|
CustomerSpringCustomStereotypeQualifiedMapper.class
|
||||||
|
})
|
||||||
|
@IssueKey( "1427" )
|
||||||
|
@WithSpring
|
||||||
|
public class SpringAnnotateWithMapperTest {
|
||||||
|
|
||||||
|
@RegisterExtension
|
||||||
|
final GeneratedSource generatedSource = new GeneratedSource();
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
public void shouldHaveComponentAnnotatedQualifiedMapper() {
|
||||||
|
|
||||||
|
// then
|
||||||
|
generatedSource.forMapper( CustomerSpringComponentQualifiedMapper.class )
|
||||||
|
.content()
|
||||||
|
.contains( "@Component(value = \"AnnotateWithComponent\")" )
|
||||||
|
.doesNotContain( "@Component" + System.lineSeparator() );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
public void shouldHaveControllerAnnotatedQualifiedMapper() {
|
||||||
|
|
||||||
|
// then
|
||||||
|
generatedSource.forMapper( CustomerSpringControllerQualifiedMapper.class )
|
||||||
|
.content()
|
||||||
|
.contains( "@Controller(value = \"AnnotateWithController\")" )
|
||||||
|
.doesNotContain( "@Component" );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
public void shouldHaveServiceAnnotatedQualifiedMapper() {
|
||||||
|
|
||||||
|
// then
|
||||||
|
generatedSource.forMapper( CustomerSpringServiceQualifiedMapper.class )
|
||||||
|
.content()
|
||||||
|
.contains( "@Service(value = \"AnnotateWithService\")" )
|
||||||
|
.doesNotContain( "@Component" );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
public void shouldHaveRepositoryAnnotatedQualifiedMapper() {
|
||||||
|
|
||||||
|
// then
|
||||||
|
generatedSource.forMapper( CustomerSpringRepositoryQualifiedMapper.class )
|
||||||
|
.content()
|
||||||
|
.contains( "@Repository(value = \"AnnotateWithRepository\")" )
|
||||||
|
.doesNotContain( "@Component" );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
public void shouldHaveCustomStereotypeAnnotatedQualifiedMapper() {
|
||||||
|
|
||||||
|
// then
|
||||||
|
generatedSource.forMapper( CustomerSpringCustomStereotypeQualifiedMapper.class )
|
||||||
|
.content()
|
||||||
|
.contains( "@CustomStereotype(value = \"AnnotateWithCustomStereotype\")" )
|
||||||
|
.doesNotContain( "@Component" );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user