#999 make sure that the qualifiedName of the class is trimmed before doing any comparisons for import check

This commit is contained in:
Filip Hrisafov 2016-12-20 00:06:12 +01:00 committed by GitHub
parent 79f87e8833
commit fc0f13a7a1
2 changed files with 32 additions and 5 deletions

View File

@ -36,7 +36,6 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentNavigableMap; import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.ConcurrentSkipListMap;
import javax.lang.model.element.Element; import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind; import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.ExecutableElement;
@ -444,16 +443,17 @@ public class TypeFactory {
private boolean isImported(String name, String qualifiedName) { private boolean isImported(String name, String qualifiedName) {
String trimmedName = TypeFactory.trimSimpleClassName( name ); String trimmedName = TypeFactory.trimSimpleClassName( name );
String trimmedQualifiedName = TypeFactory.trimSimpleClassName( qualifiedName );
String importedType = importedQualifiedTypesBySimpleName.get( trimmedName ); String importedType = importedQualifiedTypesBySimpleName.get( trimmedName );
boolean imported = false; boolean imported = false;
if ( importedType != null ) { if ( importedType != null ) {
if ( importedType.equals( qualifiedName ) ) { if ( importedType.equals( trimmedQualifiedName ) ) {
imported = true; imported = true;
} }
} }
else { else {
importedQualifiedTypesBySimpleName.put( trimmedName, qualifiedName ); importedQualifiedTypesBySimpleName.put( trimmedName, trimmedQualifiedName );
imported = true; imported = true;
} }
return imported; return imported;
@ -530,6 +530,19 @@ public class TypeFactory {
return typeMirror; return typeMirror;
} }
/**
* It strips the all the {@code []} from the {@code className}.
*
* E.g.
* <pre>
* trimSimpleClassName("String[][][]") -> "String"
* trimSimpleClassName("String[]") -> "String"
* </pre>
*
* @param className that needs to be trimmed
*
* @return the trimmed {@code className}, or {@code null} if the {@code className} was {@code null}
*/
static String trimSimpleClassName(String className) { static String trimSimpleClassName(String className) {
if ( className == null ) { if ( className == null ) {
return null; return null;

View File

@ -18,11 +18,10 @@
*/ */
package org.mapstruct.ap.test.array; package org.mapstruct.ap.test.array;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mapstruct.ap.test.array._target.ScientistDto; import org.mapstruct.ap.test.array._target.ScientistDto;
@ -30,12 +29,22 @@ import org.mapstruct.ap.test.array.source.Scientist;
import org.mapstruct.ap.testutil.IssueKey; 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 org.mapstruct.ap.testutil.runner.GeneratedSource;
import static org.assertj.core.api.Assertions.assertThat;
@WithClasses( { Scientist.class, ScientistDto.class, ScienceMapper.class } ) @WithClasses( { Scientist.class, ScientistDto.class, ScienceMapper.class } )
@RunWith(AnnotationProcessorTestRunner.class) @RunWith(AnnotationProcessorTestRunner.class)
@IssueKey("108") @IssueKey("108")
public class ArrayMappingTest { public class ArrayMappingTest {
private final GeneratedSource generatedSource = new GeneratedSource();
@Rule
public GeneratedSource getGeneratedSource() {
return generatedSource;
}
@Test @Test
public void shouldCopyArraysInBean() { public void shouldCopyArraysInBean() {
@ -226,4 +235,9 @@ public class ArrayMappingTest {
assertThat( existingTarget ).containsOnly( new long[] { 0L } ); assertThat( existingTarget ).containsOnly( new long[] { 0L } );
} }
@Test
@IssueKey( "999" )
public void shouldNotContainFQNForStringArray() {
getGeneratedSource().forMapper( ScienceMapper.class ).content().doesNotContain( "java.lang.String[]" );
}
} }