Skip to content

Commit d056570

Browse files
committed
mapstruct#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
1 parent 3277301 commit d056570

7 files changed

Lines changed: 152 additions & 2 deletions

File tree

processor/src/main/java/org/mapstruct/ap/internal/model/GeneratedType.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,15 @@ private boolean needsImportDeclaration(Type typeToAdd) {
288288
}
289289

290290
if ( typeToAdd.getPackageName().equals( packageName ) ) {
291-
if ( !typeToAdd.getTypeElement().getNestingKind().isNested() ) {
292-
return false;
291+
if ( typeToAdd.getTypeElement() != null ) {
292+
if ( !typeToAdd.getTypeElement().getNestingKind().isNested() ) {
293+
return false;
294+
}
295+
}
296+
else if ( typeToAdd.getComponentType() != null ) {
297+
if ( !typeToAdd.getComponentType().getTypeElement().getNestingKind().isNested() ) {
298+
return false;
299+
}
293300
}
294301
}
295302
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.ap.test.bugs._2001;
7+
8+
import java.util.Set;
9+
10+
/**
11+
* @author Filip Hrisafov
12+
*/
13+
public
14+
class Entity {
15+
16+
private Set<EntityExtra> extras;
17+
18+
public Set<EntityExtra> getExtras() {
19+
return extras;
20+
}
21+
22+
public void setExtras(Set<EntityExtra> extras) {
23+
this.extras = extras;
24+
}
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.ap.test.bugs._2001;
7+
8+
/**
9+
* @author Filip Hrisafov
10+
*/
11+
public
12+
class EntityExtra {
13+
14+
private String value;
15+
16+
public String getValue() {
17+
return value;
18+
}
19+
20+
public void setValue(String value) {
21+
this.value = value;
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.ap.test.bugs._2001;
7+
8+
/**
9+
* @author Filip Hrisafov
10+
*/
11+
public
12+
class Form {
13+
14+
private FormExtra[] extras;
15+
16+
public FormExtra[] getExtras() {
17+
return extras;
18+
}
19+
20+
public void setExtras(FormExtra[] extras) {
21+
this.extras = extras;
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.ap.test.bugs._2001;
7+
8+
/**
9+
* @author Filip Hrisafov
10+
*/
11+
public
12+
class FormExtra {
13+
14+
private String value;
15+
16+
public String getValue() {
17+
return value;
18+
}
19+
20+
public void setValue(String value) {
21+
this.value = value;
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.ap.test.bugs._2001;
7+
8+
import org.mapstruct.Mapper;
9+
10+
/**
11+
* @author Filip Hrisafov
12+
*/
13+
@Mapper
14+
public interface Issue2001Mapper {
15+
16+
Form map(Entity entity);
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.ap.test.bugs._2001;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.mapstruct.ap.testutil.IssueKey;
11+
import org.mapstruct.ap.testutil.WithClasses;
12+
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
13+
14+
/**
15+
* @author Filip Hrisafov
16+
*/
17+
@IssueKey("2001")
18+
@RunWith( AnnotationProcessorTestRunner.class )
19+
@WithClasses( {
20+
Entity.class,
21+
EntityExtra.class,
22+
Form.class,
23+
FormExtra.class,
24+
Issue2001Mapper.class
25+
} )
26+
public class Issue2001Test {
27+
28+
@Test
29+
public void shouldCompile() {
30+
31+
}
32+
}

0 commit comments

Comments
 (0)