2 * Copyright 2003-2008 Dave Griffith, Bas Leijdekkers
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
.siyeh
.ig
.bugs
;
18 import com
.intellij
.codeInspection
.ProblemDescriptor
;
19 import com
.intellij
.openapi
.project
.Project
;
20 import com
.intellij
.psi
.*;
21 import com
.intellij
.psi
.util
.PsiUtil
;
22 import com
.intellij
.util
.IncorrectOperationException
;
23 import com
.siyeh
.InspectionGadgetsBundle
;
24 import com
.siyeh
.ig
.BaseInspection
;
25 import com
.siyeh
.ig
.BaseInspectionVisitor
;
26 import com
.siyeh
.ig
.InspectionGadgetsFix
;
27 import org
.jetbrains
.annotations
.NonNls
;
28 import org
.jetbrains
.annotations
.NotNull
;
30 public class ArchaicSystemPropertyAccessInspection
extends BaseInspection
{
33 public String
getID(){
34 return "UseOfArchaicSystemPropertyAccessors";
38 public String
getDisplayName(){
39 return InspectionGadgetsBundle
.message(
40 "archaic.system.property.accessors.display.name");
44 public String
buildErrorString(Object
... infos
){
45 final PsiMethodCallExpression call
=
46 (PsiMethodCallExpression
) infos
[0];
47 if(isIntegerGetInteger(call
)){
48 return InspectionGadgetsBundle
.message(
49 "archaic.system.property.accessors.problem.descriptor.Integer");
50 } else if(isLongGetLong(call
)){
51 return InspectionGadgetsBundle
.message(
52 "archaic.system.property.accessors.problem.descriptor.Long");
54 return InspectionGadgetsBundle
.message(
55 "archaic.system.property.accessors.problem.descriptor.Boolean");
60 protected InspectionGadgetsFix
[] buildFixes(Object
... infos
){
61 return new InspectionGadgetsFix
[]{new ReplaceWithParseMethodFix(),
62 new ReplaceWithStandardPropertyAccessFix()};
65 private static class ReplaceWithParseMethodFix
extends InspectionGadgetsFix
{
68 public String
getName(){
69 return InspectionGadgetsBundle
.message(
70 "archaic.system.property.accessors.replace.parse.quickfix");
73 public void doFix(Project project
, ProblemDescriptor descriptor
)
74 throws IncorrectOperationException
{
75 final PsiIdentifier location
=
76 (PsiIdentifier
) descriptor
.getPsiElement();
77 final PsiElement parent
= location
.getParent();
78 assert parent
!= null;
79 final PsiMethodCallExpression call
=
80 (PsiMethodCallExpression
) parent
.getParent();
82 final PsiExpressionList argList
= call
.getArgumentList();
83 final PsiExpression
[] args
= argList
.getExpressions();
84 final String argText
= args
[0].getText();
85 @NonNls final String parseMethodCall
;
86 if(isIntegerGetInteger(call
)){
87 parseMethodCall
= "Integer.valueOf(" + argText
+ ')';
88 } else if(isLongGetLong(call
)){
89 parseMethodCall
= "Long.valueOf(" + argText
+ ')';
91 parseMethodCall
= "Boolean.valueOf(" + argText
+ ')';
93 replaceExpression(call
, parseMethodCall
);
98 private static class ReplaceWithStandardPropertyAccessFix
99 extends InspectionGadgetsFix
{
102 public String
getName(){
103 return InspectionGadgetsBundle
.message(
104 "archaic.system.property.accessors.replace.standard.quickfix");
107 public void doFix(Project project
, ProblemDescriptor descriptor
)
108 throws IncorrectOperationException
{
109 final PsiIdentifier location
=
110 (PsiIdentifier
) descriptor
.getPsiElement();
111 final PsiElement parent
= location
.getParent();
112 assert parent
!= null;
113 final PsiMethodCallExpression call
=
114 (PsiMethodCallExpression
) parent
.getParent();
116 final PsiExpressionList argList
= call
.getArgumentList();
117 final PsiExpression
[] args
= argList
.getExpressions();
118 final String argText
= args
[0].getText();
119 @NonNls final String parseMethodCall
;
120 if(isIntegerGetInteger(call
)){
121 parseMethodCall
= "Integer.parseInt(System.getProperty("
123 } else if(isLongGetLong(call
)){
124 parseMethodCall
= "Long.parseLong(System.getProperty("
127 if(!PsiUtil
.isLanguageLevel5OrHigher(call
)){
128 parseMethodCall
= "Boolean.valueOf(System.getProperty("
129 + argText
+ ")).booleanValue()";
131 parseMethodCall
= "Boolean.parseBoolean(System.getProperty("
135 replaceExpression(call
, parseMethodCall
);
140 public BaseInspectionVisitor
buildVisitor(){
141 return new ArchaicSystemPropertyAccessVisitor();
144 private static class ArchaicSystemPropertyAccessVisitor
145 extends BaseInspectionVisitor
{
147 @Override public void visitMethodCallExpression(
148 @NotNull PsiMethodCallExpression expression
){
149 super.visitMethodCallExpression(expression
);
150 if(isIntegerGetInteger(expression
) ||
151 isLongGetLong(expression
) ||
152 isBooleanGetBoolean(expression
)){
153 registerMethodCallError(expression
, expression
);
158 static boolean isIntegerGetInteger(PsiMethodCallExpression expression
){
159 return isCallTo(expression
, "java.lang.Integer", "getInteger");
162 static boolean isLongGetLong(PsiMethodCallExpression expression
){
163 return isCallTo(expression
, "java.lang.Long", "getLong");
166 static boolean isBooleanGetBoolean(PsiMethodCallExpression expression
){
167 return isCallTo(expression
, "java.lang.Boolean", "getBoolean");
170 private static boolean isCallTo(PsiMethodCallExpression expression
,
171 String className
, @NonNls String methodName
) {
172 final PsiReferenceExpression methodExpression
=
173 expression
.getMethodExpression();
174 @NonNls final String expressionMethodName
=
175 methodExpression
.getReferenceName();
176 if(!methodName
.equals(expressionMethodName
)){
179 final PsiMethod method
= expression
.resolveMethod();
183 final PsiClass aClass
= method
.getContainingClass();
187 final String expressionClassName
= aClass
.getQualifiedName();
188 if(expressionClassName
== null){
191 return className
.equals(expressionClassName
);