mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#2950 Add support for Jakarta CDI
This commit is contained in:
parent
ef4c26b075
commit
874bf1fd2c
@ -109,7 +109,12 @@ public final class MappingConstants {
|
|||||||
public static final String DEFAULT = "default";
|
public static final String DEFAULT = "default";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The generated mapper is an application-scoped CDI bean and can be retrieved via @Inject
|
* The generated mapper is an application-scoped CDI bean and can be retrieved via @Inject.
|
||||||
|
* The annotations are either from {@code javax} or {@code jakarta}.
|
||||||
|
* Priority have the {@code javax} annotations.
|
||||||
|
* In case you want to only use Jakarta then use {@link #JAKARTA_CDI}.
|
||||||
|
*
|
||||||
|
* @see #JAKARTA_CDI
|
||||||
*/
|
*/
|
||||||
public static final String CDI = "cdi";
|
public static final String CDI = "cdi";
|
||||||
|
|
||||||
@ -138,6 +143,12 @@ public final class MappingConstants {
|
|||||||
*/
|
*/
|
||||||
public static final String JAKARTA = "jakarta";
|
public static final String JAKARTA = "jakarta";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The generated mapper is an application-scoped Jakarta CDI bean and can be retrieved via @Inject.
|
||||||
|
* @see #CDI
|
||||||
|
*/
|
||||||
|
public static final String JAKARTA_CDI = "jakarta-cdi";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -64,6 +64,10 @@
|
|||||||
<groupId>jakarta.inject</groupId>
|
<groupId>jakarta.inject</groupId>
|
||||||
<artifactId>jakarta.inject-api</artifactId>
|
<artifactId>jakarta.inject-api</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jakarta.enterprise</groupId>
|
||||||
|
<artifactId>jakarta.enterprise.cdi-api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- Spring -->
|
<!-- Spring -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -160,6 +160,11 @@
|
|||||||
<artifactId>cdi-api</artifactId>
|
<artifactId>cdi-api</artifactId>
|
||||||
<version>2.0.SP1</version>
|
<version>2.0.SP1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jakarta.enterprise</groupId>
|
||||||
|
<artifactId>jakarta.enterprise.cdi-api</artifactId>
|
||||||
|
<version>4.0.1</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.inject</groupId>
|
<groupId>javax.inject</groupId>
|
||||||
<artifactId>javax.inject</artifactId>
|
<artifactId>javax.inject</artifactId>
|
||||||
|
@ -89,6 +89,11 @@
|
|||||||
<artifactId>jakarta.inject-api</artifactId>
|
<artifactId>jakarta.inject-api</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>jakarta.enterprise</groupId>
|
||||||
|
<artifactId>jakarta.enterprise.cdi-api</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- plexus-container-default is a runtime-dependency of the tycho-compiler -->
|
<!-- plexus-container-default is a runtime-dependency of the tycho-compiler -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -51,6 +51,8 @@ public final class MappingConstantsGem {
|
|||||||
public static final String JSR330 = "jsr330";
|
public static final String JSR330 = "jsr330";
|
||||||
|
|
||||||
public static final String JAKARTA = "jakarta";
|
public static final String JAKARTA = "jakarta";
|
||||||
|
|
||||||
|
public static final String JAKARTA_CDI = "jakarta-cdi";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,8 @@ import java.util.List;
|
|||||||
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;
|
||||||
import org.mapstruct.ap.internal.model.Mapper;
|
import org.mapstruct.ap.internal.model.Mapper;
|
||||||
|
import org.mapstruct.ap.internal.model.common.Type;
|
||||||
|
import org.mapstruct.ap.internal.util.AnnotationProcessingException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A {@link ModelElementProcessor} which converts the given {@link Mapper}
|
* A {@link ModelElementProcessor} which converts the given {@link Mapper}
|
||||||
@ -30,13 +32,13 @@ public class CdiComponentProcessor extends AnnotationBasedComponentModelProcesso
|
|||||||
@Override
|
@Override
|
||||||
protected List<Annotation> getTypeAnnotations(Mapper mapper) {
|
protected List<Annotation> getTypeAnnotations(Mapper mapper) {
|
||||||
return Collections.singletonList(
|
return Collections.singletonList(
|
||||||
new Annotation( getTypeFactory().getType( "javax.enterprise.context.ApplicationScoped" ) )
|
new Annotation( getType( "ApplicationScoped" ) )
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<Annotation> getMapperReferenceAnnotations() {
|
protected List<Annotation> getMapperReferenceAnnotations() {
|
||||||
return Arrays.asList( new Annotation( getTypeFactory().getType( "javax.inject.Inject" ) ) );
|
return Arrays.asList( new Annotation( getType( "Inject" ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -48,4 +50,24 @@ public class CdiComponentProcessor extends AnnotationBasedComponentModelProcesso
|
|||||||
protected boolean additionalPublicEmptyConstructor() {
|
protected boolean additionalPublicEmptyConstructor() {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Type getType(String simpleName) {
|
||||||
|
String javaxPrefix = "javax.inject.";
|
||||||
|
String jakartaPrefix = "jakarta.inject.";
|
||||||
|
if ( "ApplicationScoped".equals( simpleName ) ) {
|
||||||
|
javaxPrefix = "javax.enterprise.context.";
|
||||||
|
jakartaPrefix = "jakarta.enterprise.context.";
|
||||||
|
}
|
||||||
|
if ( getTypeFactory().isTypeAvailable( javaxPrefix + simpleName ) ) {
|
||||||
|
return getTypeFactory().getType( javaxPrefix + simpleName );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( getTypeFactory().isTypeAvailable( jakartaPrefix + simpleName ) ) {
|
||||||
|
return getTypeFactory().getType( jakartaPrefix + simpleName );
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new AnnotationProcessingException(
|
||||||
|
"Couldn't find any of the CDI or Jakarta CDI Dependency types." +
|
||||||
|
" Are you missing a dependency on your classpath?" );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* 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.processor;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.internal.gem.MappingConstantsGem;
|
||||||
|
import org.mapstruct.ap.internal.model.Annotation;
|
||||||
|
import org.mapstruct.ap.internal.model.Mapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A {@link ModelElementProcessor} which converts the given {@link Mapper}
|
||||||
|
* object into an application-scoped Jakarta CDI bean in case Jakarta CDI
|
||||||
|
* is configured as the target component model for this mapper.
|
||||||
|
*
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
public class JakartaCdiComponentProcessor extends AnnotationBasedComponentModelProcessor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getComponentModelIdentifier() {
|
||||||
|
return MappingConstantsGem.ComponentModelGem.JAKARTA_CDI;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<Annotation> getTypeAnnotations(Mapper mapper) {
|
||||||
|
return Collections.singletonList(
|
||||||
|
new Annotation( getTypeFactory().getType( "jakarta.enterprise.context.ApplicationScoped" ) )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<Annotation> getMapperReferenceAnnotations() {
|
||||||
|
return Arrays.asList( new Annotation( getTypeFactory().getType( "jakarta.inject.Inject" ) ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean requiresGenerationOfDecoratorClass() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean additionalPublicEmptyConstructor() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
# Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
# Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
org.mapstruct.ap.internal.processor.CdiComponentProcessor
|
org.mapstruct.ap.internal.processor.CdiComponentProcessor
|
||||||
|
org.mapstruct.ap.internal.processor.JakartaCdiComponentProcessor
|
||||||
org.mapstruct.ap.internal.processor.Jsr330ComponentProcessor
|
org.mapstruct.ap.internal.processor.Jsr330ComponentProcessor
|
||||||
org.mapstruct.ap.internal.processor.JakartaComponentProcessor
|
org.mapstruct.ap.internal.processor.JakartaComponentProcessor
|
||||||
org.mapstruct.ap.internal.processor.MapperCreationProcessor
|
org.mapstruct.ap.internal.processor.MapperCreationProcessor
|
||||||
|
@ -34,7 +34,7 @@ public class ConstantTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void componentModelContantsShouldBeEqual() {
|
public void componentModelConstantsShouldBeEqual() {
|
||||||
assertThat( MappingConstants.ComponentModel.DEFAULT )
|
assertThat( MappingConstants.ComponentModel.DEFAULT )
|
||||||
.isEqualTo( MappingConstantsGem.ComponentModelGem.DEFAULT );
|
.isEqualTo( MappingConstantsGem.ComponentModelGem.DEFAULT );
|
||||||
assertThat( MappingConstants.ComponentModel.CDI ).isEqualTo( MappingConstantsGem.ComponentModelGem.CDI );
|
assertThat( MappingConstants.ComponentModel.CDI ).isEqualTo( MappingConstantsGem.ComponentModelGem.CDI );
|
||||||
@ -42,5 +42,7 @@ public class ConstantTest {
|
|||||||
assertThat( MappingConstants.ComponentModel.JSR330 ).isEqualTo( MappingConstantsGem.ComponentModelGem.JSR330 );
|
assertThat( MappingConstants.ComponentModel.JSR330 ).isEqualTo( MappingConstantsGem.ComponentModelGem.JSR330 );
|
||||||
assertThat( MappingConstants.ComponentModel.JAKARTA )
|
assertThat( MappingConstants.ComponentModel.JAKARTA )
|
||||||
.isEqualTo( MappingConstantsGem.ComponentModelGem.JAKARTA );
|
.isEqualTo( MappingConstantsGem.ComponentModelGem.JAKARTA );
|
||||||
|
assertThat( MappingConstants.ComponentModel.JAKARTA_CDI )
|
||||||
|
.isEqualTo( MappingConstantsGem.ComponentModelGem.JAKARTA_CDI );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* 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.cdi._default;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerDto;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerEntity;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.Gender;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.GenderDto;
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.ProcessorTest;
|
||||||
|
import org.mapstruct.ap.testutil.WithCdi;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
import org.mapstruct.ap.testutil.runner.GeneratedSource;
|
||||||
|
|
||||||
|
import static java.lang.System.lineSeparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test field injection for component model jakarta-cdi.
|
||||||
|
* Default value option mapstruct.defaultInjectionStrategy is "field"
|
||||||
|
*
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@IssueKey("2950")
|
||||||
|
@WithClasses({
|
||||||
|
CustomerDto.class,
|
||||||
|
CustomerEntity.class,
|
||||||
|
Gender.class,
|
||||||
|
GenderDto.class,
|
||||||
|
CustomerCdiDefaultCompileOptionFieldMapper.class,
|
||||||
|
GenderCdiDefaultCompileOptionFieldMapper.class
|
||||||
|
})
|
||||||
|
@WithCdi
|
||||||
|
public class CdiDefaultCompileOptionFieldMapperTest {
|
||||||
|
|
||||||
|
@RegisterExtension
|
||||||
|
final GeneratedSource generatedSource = new GeneratedSource();
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
public void shouldHaveFieldInjection() {
|
||||||
|
generatedSource.forMapper( CustomerCdiDefaultCompileOptionFieldMapper.class )
|
||||||
|
.content()
|
||||||
|
.contains( "import javax.enterprise.context.ApplicationScoped;" )
|
||||||
|
.contains( "import javax.inject.Inject;" )
|
||||||
|
.contains( "@Inject" + lineSeparator() + " private GenderCdiDefaultCompileOptionFieldMapper" )
|
||||||
|
.contains( "@ApplicationScoped" + lineSeparator() + "public class" )
|
||||||
|
.doesNotContain( "public CustomerCdiDefaultCompileOptionFieldMapperImpl(" )
|
||||||
|
.doesNotContain( "jakarta.inject" )
|
||||||
|
.doesNotContain( "jakarta.enterprise" );
|
||||||
|
}
|
||||||
|
}
|
@ -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.cdi._default;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingConstants;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerDto;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerEntity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Mapper(componentModel = MappingConstants.ComponentModel.CDI,
|
||||||
|
uses = GenderCdiDefaultCompileOptionFieldMapper.class)
|
||||||
|
public interface CustomerCdiDefaultCompileOptionFieldMapper {
|
||||||
|
|
||||||
|
CustomerDto asTarget(CustomerEntity customerEntity);
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* 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.cdi._default;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingConstants;
|
||||||
|
import org.mapstruct.ValueMapping;
|
||||||
|
import org.mapstruct.ValueMappings;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.Gender;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.GenderDto;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Mapper(componentModel = MappingConstants.ComponentModel.CDI)
|
||||||
|
public interface GenderCdiDefaultCompileOptionFieldMapper {
|
||||||
|
|
||||||
|
@ValueMappings({
|
||||||
|
@ValueMapping(source = "MALE", target = "M"),
|
||||||
|
@ValueMapping(source = "FEMALE", target = "F")
|
||||||
|
})
|
||||||
|
GenderDto mapToDto(Gender gender);
|
||||||
|
}
|
@ -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.cdi.jakarta;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingConstants;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerDto;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerEntity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Mapper(componentModel = MappingConstants.ComponentModel.CDI,
|
||||||
|
uses = GenderCdiDefaultCompileOptionFieldMapper.class)
|
||||||
|
public interface CustomerCdiDefaultCompileOptionFieldMapper {
|
||||||
|
|
||||||
|
CustomerDto asTarget(CustomerEntity customerEntity);
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* 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.cdi.jakarta;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingConstants;
|
||||||
|
import org.mapstruct.ValueMapping;
|
||||||
|
import org.mapstruct.ValueMappings;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.Gender;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.GenderDto;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Mapper(componentModel = MappingConstants.ComponentModel.CDI)
|
||||||
|
public interface GenderCdiDefaultCompileOptionFieldMapper {
|
||||||
|
|
||||||
|
@ValueMappings({
|
||||||
|
@ValueMapping(source = "MALE", target = "M"),
|
||||||
|
@ValueMapping(source = "FEMALE", target = "F")
|
||||||
|
})
|
||||||
|
GenderDto mapToDto(Gender gender);
|
||||||
|
}
|
@ -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.test.injectionstrategy.cdi.jakarta;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerDto;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerEntity;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.Gender;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.GenderDto;
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.ProcessorTest;
|
||||||
|
import org.mapstruct.ap.testutil.WithCdi;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
import org.mapstruct.ap.testutil.WithJakartaCdi;
|
||||||
|
import org.mapstruct.ap.testutil.runner.GeneratedSource;
|
||||||
|
|
||||||
|
import static java.lang.System.lineSeparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test field injection for component model jsr330.
|
||||||
|
* Default value option mapstruct.defaultInjectionStrategy is "field"
|
||||||
|
*
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@IssueKey("2950")
|
||||||
|
@WithClasses({
|
||||||
|
CustomerDto.class,
|
||||||
|
CustomerEntity.class,
|
||||||
|
Gender.class,
|
||||||
|
GenderDto.class,
|
||||||
|
CustomerCdiDefaultCompileOptionFieldMapper.class,
|
||||||
|
GenderCdiDefaultCompileOptionFieldMapper.class
|
||||||
|
})
|
||||||
|
@WithJakartaCdi
|
||||||
|
@WithCdi
|
||||||
|
public class JakartaCdiAndCdiDefaultCompileOptionFieldMapperTest {
|
||||||
|
|
||||||
|
@RegisterExtension
|
||||||
|
final GeneratedSource generatedSource = new GeneratedSource();
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
public void shouldHaveCdiInjection() {
|
||||||
|
generatedSource.forMapper( CustomerCdiDefaultCompileOptionFieldMapper.class )
|
||||||
|
.content()
|
||||||
|
.contains( "import javax.enterprise.context.ApplicationScoped;" )
|
||||||
|
.contains( "import javax.inject.Inject;" )
|
||||||
|
.contains( "@Inject" + lineSeparator() + " private GenderCdiDefaultCompileOptionFieldMapper" )
|
||||||
|
.contains( "@ApplicationScoped" + lineSeparator() + "public class" )
|
||||||
|
.doesNotContain( "public CustomerCdiDefaultCompileOptionFieldMapperImpl(" )
|
||||||
|
.doesNotContain( "jakarta.inject" )
|
||||||
|
.doesNotContain( "jakarta.enterprise" );
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* 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.cdi.jakarta;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerDto;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerEntity;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.Gender;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.GenderDto;
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.ProcessorTest;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
import org.mapstruct.ap.testutil.WithJakartaCdi;
|
||||||
|
import org.mapstruct.ap.testutil.runner.GeneratedSource;
|
||||||
|
|
||||||
|
import static java.lang.System.lineSeparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test field injection for component model jsr330.
|
||||||
|
* Default value option mapstruct.defaultInjectionStrategy is "field"
|
||||||
|
*
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@IssueKey("2950")
|
||||||
|
@WithClasses({
|
||||||
|
CustomerDto.class,
|
||||||
|
CustomerEntity.class,
|
||||||
|
Gender.class,
|
||||||
|
GenderDto.class,
|
||||||
|
CustomerCdiDefaultCompileOptionFieldMapper.class,
|
||||||
|
GenderCdiDefaultCompileOptionFieldMapper.class
|
||||||
|
})
|
||||||
|
@WithJakartaCdi
|
||||||
|
public class JakartaCdiCdiDefaultCompileOptionFieldMapperTest {
|
||||||
|
|
||||||
|
@RegisterExtension
|
||||||
|
final GeneratedSource generatedSource = new GeneratedSource();
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
public void shouldHaveJakartaCdiInjection() {
|
||||||
|
generatedSource.forMapper( CustomerCdiDefaultCompileOptionFieldMapper.class )
|
||||||
|
.content()
|
||||||
|
.contains( "import jakarta.enterprise.context.ApplicationScoped;" )
|
||||||
|
.contains( "import jakarta.inject.Inject;" )
|
||||||
|
.contains( "@Inject" + lineSeparator() + " private GenderCdiDefaultCompileOptionFieldMapper" )
|
||||||
|
.contains( "@ApplicationScoped" + lineSeparator() + "public class" )
|
||||||
|
.doesNotContain( "public CustomerCdiDefaultCompileOptionFieldMapperImpl(" )
|
||||||
|
.doesNotContain( "javax.inject" )
|
||||||
|
.doesNotContain( "javax.enterprise" );
|
||||||
|
}
|
||||||
|
}
|
@ -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.jakarta_cdi._default;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingConstants;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerDto;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerEntity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Mapper(componentModel = MappingConstants.ComponentModel.JAKARTA_CDI,
|
||||||
|
uses = GenderJakartaCdiDefaultCompileOptionFieldMapper.class)
|
||||||
|
public interface CustomerJakartaCdiDefaultCompileOptionFieldMapper {
|
||||||
|
|
||||||
|
CustomerDto asTarget(CustomerEntity customerEntity);
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* 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.jakarta_cdi._default;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.MappingConstants;
|
||||||
|
import org.mapstruct.ValueMapping;
|
||||||
|
import org.mapstruct.ValueMappings;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.Gender;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.GenderDto;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Mapper(componentModel = MappingConstants.ComponentModel.JSR330)
|
||||||
|
public interface GenderJakartaCdiDefaultCompileOptionFieldMapper {
|
||||||
|
|
||||||
|
@ValueMappings({
|
||||||
|
@ValueMapping(source = "MALE", target = "M"),
|
||||||
|
@ValueMapping(source = "FEMALE", target = "F")
|
||||||
|
})
|
||||||
|
GenderDto mapToDto(Gender gender);
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* 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.jakarta_cdi._default;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerDto;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerEntity;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.Gender;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.GenderDto;
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.ProcessorTest;
|
||||||
|
import org.mapstruct.ap.testutil.WithCdi;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
import org.mapstruct.ap.testutil.WithJakartaCdi;
|
||||||
|
import org.mapstruct.ap.testutil.runner.GeneratedSource;
|
||||||
|
|
||||||
|
import static java.lang.System.lineSeparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test field injection for component model jakarta.
|
||||||
|
* Default value option mapstruct.defaultInjectionStrategy is "field"
|
||||||
|
*
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@IssueKey("2950")
|
||||||
|
@WithClasses({
|
||||||
|
CustomerDto.class,
|
||||||
|
CustomerEntity.class,
|
||||||
|
Gender.class,
|
||||||
|
GenderDto.class,
|
||||||
|
CustomerJakartaCdiDefaultCompileOptionFieldMapper.class,
|
||||||
|
GenderJakartaCdiDefaultCompileOptionFieldMapper.class
|
||||||
|
})
|
||||||
|
@WithJakartaCdi
|
||||||
|
@WithCdi
|
||||||
|
public class JakartaCdiAndCdiDefaultCompileOptionFieldMapperTest {
|
||||||
|
|
||||||
|
@RegisterExtension
|
||||||
|
final GeneratedSource generatedSource = new GeneratedSource();
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
public void shouldHaveJakartaInjection() {
|
||||||
|
generatedSource.forMapper( CustomerJakartaCdiDefaultCompileOptionFieldMapper.class )
|
||||||
|
.content()
|
||||||
|
.contains( "import jakarta.enterprise.context.ApplicationScoped;" )
|
||||||
|
.contains( "import jakarta.inject.Inject;" )
|
||||||
|
.contains( "@Inject" + lineSeparator() + " private GenderJakartaCdiDefaultCompileOptionFieldMapper" )
|
||||||
|
.contains( "@ApplicationScoped" + lineSeparator() + "public class" )
|
||||||
|
.doesNotContain( "javax.inject" )
|
||||||
|
.doesNotContain( "javax.enterprise" );
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* 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.jakarta_cdi._default;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerDto;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.CustomerEntity;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.Gender;
|
||||||
|
import org.mapstruct.ap.test.injectionstrategy.shared.GenderDto;
|
||||||
|
import org.mapstruct.ap.testutil.IssueKey;
|
||||||
|
import org.mapstruct.ap.testutil.ProcessorTest;
|
||||||
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
|
import org.mapstruct.ap.testutil.WithJakartaCdi;
|
||||||
|
import org.mapstruct.ap.testutil.runner.GeneratedSource;
|
||||||
|
|
||||||
|
import static java.lang.System.lineSeparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test field injection for component model jakarta-cdi.
|
||||||
|
* Default value option mapstruct.defaultInjectionStrategy is "field"
|
||||||
|
*
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@IssueKey("2950")
|
||||||
|
@WithClasses({
|
||||||
|
CustomerDto.class,
|
||||||
|
CustomerEntity.class,
|
||||||
|
Gender.class,
|
||||||
|
GenderDto.class,
|
||||||
|
CustomerJakartaCdiDefaultCompileOptionFieldMapper.class,
|
||||||
|
GenderJakartaCdiDefaultCompileOptionFieldMapper.class
|
||||||
|
})
|
||||||
|
@WithJakartaCdi
|
||||||
|
public class JakartaCdiDefaultCompileOptionFieldMapperTest {
|
||||||
|
|
||||||
|
@RegisterExtension
|
||||||
|
final GeneratedSource generatedSource = new GeneratedSource();
|
||||||
|
|
||||||
|
@ProcessorTest
|
||||||
|
public void shouldHaveFieldInjection() {
|
||||||
|
generatedSource.forMapper( CustomerJakartaCdiDefaultCompileOptionFieldMapper.class )
|
||||||
|
.content()
|
||||||
|
.contains( "import jakarta.enterprise.context.ApplicationScoped;" )
|
||||||
|
.contains( "import jakarta.inject.Inject;" )
|
||||||
|
.contains( "@Inject" + lineSeparator() + " private GenderJakartaCdiDefaultCompileOptionFieldMapper" )
|
||||||
|
.contains( "@ApplicationScoped" + lineSeparator() + "public class" )
|
||||||
|
.doesNotContain( "public CustomerJakartaCdiDefaultCompileOptionFieldMapperImpl(" )
|
||||||
|
.doesNotContain( "javax.inject" )
|
||||||
|
.doesNotContain( "javax.enterprise" );
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* 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.testutil;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Meta annotation that adds the needed Spring Dependencies
|
||||||
|
*
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
@WithTestDependency({
|
||||||
|
"javax.inject",
|
||||||
|
"cdi-api",
|
||||||
|
})
|
||||||
|
public @interface WithCdi {
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* 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.testutil;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Meta annotation that adds the needed Spring Dependencies
|
||||||
|
*
|
||||||
|
* @author Filip Hrisafov
|
||||||
|
*/
|
||||||
|
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
@WithTestDependency({
|
||||||
|
"jakarta.inject-api",
|
||||||
|
"jakarta.enterprise.cdi-api",
|
||||||
|
})
|
||||||
|
public @interface WithJakartaCdi {
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user