diff --git a/plugins/java-i18n/src/com/intellij/codeInspection/i18n/InvalidPropertyKeyInspection.java b/plugins/java-i18n/src/com/intellij/codeInspection/i18n/InvalidPropertyKeyInspection.java index 5fb49198af17..c223d812b934 100644 --- a/plugins/java-i18n/src/com/intellij/codeInspection/i18n/InvalidPropertyKeyInspection.java +++ b/plugins/java-i18n/src/com/intellij/codeInspection/i18n/InvalidPropertyKeyInspection.java @@ -138,6 +138,9 @@ public class InvalidPropertyKeyInspection extends AbstractBaseJavaLocalInspectio @Override public void visitReferenceExpression(PsiReferenceExpression expression) { super.visitReferenceExpression(expression); + if (isComputedPropertyExpression(expression)) { + return; + } final PsiElement resolvedExpression = expression.resolve(); if (!(resolvedExpression instanceof PsiField)) { return; @@ -159,7 +162,6 @@ public class InvalidPropertyKeyInspection extends AbstractBaseJavaLocalInspectio } private void visitPropertyKeyAnnotationParameter(PsiExpression expression, String key) { - if (!(expression.getParent() instanceof PsiExpressionList)) return; Ref resourceBundleName = new Ref<>(); if (!JavaI18nUtil.isValidPropertyReference(myManager.getProject(), expression, key, resourceBundleName)) { String bundleName = resourceBundleName.get(); @@ -224,7 +226,7 @@ public class InvalidPropertyKeyInspection extends AbstractBaseJavaLocalInspectio Object value = expression.getValue(); if (!(value instanceof String)) return; String key = (String)value; - if (isComputablePropertyExpression(expression)) return; + if (isComputedPropertyExpression(expression)) return; visitPropertyKeyAnnotationParameter(expression, key); } @@ -267,9 +269,21 @@ public class InvalidPropertyKeyInspection extends AbstractBaseJavaLocalInspectio && methodCall.getArgumentList().getExpressionTypes()[i] instanceof PsiArrayType; } - private static boolean isComputablePropertyExpression(PsiExpression expression) { - while (expression != null && expression.getParent() instanceof PsiParenthesizedExpression) expression = (PsiExpression)expression.getParent(); - return expression != null && expression.getParent() instanceof PsiExpression; + private static boolean isComputedPropertyExpression(PsiExpression expression) { + PsiElement parent = expression.getParent(); + while (true) { + if (parent instanceof PsiParenthesizedExpression || + (parent instanceof PsiConditionalExpression && + (expression == ((PsiConditionalExpression)parent).getThenExpression() || + expression == ((PsiConditionalExpression)parent).getElseExpression()))) { + expression = (PsiExpression)parent; + parent = expression.getParent(); + } + else { + break; + } + } + return parent instanceof PsiExpression; } public List getProblems() { diff --git a/plugins/java-i18n/testData/inspections/invalidPropertyKey/simple/expected.xml b/plugins/java-i18n/testData/inspections/invalidPropertyKey/simple/expected.xml index 0f402a094699..277010b64aac 100644 --- a/plugins/java-i18n/testData/inspections/invalidPropertyKey/simple/expected.xml +++ b/plugins/java-i18n/testData/inspections/invalidPropertyKey/simple/expected.xml @@ -21,4 +21,34 @@ 14 Property 'with.params' expected 3 parameters, passed 0 + + Test.java + 23 + Invalid property key + Invalid resource bundle reference 'invalid' + + + Test.java + 26 + Invalid property key + String literal 'invalid' doesn't appear to be valid property key + + + Test.java + 26 + Invalid property key + String literal '.params' doesn't appear to be valid property key + + + Test.java + 27 + Invalid property key + String literal 'invalid' doesn't appear to be valid property key + + + Test.java + 28 + Invalid property key + String literal '.params' doesn't appear to be valid property key + diff --git a/plugins/java-i18n/testData/inspections/invalidPropertyKey/simple/src/x/Test.java b/plugins/java-i18n/testData/inspections/invalidPropertyKey/simple/src/x/Test.java index fee7630f925c..522d04ed85f4 100644 --- a/plugins/java-i18n/testData/inspections/invalidPropertyKey/simple/src/x/Test.java +++ b/plugins/java-i18n/testData/inspections/invalidPropertyKey/simple/src/x/Test.java @@ -16,6 +16,15 @@ class Test { String ss3 = f2("with.params"); String ss4 = IBundle.message("with.params", new Object[3]); // don't check if array passed + private static final String CONST = ".params"; + String f1(@PropertyKey(resourceBundle = IBundle.BUNDLE) String s, Object...params) {return "";} String f2(@PropertyKey(resourceBundle = IBundle.BUNDLE) String s) {return "";} + void f3(@PropertyKey(resourceBundle = "invalid") String s) { + IBundle.message(s + ".params"); + IBundle.message(s + CONST); + IBundle.message(s == null ? CONST : "invalid"); + IBundle.message(((("invalid")))); + IBundle.message((((CONST)))); + } }