Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
*/
package org.mapstruct.intellij.inspection;

import java.util.Optional;

import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.ContributedReferenceHost;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiLanguageInjectionHost;
import com.intellij.psi.PsiReference;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.mapstruct.intellij.codeinsight.references.BaseReference;
import org.mapstruct.intellij.codeinsight.references.BaseValueMappingReference;
Expand Down Expand Up @@ -64,7 +68,14 @@ private boolean shouldRegisterProblem(BaseReference reference) {
if ( reference instanceof BaseValueMappingReference valueMappingReference ) {
return valueMappingReference.getEnumClass() != null;
}
return true;

return !containingClassIsAnnotationType( reference.getElement() );
}

private boolean containingClassIsAnnotationType(PsiElement element) {
return Optional.ofNullable( PsiTreeUtil.getParentOfType( element, PsiClass.class ) )
.map( PsiClass::isAnnotationType )
.orElse( false );
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is really no need to use ofNullable here. I prefer assigning this to a value and doing a good old regular null check.

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.intellij.bugs._236;

import org.jetbrains.annotations.NotNull;
import org.mapstruct.intellij.inspection.BaseInspectionTest;
import org.mapstruct.intellij.inspection.MapstructReferenceInspection;

/**
* @author Oliver Erhart
*/
public class DisableSourceAndTargetPropertyInspectionOnAnnotationsTest extends BaseInspectionTest {

@Override
protected String getTestDataPath() {
return "testData/bugs/_236";
}

@NotNull
@Override
protected Class<MapstructReferenceInspection> getInspection() {
return MapstructReferenceInspection.class;
}

public void testDisableSourceAndTargetPropertyInspectionOnAnnotations() {
doTest();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at https://www.apache.org/licenses/LICENSE-2.0
*/

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import org.mapstruct.Mapping;

@Retention(RetentionPolicy.CLASS)
@Mapping(target = "id", ignore = true)
@Mapping(target = "creationDate", expression = "java(new java.util.Date())")
@Mapping(target = "name", source = "groupName")
@interface ToEntity { }