Skip to content

Commit 849a39a

Browse files
committed
Polish
1 parent ca4c84a commit 849a39a

File tree

9 files changed

+104
-81
lines changed

9 files changed

+104
-81
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,22 @@ public class FromMapMappingMapTypeInspection extends InspectionBase {
4141
@Override
4242
PsiElementVisitor buildVisitorInternal(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
4343
return new MyJavaElementVisitor(
44-
holder, MapstructUtil.resolveMapStructProjectVersion( holder.getFile() ),
45-
MapstructUtil.getInstance( holder.getFile() ) );
44+
holder,
45+
MapstructUtil.resolveMapStructProjectVersion( holder.getFile() ),
46+
MapstructUtil.getInstance( holder.getFile() )
47+
);
4648
}
4749

4850
private static class MyJavaElementVisitor extends JavaElementVisitor {
4951
private final ProblemsHolder holder;
5052
private final MapStructVersion mapStructVersion;
5153
private final MapstructUtil mapstructUtil;
5254

53-
private MyJavaElementVisitor(
54-
ProblemsHolder holder, MapStructVersion mapStructVersion, MapstructUtil mapstructUtil) {
55-
this.holder = holder;
56-
this.mapStructVersion = mapStructVersion;
57-
this.mapstructUtil = mapstructUtil;
55+
private MyJavaElementVisitor(ProblemsHolder holder, MapStructVersion mapStructVersion,
56+
MapstructUtil mapstructUtil) {
57+
this.holder = holder;
58+
this.mapStructVersion = mapStructVersion;
59+
this.mapstructUtil = mapstructUtil;
5860
}
5961

6062
@Override
@@ -79,7 +81,11 @@ public void visitMethod(@NotNull PsiMethod method) {
7981
return;
8082
}
8183
Set<String> allTargetProperties = findAllTargetProperties(
82-
targetType, mapStructVersion, mapstructUtil, method );
84+
targetType,
85+
mapStructVersion,
86+
mapstructUtil,
87+
method
88+
);
8389
if ( allTargetProperties.contains( fromMapMappingParameter.getName() ) ) {
8490
return;
8591
}

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,19 @@ public class UnmappedTargetPropertiesInspection extends InspectionBase {
6060
@Override
6161
PsiElementVisitor buildVisitorInternal(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
6262
return new MyJavaElementVisitor(
63-
holder, MapstructUtil.resolveMapStructProjectVersion( holder.getFile() ),
64-
MapstructUtil.getInstance( holder.getFile() ) );
63+
holder,
64+
MapstructUtil.resolveMapStructProjectVersion( holder.getFile() ),
65+
MapstructUtil.getInstance( holder.getFile() )
66+
);
6567
}
6668

6769
private static class MyJavaElementVisitor extends JavaElementVisitor {
6870
private final ProblemsHolder holder;
6971
private final MapStructVersion mapStructVersion;
7072
private final MapstructUtil mapstructUtil;
7173

72-
private MyJavaElementVisitor(
73-
ProblemsHolder holder, MapStructVersion mapStructVersion, MapstructUtil mapstructUtil) {
74+
private MyJavaElementVisitor(ProblemsHolder holder, MapStructVersion mapStructVersion,
75+
MapstructUtil mapstructUtil) {
7476
this.holder = holder;
7577
this.mapStructVersion = mapStructVersion;
7678
this.mapstructUtil = mapstructUtil;
@@ -103,7 +105,11 @@ public void visitMethod(PsiMethod method) {
103105
}
104106

105107
Set<String> allTargetProperties = findAllTargetProperties(
106-
targetType, mapStructVersion, mapstructUtil, method );
108+
targetType,
109+
mapStructVersion,
110+
mapstructUtil,
111+
method
112+
);
107113

108114
// find and remove all defined mapping targets
109115
Set<String> definedTargets = findAllDefinedMappingTargets( method, mapStructVersion )

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

Lines changed: 0 additions & 14 deletions
This file was deleted.

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@
2525
* start with {@code set}.
2626
*/
2727
public class FreeBuildersMapstructUtil extends MapstructUtil {
28-
/**
29-
* Hide constructor.
30-
*/
31-
protected FreeBuildersMapstructUtil() {
32-
}
3328

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-
}
29+
static final MapstructUtil INSTANCE = new FreeBuildersMapstructUtil();
30+
31+
/**
32+
* Hide constructor.
33+
*/
34+
private FreeBuildersMapstructUtil() {
35+
}
36+
37+
@Override
38+
public boolean isFluentSetter(@NotNull PsiMethod method, PsiType psiType) {
39+
// When using FreeBuilder one needs to use the JavaBean convention,
40+
// which means that all setters will start with set
41+
return false;
42+
}
4043
}

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
* as a setter with a name {@code from}. Therefore, we are ignoring it.
1616
*/
1717
public class ImmutablesMapstructUtil extends MapstructUtil {
18-
/**
19-
* Hide constructor.
20-
*/
21-
protected ImmutablesMapstructUtil() {
22-
}
2318

24-
@Override
25-
public boolean isFluentSetter(@NotNull PsiMethod method, PsiType psiType) {
26-
return super.isFluentSetter( method, psiType ) && !method.getName().equals( "from" );
27-
}
19+
static final MapstructUtil INSTANCE = new ImmutablesMapstructUtil();
20+
21+
/**
22+
* Hide constructor.
23+
*/
24+
private ImmutablesMapstructUtil() {
25+
}
26+
27+
@Override
28+
public boolean isFluentSetter(@NotNull PsiMethod method, PsiType psiType) {
29+
return super.isFluentSetter( method, psiType ) && !method.getName().equals( "from" );
30+
}
2831
}

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

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.intellij.openapi.module.ModuleUtilCore;
2222
import com.intellij.openapi.roots.ProjectRootManager;
2323
import com.intellij.openapi.util.Pair;
24+
import com.intellij.openapi.vfs.VirtualFile;
2425
import com.intellij.psi.CommonClassNames;
2526
import com.intellij.psi.EmptySubstitutor;
2627
import com.intellij.psi.JavaPsiFacade;
@@ -72,6 +73,8 @@
7273
*/
7374
public class MapstructUtil {
7475

76+
private static final MapstructUtil INSTANCE = new MapstructUtil();
77+
7578
/**
7679
* The FQN of the {@link Mapper} annotation.
7780
*/
@@ -111,17 +114,19 @@ public class MapstructUtil {
111114
}
112115

113116
public static MapstructUtil getInstance(@Nullable PsiFile psiFile) {
114-
MapstructUtil mapstructUtil = new DefaultMapstructUtil();
115-
if (psiFile == null) {
116-
return mapstructUtil;
117+
if ( psiFile == null ) {
118+
return MapstructUtil.INSTANCE;
117119
}
118-
if (MapstructUtil.immutablesOnClassPath( psiFile )) {
119-
mapstructUtil = new ImmutablesMapstructUtil();
120+
121+
if ( MapstructUtil.immutablesOnClassPath( psiFile ) ) {
122+
return ImmutablesMapstructUtil.INSTANCE;
120123
}
121-
else if (MapstructUtil.freeBuilderOnClassPath( psiFile )) {
122-
mapstructUtil = new FreeBuildersMapstructUtil();
124+
125+
if ( MapstructUtil.freeBuilderOnClassPath( psiFile ) ) {
126+
return FreeBuildersMapstructUtil.INSTANCE;
123127
}
124-
return mapstructUtil;
128+
129+
return MapstructUtil.INSTANCE;
125130
}
126131

127132
public static LookupElement[] asLookup(Map<String, Pair<? extends PsiElement, PsiSubstitutor>> accessors,
@@ -576,9 +581,13 @@ else if ( JavaPsiFacade.getInstance( module.getProject() )
576581
} );
577582
}
578583

579-
public static boolean immutablesOnClassPath(@NotNull PsiFile psiFile) {
580-
Module module = ModuleUtilCore.findModuleForFile( psiFile.getVirtualFile(), psiFile.getProject() );
581-
if (module == null) {
584+
private static boolean immutablesOnClassPath(@NotNull PsiFile psiFile) {
585+
VirtualFile virtualFile = psiFile.getVirtualFile();
586+
if ( virtualFile == null ) {
587+
return false;
588+
}
589+
Module module = ModuleUtilCore.findModuleForFile( virtualFile, psiFile.getProject() );
590+
if ( module == null ) {
582591
return false;
583592
}
584593
return CachedValuesManager.getManager( module.getProject() ).getCachedValue( module, () -> {
@@ -591,9 +600,13 @@ public static boolean immutablesOnClassPath(@NotNull PsiFile psiFile) {
591600
} );
592601
}
593602

594-
public static boolean freeBuilderOnClassPath(@NotNull PsiFile psiFile) {
595-
Module module = ModuleUtilCore.findModuleForFile( psiFile.getVirtualFile(), psiFile.getProject() );
596-
if (module == null) {
603+
private static boolean freeBuilderOnClassPath(@NotNull PsiFile psiFile) {
604+
VirtualFile virtualFile = psiFile.getVirtualFile();
605+
if ( virtualFile == null ) {
606+
return false;
607+
}
608+
Module module = ModuleUtilCore.findModuleForFile( virtualFile, psiFile.getProject() );
609+
if ( module == null ) {
597610
return false;
598611
}
599612
return CachedValuesManager.getManager( module.getProject() ).getCachedValue( module, () -> {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,11 @@ private static Map<String, Pair<? extends PsiMember, PsiSubstitutor>> publicSett
278278
continue;
279279
}
280280
String propertyName = extractPublicSetterPropertyName(
281-
method, typeToUse, mapstructUtil, builderSupportPresent );
281+
method,
282+
typeToUse,
283+
mapstructUtil,
284+
builderSupportPresent
285+
);
282286

283287
if ( propertyName != null &&
284288
!overriddenMethods.contains( method ) ) {

src/test/java/org/mapstruct/intellij/MapstructBaseCompletionTestCase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
*/
2323
public abstract class MapstructBaseCompletionTestCase extends LightFixtureCompletionTestCase {
2424

25-
private static final String BUILD_LIBS_DIRECTORY = "build/libs";
26-
protected static final String BUILD_TEST_LIBS_DIRECTORY = "build/test-libs";
25+
private static final String BUILD_LIBS_DIRECTORY = "build/libs";
2726

2827
@Override
2928
protected void setUp() throws Exception {

src/test/java/org/mapstruct/intellij/inspection/UnmappedImmutablesFromTargetPropertiesInspectionTest.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
public class UnmappedImmutablesFromTargetPropertiesInspectionTest extends BaseInspectionTest {
2222

23+
private static final String BUILD_TEST_LIBS_DIRECTORY = "build/test-libs";
24+
2325
@NotNull
2426
@Override
2527
protected Class<UnmappedTargetPropertiesInspection> getInspection() {
@@ -29,14 +31,15 @@ protected Class<UnmappedTargetPropertiesInspection> getInspection() {
2931
@Override
3032
protected void setUp() throws Exception {
3133
super.setUp();
32-
final String immutablesLibPath = PathUtil.toSystemIndependentName( new File( BUILD_TEST_LIBS_DIRECTORY )
33-
.getAbsolutePath() );
34+
String immutablesLibPath = PathUtil.toSystemIndependentName(
35+
new File( BUILD_TEST_LIBS_DIRECTORY ).getAbsolutePath()
36+
);
3437
PsiTestUtil.addLibrary(
35-
myFixture.getProjectDisposable(),
36-
myFixture.getModule(),
37-
"Immutables",
38-
immutablesLibPath,
39-
"immutables.jar"
38+
myFixture.getProjectDisposable(),
39+
myFixture.getModule(),
40+
"Immutables",
41+
immutablesLibPath,
42+
"immutables.jar"
4043
);
4144
myFixture.copyFileToProject(
4245
"UnmappedImmutablesFromTargetPropertiesData.java",
@@ -53,20 +56,20 @@ public void testUnmappedImmutablesFromTargetProperties() {
5356
.extracting( IntentionAction::getText )
5457
.as( "Intent Text" )
5558
.containsExactly(
56-
"Ignore unmapped target property: 'builderTestName'",
57-
"Add unmapped target property: 'builderTestName'",
59+
"Ignore unmapped target property: 'builderTestName'",
60+
"Add unmapped target property: 'builderTestName'",
5861

59-
"Ignore unmapped target property: 'targetTestName'",
60-
"Add unmapped target property: 'targetTestName'",
62+
"Ignore unmapped target property: 'targetTestName'",
63+
"Add unmapped target property: 'targetTestName'",
6164

62-
"Ignore unmapped target property: 'targetTestName'",
63-
"Add unmapped target property: 'targetTestName'",
65+
"Ignore unmapped target property: 'targetTestName'",
66+
"Add unmapped target property: 'targetTestName'",
6467

65-
"Ignore unmapped target property: 'builderTestName'",
66-
"Add unmapped target property: 'builderTestName'",
68+
"Ignore unmapped target property: 'builderTestName'",
69+
"Add unmapped target property: 'builderTestName'",
6770

68-
"Ignore unmapped target property: 'targetTestName'",
69-
"Add unmapped target property: 'targetTestName'"
71+
"Ignore unmapped target property: 'targetTestName'",
72+
"Add unmapped target property: 'targetTestName'"
7073
);
7174

7275
allQuickFixes.forEach( myFixture::launchAction );

0 commit comments

Comments
 (0)