mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-10 13:17:09 +07:00
[java-inspections] IDEA-264850 MagicConstant inspection does not work on varargs
GitOrigin-RevId: 4ac1cdc65c10fc40dec8ba5a3add52d12897526a
This commit is contained in:
committed by
intellij-monorepo-bot
parent
b5f948dd5f
commit
cbdf7bb2f9
+14
-3
@@ -109,9 +109,20 @@ public final class MagicCompletionContributor extends CompletionContributor impl
|
||||
int i = ArrayUtil.indexOf(list.getExpressions(), argument);
|
||||
if (i == -1) continue;
|
||||
PsiParameter[] params = method.getParameterList().getParameters();
|
||||
if (i >= params.length) continue;
|
||||
PsiParameter parameter = params[i];
|
||||
result.add(Pair.create(parameter, parameter.getType()));
|
||||
PsiParameter parameter;
|
||||
PsiType parameterType;
|
||||
if (method.isVarArgs() && i >= params.length - 1) {
|
||||
parameter = ArrayUtil.getLastElement(params);
|
||||
parameterType = ((PsiEllipsisType)parameter.getType()).getComponentType();
|
||||
}
|
||||
else if (i < params.length) {
|
||||
parameter = params[i];
|
||||
parameterType = parameter.getType();
|
||||
}
|
||||
else {
|
||||
continue;
|
||||
}
|
||||
result.add(Pair.create(parameter, parameterType));
|
||||
}
|
||||
}
|
||||
else if (IN_BINARY_COMPARISON.accepts(pos)) {
|
||||
|
||||
+13
-5
@@ -227,14 +227,22 @@ public final class MagicConstantInspection extends AbstractBaseJavaLocalInspecti
|
||||
PsiExpression[] arguments = argumentList.getExpressions();
|
||||
for (int i = 0; i < parameters.length; i++) {
|
||||
PsiParameter parameter = parameters[i];
|
||||
AllowedValues values = MagicConstantUtils.getAllowedValues(parameter, parameter.getType());
|
||||
PsiType type = parameter.getType();
|
||||
int stopArg = i;
|
||||
if (type instanceof PsiEllipsisType) {
|
||||
type = ((PsiEllipsisType)type).getComponentType();
|
||||
stopArg = arguments.length - 1;
|
||||
}
|
||||
AllowedValues values = MagicConstantUtils.getAllowedValues(parameter, type);
|
||||
if (values == null) continue;
|
||||
if (i >= arguments.length) break;
|
||||
PsiExpression argument = arguments[i];
|
||||
argument = PsiUtil.deparenthesizeExpression(argument);
|
||||
if (argument == null) continue;
|
||||
for (int j = i; j <= stopArg; j++) {
|
||||
PsiExpression argument = arguments[j];
|
||||
argument = PsiUtil.deparenthesizeExpression(argument);
|
||||
if (argument == null) continue;
|
||||
|
||||
checkMagicParameterArgument(parameter, argument, values, holder);
|
||||
checkMagicParameterArgument(parameter, argument, values, holder);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2000-2011 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import org.intellij.lang.annotations.MagicConstant;
|
||||
|
||||
public class VarargMethodCall {
|
||||
private static class Constants {
|
||||
public static final int ONE = 1;
|
||||
public static final int TWO = 2;
|
||||
}
|
||||
public static void testAnnotation1(@MagicConstant(valuesFromClass = Constants.class) int var0) {
|
||||
}
|
||||
public static void testAnnotation2(@MagicConstant(valuesFromClass = Constants.class) int... vars) {
|
||||
}
|
||||
public static void testMethod() {
|
||||
testAnnotation1(<warning descr="Should be one of: Constants.ONE, Constants.TWO">1</warning>);
|
||||
testAnnotation2(<warning descr="Should be one of: Constants.ONE, Constants.TWO">1</warning>, <warning descr="Should be one of: Constants.ONE, Constants.TWO">2</warning>);
|
||||
}
|
||||
}
|
||||
+50
@@ -157,4 +157,54 @@ interface Foo {
|
||||
myFixture.complete(CompletionType.SMART)
|
||||
myFixture.assertPreferredCompletionItems 0, 'BAR', 'FOO'
|
||||
}
|
||||
|
||||
@NeedsIndex.ForStandardLibrary
|
||||
void testVarargMethodCall() {
|
||||
addMagicConstant()
|
||||
|
||||
@Language("JAVA")
|
||||
def s = """
|
||||
import org.intellij.lang.annotations.MagicConstant;
|
||||
|
||||
public class VarargMethodCall {
|
||||
private static class Constants {
|
||||
public static final int ONE = 1;
|
||||
public static final int TWO = 2;
|
||||
}
|
||||
public static void testAnnotation2(@MagicConstant(valuesFromClass = Constants.class) int... vars) {
|
||||
}
|
||||
public static void testMethod() {
|
||||
testAnnotation2(<caret>);
|
||||
}
|
||||
}
|
||||
"""
|
||||
myFixture.configureByText "a.java", s
|
||||
myFixture.complete(CompletionType.SMART)
|
||||
myFixture.assertPreferredCompletionItems 0, 'ONE', 'TWO'
|
||||
}
|
||||
|
||||
@NeedsIndex.ForStandardLibrary
|
||||
void testVarargMethodCall2() {
|
||||
addMagicConstant()
|
||||
|
||||
@Language("JAVA")
|
||||
def s = """
|
||||
import org.intellij.lang.annotations.MagicConstant;
|
||||
|
||||
public class VarargMethodCall {
|
||||
private static class Constants {
|
||||
public static final int ONE = 1;
|
||||
public static final int TWO = 2;
|
||||
}
|
||||
public static void testAnnotation2(@MagicConstant(valuesFromClass = Constants.class) int... vars) {
|
||||
}
|
||||
public static void testMethod() {
|
||||
testAnnotation2(Constants.ONE, <caret>);
|
||||
}
|
||||
}
|
||||
"""
|
||||
myFixture.configureByText "a.java", s
|
||||
myFixture.complete(CompletionType.SMART)
|
||||
myFixture.assertPreferredCompletionItems 0, 'ONE', 'TWO'
|
||||
}
|
||||
}
|
||||
|
||||
+1
@@ -45,6 +45,7 @@ public class MagicConstantInspectionTest extends LightJavaCodeInsightFixtureTest
|
||||
// test that the optimisation for not loading AST works
|
||||
public void testWithLibrary() { doTest(); }
|
||||
public void testSpecialCases() { doTest(); }
|
||||
public void testVarargMethodCall() { doTest(); }
|
||||
|
||||
private void doTest() {
|
||||
myFixture.configureByFile(getTestName(false) + ".java");
|
||||
|
||||
Reference in New Issue
Block a user