Java: offer more quickfixes on enum constant with incorrect argument type

GitOrigin-RevId: 080edd03ae7d11746fe303dd7b59720f98c0d115
This commit is contained in:
Bas Leijdekkers
2023-03-01 13:55:34 +01:00
committed by intellij-monorepo-bot
parent ec5b3502b3
commit 5147fdd123
4 changed files with 32 additions and 1 deletions

View File

@@ -1971,6 +1971,9 @@ public final class HighlightMethodUtil {
ConstructorParametersFixer.registerFixActions(classReference, constructorCall, builder, fixRange);
ChangeTypeArgumentsFix.registerIntentions(results, list, builder, aClass, fixRange);
}
else if (aClass.isEnum()) {
ConstructorParametersFixer.registerEnumConstantFixActions(aClass, constructorCall, builder, fixRange);
}
ChangeStringLiteralToCharInMethodCallFix.registerFixes(constructors, constructorCall, builder, fixRange);
IntentionAction action = QUICK_FIX_FACTORY.createSurroundWithArrayFix(constructorCall, null);
builder.registerFix(action, null, null, fixRange, null);

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
/*
Propose to cast one argument to corresponding type
@@ -34,4 +34,18 @@ public final class ConstructorParametersFixer {
WrapObjectWithOptionalOfNullableFix.REGISTAR.registerCastActions(candidates, constructorCall, builder, fixRange);
WrapWithAdapterMethodCallFix.registerCastActions(candidates, constructorCall, builder, fixRange);
}
public static void registerEnumConstantFixActions(@NotNull PsiClass aClass,
@NotNull PsiConstructorCall constructorCall,
@NotNull HighlightInfo.Builder builder,
@NotNull TextRange fixRange) {
PsiMethod[] methods = aClass.getConstructors();
CandidateInfo[] candidates = new CandidateInfo[methods.length];
for (int i = 0; i < candidates.length; i++) {
candidates[i] = new CandidateInfo(methods[i], PsiSubstitutor.EMPTY);
}
CastMethodArgumentFix.REGISTRAR.registerCastActions(candidates, constructorCall, builder, fixRange);
WrapObjectWithOptionalOfNullableFix.REGISTAR.registerCastActions(candidates, constructorCall, builder, fixRange);
WrapWithAdapterMethodCallFix.registerCastActions(candidates, constructorCall, builder, fixRange);
}
}

View File

@@ -0,0 +1,7 @@
// "Cast argument to 'byte'" "true-preview"
enum E {
A((byte) 1);
E(byte b) {}
}

View File

@@ -0,0 +1,7 @@
// "Cast argument to 'byte'" "true-preview"
enum E {
A(<caret>1);
E(byte b) {}
}