Skip to content

Commit b9d0c60

Browse files
committed
Add an enum for the known MapStruct versions to simplify different configuration options
1 parent 1eaab98 commit b9d0c60

File tree

5 files changed

+68
-22
lines changed

5 files changed

+68
-22
lines changed

src/main/java/org/mapstruct/intellij/codeinsight/references/MapstructTargetReference.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.intellij.psi.util.PsiUtil;
2525
import org.jetbrains.annotations.NotNull;
2626
import org.jetbrains.annotations.Nullable;
27+
import org.mapstruct.intellij.util.MapStructVersion;
2728
import org.mapstruct.intellij.util.MapstructUtil;
2829

2930
import static org.mapstruct.intellij.util.MapstructUtil.asLookup;
@@ -39,7 +40,7 @@
3940
*/
4041
class MapstructTargetReference extends MapstructBaseReference {
4142

42-
private final boolean builderSupportPresent;
43+
private final MapStructVersion mapStructVersion;
4344

4445
/**
4546
* Create a new {@link MapstructTargetReference} with the provided parameters
@@ -51,12 +52,13 @@ class MapstructTargetReference extends MapstructBaseReference {
5152
private MapstructTargetReference(PsiLiteral element, MapstructTargetReference previousReference,
5253
TextRange rangeInElement) {
5354
super( element, previousReference, rangeInElement );
54-
builderSupportPresent = MapstructUtil.isMapStructBuilderSupportPresent( element.getContainingFile()
55+
mapStructVersion = MapstructUtil.resolveMapStructProjectVersion( element.getContainingFile()
5556
.getOriginalFile() );
5657
}
5758

5859
@Override
5960
PsiElement resolveInternal(@NotNull String value, @NotNull PsiType psiType) {
61+
boolean builderSupportPresent = mapStructVersion.isBuilderSupported();
6062
Pair<PsiClass, PsiType> pair = resolveBuilderOrSelfClass( psiType, builderSupportPresent );
6163
if ( pair == null ) {
6264
return null;
@@ -113,7 +115,7 @@ PsiElement resolveInternal(@NotNull String value, @NotNull PsiMethod mappingMeth
113115
@Override
114116
Object[] getVariantsInternal(@NotNull PsiType psiType) {
115117
return asLookup(
116-
publicWriteAccessors( psiType, builderSupportPresent ),
118+
publicWriteAccessors( psiType, mapStructVersion ),
117119
MapstructTargetReference::memberPsiType
118120
);
119121
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.jetbrains.annotations.NotNull;
3434
import org.jetbrains.annotations.Nullable;
3535
import org.mapstruct.intellij.MapStructBundle;
36+
import org.mapstruct.intellij.util.MapStructVersion;
3637
import org.mapstruct.intellij.util.MapstructUtil;
3738
import org.mapstruct.intellij.util.TargetUtils;
3839

@@ -52,16 +53,16 @@ public class UnmappedTargetPropertiesInspection extends InspectionBase {
5253
@NotNull
5354
@Override
5455
PsiElementVisitor buildVisitorInternal(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
55-
return new MyJavaElementVisitor( holder, MapstructUtil.isMapStructBuilderSupportPresent( holder.getFile() ) );
56+
return new MyJavaElementVisitor( holder, MapstructUtil.resolveMapStructProjectVersion( holder.getFile() ) );
5657
}
5758

5859
private static class MyJavaElementVisitor extends JavaElementVisitor {
5960
private final ProblemsHolder holder;
60-
private final boolean builderSupportPresent;
61+
private final MapStructVersion mapStructVersion;
6162

62-
private MyJavaElementVisitor(ProblemsHolder holder, boolean builderSupportPresent) {
63+
private MyJavaElementVisitor(ProblemsHolder holder, MapStructVersion mapStructVersion) {
6364
this.holder = holder;
64-
this.builderSupportPresent = builderSupportPresent;
65+
this.mapStructVersion = mapStructVersion;
6566
}
6667

6768
@Override
@@ -73,7 +74,7 @@ public void visitMethod(PsiMethod method) {
7374
return;
7475
}
7576

76-
Set<String> allTargetProperties = findAllTargetProperties( targetType, builderSupportPresent );
77+
Set<String> allTargetProperties = findAllTargetProperties( targetType, mapStructVersion );
7778

7879
// find and remove all defined mapping targets
7980
Set<String> definedTargets = TargetUtils.findAllDefinedMappingTargets( method )
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.intellij.util;
7+
8+
/**
9+
* @author Filip Hrisafov
10+
*/
11+
public enum MapStructVersion {
12+
13+
V1_2_O( false, false ),
14+
V1_3_O( true, false ),
15+
V1_4_O( true, true );
16+
17+
private final boolean builderSupported;
18+
private final boolean constructorSupported;
19+
20+
MapStructVersion(boolean builderSupported, boolean constructorSupported) {
21+
this.builderSupported = builderSupported;
22+
this.constructorSupported = constructorSupported;
23+
}
24+
25+
public boolean isBuilderSupported() {
26+
return builderSupported;
27+
}
28+
29+
public boolean isConstructorSupported() {
30+
return constructorSupported;
31+
}
32+
}

src/main/java/org/mapstruct/intellij/util/MapstructUtil.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public final class MapstructUtil {
8686
private static final String CONTEXT_ANNOTATION_FQN = "org.mapstruct.Context";
8787
private static final String INHERIT_INVERSE_CONFIGURATION = InheritInverseConfiguration.class.getName();
8888
private static final String BUILDER_ANNOTATION_FQN = "org.mapstruct.Builder";
89+
private static final String ENUM_MAPPING_ANNOTATION_FQN = "org.mapstruct.EnumMapping";
8990

9091
/**
9192
* Hide constructor.
@@ -457,22 +458,30 @@ private static boolean isMapStructPresent(@NotNull Module module) {
457458
}
458459

459460
/**
460-
* Checks if MapStruct 1.3.0 is within the module of the provided psi file.
461-
*
461+
* Resolve the MapStruct project version with the module of the provided psi file
462462
* @param psiFile that needs to be checked
463-
*
464-
* @return {@code true} if MapStruct is present within the {@code psiFIle}, {@code false} otherwise
463+
* @return the MapStruct project version
465464
*/
466-
public static boolean isMapStructBuilderSupportPresent(@NotNull PsiFile psiFile) {
465+
public static MapStructVersion resolveMapStructProjectVersion(@NotNull PsiFile psiFile) {
467466
Module module = ModuleUtilCore.findModuleForFile( psiFile.getVirtualFile(), psiFile.getProject() );
468467
if ( module == null ) {
469-
return false;
468+
return MapStructVersion.V1_2_O;
470469
}
471470
return CachedValuesManager.getManager( module.getProject() ).getCachedValue( module, () -> {
472-
boolean foundMarkerClass = JavaPsiFacade.getInstance( module.getProject() )
473-
.findClass( BUILDER_ANNOTATION_FQN, module.getModuleRuntimeScope( false ) ) != null;
471+
MapStructVersion mapStructVersion;
472+
if ( JavaPsiFacade.getInstance( module.getProject() )
473+
.findClass( ENUM_MAPPING_ANNOTATION_FQN, module.getModuleRuntimeScope( false ) ) != null ) {
474+
mapStructVersion = MapStructVersion.V1_4_O;
475+
}
476+
else if ( JavaPsiFacade.getInstance( module.getProject() )
477+
.findClass( BUILDER_ANNOTATION_FQN, module.getModuleRuntimeScope( false ) ) != null ) {
478+
mapStructVersion = MapStructVersion.V1_3_O;
479+
}
480+
else {
481+
mapStructVersion = MapStructVersion.V1_2_O;
482+
}
474483
return CachedValueProvider.Result.createSingleDependency(
475-
foundMarkerClass,
484+
mapStructVersion,
476485
ProjectRootManager.getInstance( module.getProject() )
477486
);
478487
} );

src/main/java/org/mapstruct/intellij/util/TargetUtils.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ public static PsiType getRelevantType(@NotNull PsiMethod mappingMethod) {
8080
* with their psi substitutors from the given {@code psiType}
8181
*
8282
* @param psiType to use to extract the accessors
83+
* @param mapStructVersion the MapStruct project version
8384
*
8485
* @return a stream that holds all public write accessors for the given {@code psiType}
8586
*/
8687
public static Map<String, Pair<? extends PsiMember, PsiSubstitutor>> publicWriteAccessors(@NotNull PsiType psiType,
87-
boolean builderSupportPresent) {
88+
MapStructVersion mapStructVersion) {
89+
boolean builderSupportPresent = mapStructVersion.isBuilderSupported();
8890
Pair<PsiClass, PsiType> classAndType = resolveBuilderOrSelfClass( psiType, builderSupportPresent );
8991
if ( classAndType == null ) {
9092
return Collections.emptyMap();
@@ -106,7 +108,7 @@ public static Map<String, Pair<? extends PsiMember, PsiSubstitutor>> publicWrite
106108
*
107109
* @param psiClass to use to extract the setters
108110
* @param typeToUse the type in which the methods are located (needed for fluent setters)
109-
* @param builderSupportPresent whether MapStruct (1.3) with builder suppot is present
111+
* @param builderSupportPresent whether MapStruct (1.3) with builder support is present
110112
*
111113
* @return a stream that holds all public setters for the given {@code psiType}
112114
*/
@@ -290,11 +292,11 @@ private static boolean isMappingAnnotation(PsiAnnotation psiAnnotation) {
290292
* Find all target properties from the {@code targetClass} that can be used for mapping
291293
*
292294
* @param targetType that needs to be used
293-
* @param builderSupportPresent whether MapStruct (1.3) with builder support is present
295+
* @param mapStructVersion the MapStruct project version
294296
*
295297
* @return all target properties for the given {@code targetClass}
296298
*/
297-
public static Set<String> findAllTargetProperties(@NotNull PsiType targetType, boolean builderSupportPresent) {
298-
return publicWriteAccessors( targetType, builderSupportPresent ).keySet();
299+
public static Set<String> findAllTargetProperties(@NotNull PsiType targetType, MapStructVersion mapStructVersion) {
300+
return publicWriteAccessors( targetType, mapStructVersion ).keySet();
299301
}
300302
}

0 commit comments

Comments
 (0)