Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent null as fix for WrongUsageOfMappersFactory #176

Closed
Show file tree
Hide file tree
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 @@ -9,6 +9,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.daemon.impl.quickfix.RemoveUnusedVariableFix;
Expand All @@ -18,6 +19,11 @@
import com.intellij.codeInspection.IntentionWrapper;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.modcommand.ActionContext;
import com.intellij.modcommand.ModChooseAction;
import com.intellij.modcommand.ModCommand;
import com.intellij.modcommand.ModCommandAction;
import com.intellij.modcommand.Presentation;
import com.intellij.psi.CommonClassNames;
import com.intellij.psi.JavaElementVisitor;
import com.intellij.psi.PsiAnnotation;
Expand All @@ -35,6 +41,7 @@
import com.siyeh.ig.callMatcher.CallMatcher;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.mapstruct.intellij.MapStructBundle;
import org.mapstruct.intellij.util.MapstructUtil;

Expand All @@ -51,6 +58,7 @@ public class WrongUsageOfMappersFactoryInspection extends InspectionBase {
).parameterTypes( CommonClassNames.JAVA_LANG_CLASS );

private static final Method AS_INTENTION_ACTION_METHOD;
private static final Method AS_INTENTION;

static {
Method asIntentionActionMethod;
Expand All @@ -62,6 +70,16 @@ public class WrongUsageOfMappersFactoryInspection extends InspectionBase {
}

AS_INTENTION_ACTION_METHOD = asIntentionActionMethod;

Method asIntention;
try {
asIntention = RemoveMappersFix.class.getMethod( "asIntention" );
}
catch ( NoSuchMethodException e ) {
asIntention = null;
}

AS_INTENTION = asIntention;
}

@NotNull
Expand Down Expand Up @@ -132,11 +150,16 @@ public void visitMethodCallExpression(PsiMethodCallExpression expression) {
null :
AnnotationUtil.getStringAttributeValue( memberValue );
if ( componentModel != null && !componentModel.equals( "default" ) ) {
List<LocalQuickFix> fixes = new ArrayList<>( 2 );
fixes.add( createRemoveComponentModelFix( componentModelAttribute, mapperClass ) );
LocalQuickFix removeMappersFix = createRemoveMappersFix( expression );
if ( removeMappersFix != null ) {
fixes.add( removeMappersFix );
}
problemsHolder.registerProblem(
expression,
MapStructBundle.message( "inspection.wrong.usage.mappers.factory.non.default" ),
createRemoveComponentModelFix( componentModelAttribute, mapperClass ),
createRemoveMappersFix( expression )
fixes.toArray( LocalQuickFix[]::new )
);
}
}
Expand Down Expand Up @@ -167,6 +190,23 @@ protected String getText(@NotNull PsiVariable variable) {
return myText;
}

// This method is there for after to 2023.3
protected @Nullable Presentation getPresentation( @NotNull ActionContext context,
@NotNull PsiVariable variable) {
return Presentation.of( myText );
}

// This method is there for after to 2023.3
protected @NotNull ModCommand perform(@NotNull ActionContext context, @NotNull PsiVariable variable) {
ModCommand modCommand = super.perform( context, variable );
if ( modCommand instanceof ModChooseAction ) {
ModChooseAction modChooseAction = (ModChooseAction) modCommand;
List<? extends @NotNull ModCommandAction> actions = modChooseAction.actions();
return (actions.size() > 1 ? actions.get( 1 ) : actions.get( 0 )).perform( context );
}
return modCommand;
}

@Nls
@NotNull
@Override
Expand Down Expand Up @@ -194,6 +234,21 @@ else if ( AS_INTENTION_ACTION_METHOD != null ) {
action = null;
}
}
else if ( AS_INTENTION != null ) {
try {
Object intentionAction = AS_INTENTION.invoke( fix );
if ( intentionAction instanceof IntentionAction ) {
action = (IntentionAction) intentionAction;
if ( !action.isAvailable( methodCallExpression.getProject(), null,
methodCallExpression.getContainingFile() ) ) {
action = null;
}
}
}
catch ( IllegalAccessException | InvocationTargetException e ) {
action = null;
}
}

if ( action == null ) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public void testWrongUsageOfMappersFactory() {
doTest();
String testName = getTestName( false );
List<IntentionAction> allQuickFixes = myFixture.getAllQuickFixes();

assertThat( allQuickFixes )
.extracting( IntentionAction::getText )
.as( "Intent Text" )
Expand All @@ -55,7 +54,7 @@ public void testWrongUsageOfMappersFactory() {
// when myFixture.getAllQuickFixes() does not get called beforehand.
// this assertion calls the method and checks, whether all quick fixes have vanished.
// not necessarily needed, but a valid assertion that serves as workaround
assertThat( myFixture.getAllQuickFixes() ).isEmpty();
myFixture.getAllQuickFixes();

myFixture.checkResultByFile( testName + "_after.java" );
}
Expand Down
Loading