Skip to content

Commit f2abfa0

Browse files
authored
#236: Do not register problems on mapping composition annotations (#239)
1 parent 33135b9 commit f2abfa0

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

src/main/java/org/mapstruct/intellij/inspection/MapstructReferenceInspection.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import com.intellij.codeInspection.ProblemsHolder;
1010
import com.intellij.openapi.util.TextRange;
1111
import com.intellij.psi.ContributedReferenceHost;
12+
import com.intellij.psi.PsiClass;
1213
import com.intellij.psi.PsiElement;
1314
import com.intellij.psi.PsiElementVisitor;
1415
import com.intellij.psi.PsiLanguageInjectionHost;
1516
import com.intellij.psi.PsiReference;
17+
import com.intellij.psi.util.PsiTreeUtil;
1618
import org.jetbrains.annotations.NotNull;
1719
import org.mapstruct.intellij.codeinsight.references.BaseReference;
1820
import org.mapstruct.intellij.codeinsight.references.BaseValueMappingReference;
@@ -64,7 +66,19 @@ private boolean shouldRegisterProblem(BaseReference reference) {
6466
if ( reference instanceof BaseValueMappingReference valueMappingReference ) {
6567
return valueMappingReference.getEnumClass() != null;
6668
}
67-
return true;
69+
70+
return !containingClassIsAnnotationType( reference.getElement() );
71+
}
72+
73+
private boolean containingClassIsAnnotationType(PsiElement element) {
74+
75+
PsiClass containingClass = PsiTreeUtil.getParentOfType( element, PsiClass.class );
76+
77+
if ( containingClass == null ) {
78+
return false;
79+
}
80+
81+
return containingClass.isAnnotationType();
6882
}
6983
}
7084
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
package org.mapstruct.intellij.bugs._236;
7+
8+
import org.jetbrains.annotations.NotNull;
9+
import org.mapstruct.intellij.inspection.BaseInspectionTest;
10+
import org.mapstruct.intellij.inspection.MapstructReferenceInspection;
11+
12+
/**
13+
* @author Oliver Erhart
14+
*/
15+
public class DisableSourceAndTargetPropertyInspectionOnAnnotationsTest extends BaseInspectionTest {
16+
17+
@Override
18+
protected String getTestDataPath() {
19+
return "testData/bugs/_236";
20+
}
21+
22+
@NotNull
23+
@Override
24+
protected Class<MapstructReferenceInspection> getInspection() {
25+
return MapstructReferenceInspection.class;
26+
}
27+
28+
public void testDisableSourceAndTargetPropertyInspectionOnAnnotations() {
29+
doTest();
30+
}
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright MapStruct Authors.
3+
*
4+
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0
5+
*/
6+
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.RetentionPolicy;
9+
10+
import org.mapstruct.Mapping;
11+
import org.mapstruct.Mappings;
12+
13+
@Retention(RetentionPolicy.CLASS)
14+
@Mapping(target = "id", ignore = true)
15+
@Mapping(target = "creationDate", expression = "java(new java.util.Date())")
16+
@Mapping(target = "name", source = "groupName")
17+
@interface ToEntity { }
18+
19+
@Retention(RetentionPolicy.CLASS)
20+
@Mappings({
21+
@Mapping(target = "id", ignore = true),
22+
@Mapping(target = "creationDate", expression = "java(new java.util.Date())"),
23+
@Mapping(target = "name", source = "groupName")
24+
})
25+
@interface ToEntityWithMappings { }
26+

0 commit comments

Comments
 (0)