Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiAnnotationMemberValue;
import com.intellij.psi.PsiArrayInitializerMemberValue;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiClassObjectAccessExpression;
import com.intellij.psi.PsiElement;
Expand All @@ -52,6 +51,7 @@
import org.jetbrains.annotations.Nullable;
import org.mapstruct.ReportingPolicy;

import static com.intellij.codeInsight.AnnotationUtil.arrayAttributeValues;
import static com.intellij.codeInsight.AnnotationUtil.findAnnotation;
import static com.intellij.codeInsight.intention.AddAnnotationPsiFix.addPhysicalAnnotationTo;
import static com.intellij.codeInsight.intention.AddAnnotationPsiFix.removePhysicalAnnotations;
Expand Down Expand Up @@ -309,18 +309,10 @@ public static Stream<PsiAnnotation> extractMappingAnnotationsFromMappings(@Nulla
if ( mappings == null ) {
return Stream.empty();
}
//TODO maybe there is a better way to do this, but currently I don't have that much knowledge
PsiAnnotationMemberValue mappingsValue = mappings.findDeclaredAttributeValue( null );
if ( mappingsValue instanceof PsiArrayInitializerMemberValue mappingsArrayInitializerMemberValue ) {
return Stream.of( mappingsArrayInitializerMemberValue
.getInitializers() )
.filter( MapstructAnnotationUtils::isMappingPsiAnnotation )
.map( PsiAnnotation.class::cast );
}
else if ( mappingsValue instanceof PsiAnnotation mappingsAnnotation ) {
return Stream.of( mappingsAnnotation );
}
return Stream.empty();
return arrayAttributeValues( mappings.findDeclaredAttributeValue( null ) )
.stream()
.filter( MapstructAnnotationUtils::isMappingPsiAnnotation )
.map( PsiAnnotation.class::cast );
}

private static Stream<PsiAnnotation> findMappingAnnotations(@NotNull PsiModifierListOwner method,
Expand Down Expand Up @@ -375,15 +367,10 @@ public static Stream<PsiAnnotation> findAllDefinedValueMappingAnnotations(@NotNu
Stream<PsiAnnotation> valueMappingsAnnotations = Stream.empty();
PsiAnnotation valueMappings = findAnnotation( method, true, MapstructUtil.VALUE_MAPPINGS_ANNOTATION_FQN );
if ( valueMappings != null ) {
PsiAnnotationMemberValue mappingsValue = valueMappings.findDeclaredAttributeValue( null );
if ( mappingsValue instanceof PsiArrayInitializerMemberValue mappingsArrayInitializerMemberValue ) {
valueMappingsAnnotations = Stream.of( mappingsArrayInitializerMemberValue.getInitializers() )
.filter( MapstructAnnotationUtils::isValueMappingPsiAnnotation )
.map( PsiAnnotation.class::cast );
}
else if ( mappingsValue instanceof PsiAnnotation mappingsAnnotation ) {
valueMappingsAnnotations = Stream.of( mappingsAnnotation );
}
valueMappingsAnnotations = arrayAttributeValues( valueMappings.findDeclaredAttributeValue( null ) )
.stream()
.filter( MapstructAnnotationUtils::isValueMappingPsiAnnotation )
.map( PsiAnnotation.class::cast );
}

Stream<PsiAnnotation> valueMappingAnnotations = findValueMappingAnnotations( method );
Expand All @@ -403,8 +390,7 @@ private static Stream<PsiAnnotation> findValueMappingAnnotations(@NotNull PsiMet
* {@code false} otherwise
*/
private static boolean isMappingPsiAnnotation(PsiAnnotationMemberValue memberValue) {
return memberValue instanceof PsiAnnotation
&& isMappingAnnotation( (PsiAnnotation) memberValue );
return memberValue instanceof PsiAnnotation psiAnnotation && isMappingAnnotation( psiAnnotation );
}

/**
Expand All @@ -414,8 +400,7 @@ private static boolean isMappingPsiAnnotation(PsiAnnotationMemberValue memberVal
* {@code false} otherwise
*/
private static boolean isValueMappingPsiAnnotation(PsiAnnotationMemberValue memberValue) {
return memberValue instanceof PsiAnnotation
&& isValueMappingAnnotation( (PsiAnnotation) memberValue );
return memberValue instanceof PsiAnnotation psiAnnotation && isValueMappingAnnotation( psiAnnotation );
}

/**
Expand Down Expand Up @@ -496,19 +481,10 @@ public static Stream<PsiClass> findReferencedMapperClasses(PsiAnnotation mapperA

@NotNull
private static Stream<PsiClass> findReferencedMappers(@NotNull PsiAnnotation mapperAnnotation) {
PsiAnnotationMemberValue usesValue = mapperAnnotation.findDeclaredAttributeValue( "uses" );

Stream<PsiClassObjectAccessExpression> usesExpressions = Stream.empty();
if ( usesValue instanceof PsiArrayInitializerMemberValue psiArrayInitializerMemberValue ) {
usesExpressions = Stream.of( psiArrayInitializerMemberValue.getInitializers() )
.filter( PsiClassObjectAccessExpression.class::isInstance )
.map( PsiClassObjectAccessExpression.class::cast );
}
else if ( usesValue instanceof PsiClassObjectAccessExpression usedPsiClassObjectAccessExpression ) {
usesExpressions = Stream.of( usedPsiClassObjectAccessExpression );
}

return usesExpressions
return arrayAttributeValues( mapperAnnotation.findDeclaredAttributeValue( "uses" ) )
.stream()
.filter( PsiClassObjectAccessExpression.class::isInstance )
.map( PsiClassObjectAccessExpression.class::cast )
.map( usesExpression -> usesExpression.getOperand().getInnermostComponentReferenceElement() )
.filter( Objects::nonNull )
.map( PsiReference::resolve )
Expand Down
Loading