#2001 Avoid NPE when checking whether import type element is nested

When the typeToAdd is an array then TypeElement is null and ComponentType is the one that would be imported
This commit is contained in:
Filip Hrisafov 2020-01-15 21:33:11 +01:00
parent 327730127b
commit d056570267
7 changed files with 152 additions and 2 deletions

View File

@ -288,10 +288,17 @@ public abstract class GeneratedType extends ModelElement {
} }
if ( typeToAdd.getPackageName().equals( packageName ) ) { if ( typeToAdd.getPackageName().equals( packageName ) ) {
if ( typeToAdd.getTypeElement() != null ) {
if ( !typeToAdd.getTypeElement().getNestingKind().isNested() ) { if ( !typeToAdd.getTypeElement().getNestingKind().isNested() ) {
return false; return false;
} }
} }
else if ( typeToAdd.getComponentType() != null ) {
if ( !typeToAdd.getComponentType().getTypeElement().getNestingKind().isNested() ) {
return false;
}
}
}
} }
return true; return true;

View File

@ -0,0 +1,25 @@
/*
* 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.bugs._2001;
import java.util.Set;
/**
* @author Filip Hrisafov
*/
public
class Entity {
private Set<EntityExtra> extras;
public Set<EntityExtra> getExtras() {
return extras;
}
public void setExtras(Set<EntityExtra> extras) {
this.extras = extras;
}
}

View File

@ -0,0 +1,23 @@
/*
* 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.bugs._2001;
/**
* @author Filip Hrisafov
*/
public
class EntityExtra {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,23 @@
/*
* 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.bugs._2001;
/**
* @author Filip Hrisafov
*/
public
class Form {
private FormExtra[] extras;
public FormExtra[] getExtras() {
return extras;
}
public void setExtras(FormExtra[] extras) {
this.extras = extras;
}
}

View File

@ -0,0 +1,23 @@
/*
* 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.bugs._2001;
/**
* @author Filip Hrisafov
*/
public
class FormExtra {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}

View File

@ -0,0 +1,17 @@
/*
* 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.bugs._2001;
import org.mapstruct.Mapper;
/**
* @author Filip Hrisafov
*/
@Mapper
public interface Issue2001Mapper {
Form map(Entity entity);
}

View File

@ -0,0 +1,32 @@
/*
* 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.bugs._2001;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
/**
* @author Filip Hrisafov
*/
@IssueKey("2001")
@RunWith( AnnotationProcessorTestRunner.class )
@WithClasses( {
Entity.class,
EntityExtra.class,
Form.class,
FormExtra.class,
Issue2001Mapper.class
} )
public class Issue2001Test {
@Test
public void shouldCompile() {
}
}