fix Invalid resource bundle reference warning

in "Java | Properties Files | Invalid property key" inspection
This commit is contained in:
Bas Leijdekkers
2017-11-30 16:31:48 +01:00
parent 114da7e3fe
commit d088f5022d
3 changed files with 58 additions and 5 deletions
@@ -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<String> 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<ProblemDescriptor> getProblems() {
@@ -21,4 +21,34 @@
<line>14</line>
<description>Property 'with.params' expected 3 parameters, passed 0</description>
</problem>
<problem>
<file>Test.java</file>
<line>23</line>
<problem_class severity="ERROR" attribute_key="WRONG_REFERENCES_ATTRIBUTES">Invalid property key</problem_class>
<description>Invalid resource bundle reference 'invalid'</description>
</problem>
<problem>
<file>Test.java</file>
<line>26</line>
<problem_class severity="ERROR" attribute_key="WRONG_REFERENCES_ATTRIBUTES">Invalid property key</problem_class>
<description>String literal 'invalid' doesn't appear to be valid property key</description>
</problem>
<problem>
<file>Test.java</file>
<line>26</line>
<problem_class severity="ERROR" attribute_key="WRONG_REFERENCES_ATTRIBUTES">Invalid property key</problem_class>
<description>String literal '.params' doesn't appear to be valid property key</description>
</problem>
<problem>
<file>Test.java</file>
<line>27</line>
<problem_class severity="ERROR" attribute_key="WRONG_REFERENCES_ATTRIBUTES">Invalid property key</problem_class>
<description>String literal 'invalid' doesn't appear to be valid property key</description>
</problem>
<problem>
<file>Test.java</file>
<line>28</line>
<problem_class severity="ERROR" attribute_key="WRONG_REFERENCES_ATTRIBUTES">Invalid property key</problem_class>
<description>String literal '.params' doesn't appear to be valid property key</description>
</problem>
</problems>
@@ -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))));
}
}