allow @PropertyKey on variables (IDEA-90159)

This commit is contained in:
anna
2012-09-18 09:16:10 +02:00
parent 4adc3db92c
commit 0f0f7aa682
3 changed files with 38 additions and 21 deletions
@@ -29,7 +29,7 @@ import java.lang.annotation.*;
*/
@Documented
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
@Target({ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.FIELD})
public @interface PropertyKey {
/**
* The full-qualified name of the resource bundle in which the property keys must
@@ -37,10 +37,7 @@ import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* @author max
@@ -96,13 +93,22 @@ public class InvalidPropertyKeyInspection extends BaseJavaLocalInspectionTool {
@Override
@Nullable
public ProblemDescriptor[] checkField(@NotNull PsiField field, @NotNull InspectionManager manager, boolean isOnTheFly) {
final PsiExpression initializer = field.getInitializer();
if (initializer != null) return checkElement(initializer, manager, isOnTheFly);
List<ProblemDescriptor> result = new ArrayList<ProblemDescriptor>();
appendProblems(manager, isOnTheFly, result, field.getInitializer());
appendProblems(manager, isOnTheFly, result, field.getModifierList());
if (field instanceof PsiEnumConstant) {
return checkElement(((PsiEnumConstant)field).getArgumentList(), manager, isOnTheFly);
appendProblems(manager, isOnTheFly, result, ((PsiEnumConstant)field).getArgumentList());
}
return result.isEmpty() ? null : result.toArray(new ProblemDescriptor[result.size()]);
}
private static void appendProblems(InspectionManager manager, boolean isOnTheFly, List<ProblemDescriptor> result, PsiElement element) {
if (element != null){
final ProblemDescriptor[] descriptors = checkElement(element, manager, isOnTheFly);
if (descriptors != null) {
Collections.addAll(result, descriptors);
}
}
return null;
}
@Nullable
@@ -70,6 +70,13 @@ public class JavaI18nUtil extends I18nUtil {
public static boolean mustBePropertyKey(@NotNull Project project,
@NotNull PsiLiteralExpression expression,
@NotNull Map<String, Object> annotationAttributeValues) {
final PsiElement parent = expression.getParent();
if (parent instanceof PsiVariable) {
final PsiAnnotation annotation = AnnotationUtil.findAnnotation((PsiVariable)parent, AnnotationUtil.PROPERTY_KEY);
if (annotation != null) {
return processAnnotationAttributes(annotationAttributeValues, annotation);
}
}
return isPassedToAnnotatedParam(project, expression, AnnotationUtil.PROPERTY_KEY, annotationAttributeValues, null);
}
@@ -191,17 +198,7 @@ public class JavaI18nUtil extends I18nUtil {
}
final PsiAnnotation annotation = AnnotationUtil.findAnnotation(param, annFqn);
if (annotation != null) {
if (annotationAttributeValues != null) {
final PsiAnnotationParameterList parameterList = annotation.getParameterList();
final PsiNameValuePair[] attributes = parameterList.getAttributes();
for (PsiNameValuePair attribute : attributes) {
final String name = attribute.getName();
if (annotationAttributeValues.containsKey(name)) {
annotationAttributeValues.put(name, attribute.getValue());
}
}
}
return true;
return processAnnotationAttributes(annotationAttributeValues, annotation);
}
if (nonNlsTargets != null) {
nonNlsTargets.add(param);
@@ -215,6 +212,20 @@ public class JavaI18nUtil extends I18nUtil {
return false;
}
private static boolean processAnnotationAttributes(@Nullable Map<String, Object> annotationAttributeValues, @NotNull PsiAnnotation annotation) {
if (annotationAttributeValues != null) {
final PsiAnnotationParameterList parameterList = annotation.getParameterList();
final PsiNameValuePair[] attributes = parameterList.getAttributes();
for (PsiNameValuePair attribute : attributes) {
final String name = attribute.getName();
if (annotationAttributeValues.containsKey(name)) {
annotationAttributeValues.put(name, attribute.getValue());
}
}
}
return true;
}
public static boolean isValidPropertyReference(@NotNull Project project,
@NotNull PsiLiteralExpression expression,
@NotNull String key,