forked from mapstruct/mapstruct-idea
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapstructUtil.java
More file actions
658 lines (587 loc) · 25.9 KB
/
MapstructUtil.java
File metadata and controls
658 lines (587 loc) · 25.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
* 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.util;
import java.beans.Introspector;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;
import javax.swing.Icon;
import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtilCore;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.EmptySubstitutor;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiArrayType;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiClassType;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiEnumConstant;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiModifierList;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.PsiRecordComponent;
import com.intellij.psi.PsiSubstitutor;
import com.intellij.psi.PsiType;
import com.intellij.psi.impl.PsiClassImplUtil;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.PlatformIcons;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mapstruct.BeanMapping;
import org.mapstruct.Builder;
import org.mapstruct.Context;
import org.mapstruct.EnumMapping;
import org.mapstruct.InheritConfiguration;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.MapperConfig;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.ValueMapping;
import org.mapstruct.ValueMappings;
import org.mapstruct.factory.Mappers;
import static com.intellij.codeInsight.AnnotationUtil.findAnnotation;
import static com.intellij.codeInsight.AnnotationUtil.isAnnotated;
/**
* @author Filip Hrisafov
*/
public class MapstructUtil {
private static final MapstructUtil INSTANCE = new MapstructUtil();
/**
* The FQN of the {@link Mapper} annotation.
*/
public static final String MAPPER_ANNOTATION_FQN = Mapper.class.getName();
/**
* The FQN of the {@link MapperConfig} annotation.
*/
public static final String MAPPER_CONFIG_ANNOTATION_FQN = MapperConfig.class.getName();
/**
* The FQN of the {@link Mapping} annotation.
*/
public static final String MAPPING_ANNOTATION_FQN = Mapping.class.getName();
public static final String MAPPERS_FQN = Mappers.class.getName();
public static final String BEAN_MAPPING_FQN = BeanMapping.class.getName();
public static final String NAMED_ANNOTATION_FQN = Named.class.getName();
public static final String INHERIT_CONFIGURATION_FQN = InheritConfiguration.class.getName();
public static final String INHERIT_INVERSE_CONFIGURATION_FQN = InheritInverseConfiguration.class.getName();
public static final String MAPPINGS_ANNOTATION_FQN = Mappings.class.getName();
static final String VALUE_MAPPING_ANNOTATION_FQN = ValueMapping.class.getName();
static final String VALUE_MAPPINGS_ANNOTATION_FQN = ValueMappings.class.getName();
private static final String MAPPING_TARGET_ANNOTATION_FQN = MappingTarget.class.getName();
private static final String CONTEXT_ANNOTATION_FQN = Context.class.getName();
private static final String BUILDER_ANNOTATION_FQN = Builder.class.getName();
private static final String ENUM_MAPPING_ANNOTATION_FQN = EnumMapping.class.getName();
private static final String IMMUTABLE_FQN = "org.immutables.value.Value.Immutable";
private static final String FREE_BUILDER_FQN = "org.inferred.freebuilder.FreeBuilder";
/**
* Hide constructor.
*/
MapstructUtil() {
}
public static MapstructUtil getInstance(@Nullable PsiFile psiFile) {
if ( psiFile == null ) {
return MapstructUtil.INSTANCE;
}
if ( MapstructUtil.immutablesOnClassPath( psiFile ) ) {
return ImmutablesMapstructUtil.INSTANCE;
}
if ( MapstructUtil.freeBuilderOnClassPath( psiFile ) ) {
return FreeBuildersMapstructUtil.INSTANCE;
}
return MapstructUtil.INSTANCE;
}
public static LookupElement[] asLookup(Map<String, Pair<? extends PsiElement, PsiSubstitutor>> accessors,
Function<PsiElement, PsiType> typeMapper) {
if ( !accessors.isEmpty() ) {
LookupElement[] lookupElements = new LookupElement[accessors.size()];
int index = 0;
for ( Map.Entry<String, Pair<? extends PsiElement, PsiSubstitutor>> entry :
accessors.entrySet() ) {
String propertyName = entry.getKey();
Pair<? extends PsiElement, PsiSubstitutor> pair = entry.getValue();
lookupElements[index++] = asLookup(
propertyName,
pair,
typeMapper,
PlatformIcons.VARIABLE_ICON
);
}
return lookupElements;
}
else {
return LookupElement.EMPTY_ARRAY;
}
}
public static LookupElement asLookup(PsiParameter parameter) {
return asLookup( parameter.getName(), parameter, PsiParameter::getType, PlatformIcons.PARAMETER_ICON );
}
public static LookupElement asLookup(PsiEnumConstant enumConstant) {
return asLookup( enumConstant.getName(), enumConstant, PsiField::getType, PlatformIcons.FIELD_ICON );
}
public static LookupElement asLookupWithRepresentableText(PsiMethod method, String lookupString,
String representableText, String tailText) {
LookupElementBuilder builder = LookupElementBuilder.create( method, lookupString )
.withIcon( PlatformIcons.METHOD_ICON )
.withPresentableText( representableText )
.withTailText( tailText );
final PsiType type = method.getReturnType();
if ( type != null ) {
builder = builder.withTypeText( EmptySubstitutor.getInstance().substitute( type ).getPresentableText() );
}
return builder;
}
public static <T extends PsiElement> LookupElement asLookup(String propertyName, @NotNull T psiElement,
Function<T, PsiType> typeMapper, Icon icon) {
//noinspection unchecked
return asLookup( propertyName, Pair.pair( psiElement, EmptySubstitutor.getInstance() ),
(Function<PsiElement, PsiType>) typeMapper, icon
);
}
public static LookupElement asLookup(String propertyName, @NotNull Pair<? extends PsiElement, PsiSubstitutor> pair,
Function<PsiElement, PsiType> typeMapper, Icon icon) {
PsiElement member = pair.getFirst();
PsiSubstitutor substitutor = pair.getSecond();
LookupElementBuilder builder = LookupElementBuilder.create( member, propertyName )
.withIcon( icon )
.withPresentableText( propertyName );
final PsiType type = typeMapper.apply( member );
if ( type != null ) {
builder = builder.withTypeText( substitutor.substitute( type ).getPresentableText() );
}
return builder;
}
public static boolean isPublic(@NotNull PsiMethod method) {
return method.hasModifierProperty( PsiModifier.PUBLIC );
}
public static boolean isPublicNonStatic(@NotNull PsiMethod method) {
return isPublic( method ) && !method.hasModifierProperty( PsiModifier.STATIC );
}
public static boolean isPublicStatic(@NotNull PsiMethod method) {
return isPublic( method ) && method.hasModifierProperty( PsiModifier.STATIC );
}
public static boolean isPublicNonStatic(@NotNull PsiField field) {
return isPublic( field ) && !field.hasModifierProperty( PsiModifier.STATIC );
}
private static boolean isPublic(@NotNull PsiField field) {
return field.hasModifierProperty( PsiModifier.PUBLIC );
}
public static boolean isPublicModifiable(@NotNull PsiField field) {
return isPublicNonStatic( field ) &&
!field.hasModifierProperty( PsiModifier.FINAL );
}
public boolean isFluentSetter(@NotNull PsiMethod method, PsiType psiType) {
return !psiType.getCanonicalText().startsWith( "java.lang" ) &&
method.getReturnType() != null &&
!isAdderWithUpperCase4thCharacter( method ) &&
isAssignableFromReturnTypeOrSuperTypes( psiType, method.getReturnType() );
}
private static boolean isAssignableFromReturnTypeOrSuperTypes(PsiType psiType, PsiType returnType) {
if ( isAssignableFrom( psiType, returnType ) ) {
return true;
}
for ( PsiType superType : returnType.getSuperTypes() ) {
if ( isAssignableFrom( psiType, superType ) ) {
return true;
}
}
return false;
}
private static boolean isAssignableFrom(PsiType psiType, @Nullable PsiType returnType) {
return TypeConversionUtil.isAssignable(
psiType,
PsiUtil.resolveGenericsClassInType( psiType ).getSubstitutor().substitute( returnType )
);
}
private static boolean isAdderWithUpperCase4thCharacter(@NotNull PsiMethod method) {
String methodName = method.getName();
return methodName.startsWith( "add" ) &&
methodName.length() > 3 &&
Character.isUpperCase( methodName.charAt( 3 ) );
}
/**
* Checks if the {@code method} is a possible builder creation method.
* <p>
* The default implementation considers a method as a possible creation method if the following is satisfied:
* <ul>
* <li>The method has no parameters</li>
* <li>It is a {@code public static} method</li>
* <li>The return type of the {@code method} is not the same as the {@code type}</li>
* <li></li>
* </ul>
*
* See also {@code DefaultBuilderProvider} in the mapstruct processor.
*
* @param method The method that needs to be checked
* @param type the enclosing element of the method, i.e. the type in which the method is located in
* @return {@code true} if the {@code method} is a possible builder creation method, {@code false} otherwise
*/
public static boolean isPossibleBuilderCreationMethod(@NotNull PsiMethod method, @NotNull PsiType type) {
return method.getParameterList().isEmpty()
&& MapstructUtil.isPublicStatic( method )
&& !type.equals( method.getReturnType() );
}
/**
* Checks if the {@code buildMethod} is a method that creates {@code typeToBuild}.
* <p>
* The default implementation considers a method to be a build method if the following is satisfied:
* <ul>
* <li>The method has no parameters</li>
* <li>The method is public</li>
* <li>The return type of method is assignable to the {@code typeElement}</li>
* </ul>
*
* @param buildMethod the method that should be checked
* @param typeToBuild the type element that needs to be built
* @return {@code true} if the {@code buildMethod} is a build method for {@code typeToBuild}, {@code false}
* otherwise
*/
public static boolean isBuildMethod(@NotNull PsiMethod buildMethod, @NotNull PsiType typeToBuild) {
return buildMethod.getParameterList().isEmpty() &&
isPublic( buildMethod ) &&
buildMethod.getReturnType() != null &&
TypeConversionUtil.isAssignable( typeToBuild, buildMethod.getReturnType() );
}
@NotNull
@NonNls
public static String getPropertyName(@NotNull PsiMethod method) {
//TODO if we can use the AccessorNamingStrategy it would be awesome
String methodName = method.getName();
return getPropertyName( methodName );
}
@NotNull
@NonNls
public static String getPropertyName(@NotNull String methodName) {
String name = "";
if ( methodName.startsWith( "is" ) ) {
name = Introspector.decapitalize( methodName.substring( 2 ) );
}
else if ( methodName.startsWith( "get" ) || methodName.startsWith( "set" ) ) {
name = Introspector.decapitalize( methodName.substring( 3 ) );
}
else {
name = methodName;
}
return name;
}
/**
* Check if the parameter is a Mapping Target parameter.
*
* @param psiParameter to be checked
*
* @return {@code true} if the parameter is a MappingTarget, {@code false} otherwise
*/
public static boolean isMappingTarget(PsiParameter psiParameter) {
return hasAnnotation( psiParameter, MAPPING_TARGET_ANNOTATION_FQN );
}
/**
* Checks of the {@code psiClass} is annotated with {@link Mapper}.
*
* @param psiClass the class that needs to be checked
*
* @return {@code true} if the {@code psiClass} is annotated with {@link Mapper}, {@code false} otherwise
*/
public static boolean isMapper(PsiClass psiClass) {
return isAnnotated( psiClass, MAPPER_ANNOTATION_FQN, AnnotationUtil.CHECK_TYPE );
}
/**
* Checks of the {@code psiClass} is annotated with {@link MapperConfig}.
*
* @param psiClass the class that needs to be checked
*
* @return {@code true} if the {@code psiClass} is annotated with {@link MapperConfig}, {@code false} otherwise
*/
public static boolean isMapperConfig(PsiClass psiClass) {
return isAnnotated( psiClass, MAPPER_CONFIG_ANNOTATION_FQN, AnnotationUtil.CHECK_TYPE );
}
/**
* Checks if the {@code psiMethod} is a mapping method. A mapping method is a method that is annotated with one of:
* <ul>
* <li>{@link Mapping}</li>
* <li>{@link Mappings}</li>
* <li>{@link ValueMapping}</li>
* <li>{@link ValueMappings}</li>
* </ul>
*
* @param psiMethod
*
* @return
*/
public static boolean isMappingMethod(PsiMethod psiMethod) {
return isAnnotated( psiMethod, MAPPING_ANNOTATION_FQN, AnnotationUtil.CHECK_TYPE )
|| isAnnotated( psiMethod, MAPPINGS_ANNOTATION_FQN, AnnotationUtil.CHECK_TYPE )
|| isAnnotated( psiMethod, VALUE_MAPPING_ANNOTATION_FQN, AnnotationUtil.CHECK_TYPE )
|| isAnnotated( psiMethod, VALUE_MAPPINGS_ANNOTATION_FQN, AnnotationUtil.CHECK_TYPE );
}
/**
* Checks if the method is annotated with {@code Named}.
*
* @param psiMethod to be checked
* @return {@code true} if the method is annotated with {@code Named}, {@code false} otherwise
*/
public static boolean isNamedMethod(PsiMethod psiMethod) {
return isAnnotated( psiMethod, NAMED_ANNOTATION_FQN, AnnotationUtil.CHECK_TYPE );
}
/**
* Checks if the parameter is a valid source parameter. A valid source parameter is a paremeter that is not a
* {@code MappingTarget} or a {@code Context}.
*
* @param psiParameter to be checked
*
* @return {@code true} if the parameter is a valid source parameter, {@code false} otherwise
*/
public static boolean isValidSourceParameter(PsiParameter psiParameter) {
return !isMappingTarget( psiParameter ) && !isContextParameter( psiParameter );
}
/**
* Checks if the parameter is a Context parameter.
*
* @param psiParameter to be checked
*
* @return {@code true} if the parameter is a Context parameter, {@code false} otherwise
*/
private static boolean isContextParameter(PsiParameter psiParameter) {
return hasAnnotation( psiParameter, CONTEXT_ANNOTATION_FQN );
}
/**
* Checks if the parameter is annotated with the provided {@code annotation}.
*
* @param psiParameter the parameter on which we need to check for the annotation
* @param annotation the annotation that we need to find
*
* @return {@code true} if the {@code psiParameter} is annotated with the {@code annotation}, {@code false}
* otherwise
*/
private static boolean hasAnnotation(PsiParameter psiParameter, String annotation) {
PsiModifierList modifierList = psiParameter.getModifierList();
return modifierList != null && modifierList.findAnnotation( annotation ) != null;
}
/**
* Extract all valid source parameters from the provided {@code mappingMethod}
*
* @param mappingMethod the mapping method
*
* @return all source parameters from the provided {@code mappingMethod}
*/
@NotNull
public static PsiParameter[] getSourceParameters(@NotNull PsiMethod mappingMethod) {
if ( mappingMethod.getParameterList().getParametersCount() == 0 ) {
return PsiParameter.EMPTY_ARRAY;
}
return Stream.of( mappingMethod.getParameterList().getParameters() )
.filter( MapstructUtil::isValidSourceParameter )
.toArray( PsiParameter[]::new );
}
public static Map<String, Pair<PsiField, PsiSubstitutor>> publicFields(PsiClass psiClass) {
List<Pair<PsiField, PsiSubstitutor>> fieldPairs = PsiClassImplUtil.getAllWithSubstitutorsByMap(
psiClass,
PsiClassImplUtil.MemberType.FIELD
);
if ( fieldPairs.isEmpty() ) {
return Collections.emptyMap();
}
Map<String, Pair<PsiField, PsiSubstitutor>> publicFields = new HashMap<>();
for ( Pair<PsiField, PsiSubstitutor> fieldPair : fieldPairs ) {
PsiField field = fieldPair.getFirst();
if ( MapstructUtil.isPublicNonStatic( field ) ) {
publicFields.put( field.getName(), fieldPair );
}
}
return publicFields;
}
public static PsiRecordComponent findRecordComponent(@NotNull String componentName, @NotNull PsiClass psiClass) {
if ( psiClass.isRecord() ) {
for ( PsiRecordComponent recordComponent : psiClass.getRecordComponents() ) {
if ( componentName.equals( recordComponent.getName() ) ) {
return recordComponent;
}
}
}
return null;
}
/**
* Checks if MapStruct can descend into a type. MapStruct, cannot descend into following types:
* <ul>
* <li>An Array</li>
* <li>An Iterable</li>
* <li>A Map</li>
* </ul>
*
* @param psiType the type to be checked
*
* @return {@code true} if MapStruct can descend into type
*/
public static boolean canDescendIntoType(@Nullable PsiType psiType) {
if ( psiType == null || psiType instanceof PsiArrayType ) {
return false;
}
GlobalSearchScope resolveScope = psiType.getResolveScope();
if ( resolveScope == null || resolveScope.getProject() == null ) {
return true;
}
if ( psiType instanceof PsiClassType ) {
return !PsiType.getTypeByName(
"java.lang.Iterable",
resolveScope.getProject(),
resolveScope
).isAssignableFrom( psiType )
&& !PsiType.getTypeByName(
"java.util.Map",
resolveScope.getProject(),
resolveScope
).isAssignableFrom( psiType );
}
return true;
}
public static String capitalize(String string) {
return string == null ? null : string.substring( 0, 1 ).toUpperCase() + string.substring( 1 );
}
/**
* Checks if the {@code psiFile} is located within a module that contains MapStruct.
*
* @param psiFile the file for which the check needs to be done
*
* @return {@code true} if MapStruct is in the module of the given {@code psiFile}, {@code false} otherwise
*/
public static boolean isMapStructPresent(@NotNull PsiFile psiFile) {
Module module = ModuleUtilCore.findModuleForFile( psiFile.getVirtualFile(), psiFile.getProject() );
return module != null && isMapStructPresent( module );
}
/**
* Checks if MapStruct is withing the provided module.
*
* @param module that needs to be checked
*
* @return {@code true} if MapStruct is present within the {@code module}, {@code false} otherwise
*/
private static boolean isMapStructPresent(@NotNull Module module) {
return CachedValuesManager.getManager( module.getProject() ).getCachedValue( module, () -> {
boolean foundMarkerClass = JavaPsiFacade.getInstance( module.getProject() )
.findClass( MAPPER_ANNOTATION_FQN, module.getModuleRuntimeScope( false ) ) != null;
return CachedValueProvider.Result.createSingleDependency(
foundMarkerClass,
ProjectRootManager.getInstance( module.getProject() )
);
} );
}
/**
* Resolve the MapStruct project version with the module of the provided psi file
* @param psiFile that needs to be checked
* @return the MapStruct project version
*/
public static MapStructVersion resolveMapStructProjectVersion(@NotNull PsiFile psiFile) {
Module module = ModuleUtilCore.findModuleForFile( psiFile.getVirtualFile(), psiFile.getProject() );
if ( module == null ) {
return MapStructVersion.V1_2_O;
}
return CachedValuesManager.getManager( module.getProject() ).getCachedValue( module, () -> {
MapStructVersion mapStructVersion;
if ( JavaPsiFacade.getInstance( module.getProject() )
.findClass( ENUM_MAPPING_ANNOTATION_FQN, module.getModuleRuntimeScope( false ) ) != null ) {
mapStructVersion = MapStructVersion.V1_4_O;
}
else if ( JavaPsiFacade.getInstance( module.getProject() )
.findClass( BUILDER_ANNOTATION_FQN, module.getModuleRuntimeScope( false ) ) != null ) {
mapStructVersion = MapStructVersion.V1_3_O;
}
else {
mapStructVersion = MapStructVersion.V1_2_O;
}
return CachedValueProvider.Result.createSingleDependency(
mapStructVersion,
ProjectRootManager.getInstance( module.getProject() )
);
} );
}
private static boolean immutablesOnClassPath(@NotNull PsiFile psiFile) {
VirtualFile virtualFile = psiFile.getVirtualFile();
if ( virtualFile == null ) {
return false;
}
Module module = ModuleUtilCore.findModuleForFile( virtualFile, psiFile.getProject() );
if ( module == null ) {
return false;
}
return CachedValuesManager.getManager( module.getProject() ).getCachedValue( module, () -> {
boolean immutablesOnClassPath = JavaPsiFacade.getInstance( module.getProject() )
.findClass( IMMUTABLE_FQN, module.getModuleRuntimeScope( false ) ) != null;
return CachedValueProvider.Result.createSingleDependency(
immutablesOnClassPath,
ProjectRootManager.getInstance( module.getProject() )
);
} );
}
private static boolean freeBuilderOnClassPath(@NotNull PsiFile psiFile) {
VirtualFile virtualFile = psiFile.getVirtualFile();
if ( virtualFile == null ) {
return false;
}
Module module = ModuleUtilCore.findModuleForFile( virtualFile, psiFile.getProject() );
if ( module == null ) {
return false;
}
return CachedValuesManager.getManager( module.getProject() ).getCachedValue( module, () -> {
boolean freeBuilderOnClassPath = JavaPsiFacade.getInstance( module.getProject() )
.findClass( FREE_BUILDER_FQN, module.getModuleRuntimeScope( false ) ) != null;
return CachedValueProvider.Result.createSingleDependency(
freeBuilderOnClassPath,
ProjectRootManager.getInstance( module.getProject() )
);
} );
}
/**
* Checks if MapStruct jdk8 is within the provided module. The MapStruct JDK 8 module is present when the
* {@link Mapping} annotation is annotated with {@link java.lang.annotation.Repeatable}
*
* @param module that needs to be checked
*
* @return {@code true} if MapStruct jdk8 is present within the {@code module}, {@code false} otherwise
*/
static boolean isMapStructJdk8Present(@NotNull Module module) {
return CachedValuesManager.getManager( module.getProject() ).getCachedValue( module, () -> {
PsiClass mappingAnnotation = JavaPsiFacade.getInstance( module.getProject() )
.findClass( MAPPING_ANNOTATION_FQN, module.getModuleRuntimeScope( false ) );
boolean mapstructJdk8Present = findAnnotation(
mappingAnnotation,
true,
CommonClassNames.JAVA_LANG_ANNOTATION_REPEATABLE
) != null;
return CachedValueProvider.Result.createSingleDependency(
mapstructJdk8Present,
ProjectRootManager.getInstance( module.getProject() )
);
} );
}
/**
* Checks if the {@code psiMethod} is annotated with {@link InheritInverseConfiguration}.
*
* @param method to be checked
*
* @return {@code true} if the {@code method} is annotated with {@link InheritInverseConfiguration},
* {@code false} otherwise
*/
public static boolean isInheritInverseConfiguration(PsiMethod method) {
return isAnnotated( method, INHERIT_INVERSE_CONFIGURATION_FQN, AnnotationUtil.CHECK_TYPE );
}
}