mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#272 Basic decorator implementation for CDI and Spring
This commit is contained in:
parent
174c53cb48
commit
b37a1d24d9
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.itest.cdi;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.DecoratedWith;
|
||||
import org.mapstruct.itest.cdi.other.DateMapper;
|
||||
|
||||
@Mapper( componentModel = "cdi", uses = DateMapper.class )
|
||||
@DecoratedWith( SourceTargetMapperDecorator.class )
|
||||
public interface DecoratedSourceTargetMapper {
|
||||
|
||||
Target sourceToTarget(Source source);
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.itest.cdi;
|
||||
|
||||
import javax.decorator.Decorator;
|
||||
import javax.decorator.Delegate;
|
||||
import javax.inject.Inject;
|
||||
|
||||
@Decorator
|
||||
public class SourceTargetMapperDecorator implements DecoratedSourceTargetMapper {
|
||||
|
||||
@Delegate
|
||||
@Inject
|
||||
private DecoratedSourceTargetMapper delegate;
|
||||
|
||||
public SourceTargetMapperDecorator() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Target sourceToTarget(Source source) {
|
||||
Target t = delegate.sourceToTarget( source );
|
||||
t.setFoo( 43L );
|
||||
return t;
|
||||
}
|
||||
}
|
@ -25,7 +25,7 @@ import javax.inject.Inject;
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
|
||||
import org.jboss.shrinkwrap.api.asset.StringAsset;
|
||||
import org.jboss.shrinkwrap.api.spec.JavaArchive;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@ -42,12 +42,17 @@ public class CdiBasedMapperTest {
|
||||
@Inject
|
||||
private SourceTargetMapper mapper;
|
||||
|
||||
@Inject
|
||||
private DecoratedSourceTargetMapper decoratedMapper;
|
||||
|
||||
@Deployment
|
||||
public static JavaArchive createDeployment() {
|
||||
return ShrinkWrap.create( JavaArchive.class )
|
||||
.addPackage( SourceTargetMapper.class.getPackage() )
|
||||
.addPackage( DateMapper.class.getPackage() )
|
||||
.addAsManifestResource( EmptyAsset.INSTANCE, "beans.xml" );
|
||||
.addAsManifestResource(
|
||||
new StringAsset("<decorators><class>org.mapstruct.itest.cdi.SourceTargetMapperDecorator</class></decorators>"),
|
||||
"beans.xml" );
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -60,4 +65,14 @@ public class CdiBasedMapperTest {
|
||||
assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 42 ) );
|
||||
assertThat( target.getDate() ).isEqualTo( "1980" );
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldInjectDecorator() {
|
||||
Source source = new Source();
|
||||
|
||||
Target target = decoratedMapper.sourceToTarget( source );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 43 ) );
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.itest.spring;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.DecoratedWith;
|
||||
import org.mapstruct.itest.spring.other.DateMapper;
|
||||
|
||||
@Mapper( componentModel = "spring", uses = DateMapper.class )
|
||||
@DecoratedWith( SourceTargetMapperDecorator.class )
|
||||
public interface DecoratedSourceTargetMapper {
|
||||
|
||||
Target sourceToTarget(Source source);
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
* and/or other contributors as indicated by the @authors tag. See the
|
||||
* copyright.txt file in the distribution for a full listing of all
|
||||
* contributors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.itest.spring;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SourceTargetMapperDecorator implements DecoratedSourceTargetMapper {
|
||||
|
||||
@Autowired
|
||||
@Qualifier( "decoratedSourceTargetMapperImpl_" )
|
||||
private DecoratedSourceTargetMapper delegate;
|
||||
|
||||
public SourceTargetMapperDecorator() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Target sourceToTarget(Source source) {
|
||||
Target t = delegate.sourceToTarget( source );
|
||||
t.setFoo( 43L );
|
||||
return t;
|
||||
}
|
||||
}
|
@ -24,6 +24,7 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mapstruct.itest.spring.SpringBasedMapperTest.SpringTestConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
@ -45,6 +46,9 @@ public class SpringBasedMapperTest {
|
||||
@Autowired
|
||||
private SourceTargetMapper mapper;
|
||||
|
||||
@Autowired
|
||||
@Qualifier( "sourceTargetMapperDecorator" )
|
||||
private DecoratedSourceTargetMapper decoratedMapper;
|
||||
|
||||
@Test
|
||||
public void shouldCreateSpringBasedMapper() {
|
||||
@ -56,4 +60,15 @@ public class SpringBasedMapperTest {
|
||||
assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 42 ) );
|
||||
assertThat( target.getDate() ).isEqualTo( "1980" );
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldInjectDecorator() {
|
||||
Source source = new Source();
|
||||
|
||||
Target target = decoratedMapper.sourceToTarget( source );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 43 ) );
|
||||
}
|
||||
|
||||
}
|
@ -43,7 +43,7 @@ public class Mapper extends GeneratedType {
|
||||
private static final String DECORATED_IMPLEMENTATION_SUFFIX = "Impl_";
|
||||
|
||||
private final List<MapperReference> referencedMappers;
|
||||
private final Decorator decorator;
|
||||
private Decorator decorator;
|
||||
|
||||
@SuppressWarnings( "checkstyle:parameternumber" )
|
||||
private Mapper(TypeFactory typeFactory, String packageName, String name, String superClassName,
|
||||
@ -158,6 +158,10 @@ public class Mapper extends GeneratedType {
|
||||
return decorator;
|
||||
}
|
||||
|
||||
public void removeDecorator() {
|
||||
this.decorator = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getTemplateName() {
|
||||
return getTemplateNameForClass( GeneratedType.class );
|
||||
|
@ -56,6 +56,10 @@ public abstract class AnnotationBasedComponentModelProcessor implements ModelEle
|
||||
return mapper;
|
||||
}
|
||||
|
||||
if ( shouldDecoratorBeRemoved() ) {
|
||||
mapper.removeDecorator();
|
||||
}
|
||||
|
||||
mapper.addAnnotation( getTypeAnnotation() );
|
||||
|
||||
ListIterator<MapperReference> iterator = mapper.getReferencedMappers().listIterator();
|
||||
@ -97,6 +101,11 @@ public abstract class AnnotationBasedComponentModelProcessor implements ModelEle
|
||||
*/
|
||||
protected abstract Annotation getMapperReferenceAnnotation();
|
||||
|
||||
/**
|
||||
* @return if generated decorator class should be removed
|
||||
*/
|
||||
protected abstract boolean shouldDecoratorBeRemoved();
|
||||
|
||||
@Override
|
||||
public int getPriority() {
|
||||
return 1100;
|
||||
|
@ -44,4 +44,9 @@ public class CdiComponentProcessor extends AnnotationBasedComponentModelProcesso
|
||||
protected Annotation getMapperReferenceAnnotation() {
|
||||
return new Annotation( getTypeFactory().getType( "javax.inject.Inject" ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldDecoratorBeRemoved() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -44,4 +44,9 @@ public class Jsr330ComponentProcessor extends AnnotationBasedComponentModelProce
|
||||
protected Annotation getMapperReferenceAnnotation() {
|
||||
return new Annotation( getTypeFactory().getType( "javax.inject.Inject" ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldDecoratorBeRemoved() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -44,4 +44,9 @@ public class SpringComponentProcessor extends AnnotationBasedComponentModelProce
|
||||
protected Annotation getMapperReferenceAnnotation() {
|
||||
return new Annotation( getTypeFactory().getType( "org.springframework.beans.factory.annotation.Autowired" ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldDecoratorBeRemoved() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user