Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#151 rework inheritance and support auto inheritance #152

Merged
merged 8 commits into from
Sep 17, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static org.mapstruct.intellij.util.TargetUtils.isBuilderEnabled;
import static org.mapstruct.intellij.util.TargetUtils.publicWriteAccessors;
import static org.mapstruct.intellij.util.TargetUtils.resolveBuilderOrSelfClass;
import static org.mapstruct.intellij.util.TypeUtils.firstParameterPsiType;

/**
* Reference for {@link org.mapstruct.Mapping#target()}.
Expand Down Expand Up @@ -227,19 +228,4 @@ else if ( psiMember instanceof PsiVariable ) {
}

}

/**
* Util function for extracting the type of the first parameter of a method.
*
* @param psiMethod the method to extract the parameter from
*
* @return the type of the first parameter of the method
*/
private static PsiType firstParameterPsiType(PsiMethod psiMethod) {
PsiParameter[] psiParameters = psiMethod.getParameterList().getParameters();
if ( psiParameters.length == 0) {
return null;
}
return psiParameters[0].getType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -41,16 +42,17 @@

import static com.intellij.codeInsight.AnnotationUtil.findAnnotation;
import static com.intellij.codeInsight.AnnotationUtil.getBooleanAttributeValue;
import static org.mapstruct.intellij.inspection.inheritance.InheritConfigurationUtils.findInheritedTargetProperties;
import static org.mapstruct.intellij.util.MapstructAnnotationUtils.addMappingAnnotation;
import static org.mapstruct.intellij.util.MapstructAnnotationUtils.getUnmappedTargetPolicy;
import static org.mapstruct.intellij.util.MapstructUtil.MAPPER_CONFIG_ANNOTATION_FQN;
import static org.mapstruct.intellij.util.MapstructUtil.isInheritInverseConfiguration;
import static org.mapstruct.intellij.util.MapstructUtil.isMapper;
import static org.mapstruct.intellij.util.MapstructUtil.isMapperConfig;
import static org.mapstruct.intellij.util.SourceUtils.findAllSourceProperties;
import static org.mapstruct.intellij.util.TargetUtils.findAllDefinedMappingTargets;
import static org.mapstruct.intellij.util.TargetUtils.findAllSourcePropertiesForCurrentTarget;
import static org.mapstruct.intellij.util.TargetUtils.findAllTargetProperties;
import static org.mapstruct.intellij.util.TargetUtils.findInheritedTargetProperties;
import static org.mapstruct.intellij.util.TargetUtils.getRelevantType;

/**
Expand Down Expand Up @@ -86,11 +88,18 @@ public void visitMethod(PsiMethod method) {
if ( isBeanMappingIgnoreByDefault( method ) ) {
return;
}

ReportingPolicy reportingPolicy = getUnmappedTargetPolicy( method );
if (reportingPolicy == ReportingPolicy.IGNORE) {
return;
}

if ( isPrototypeMethod( method )) {
// prototype methods can be incomplete and therefore should not provide errors or warnings
// that cannot be solved with quick fixes
return;
}


Set<String> allTargetProperties = findAllTargetProperties( targetType, mapStructVersion, method );

Expand Down Expand Up @@ -181,6 +190,19 @@ private static boolean isBeanMappingIgnoreByDefault(PsiMethod method) {
return false;
}

private static boolean isPrototypeMethod(@NotNull PsiMethod method) {

return isContainedInsideMapperConfig( method );
}

private static boolean isContainedInsideMapperConfig(@NotNull PsiMethod method) {

return Optional.ofNullable( method.getContainingClass() )
.map( containingClass -> findAnnotation( containingClass, MAPPER_CONFIG_ANNOTATION_FQN ) )
.stream().findFirst()
.isPresent();
filiphr marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param method the method to be used
*
Expand All @@ -204,6 +226,7 @@ private static PsiType getTargetType(PsiMethod method) {
}
return getRelevantType( method );
}

}

private static class UnmappedTargetPropertyFix extends LocalQuickFixOnPsiElement {
Expand Down
Loading
Loading