IDEADEV-31824 (Incorrect "manual array copy" warning)
[fedora-idea.git] / plugins / InspectionGadgets / src / com / siyeh / ig / bugs / ArchaicSystemPropertyAccessInspection.java
blob11ffea94221e552f35dff9aee49dbab1bdecc442
1 /*
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 {
32 @NotNull
33 public String getID(){
34 return "UseOfArchaicSystemPropertyAccessors";
37 @NotNull
38 public String getDisplayName(){
39 return InspectionGadgetsBundle.message(
40 "archaic.system.property.accessors.display.name");
43 @NotNull
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");
53 } else{
54 return InspectionGadgetsBundle.message(
55 "archaic.system.property.accessors.problem.descriptor.Boolean");
59 @NotNull
60 protected InspectionGadgetsFix[] buildFixes(Object... infos){
61 return new InspectionGadgetsFix[]{new ReplaceWithParseMethodFix(),
62 new ReplaceWithStandardPropertyAccessFix()};
65 private static class ReplaceWithParseMethodFix extends InspectionGadgetsFix{
67 @NotNull
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();
81 assert call != null;
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 + ')';
90 } else{
91 parseMethodCall = "Boolean.valueOf(" + argText + ')';
93 replaceExpression(call, parseMethodCall);
98 private static class ReplaceWithStandardPropertyAccessFix
99 extends InspectionGadgetsFix{
101 @NotNull
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();
115 assert call != null;
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("
122 + argText + "))";
123 } else if(isLongGetLong(call)){
124 parseMethodCall = "Long.parseLong(System.getProperty("
125 + argText + "))";
126 } else{
127 if(!PsiUtil.isLanguageLevel5OrHigher(call)){
128 parseMethodCall = "Boolean.valueOf(System.getProperty("
129 + argText + ")).booleanValue()";
130 } else{
131 parseMethodCall = "Boolean.parseBoolean(System.getProperty("
132 + argText + "))";
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)){
177 return false;
179 final PsiMethod method = expression.resolveMethod();
180 if(method == null){
181 return false;
183 final PsiClass aClass = method.getContainingClass();
184 if(aClass == null){
185 return false;
187 final String expressionClassName = aClass.getQualifiedName();
188 if(expressionClassName == null){
189 return false;
191 return className.equals(expressionClassName);