2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.lang
.properties
;
18 import com
.intellij
.codeInspection
.InspectionManager
;
19 import com
.intellij
.codeInspection
.LocalQuickFix
;
20 import com
.intellij
.codeInspection
.ProblemDescriptor
;
21 import com
.intellij
.codeInspection
.ProblemHighlightType
;
22 import com
.intellij
.codeInspection
.ex
.BaseLocalInspectionTool
;
23 import com
.intellij
.lang
.ASTNode
;
24 import com
.intellij
.lang
.properties
.psi
.PropertiesFile
;
25 import com
.intellij
.lang
.properties
.psi
.Property
;
26 import com
.intellij
.psi
.PsiElement
;
27 import com
.intellij
.psi
.PsiFile
;
28 import org
.jetbrains
.annotations
.NonNls
;
29 import org
.jetbrains
.annotations
.NotNull
;
30 import org
.jetbrains
.annotations
.Nullable
;
32 import java
.util
.ArrayList
;
33 import java
.util
.HashSet
;
34 import java
.util
.List
;
41 public class UnusedMessageFormatParameterInspection
extends BaseLocalInspectionTool
{
43 public String
getGroupDisplayName() {
44 return PropertiesBundle
.message("properties.files.inspection.group.display.name");
48 public String
getDisplayName() {
49 return PropertiesBundle
.message("unused.message.format.parameter.display.name");
54 public String
getShortName() {
55 return "UnusedMessageFormatParameter";
59 public ProblemDescriptor
[] checkFile(@NotNull PsiFile file
, @NotNull InspectionManager manager
, boolean isOnTheFly
) {
60 if (!(file
instanceof PropertiesFile
)) return null;
61 PropertiesFile propertiesFile
= (PropertiesFile
)file
;
62 final List
<Property
> properties
= propertiesFile
.getProperties();
63 List
<ProblemDescriptor
> problemDescriptors
= new ArrayList
<ProblemDescriptor
>();
64 for (Property property
: properties
) {
65 @NonNls String name
= property
.getName();
66 if (name
!= null && name
.startsWith("log4j")) continue;
67 String value
= property
.getValue();
68 Set
<Integer
> parameters
= new HashSet
<Integer
>();
70 int index
= value
.indexOf('{');
72 value
= value
.substring(index
+ 1);
73 final int comma
= value
.indexOf(',');
74 final int brace
= value
.indexOf('}');
75 if (brace
== -1) break; //misformatted string
80 index
= Math
.min(comma
, brace
);
83 parameters
.add(new Integer(value
.substring(0, index
)));
85 catch (NumberFormatException e
) {
88 index
= value
.indexOf('{');
90 for (Integer integer
: parameters
) {
91 for (int i
= 0; i
< integer
.intValue(); i
++) {
92 if (!parameters
.contains(new Integer(i
))) {
93 ASTNode
[] nodes
= property
.getNode().getChildren(null);
94 PsiElement valElement
= nodes
.length
< 3 ? property
: nodes
[2].getPsi();
95 problemDescriptors
.add(manager
.createProblemDescriptor(valElement
, PropertiesBundle
.message(
96 "unused.message.format.parameter.problem.descriptor", integer
.toString(), Integer
.toString(i
)),
97 (LocalQuickFix
[])null, ProblemHighlightType
.GENERIC_ERROR_OR_WARNING
,
105 return problemDescriptors
.isEmpty() ?
null : problemDescriptors
.toArray(new ProblemDescriptor
[problemDescriptors
.size()]);