diff --git a/plugins/java-i18n/src/com/intellij/codeInspection/i18n/JavaI18nUtil.java b/plugins/java-i18n/src/com/intellij/codeInspection/i18n/JavaI18nUtil.java index e871de626d74..3a41301231e1 100644 --- a/plugins/java-i18n/src/com/intellij/codeInspection/i18n/JavaI18nUtil.java +++ b/plugins/java-i18n/src/com/intellij/codeInspection/i18n/JavaI18nUtil.java @@ -15,12 +15,10 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.TextRange; import com.intellij.psi.*; -import com.intellij.psi.scope.PsiScopeProcessor; import com.intellij.psi.scope.util.PsiScopesUtil; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.TypeConversionUtil; import com.intellij.util.ArrayUtil; -import com.intellij.util.IncorrectOperationException; import gnu.trove.THashSet; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -33,16 +31,8 @@ import java.util.*; * @author max */ public class JavaI18nUtil extends I18nUtil { - public static final PropertyCreationHandler DEFAULT_PROPERTY_CREATION_HANDLER = new PropertyCreationHandler() { - @Override - public void createProperty(@NotNull final Project project, - @NotNull final Collection propertiesFiles, - @NotNull final String key, - @NotNull final String value, - @NotNull final PsiExpression[] parameters) throws IncorrectOperationException { - I18nUtil.createProperty(project, propertiesFiles, key, value, true); - } - }; + public static final PropertyCreationHandler DEFAULT_PROPERTY_CREATION_HANDLER = + (project, propertiesFiles, key, value, parameters) -> createProperty(project, propertiesFiles, key, value, true); private JavaI18nUtil() { } @@ -59,9 +49,8 @@ public class JavaI18nUtil extends I18nUtil { return psiElement.getTextRange(); } - public static boolean mustBePropertyKey(@NotNull PsiExpression expression, - @Nullable Ref resourceBundleRef) { - final PsiElement parent = expression.getParent(); + public static boolean mustBePropertyKey(@NotNull PsiExpression expression, @Nullable Ref resourceBundleRef) { + PsiElement parent = expression.getParent(); if (parent instanceof PsiVariable) { final PsiAnnotation annotation = AnnotationUtil.findAnnotation((PsiVariable)parent, AnnotationUtil.PROPERTY_KEY); if (annotation != null) { @@ -94,11 +83,10 @@ public class JavaI18nUtil extends I18nUtil { static boolean isPassedToAnnotatedParam(@NotNull PsiExpression expression, final String annFqn, - @Nullable Ref resourceBundleRef, - @Nullable final Set nonNlsTargets) { + @Nullable Ref resourceBundleRef, + @Nullable final Set nonNlsTargets) { expression = getTopLevelExpression(expression); final PsiElement parent = expression.getParent(); - if (!(parent instanceof PsiExpressionList)) return false; int idx = -1; final PsiExpression[] args = ((PsiExpressionList)parent).getExpressions(); @@ -119,9 +107,7 @@ public class JavaI18nUtil extends I18nUtil { if (grParent instanceof PsiCall) { PsiMethod method = ((PsiCall)grParent).resolveMethod(); - if (method != null && isMethodParameterAnnotatedWith(method, idx, null, annFqn, resourceBundleRef, nonNlsTargets)) { - return true; - } + return method != null && isMethodParameterAnnotatedWith(method, idx, null, annFqn, resourceBundleRef, nonNlsTargets); } return false; @@ -143,10 +129,10 @@ public class JavaI18nUtil extends I18nUtil { static boolean isMethodParameterAnnotatedWith(final PsiMethod method, final int idx, - @Nullable Collection processed, + @Nullable Collection processed, final String annFqn, - @Nullable Ref resourceBundleRef, - @Nullable final Set nonNlsTargets) { + @Nullable Ref resourceBundleRef, + @Nullable final Set nonNlsTargets) { if (processed != null) { if (processed.contains(method)) return false; } @@ -189,7 +175,7 @@ public class JavaI18nUtil extends I18nUtil { return false; } - private static void processAnnotationAttributes(@Nullable Ref resourceBundleRef, + private static void processAnnotationAttributes(@Nullable Ref resourceBundleRef, @NotNull PsiAnnotation annotation) { if (resourceBundleRef != null) { final PsiAnnotationParameterList parameterList = annotation.getParameterList(); @@ -206,7 +192,7 @@ public class JavaI18nUtil extends I18nUtil { static boolean isValidPropertyReference(@NotNull Project project, @NotNull PsiExpression expression, @NotNull String key, - @NotNull Ref outResourceBundle) { + @NotNull Ref outResourceBundle) { Ref resourceBundleRef = Ref.create(); if (mustBePropertyKey(expression, resourceBundleRef)) { final Object resourceBundleName = resourceBundleRef.get(); @@ -255,9 +241,7 @@ public class JavaI18nUtil extends I18nUtil { bundleRef.set(propertiesFile.getResourceBundle()); } else { - if (!bundleRef.get().equals(propertiesFile.getResourceBundle())) { - return false; - } + return bundleRef.get().equals(propertiesFile.getResourceBundle()); } } return true; @@ -284,8 +268,9 @@ public class JavaI18nUtil extends I18nUtil { Set result = new LinkedHashSet<>(); for (PsiVariable var : variables) { PsiType varType = var.getType(); - if (type == null || type.isAssignableFrom(varType)) { - result.add(var.getNameIdentifier().getText()); + PsiIdentifier identifier = var.getNameIdentifier(); + if ((type == null || type.isAssignableFrom(varType)) && identifier != null) { + result.add(identifier.getText()); } } @@ -301,20 +286,17 @@ public class JavaI18nUtil extends I18nUtil { private static void addAvailableMethodsOfType(final PsiClassType type, final PsiLiteralExpression context, - final Collection result) { - PsiScopesUtil.treeWalkUp(new PsiScopeProcessor() { - @Override - public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) { - if (element instanceof PsiMethod) { - PsiMethod method = (PsiMethod)element; - PsiType returnType = method.getReturnType(); - if (returnType != null && TypeConversionUtil.isAssignable(type, returnType) - && method.getParameterList().isEmpty()) { - result.add(method.getName() + "()"); - } + final Collection result) { + PsiScopesUtil.treeWalkUp((element, state) -> { + if (element instanceof PsiMethod) { + PsiMethod method = (PsiMethod)element; + PsiType returnType = method.getReturnType(); + if (returnType != null && TypeConversionUtil.isAssignable(type, returnType) + && method.getParameterList().isEmpty()) { + result.add(method.getName() + "()"); } - return true; } + return true; }, context, null); } @@ -347,11 +329,7 @@ public class JavaI18nUtil extends I18nUtil { * @return number of parameters */ public static int getPropertyValueParamsMaxCount(@NotNull final UExpression expression) { - return getPropertyValueParamsMaxCount(expression, null); - } - - private static int getPropertyValueParamsMaxCount(@NotNull final UExpression expression, @Nullable final String resourceBundleName) { - final SortedSet paramsCount = getPropertyValueParamsCount(expression, resourceBundleName); + final SortedSet paramsCount = getPropertyValueParamsCount(expression, null); if (paramsCount.isEmpty()) { return -1; } @@ -367,7 +345,8 @@ public class JavaI18nUtil extends I18nUtil { } @NotNull - static SortedSet getPropertyValueParamsCount(@NotNull final UExpression expression, @Nullable final String resourceBundleName) { + private static SortedSet getPropertyValueParamsCount(@NotNull final UExpression expression, + @Nullable final String resourceBundleName) { final ULiteralExpression literalExpression; if (expression instanceof ULiteralExpression) { literalExpression = (ULiteralExpression)expression; diff --git a/plugins/java-i18n/src/com/intellij/lang/properties/PropertiesReferenceProvider.java b/plugins/java-i18n/src/com/intellij/lang/properties/PropertiesReferenceProvider.java index 10f83bdfde94..9d71821888bd 100644 --- a/plugins/java-i18n/src/com/intellij/lang/properties/PropertiesReferenceProvider.java +++ b/plugins/java-i18n/src/com/intellij/lang/properties/PropertiesReferenceProvider.java @@ -15,7 +15,6 @@ */ package com.intellij.lang.properties; -import com.intellij.codeInsight.AnnotationUtil; import com.intellij.codeInspection.i18n.JavaI18nUtil; import com.intellij.lang.properties.references.PropertyReference; import com.intellij.openapi.util.Ref; @@ -31,8 +30,6 @@ import com.intellij.xml.util.XmlUtil; import org.jetbrains.annotations.NotNull; import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; /** * @author cdr @@ -55,6 +52,7 @@ public class PropertiesReferenceProvider extends PsiReferenceProvider { return target instanceof IProperty; } + @Override @NotNull public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull final ProcessingContext context) { Object value = null; @@ -62,7 +60,7 @@ public class PropertiesReferenceProvider extends PsiReferenceProvider { boolean propertyRefWithPrefix = false; boolean soft = myDefaultSoft; - if (element instanceof PsiLiteralExpression && canBePropertyKeyRef(element)) { + if (element instanceof PsiLiteralExpression && canBePropertyKeyRef((PsiExpression)element)) { PsiLiteralExpression literalExpression = (PsiLiteralExpression)element; value = literalExpression.getValue(); @@ -72,7 +70,8 @@ public class PropertiesReferenceProvider extends PsiReferenceProvider { PsiAnnotationMemberValue resourceBundleName = resourceBundleValue.get(); if (resourceBundleName instanceof PsiExpression) { PsiExpression expr = (PsiExpression)resourceBundleName; - final Object bundleValue = JavaPsiFacade.getInstance(expr.getProject()).getConstantEvaluationHelper().computeConstantExpression(expr); + final Object bundleValue = + JavaPsiFacade.getInstance(expr.getProject()).getConstantEvaluationHelper().computeConstantExpression(expr); bundleName = bundleValue == null ? null : bundleValue.toString(); } } @@ -102,23 +101,23 @@ public class PropertiesReferenceProvider extends PsiReferenceProvider { } static boolean isNonDynamicAttribute(final PsiElement element) { - return PsiTreeUtil.getChildOfAnyType(element, OuterLanguageElement.class,JspXmlTagBase.class) == null; + return PsiTreeUtil.getChildOfAnyType(element, OuterLanguageElement.class, JspXmlTagBase.class) == null; } - private static boolean canBePropertyKeyRef(PsiElement element) { + private static boolean canBePropertyKeyRef(@NotNull PsiExpression element) { PsiElement parent = element.getParent(); if (parent instanceof PsiExpression) { - if ((parent instanceof PsiConditionalExpression)) { + if (parent instanceof PsiConditionalExpression) { PsiExpression elseExpr = ((PsiConditionalExpression)parent).getElseExpression(); PsiExpression thenExpr = ((PsiConditionalExpression)parent).getThenExpression(); - return (element == thenExpr || element == elseExpr) && canBePropertyKeyRef(parent); + return (element == thenExpr || element == elseExpr) && canBePropertyKeyRef((PsiExpression)parent); } else { return false; } - } else { + } + else { return true; } - } }