mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
CustomMethodHandlers: evaluate enum name()
GitOrigin-RevId: 2172db0a3c0355359d9e0f822409483b8f3c35e7
This commit is contained in:
committed by
intellij-monorepo-bot
parent
01568dcb42
commit
6ac3052a65
+13
@@ -88,6 +88,8 @@ class CustomMethodHandlers {
|
||||
(args, memState, factory, method) -> numberAsString(args, memState, factory, 3, Long.SIZE))
|
||||
.register(staticCall(JAVA_LANG_LONG, "toBinaryString").parameterCount(1),
|
||||
(args, memState, factory, method) -> numberAsString(args, memState, factory, 1, Long.SIZE))
|
||||
.register(instanceCall(JAVA_LANG_ENUM, "name").parameterCount(0),
|
||||
(args, memState, factory, method) -> enumName(args.myQualifier, memState, factory, method.getReturnType()))
|
||||
.register(anyOf(
|
||||
staticCall(JAVA_UTIL_COLLECTIONS, "emptyList", "emptySet", "emptyMap", "singleton", "singletonList", "singletonMap"),
|
||||
staticCall(JAVA_UTIL_LIST, "of"),
|
||||
@@ -341,6 +343,17 @@ class CustomMethodHandlers {
|
||||
return factory.getFactFactory().createValue(map);
|
||||
}
|
||||
|
||||
private static DfaValue enumName(DfaValue qualifier, DfaMemoryState state, DfaValueFactory factory, PsiType type) {
|
||||
DfaConstValue constant = state.getConstantValue(qualifier);
|
||||
if (constant != null) {
|
||||
Object value = constant.getValue();
|
||||
if (value instanceof PsiEnumConstant) {
|
||||
return factory.getConstFactory().createFromValue(((PsiEnumConstant)value).getName(), type);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static Object getConstantValue(DfaMemoryState memoryState, DfaValue value) {
|
||||
if (value != null) {
|
||||
LongRangeSet fact = memoryState.getValueFact(value, DfaFactType.RANGE);
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
enum X {
|
||||
FOO, BAR, BAZ
|
||||
}
|
||||
|
||||
class C {
|
||||
void test(X x) {
|
||||
if (x == X.FOO) {
|
||||
if (<warning descr="Condition 'x.name().equals(\"FOO\")' is always 'true'">x.name().equals("FOO")</warning>) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -671,4 +671,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
|
||||
public void testCellsComplex() { doTest(); }
|
||||
public void testArraysAsList() { doTest(); }
|
||||
public void testArraycopy() { doTest(); }
|
||||
public void testEnumName() { doTest(); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user