Skip to content

Commit 3c460cd

Browse files
authored
Use findDeclaredAttributeValue to get PsiAnnotationMemberValue directly (#138)
1 parent a6f52b8 commit 3c460cd

File tree

2 files changed

+27
-46
lines changed

2 files changed

+27
-46
lines changed

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

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
*/
66
package org.mapstruct.intellij.util;
77

8-
import java.util.ArrayList;
98
import java.util.Arrays;
109
import java.util.Collections;
1110
import java.util.HashSet;
1211
import java.util.List;
1312
import java.util.Objects;
1413
import java.util.Optional;
1514
import java.util.Set;
16-
import java.util.stream.Collectors;
1715
import java.util.stream.Stream;
1816

1917
import com.intellij.codeInsight.AnnotationUtil;
@@ -50,10 +48,10 @@
5048
import com.intellij.util.IncorrectOperationException;
5149
import com.intellij.util.containers.ContainerUtil;
5250
import org.jetbrains.annotations.NotNull;
51+
import org.jetbrains.annotations.Nullable;
5352
import org.mapstruct.ReportingPolicy;
5453

5554
import static com.intellij.codeInsight.AnnotationUtil.findAnnotation;
56-
import static com.intellij.codeInsight.AnnotationUtil.findDeclaredAttribute;
5755
import static com.intellij.codeInsight.intention.AddAnnotationPsiFix.addPhysicalAnnotationTo;
5856
import static com.intellij.codeInsight.intention.AddAnnotationPsiFix.removePhysicalAnnotations;
5957
import static org.mapstruct.intellij.util.MapstructUtil.MAPPING_ANNOTATION_FQN;
@@ -302,15 +300,15 @@ private static Stream<PsiAnnotation> findAllDefinedMappingAnnotations(@NotNull P
302300
PsiAnnotation mappings = findAnnotation( owner, true, MapstructUtil.MAPPINGS_ANNOTATION_FQN );
303301
if ( mappings != null ) {
304302
//TODO maybe there is a better way to do this, but currently I don't have that much knowledge
305-
PsiNameValuePair mappingsValue = findDeclaredAttribute( mappings, null );
306-
if ( mappingsValue != null && mappingsValue.getValue() instanceof PsiArrayInitializerMemberValue ) {
307-
mappingsAnnotations = Stream.of( ( (PsiArrayInitializerMemberValue) mappingsValue.getValue() )
303+
PsiAnnotationMemberValue mappingsValue = mappings.findDeclaredAttributeValue( null );
304+
if ( mappingsValue instanceof PsiArrayInitializerMemberValue ) {
305+
mappingsAnnotations = Stream.of( ( (PsiArrayInitializerMemberValue) mappingsValue )
308306
.getInitializers() )
309307
.filter( MapstructAnnotationUtils::isMappingPsiAnnotation )
310-
.map( memberValue -> (PsiAnnotation) memberValue );
308+
.map( PsiAnnotation.class::cast );
311309
}
312-
else if ( mappingsValue != null && mappingsValue.getValue() instanceof PsiAnnotation ) {
313-
mappingsAnnotations = Stream.of( (PsiAnnotation) mappingsValue.getValue() );
310+
else if ( mappingsValue instanceof PsiAnnotation ) {
311+
mappingsAnnotations = Stream.of( (PsiAnnotation) mappingsValue );
314312
}
315313
}
316314

@@ -375,15 +373,15 @@ public static Stream<PsiAnnotation> findAllDefinedValueMappingAnnotations(@NotNu
375373
Stream<PsiAnnotation> valueMappingsAnnotations = Stream.empty();
376374
PsiAnnotation valueMappings = findAnnotation( method, true, MapstructUtil.VALUE_MAPPINGS_ANNOTATION_FQN );
377375
if ( valueMappings != null ) {
378-
PsiNameValuePair mappingsValue = findDeclaredAttribute( valueMappings, null );
379-
if ( mappingsValue != null && mappingsValue.getValue() instanceof PsiArrayInitializerMemberValue ) {
380-
valueMappingsAnnotations = Stream.of( ( (PsiArrayInitializerMemberValue) mappingsValue.getValue() )
376+
PsiAnnotationMemberValue mappingsValue = valueMappings.findDeclaredAttributeValue( null );
377+
if ( mappingsValue instanceof PsiArrayInitializerMemberValue ) {
378+
valueMappingsAnnotations = Stream.of( ( (PsiArrayInitializerMemberValue) mappingsValue )
381379
.getInitializers() )
382380
.filter( MapstructAnnotationUtils::isValueMappingPsiAnnotation )
383-
.map( memberValue -> (PsiAnnotation) memberValue );
381+
.map( PsiAnnotation.class::cast );
384382
}
385-
else if ( mappingsValue != null && mappingsValue.getValue() instanceof PsiAnnotation ) {
386-
valueMappingsAnnotations = Stream.of( (PsiAnnotation) mappingsValue.getValue() );
383+
else if ( mappingsValue instanceof PsiAnnotation ) {
384+
valueMappingsAnnotations = Stream.of( (PsiAnnotation) mappingsValue );
387385
}
388386
}
389387

@@ -447,13 +445,9 @@ private static boolean isMappingAnnotation(PsiAnnotation psiAnnotation) {
447445
* @return the class / interface that is defined in the mapper config,
448446
* or {@code null} if there isn't anything defined
449447
*/
450-
public static PsiModifierListOwner findMapperConfigReference(PsiAnnotation mapperAnnotation) {
451-
PsiNameValuePair configAttribute = findDeclaredAttribute( mapperAnnotation, "config" );
452-
if ( configAttribute == null ) {
453-
return null;
454-
}
455-
456-
PsiAnnotationMemberValue configValue = configAttribute.getValue();
448+
@Nullable
449+
public static PsiModifierListOwner findMapperConfigReference(@NotNull PsiAnnotation mapperAnnotation) {
450+
PsiAnnotationMemberValue configValue = mapperAnnotation.findDeclaredAttributeValue( "config" );
457451
if ( !( configValue instanceof PsiClassObjectAccessExpression ) ) {
458452
return null;
459453
}
@@ -489,27 +483,21 @@ public static Stream<PsiClass> findReferencedMapperClasses(PsiAnnotation mapperA
489483
}
490484

491485
@NotNull
492-
private static Stream<PsiClass> findReferencedMappers(PsiAnnotation mapperAnnotation) {
493-
PsiNameValuePair usesAttribute = findDeclaredAttribute( mapperAnnotation, "uses" );
494-
if ( usesAttribute == null ) {
495-
return Stream.empty();
496-
}
497-
498-
PsiAnnotationMemberValue usesValue = usesAttribute.getValue();
486+
private static Stream<PsiClass> findReferencedMappers(@NotNull PsiAnnotation mapperAnnotation) {
487+
PsiAnnotationMemberValue usesValue = mapperAnnotation.findDeclaredAttributeValue( "uses" );
499488

500-
List<PsiClassObjectAccessExpression> usesExpressions = new ArrayList<>();
489+
Stream<PsiClassObjectAccessExpression> usesExpressions = Stream.empty();
501490
if ( usesValue instanceof PsiArrayInitializerMemberValue ) {
502491
usesExpressions = Stream.of( ( (PsiArrayInitializerMemberValue) usesValue )
503492
.getInitializers() )
504493
.filter( PsiClassObjectAccessExpression.class::isInstance )
505-
.map( PsiClassObjectAccessExpression.class::cast )
506-
.collect( Collectors.toList() );
494+
.map( PsiClassObjectAccessExpression.class::cast );
507495
}
508496
else if ( usesValue instanceof PsiClassObjectAccessExpression ) {
509-
usesExpressions = List.of( (PsiClassObjectAccessExpression) usesValue );
497+
usesExpressions = Stream.of( (PsiClassObjectAccessExpression) usesValue );
510498
}
511499

512-
return usesExpressions.stream()
500+
return usesExpressions
513501
.map( usesExpression -> usesExpression.getOperand().getInnermostComponentReferenceElement() )
514502
.filter( Objects::nonNull )
515503
.map( PsiReference::resolve )

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.intellij.psi.PsiMember;
3131
import com.intellij.psi.PsiMethod;
3232
import com.intellij.psi.PsiModifierListOwner;
33-
import com.intellij.psi.PsiNameValuePair;
3433
import com.intellij.psi.PsiParameter;
3534
import com.intellij.psi.PsiSubstitutor;
3635
import com.intellij.psi.PsiType;
@@ -40,7 +39,6 @@
4039
import org.jetbrains.annotations.Nullable;
4140

4241
import static com.intellij.codeInsight.AnnotationUtil.findAnnotation;
43-
import static com.intellij.codeInsight.AnnotationUtil.findDeclaredAttribute;
4442
import static com.intellij.codeInsight.AnnotationUtil.getBooleanAttributeValue;
4543
import static org.mapstruct.intellij.util.InheritConfigurationUtils.findMappingMethodsFromInheritScope;
4644
import static org.mapstruct.intellij.util.InheritConfigurationUtils.findSingleMatchingInheritMappingMethod;
@@ -179,13 +177,10 @@ private static Optional<Boolean> findDisableBuilder(@Nullable PsiModifierListOwn
179177

180178
private static Optional<Boolean> findDisabledBuilder(@Nullable PsiAnnotation requestedAnnotation) {
181179
if ( requestedAnnotation != null ) {
182-
PsiNameValuePair builderAttribute = findDeclaredAttribute( requestedAnnotation, "builder" );
183-
if ( builderAttribute != null ) {
184-
PsiAnnotationMemberValue builderValue = builderAttribute.getValue();
185-
if ( builderValue instanceof PsiAnnotation ) {
186-
Boolean disableBuilder = getBooleanAttributeValue( (PsiAnnotation) builderValue, "disableBuilder" );
187-
return Optional.ofNullable( disableBuilder );
188-
}
180+
PsiAnnotationMemberValue builderValue = requestedAnnotation.findDeclaredAttributeValue( "builder" );
181+
if ( builderValue instanceof PsiAnnotation ) {
182+
Boolean disableBuilder = getBooleanAttributeValue( (PsiAnnotation) builderValue, "disableBuilder" );
183+
return Optional.ofNullable( disableBuilder );
189184
}
190185
}
191186

@@ -420,9 +415,7 @@ public static Stream<String> findAllSourcePropertiesForCurrentTarget(@NotNull Ps
420415
psiAnnotation,
421416
"target"
422417
) ) )
423-
.map( psiAnnotation -> findDeclaredAttribute( psiAnnotation, "source" ) )
424-
.filter( Objects::nonNull )
425-
.map( PsiNameValuePair::getValue )
418+
.map( psiAnnotation -> psiAnnotation.findDeclaredAttributeValue( "source" ) )
426419
.filter( Objects::nonNull )
427420
.map( ReferenceProvidersRegistry::getReferencesFromProviders )
428421
.filter( references -> references.length > 0 )

0 commit comments

Comments
 (0)