Skip to content

Commit be54e14

Browse files
author
Stephan Leicht Vogt (C803964)
committed
#124 Add custom Immutables and FreeBuilder fluent setter MapstructUtils
1 parent 451f63e commit be54e14

14 files changed

+866
-524
lines changed

build.gradle

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ dependencies {
9999
testRuntimeOnly('org.junit.vintage:junit-vintage-engine')
100100
testImplementation('org.assertj:assertj-core:3.11.1')
101101
testImplementation('org.apache.commons:commons-text:1.9')
102+
testRuntimeOnly('org.immutables:value:2.5.6')
102103
}
103104

104105
task libs(type: Sync) {
@@ -110,6 +111,12 @@ task libs(type: Sync) {
110111
rename 'mapstruct-1.5.3.Final.jar', 'mapstruct.jar'
111112
}
112113

114+
task testLibs(type: Sync) {
115+
from configurations.testRuntimeClasspath
116+
into "$buildDir/test-libs"
117+
rename 'value-2.5.6.jar', 'immutables.jar'
118+
}
119+
113120
def mockJdkLocation = "https://github.com/JetBrains/intellij-community/raw/master/java/mock"
114121
def mockJdkDest = "$buildDir/mock"
115122
def downloadMockJdk(mockJdkLocation, mockJdkDest, mockJdkVersion) {
@@ -149,8 +156,8 @@ task downloadMockJdk11() {
149156
downloadMockJdk(mockJdkLocation, mockJdkDest, "JDK-11")
150157
}
151158

152-
test.dependsOn( libs, downloadMockJdk7, downloadMockJdk8, downloadMockJdk11 )
153-
prepareTestingSandbox.dependsOn( libs )
159+
test.dependsOn( libs, testLibs, downloadMockJdk7, downloadMockJdk8, downloadMockJdk11 )
160+
prepareTestingSandbox.dependsOn( libs, testLibs )
154161
prepareSandbox.dependsOn( libs )
155162

156163
test {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ abstract class MapstructBaseReference extends BaseReference {
3434

3535
private final MapstructBaseReference previous;
3636
private final String value;
37+
protected final MapstructUtil mapstructUtil;
3738

3839
/**
3940
* Create a reference.
@@ -47,6 +48,7 @@ abstract class MapstructBaseReference extends BaseReference {
4748
super( element, rangeInElement );
4849
this.previous = previous;
4950
this.value = value;
51+
this.mapstructUtil = MapstructUtil.getInstance(element.getContainingFile());
5052
}
5153

5254
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ builderSupportPresent && isBuilderEnabled( getMappingMethod() )
100100
if ( builderSupportPresent ) {
101101
for ( PsiMethod method : psiClass.findMethodsByName( value, true ) ) {
102102
if ( method.getParameterList().getParametersCount() == 1 &&
103-
MapstructUtil.isFluentSetter( method, typeToUse ) ) {
103+
mapstructUtil.isFluentSetter( method, typeToUse ) ) {
104104
return method;
105105
}
106106
}
@@ -140,7 +140,7 @@ PsiElement resolveInternal(@NotNull String value, @NotNull PsiMethod mappingMeth
140140
@Override
141141
Object[] getVariantsInternal(@NotNull PsiType psiType) {
142142
return asLookup(
143-
publicWriteAccessors( psiType, mapStructVersion, getMappingMethod() ),
143+
publicWriteAccessors( psiType, mapStructVersion, mapstructUtil, getMappingMethod() ),
144144
MapstructTargetReference::memberPsiType
145145
);
146146
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,19 @@ public class UnmappedTargetPropertiesInspection extends InspectionBase {
5757
@NotNull
5858
@Override
5959
PsiElementVisitor buildVisitorInternal(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
60-
return new MyJavaElementVisitor( holder, MapstructUtil.resolveMapStructProjectVersion( holder.getFile() ) );
60+
return new MyJavaElementVisitor( holder, MapstructUtil.resolveMapStructProjectVersion( holder.getFile() ), MapstructUtil.getInstance( holder.getFile() ) );
6161
}
6262

6363
private static class MyJavaElementVisitor extends JavaElementVisitor {
6464
private final ProblemsHolder holder;
6565
private final MapStructVersion mapStructVersion;
66+
private final MapstructUtil mapstructUtil;
6667

67-
private MyJavaElementVisitor(ProblemsHolder holder, MapStructVersion mapStructVersion) {
68+
private MyJavaElementVisitor(ProblemsHolder holder, MapStructVersion mapStructVersion, MapstructUtil mapstructUtil) {
6869
this.holder = holder;
6970
this.mapStructVersion = mapStructVersion;
70-
}
71+
this.mapstructUtil = mapstructUtil;
72+
}
7173

7274
@Override
7375
public void visitMethod(PsiMethod method) {
@@ -82,7 +84,7 @@ public void visitMethod(PsiMethod method) {
8284
return;
8385
}
8486

85-
Set<String> allTargetProperties = findAllTargetProperties( targetType, mapStructVersion, method );
87+
Set<String> allTargetProperties = findAllTargetProperties( targetType, mapStructVersion, mapstructUtil, method );
8688

8789
// find and remove all defined mapping targets
8890
Set<String> definedTargets = TargetUtils.findAllDefinedMappingTargets( method, mapStructVersion )
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
public class DefaultMapstructUtil implements MapstructUtil {
9+
/**
10+
* Hide constructor.
11+
*/
12+
protected DefaultMapstructUtil() {
13+
}
14+
15+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
import com.intellij.psi.PsiMethod;
9+
import com.intellij.psi.PsiType;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
/**
13+
* Mapstruct util for FreeBuilder.
14+
* FreeBuilder adds a lot of other methods that can be considered as fluent setters. Such as:
15+
* <ul>
16+
* <li>{@code from(Target)}</li>
17+
* <li>{@code mapXXX(UnaryOperator)}</li>
18+
* <li>{@code mutateXXX(Consumer)}</li>
19+
* <li>{@code mergeFrom(Target)}</li>
20+
* <li>{@code mergeFrom(Target.Builder)}</li>
21+
* </ul>
22+
* <p>
23+
* When the JavaBean convention is not used with FreeBuilder then the getters are non-standard and MapStruct
24+
* won't recognize them. Therefore, one needs to use the JavaBean convention in which the fluent setters
25+
* start with {@code set}.
26+
*/
27+
public class FreeBuildersMapstructUtil implements MapstructUtil {
28+
/**
29+
* Hide constructor.
30+
*/
31+
protected FreeBuildersMapstructUtil() {
32+
}
33+
34+
@Override
35+
public boolean isFluentSetter(@NotNull PsiMethod method, PsiType psiType) {
36+
// When using FreeBuilder one needs to use the JavaBean convention, which means that all setters will start
37+
// with set
38+
return false;
39+
}
40+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
import com.intellij.psi.PsiMethod;
9+
import com.intellij.psi.PsiType;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
/**
13+
* Mapstruct util for Immutables.
14+
* The generated Immutables also have a from that works as a copy. Our default strategy considers this method
15+
* as a setter with a name {@code from}. Therefore, we are ignoring it.
16+
*/
17+
public class ImmutablesMapstructUtil implements MapstructUtil {
18+
/**
19+
* Hide constructor.
20+
*/
21+
protected ImmutablesMapstructUtil() {
22+
}
23+
24+
@Override
25+
public boolean isFluentSetter(@NotNull PsiMethod method, PsiType psiType) {
26+
return MapstructUtil.super.isFluentSetter( method, psiType ) && !method.getName().equals( "from" );
27+
}
28+
}

0 commit comments

Comments
 (0)