From 66b886fa91ec06a2cd074a0b2dc83e76b3fb0f42 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 1 Sep 2011 16:53:40 +0200 Subject: [PATCH] IDEA-73463 (Inspection "UseOfPropertiesAsHashtable" should replace "put" with "setProperty" with ALT+ENTER) --- .../siyeh/InspectionGadgetsBundle.properties | 2 + .../ig/bugs/ReplaceAllDotInspection.java | 6 +- .../UseOfPropertiesAsHashtableInspection.java | 109 ++++++++++++++++-- .../security/SystemPropertiesInspection.java | 6 +- 4 files changed, 106 insertions(+), 17 deletions(-) diff --git a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties index b139036737f9..59831b7c5979 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/src/com/siyeh/InspectionGadgetsBundle.properties @@ -1937,3 +1937,5 @@ unnecessary.return.option=Ignore in then branch branch of 'if' statement with 'e usage.of.obsolete.assert.display.name=Usage of obsolete 'junit.framework.Assert' use.of.obsolete.assert.problem.descriptor=Call to #ref() from 'org.junit.framework.Assert' should be replaced with call to method from 'org.junit.Assert' use.of.obsolete.assert.quickfix=Replace with 'org.junit.Assert' method call +properties.object.as.hashtable.set.quickfix=Replace with call to 'setProperty()' +properties.object.as.hashtable.get.quickfix=Replace with call to 'getProperty()' diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ReplaceAllDotInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ReplaceAllDotInspection.java index 285cb1b7d852..81a40edce13a 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ReplaceAllDotInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/ReplaceAllDotInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2010 Dave Griffith, Bas Leijdekkers + * Copyright 2006-2011 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,10 +42,10 @@ public class ReplaceAllDotInspection extends BaseInspection { @Override public BaseInspectionVisitor buildVisitor() { - return new SystemSetSecurityManagerVisitor(); + return new ReplaceAllDotVisitor(); } - private static class SystemSetSecurityManagerVisitor + private static class ReplaceAllDotVisitor extends BaseInspectionVisitor { @Override public void visitMethodCallExpression( diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/UseOfPropertiesAsHashtableInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/UseOfPropertiesAsHashtableInspection.java index f9db480acbd3..c440d4bd0f86 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/UseOfPropertiesAsHashtableInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/bugs/UseOfPropertiesAsHashtableInspection.java @@ -15,13 +15,18 @@ */ package com.siyeh.ig.bugs; +import com.intellij.codeInspection.ProblemDescriptor; +import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.util.InheritanceUtil; +import com.intellij.util.IncorrectOperationException; import com.siyeh.HardcodedMethodConstants; import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; +import com.siyeh.ig.InspectionGadgetsFix; import com.siyeh.ig.psiutils.TypeUtils; +import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; public class UseOfPropertiesAsHashtableInspection extends BaseInspection { @@ -41,11 +46,97 @@ public class UseOfPropertiesAsHashtableInspection extends BaseInspection { } @Override - public BaseInspectionVisitor buildVisitor(){ - return new SystemSetSecurityManagerVisitor(); + protected InspectionGadgetsFix buildFix(Object... infos) { + final PsiMethodCallExpression methodCallExpression = + (PsiMethodCallExpression) infos[0]; + final String methodName = + methodCallExpression.getMethodExpression().getReferenceName(); + final boolean put = HardcodedMethodConstants.PUT.equals(methodName); + if (!(put || HardcodedMethodConstants.GET.equals(methodName))) { + return null; + } + final PsiExpressionList argumentList = + methodCallExpression.getArgumentList(); + final PsiExpression[] arguments = argumentList.getExpressions(); + for (PsiExpression argument : arguments) { + final PsiType type = argument.getType(); + if (type == null || + !type.equalsToText(CommonClassNames.JAVA_LANG_STRING)) { + return null; + } + } + return new UseOfPropertiesAsHashtableFix(put); } - private static class SystemSetSecurityManagerVisitor + private static class UseOfPropertiesAsHashtableFix + extends InspectionGadgetsFix { + + private final boolean put; + + public UseOfPropertiesAsHashtableFix(boolean put) { + this.put = put; + } + + @NotNull + @Override + public String getName() { + if (put) { + return InspectionGadgetsBundle.message( + "properties.object.as.hashtable.set.quickfix"); + } else { + return InspectionGadgetsBundle.message( + "properties.object.as.hashtable.get.quickfix"); + } + } + + @Override + protected void doFix(Project project, ProblemDescriptor descriptor) + throws IncorrectOperationException { + final PsiElement element = descriptor.getPsiElement(); + final PsiElement parent = element.getParent(); + final PsiElement grandParent = parent.getParent(); + if (!(grandParent instanceof PsiMethodCallExpression)) { + return; + } + final PsiMethodCallExpression methodCallExpression = + (PsiMethodCallExpression) grandParent; + final PsiReferenceExpression methodExpression = + methodCallExpression.getMethodExpression(); + @NonNls final StringBuilder newExpression = new StringBuilder(); + final PsiExpression qualifierExpression = + methodExpression.getQualifierExpression(); + if (qualifierExpression != null) { + newExpression.append(qualifierExpression.getText()); + newExpression.append('.'); + } + if (put) { + newExpression.append("setProperty("); + } else { + newExpression.append("getProperty("); + } + final PsiExpressionList argumentList = + methodCallExpression.getArgumentList(); + final PsiExpression[] arguments = argumentList.getExpressions(); + boolean first = true; + for (PsiExpression argument : arguments) { + if (!first) { + newExpression.append(','); + } else { + first = false; + } + newExpression.append(argument.getText()); + } + newExpression.append(')'); + replaceExpression(methodCallExpression, newExpression.toString()); + } + } + + @Override + public BaseInspectionVisitor buildVisitor(){ + return new UseOfPropertiesAsHashtableVisitor(); + } + + private static class UseOfPropertiesAsHashtableVisitor extends BaseInspectionVisitor{ @Override public void visitMethodCallExpression( @@ -54,7 +145,9 @@ public class UseOfPropertiesAsHashtableInspection extends BaseInspection { final PsiReferenceExpression methodExpression = expression.getMethodExpression(); final String methodName = methodExpression.getReferenceName(); - if(!isHashtableMethod(methodName)){ + if(!(HardcodedMethodConstants.PUT.equals(methodName) || + HardcodedMethodConstants.PUTALL.equals(methodName) || + HardcodedMethodConstants.GET.equals(methodName))){ return; } final PsiMethod method = expression.resolveMethod(); @@ -78,13 +171,7 @@ public class UseOfPropertiesAsHashtableInspection extends BaseInspection { CommonClassNames.JAVA_UTIL_PROPERTIES)){ return; } - registerMethodCallError(expression); - } - - private static boolean isHashtableMethod(String name){ - return HardcodedMethodConstants.PUT.equals(name) || - HardcodedMethodConstants.PUTALL.equals(name) || - HardcodedMethodConstants.GET.equals(name); + registerMethodCallError(expression, expression); } } } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/security/SystemPropertiesInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/security/SystemPropertiesInspection.java index 0a89463e9e74..bbf2a84aca4a 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/security/SystemPropertiesInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/security/SystemPropertiesInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2010 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2011 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,10 +58,10 @@ public class SystemPropertiesInspection extends BaseInspection { @Override public BaseInspectionVisitor buildVisitor() { - return new SystemSetSecurityManagerVisitor(); + return new SystemPropertiesVisitor(); } - private static class SystemSetSecurityManagerVisitor + private static class SystemPropertiesVisitor extends BaseInspectionVisitor { @Override public void visitMethodCallExpression(