|
5 | 5 | */ |
6 | 6 | package org.mapstruct.intellij.util; |
7 | 7 |
|
8 | | -import java.util.ArrayList; |
9 | 8 | import java.util.Arrays; |
10 | 9 | import java.util.Collections; |
11 | 10 | import java.util.HashSet; |
12 | 11 | import java.util.List; |
13 | 12 | import java.util.Objects; |
14 | 13 | import java.util.Optional; |
15 | 14 | import java.util.Set; |
16 | | -import java.util.stream.Collectors; |
17 | 15 | import java.util.stream.Stream; |
18 | 16 |
|
19 | 17 | import com.intellij.codeInsight.AnnotationUtil; |
|
50 | 48 | import com.intellij.util.IncorrectOperationException; |
51 | 49 | import com.intellij.util.containers.ContainerUtil; |
52 | 50 | import org.jetbrains.annotations.NotNull; |
| 51 | +import org.jetbrains.annotations.Nullable; |
53 | 52 | import org.mapstruct.ReportingPolicy; |
54 | 53 |
|
55 | 54 | import static com.intellij.codeInsight.AnnotationUtil.findAnnotation; |
56 | | -import static com.intellij.codeInsight.AnnotationUtil.findDeclaredAttribute; |
57 | 55 | import static com.intellij.codeInsight.intention.AddAnnotationPsiFix.addPhysicalAnnotationTo; |
58 | 56 | import static com.intellij.codeInsight.intention.AddAnnotationPsiFix.removePhysicalAnnotations; |
59 | 57 | import static org.mapstruct.intellij.util.MapstructUtil.MAPPING_ANNOTATION_FQN; |
@@ -302,15 +300,15 @@ private static Stream<PsiAnnotation> findAllDefinedMappingAnnotations(@NotNull P |
302 | 300 | PsiAnnotation mappings = findAnnotation( owner, true, MapstructUtil.MAPPINGS_ANNOTATION_FQN ); |
303 | 301 | if ( mappings != null ) { |
304 | 302 | //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 ) |
308 | 306 | .getInitializers() ) |
309 | 307 | .filter( MapstructAnnotationUtils::isMappingPsiAnnotation ) |
310 | | - .map( memberValue -> (PsiAnnotation) memberValue ); |
| 308 | + .map( PsiAnnotation.class::cast ); |
311 | 309 | } |
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 ); |
314 | 312 | } |
315 | 313 | } |
316 | 314 |
|
@@ -375,15 +373,15 @@ public static Stream<PsiAnnotation> findAllDefinedValueMappingAnnotations(@NotNu |
375 | 373 | Stream<PsiAnnotation> valueMappingsAnnotations = Stream.empty(); |
376 | 374 | PsiAnnotation valueMappings = findAnnotation( method, true, MapstructUtil.VALUE_MAPPINGS_ANNOTATION_FQN ); |
377 | 375 | 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 ) |
381 | 379 | .getInitializers() ) |
382 | 380 | .filter( MapstructAnnotationUtils::isValueMappingPsiAnnotation ) |
383 | | - .map( memberValue -> (PsiAnnotation) memberValue ); |
| 381 | + .map( PsiAnnotation.class::cast ); |
384 | 382 | } |
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 ); |
387 | 385 | } |
388 | 386 | } |
389 | 387 |
|
@@ -447,13 +445,9 @@ private static boolean isMappingAnnotation(PsiAnnotation psiAnnotation) { |
447 | 445 | * @return the class / interface that is defined in the mapper config, |
448 | 446 | * or {@code null} if there isn't anything defined |
449 | 447 | */ |
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" ); |
457 | 451 | if ( !( configValue instanceof PsiClassObjectAccessExpression ) ) { |
458 | 452 | return null; |
459 | 453 | } |
@@ -489,27 +483,21 @@ public static Stream<PsiClass> findReferencedMapperClasses(PsiAnnotation mapperA |
489 | 483 | } |
490 | 484 |
|
491 | 485 | @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" ); |
499 | 488 |
|
500 | | - List<PsiClassObjectAccessExpression> usesExpressions = new ArrayList<>(); |
| 489 | + Stream<PsiClassObjectAccessExpression> usesExpressions = Stream.empty(); |
501 | 490 | if ( usesValue instanceof PsiArrayInitializerMemberValue ) { |
502 | 491 | usesExpressions = Stream.of( ( (PsiArrayInitializerMemberValue) usesValue ) |
503 | 492 | .getInitializers() ) |
504 | 493 | .filter( PsiClassObjectAccessExpression.class::isInstance ) |
505 | | - .map( PsiClassObjectAccessExpression.class::cast ) |
506 | | - .collect( Collectors.toList() ); |
| 494 | + .map( PsiClassObjectAccessExpression.class::cast ); |
507 | 495 | } |
508 | 496 | else if ( usesValue instanceof PsiClassObjectAccessExpression ) { |
509 | | - usesExpressions = List.of( (PsiClassObjectAccessExpression) usesValue ); |
| 497 | + usesExpressions = Stream.of( (PsiClassObjectAccessExpression) usesValue ); |
510 | 498 | } |
511 | 499 |
|
512 | | - return usesExpressions.stream() |
| 500 | + return usesExpressions |
513 | 501 | .map( usesExpression -> usesExpression.getOperand().getInnermostComponentReferenceElement() ) |
514 | 502 | .filter( Objects::nonNull ) |
515 | 503 | .map( PsiReference::resolve ) |
|
0 commit comments