mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#194 Only generate field / imports for a referenced mapper if it is actually used
This commit is contained in:
parent
0772dce41b
commit
4e10c8451c
@ -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.ap.model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basic interface class that facilitates an empty constructor
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public interface Constructor {
|
||||||
|
|
||||||
|
String getName();
|
||||||
|
|
||||||
|
}
|
@ -27,7 +27,6 @@ import javax.lang.model.element.TypeElement;
|
|||||||
import javax.lang.model.util.Elements;
|
import javax.lang.model.util.Elements;
|
||||||
|
|
||||||
import org.mapstruct.ap.model.common.Accessibility;
|
import org.mapstruct.ap.model.common.Accessibility;
|
||||||
import org.mapstruct.ap.model.common.ModelElement;
|
|
||||||
import org.mapstruct.ap.model.common.Type;
|
import org.mapstruct.ap.model.common.Type;
|
||||||
import org.mapstruct.ap.model.common.TypeFactory;
|
import org.mapstruct.ap.model.common.TypeFactory;
|
||||||
import org.mapstruct.ap.option.Options;
|
import org.mapstruct.ap.option.Options;
|
||||||
@ -43,9 +42,86 @@ public class Decorator extends GeneratedType {
|
|||||||
|
|
||||||
private static final String IMPLEMENTATION_SUFFIX = "Impl";
|
private static final String IMPLEMENTATION_SUFFIX = "Impl";
|
||||||
|
|
||||||
|
public static class Builder {
|
||||||
|
|
||||||
|
private Elements elementUtils;
|
||||||
|
private TypeFactory typeFactory;
|
||||||
|
private TypeElement mapperElement;
|
||||||
|
private DecoratedWithPrism decoratorPrism;
|
||||||
|
private List<MappingMethod> methods;
|
||||||
|
private Options options;
|
||||||
|
private VersionInformation versionInformation;
|
||||||
|
private boolean hasDelegateConstructor;
|
||||||
|
|
||||||
|
public Builder elementUtils(Elements elementUtils) {
|
||||||
|
this.elementUtils = elementUtils;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder typeFactory(TypeFactory typeFactory) {
|
||||||
|
this.typeFactory = typeFactory;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder mapperElement(TypeElement mapperElement) {
|
||||||
|
this.mapperElement = mapperElement;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder decoratorPrism(DecoratedWithPrism decoratorPrism) {
|
||||||
|
this.decoratorPrism = decoratorPrism;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder methods(List<MappingMethod> methods) {
|
||||||
|
this.methods = methods;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder options(Options options) {
|
||||||
|
this.options = options;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder versionInformation(VersionInformation versionInformation) {
|
||||||
|
this.versionInformation = versionInformation;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder hasDelegateConstructor(boolean hasDelegateConstructor) {
|
||||||
|
this.hasDelegateConstructor = hasDelegateConstructor;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Decorator build() {
|
||||||
|
Type decoratorType = typeFactory.getType( decoratorPrism.value() );
|
||||||
|
DecoratorConstructor decoratorConstructor = new DecoratorConstructor(
|
||||||
|
mapperElement.getSimpleName().toString() + IMPLEMENTATION_SUFFIX,
|
||||||
|
mapperElement.getSimpleName().toString() + "Impl_",
|
||||||
|
hasDelegateConstructor );
|
||||||
|
|
||||||
|
|
||||||
|
return new Decorator(
|
||||||
|
typeFactory,
|
||||||
|
elementUtils.getPackageOf( mapperElement ).getQualifiedName().toString(),
|
||||||
|
mapperElement.getSimpleName().toString() + IMPLEMENTATION_SUFFIX,
|
||||||
|
decoratorType.getName(),
|
||||||
|
mapperElement.getKind() == ElementKind.INTERFACE ? mapperElement.getSimpleName().toString() : null,
|
||||||
|
methods,
|
||||||
|
Arrays.asList( new Field( typeFactory.getType( mapperElement ), "delegate", true ) ) ,
|
||||||
|
options,
|
||||||
|
versionInformation,
|
||||||
|
Accessibility.fromModifiers( mapperElement.getModifiers() ),
|
||||||
|
decoratorConstructor
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings( "checkstyle:parameternumber" )
|
||||||
private Decorator(TypeFactory typeFactory, String packageName, String name, String superClassName,
|
private Decorator(TypeFactory typeFactory, String packageName, String name, String superClassName,
|
||||||
String interfaceName, List<MappingMethod> methods, List<? extends ModelElement> fields,
|
String interfaceName, List<MappingMethod> methods, List<? extends Field> fields,
|
||||||
Options options, VersionInformation versionInformation, Accessibility accessibility) {
|
Options options, VersionInformation versionInformation, Accessibility accessibility,
|
||||||
|
DecoratorConstructor decoratorConstructor) {
|
||||||
super(
|
super(
|
||||||
typeFactory,
|
typeFactory,
|
||||||
packageName,
|
packageName,
|
||||||
@ -57,34 +133,8 @@ public class Decorator extends GeneratedType {
|
|||||||
options,
|
options,
|
||||||
versionInformation,
|
versionInformation,
|
||||||
accessibility,
|
accessibility,
|
||||||
new TreeSet<Type>()
|
new TreeSet<Type>(),
|
||||||
);
|
decoratorConstructor
|
||||||
}
|
|
||||||
|
|
||||||
public static Decorator getInstance(Elements elementUtils, TypeFactory typeFactory, TypeElement mapperElement,
|
|
||||||
DecoratedWithPrism decoratorPrism, List<MappingMethod> methods,
|
|
||||||
boolean hasDelegateConstructor, Options options,
|
|
||||||
VersionInformation versionInformation) {
|
|
||||||
Type decoratorType = typeFactory.getType( decoratorPrism.value() );
|
|
||||||
|
|
||||||
return new Decorator(
|
|
||||||
typeFactory,
|
|
||||||
elementUtils.getPackageOf( mapperElement ).getQualifiedName().toString(),
|
|
||||||
mapperElement.getSimpleName().toString() + IMPLEMENTATION_SUFFIX,
|
|
||||||
decoratorType.getName(),
|
|
||||||
mapperElement.getKind() == ElementKind.INTERFACE ? mapperElement.getSimpleName().toString() : null,
|
|
||||||
methods,
|
|
||||||
Arrays.asList(
|
|
||||||
new Field( typeFactory.getType( mapperElement ), "delegate", true ),
|
|
||||||
new DecoratorConstructor(
|
|
||||||
mapperElement.getSimpleName().toString() + IMPLEMENTATION_SUFFIX,
|
|
||||||
mapperElement.getSimpleName().toString() + "Impl_",
|
|
||||||
hasDelegateConstructor
|
|
||||||
)
|
|
||||||
),
|
|
||||||
options,
|
|
||||||
versionInformation,
|
|
||||||
Accessibility.fromModifiers( mapperElement.getModifiers() )
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,8 +20,8 @@ package org.mapstruct.ap.model;
|
|||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.mapstruct.ap.model.common.ModelElement;
|
import org.mapstruct.ap.model.common.ModelElement;
|
||||||
|
|
||||||
import org.mapstruct.ap.model.common.Type;
|
import org.mapstruct.ap.model.common.Type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,7 +29,7 @@ import org.mapstruct.ap.model.common.Type;
|
|||||||
*
|
*
|
||||||
* @author Gunnar Morling
|
* @author Gunnar Morling
|
||||||
*/
|
*/
|
||||||
public class DecoratorConstructor extends ModelElement {
|
public class DecoratorConstructor extends ModelElement implements Constructor {
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final String delegateName;
|
private final String delegateName;
|
||||||
@ -46,6 +46,7 @@ public class DecoratorConstructor extends ModelElement {
|
|||||||
return Collections.emptySet();
|
return Collections.emptySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
@ -34,16 +34,19 @@ public class Field extends ModelElement {
|
|||||||
private final Type type;
|
private final Type type;
|
||||||
private final String variableName;
|
private final String variableName;
|
||||||
private boolean used;
|
private boolean used;
|
||||||
|
private boolean typeRequiresImport;
|
||||||
|
|
||||||
public Field(Type type, String variableName, boolean used) {
|
public Field(Type type, String variableName, boolean used) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.variableName = variableName;
|
this.variableName = variableName;
|
||||||
this.used = used;
|
this.used = used;
|
||||||
|
this.typeRequiresImport = used;
|
||||||
}
|
}
|
||||||
public Field(Type type, String variableName) {
|
public Field(Type type, String variableName) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.variableName = variableName;
|
this.variableName = variableName;
|
||||||
this.used = false;
|
this.used = false;
|
||||||
|
this.typeRequiresImport = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -85,4 +88,20 @@ public class Field extends ModelElement {
|
|||||||
this.used = isUsed;
|
this.used = isUsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* field needs to be imported
|
||||||
|
* @return true if the type should be included in the import of the generated type
|
||||||
|
*/
|
||||||
|
public boolean isTypeRequiresImport() {
|
||||||
|
return typeRequiresImport;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set field needs to be imported
|
||||||
|
* @param typeRequiresImport needs to be imported
|
||||||
|
*/
|
||||||
|
public void setTypeRequiresImport(boolean typeRequiresImport) {
|
||||||
|
this.typeRequiresImport = typeRequiresImport;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -49,13 +49,14 @@ public abstract class GeneratedType extends ModelElement {
|
|||||||
|
|
||||||
private final List<Annotation> annotations;
|
private final List<Annotation> annotations;
|
||||||
private final List<MappingMethod> methods;
|
private final List<MappingMethod> methods;
|
||||||
private final List<? extends ModelElement> fields;
|
private final List<? extends Field> fields;
|
||||||
private final SortedSet<Type> extraImportedTypes;
|
private final SortedSet<Type> extraImportedTypes;
|
||||||
|
|
||||||
private final boolean suppressGeneratorTimestamp;
|
private final boolean suppressGeneratorTimestamp;
|
||||||
private final boolean suppressGeneratorVersionComment;
|
private final boolean suppressGeneratorVersionComment;
|
||||||
private final VersionInformation versionInformation;
|
private final VersionInformation versionInformation;
|
||||||
private final Accessibility accessibility;
|
private final Accessibility accessibility;
|
||||||
|
private final Constructor constructor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Type representing the {@code @Generated} annotation
|
* Type representing the {@code @Generated} annotation
|
||||||
@ -66,11 +67,12 @@ public abstract class GeneratedType extends ModelElement {
|
|||||||
protected GeneratedType(TypeFactory typeFactory, String packageName, String name, String superClassName,
|
protected GeneratedType(TypeFactory typeFactory, String packageName, String name, String superClassName,
|
||||||
String interfaceName,
|
String interfaceName,
|
||||||
List<MappingMethod> methods,
|
List<MappingMethod> methods,
|
||||||
List<? extends ModelElement> fields,
|
List<? extends Field> fields,
|
||||||
Options options,
|
Options options,
|
||||||
VersionInformation versionInformation,
|
VersionInformation versionInformation,
|
||||||
Accessibility accessibility,
|
Accessibility accessibility,
|
||||||
SortedSet<Type> extraImportedTypes) {
|
SortedSet<Type> extraImportedTypes,
|
||||||
|
Constructor constructor ) {
|
||||||
this.packageName = packageName;
|
this.packageName = packageName;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.superClassName = superClassName;
|
this.superClassName = superClassName;
|
||||||
@ -87,6 +89,7 @@ public abstract class GeneratedType extends ModelElement {
|
|||||||
this.accessibility = accessibility;
|
this.accessibility = accessibility;
|
||||||
|
|
||||||
this.generatedType = typeFactory.getType( Generated.class );
|
this.generatedType = typeFactory.getType( Generated.class );
|
||||||
|
this.constructor = constructor;
|
||||||
}
|
}
|
||||||
|
|
||||||
// CHECKSTYLE:ON
|
// CHECKSTYLE:ON
|
||||||
@ -150,9 +153,11 @@ public abstract class GeneratedType extends ModelElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for ( ModelElement field : fields ) {
|
for ( Field field : fields ) {
|
||||||
for ( Type type : field.getImportTypes() ) {
|
if ( field.isTypeRequiresImport() ) {
|
||||||
addWithDependents( importedTypes, type );
|
for ( Type type : field.getImportTypes() ) {
|
||||||
|
addWithDependents( importedTypes, type );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -167,6 +172,10 @@ public abstract class GeneratedType extends ModelElement {
|
|||||||
return importedTypes;
|
return importedTypes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Constructor getConstructor() {
|
||||||
|
return constructor;
|
||||||
|
}
|
||||||
|
|
||||||
private void addWithDependents(Collection<Type> collection, Type typeToAdd) {
|
private void addWithDependents(Collection<Type> collection, Type typeToAdd) {
|
||||||
if ( typeToAdd == null ) {
|
if ( typeToAdd == null ) {
|
||||||
return;
|
return;
|
||||||
|
@ -63,7 +63,8 @@ public class Mapper extends GeneratedType {
|
|||||||
options,
|
options,
|
||||||
versionInformation,
|
versionInformation,
|
||||||
accessibility,
|
accessibility,
|
||||||
extraImportedTypes
|
extraImportedTypes,
|
||||||
|
null
|
||||||
);
|
);
|
||||||
|
|
||||||
this.referencedMappers = referencedMappers;
|
this.referencedMappers = referencedMappers;
|
||||||
|
@ -205,16 +205,18 @@ public class MapperCreationProcessor implements ModelElementProcessor<List<Sourc
|
|||||||
messager.printMessage( element, decoratorPrism.mirror, Message.DECORATOR_CONSTRUCTOR );
|
messager.printMessage( element, decoratorPrism.mirror, Message.DECORATOR_CONSTRUCTOR );
|
||||||
}
|
}
|
||||||
|
|
||||||
return Decorator.getInstance(
|
Decorator decorator = new Decorator.Builder()
|
||||||
elementUtils,
|
.elementUtils( elementUtils )
|
||||||
typeFactory,
|
.typeFactory( typeFactory )
|
||||||
element,
|
.mapperElement( element )
|
||||||
decoratorPrism,
|
.decoratorPrism( decoratorPrism )
|
||||||
mappingMethods,
|
.methods( mappingMethods )
|
||||||
hasDelegateConstructor,
|
.hasDelegateConstructor( hasDelegateConstructor )
|
||||||
options,
|
.options( options )
|
||||||
versionInformation
|
.versionInformation( versionInformation )
|
||||||
);
|
.build();
|
||||||
|
|
||||||
|
return decorator;
|
||||||
}
|
}
|
||||||
|
|
||||||
private SortedSet<Type> getExtraImports(TypeElement element) {
|
private SortedSet<Type> getExtraImports(TypeElement element) {
|
||||||
|
@ -502,6 +502,7 @@ public class MappingResolverImpl implements MappingResolver {
|
|||||||
for ( MapperReference ref : mapperReferences ) {
|
for ( MapperReference ref : mapperReferences ) {
|
||||||
if ( ref.getType().equals( method.getDeclaringMapper() ) ) {
|
if ( ref.getType().equals( method.getDeclaringMapper() ) ) {
|
||||||
ref.setUsed( !method.isStatic() );
|
ref.setUsed( !method.isStatic() );
|
||||||
|
ref.setTypeRequiresImport( true );
|
||||||
return ref;
|
return ref;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,5 +18,5 @@
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
|
|
||||||
-->
|
-->
|
||||||
<#if used><#nt><@includeModel object=annotation/>
|
<#nt><@includeModel object=annotation/>
|
||||||
private <@includeModel object=type/> ${variableName};</#if>
|
private <@includeModel object=type/> ${variableName};
|
@ -18,4 +18,4 @@
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
|
|
||||||
-->
|
-->
|
||||||
<#if used>private final <@includeModel object=type/> ${variableName} = <#if annotatedMapper>Mappers.getMapper( <@includeModel object=type/>.class );<#else>new <@includeModel object=type/>();</#if></#if>
|
private final <@includeModel object=type/> ${variableName} = <#if annotatedMapper>Mappers.getMapper( <@includeModel object=type/>.class );<#else>new <@includeModel object=type/>();</#if>
|
@ -18,4 +18,4 @@
|
|||||||
limitations under the License.
|
limitations under the License.
|
||||||
|
|
||||||
-->
|
-->
|
||||||
<#if used>private final <@includeModel object=type/> ${variableName};</#if>
|
private final <@includeModel object=type/> ${variableName};
|
@ -34,9 +34,10 @@ import ${importedType.importName};
|
|||||||
</#list>
|
</#list>
|
||||||
<#lt>${accessibility.keyword} class ${name}<#if superClassName??> extends ${superClassName}</#if><#if interfaceName??> implements ${interfaceName}</#if> {
|
<#lt>${accessibility.keyword} class ${name}<#if superClassName??> extends ${superClassName}</#if><#if interfaceName??> implements ${interfaceName}</#if> {
|
||||||
|
|
||||||
<#list fields as field>
|
<#list fields as field><#if field.used><#nt> <@includeModel object=field/>
|
||||||
<#nt> <@includeModel object=field/>
|
</#if></#list>
|
||||||
</#list>
|
|
||||||
|
<#if constructor??><#nt> <@includeModel object=constructor/></#if>
|
||||||
|
|
||||||
<#list methods as method>
|
<#list methods as method>
|
||||||
<#nt> <@includeModel object=method/>
|
<#nt> <@includeModel object=method/>
|
||||||
|
@ -0,0 +1,53 @@
|
|||||||
|
/**
|
||||||
|
* 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.ap.test.references.statics;
|
||||||
|
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.Mapping;
|
||||||
|
import org.mapstruct.ap.test.references.statics.nonused.NonUsedMapper;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
@Mapper(uses = NonUsedMapper.class )
|
||||||
|
public abstract class BeerMapperWithNonUsedMapper {
|
||||||
|
|
||||||
|
public static final BeerMapperWithNonUsedMapper INSTANCE = Mappers.getMapper( BeerMapperWithNonUsedMapper.class );
|
||||||
|
|
||||||
|
@Mapping( target = "category", source = "percentage")
|
||||||
|
public abstract BeerDto mapBeer(Beer beer);
|
||||||
|
|
||||||
|
public static Category toCategory(float in) {
|
||||||
|
|
||||||
|
if ( in < 2.5 ) {
|
||||||
|
return Category.LIGHT;
|
||||||
|
}
|
||||||
|
else if ( in < 5.5 ) {
|
||||||
|
return Category.LAGER;
|
||||||
|
}
|
||||||
|
else if ( in < 10 ) {
|
||||||
|
return Category.STRONG;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return Category.BARLEY_WINE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -23,18 +23,29 @@ import org.mapstruct.ap.testutil.IssueKey;
|
|||||||
import org.mapstruct.ap.testutil.WithClasses;
|
import org.mapstruct.ap.testutil.WithClasses;
|
||||||
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||||
import static org.fest.assertions.Assertions.assertThat;
|
import static org.fest.assertions.Assertions.assertThat;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.mapstruct.ap.test.references.statics.nonused.NonUsedMapper;
|
||||||
|
import org.mapstruct.ap.testutil.runner.GeneratedSource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author Sjaak Derksen
|
* @author Sjaak Derksen
|
||||||
*/
|
*/
|
||||||
@IssueKey( "410" )
|
@IssueKey( "410" )
|
||||||
@WithClasses( { Beer.class, BeerDto.class, BeerMapper.class, Category.class, CustomMapper.class } )
|
@WithClasses( { Beer.class, BeerDto.class, Category.class } )
|
||||||
@RunWith(AnnotationProcessorTestRunner.class)
|
@RunWith(AnnotationProcessorTestRunner.class)
|
||||||
public class StaticsTest {
|
public class StaticsTest {
|
||||||
|
|
||||||
|
private final GeneratedSource generatedSource = new GeneratedSource();
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public GeneratedSource getGeneratedSource() {
|
||||||
|
return generatedSource;
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@WithClasses( { BeerMapper.class, CustomMapper.class } )
|
||||||
public void shouldUseStaticMethod() {
|
public void shouldUseStaticMethod() {
|
||||||
|
|
||||||
Beer beer = new Beer(); // what the heck, open another one..
|
Beer beer = new Beer(); // what the heck, open another one..
|
||||||
@ -44,4 +55,19 @@ public class StaticsTest {
|
|||||||
assertThat( result ).isNotNull();
|
assertThat( result ).isNotNull();
|
||||||
assertThat( result.getCategory() ).isEqualTo( Category.STRONG ); // why settle for less?
|
assertThat( result.getCategory() ).isEqualTo( Category.STRONG ); // why settle for less?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@WithClasses( { BeerMapperWithNonUsedMapper.class, NonUsedMapper.class } )
|
||||||
|
public void shouldNotImportNonUsed() {
|
||||||
|
|
||||||
|
Beer beer = new Beer(); // what the heck, open another one..
|
||||||
|
beer.setPercentage( 7 );
|
||||||
|
|
||||||
|
BeerDto result = BeerMapperWithNonUsedMapper.INSTANCE.mapBeer( beer );
|
||||||
|
assertThat( result ).isNotNull();
|
||||||
|
assertThat( result.getCategory() ).isEqualTo( Category.STRONG ); // I could shurly use one now..
|
||||||
|
generatedSource.forMapper( BeerMapperWithNonUsedMapper.class ).containsNoImportFor( NonUsedMapper.class );
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
/**
|
||||||
|
* 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.ap.test.references.statics.nonused;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Sjaak Derksen
|
||||||
|
*/
|
||||||
|
public class NonUsedMapper {
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user