#660 Fix custom package / implementation name when using componentModel="default" with abstract classes

This commit is contained in:
Andreas Gudian 2015-10-24 20:55:45 +02:00
parent 82ab85d3bc
commit 1f091c35e2
4 changed files with 90 additions and 7 deletions

View File

@ -18,6 +18,13 @@
*/
package org.mapstruct.ap.internal.processor;
import java.io.IOException;
import javax.annotation.processing.Filer;
import javax.lang.model.element.TypeElement;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import org.mapstruct.ap.internal.model.GeneratedType;
import org.mapstruct.ap.internal.model.Mapper;
import org.mapstruct.ap.internal.model.ServicesEntry;
@ -25,12 +32,6 @@ import org.mapstruct.ap.internal.option.OptionsHelper;
import org.mapstruct.ap.internal.util.MapperConfiguration;
import org.mapstruct.ap.internal.writer.ModelWriter;
import javax.annotation.processing.Filer;
import javax.lang.model.element.TypeElement;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import java.io.IOException;
/**
* A {@link ModelElementProcessor} which creates files in the {@code META-INF/services}
* hierarchy for classes with custom implementation class or package name.
@ -77,7 +78,9 @@ public class MapperServiceProcessor implements ModelElementProcessor<Mapper, Vo
}
private ServicesEntry getServicesEntry(GeneratedType model) {
return new ServicesEntry(model.getInterfacePackage(), model.getInterfaceName(),
String mapperName = model.getInterfaceName() != null ? model.getInterfaceName() : model.getSuperClassName();
return new ServicesEntry(model.getInterfacePackage(), mapperName,
model.getPackageName(), model.getName());
}

View File

@ -0,0 +1,33 @@
/**
* 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.destination;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* @author Andreas Gudian
*/
@Mapper(implementationName = "My<CLASS_NAME>CustomImpl")
public abstract class AbstractDestinationClassNameMapper {
public static final AbstractDestinationClassNameMapper INSTANCE =
Mappers.getMapper( AbstractDestinationClassNameMapper.class );
public abstract String intToString(Integer source);
}

View File

@ -0,0 +1,33 @@
/**
* 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.destination;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* @author Andreas Gudian
*/
@Mapper(implementationPackage = "<PACKAGE_NAME>.dest")
public abstract class AbstractDestinationPackageNameMapper {
public static final AbstractDestinationPackageNameMapper INSTANCE =
Mappers.getMapper( AbstractDestinationPackageNameMapper.class );
public abstract String intToString(Integer source);
}

View File

@ -85,4 +85,18 @@ public class DestinationClassNameTest {
assertThat( ( (DestinationClassNameMapperDecorator) instance ).delegate.getClass().getSimpleName() )
.isEqualTo( "MyDestinationClassNameMapperDecoratedCustomImpl_" );
}
@Test
@WithClasses({ AbstractDestinationClassNameMapper.class, AbstractDestinationPackageNameMapper.class })
public void shouldWorkWithAbstractClasses() {
AbstractDestinationClassNameMapper mapper1 = AbstractDestinationClassNameMapper.INSTANCE;
assertThat( mapper1.getClass().getPackage().getName() )
.isEqualTo( AbstractDestinationClassNameMapper.class.getPackage().getName() );
assertThat( mapper1.getClass().getSimpleName() ).isEqualTo( "MyAbstractDestinationClassNameMapperCustomImpl" );
AbstractDestinationPackageNameMapper mapper2 = AbstractDestinationPackageNameMapper.INSTANCE;
assertThat( mapper2.getClass().getPackage().getName() )
.isEqualTo( AbstractDestinationPackageNameMapper.class.getPackage().getName() + ".dest" );
assertThat( mapper2.getClass().getSimpleName() ).isEqualTo( "AbstractDestinationPackageNameMapperImpl" );
}
}