summaryrefslogtreecommitdiff
path: root/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/DynamicRegexReplaceableByCompiledPatternInspectionBase.java
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/DynamicRegexReplaceableByCompiledPatternInspectionBase.java')
-rw-r--r--plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/DynamicRegexReplaceableByCompiledPatternInspectionBase.java102
1 files changed, 102 insertions, 0 deletions
diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/DynamicRegexReplaceableByCompiledPatternInspectionBase.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/DynamicRegexReplaceableByCompiledPatternInspectionBase.java
new file mode 100644
index 000000000000..86e5f9c3cfe2
--- /dev/null
+++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/performance/DynamicRegexReplaceableByCompiledPatternInspectionBase.java
@@ -0,0 +1,102 @@
+/*
+ * Copyright 2000-2013 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.siyeh.ig.performance;
+
+import com.intellij.psi.*;
+import com.intellij.psi.util.PsiUtil;
+import com.siyeh.InspectionGadgetsBundle;
+import com.siyeh.ig.BaseInspection;
+import com.siyeh.ig.BaseInspectionVisitor;
+import org.jetbrains.annotations.Nls;
+import org.jetbrains.annotations.NonNls;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Collection;
+import java.util.HashSet;
+
+public class DynamicRegexReplaceableByCompiledPatternInspectionBase extends BaseInspection {
+ @NonNls
+ protected static final Collection<String> regexMethodNames = new HashSet<String>(4);
+ static {
+ regexMethodNames.add("matches");
+ regexMethodNames.add("replaceFirst");
+ regexMethodNames.add("replaceAll");
+ regexMethodNames.add("split");
+ }
+
+ @Override
+ @Nls
+ @NotNull
+ public String getDisplayName() {
+ return InspectionGadgetsBundle.message(
+ "dynamic.regex.replaceable.by.compiled.pattern.display.name");
+ }
+
+ @Override
+ @NotNull
+ protected String buildErrorString(Object... infos) {
+ return InspectionGadgetsBundle.message(
+ "dynamic.regex.replaceable.by.compiled.pattern.problem.descriptor");
+ }
+
+ @Override
+ protected boolean buildQuickFixesOnlyForOnTheFlyErrors() {
+ return true;
+ }
+
+ @Override
+ public BaseInspectionVisitor buildVisitor() {
+ return new DynamicRegexReplaceableByCompiledPatternVisitor();
+ }
+
+ private static class DynamicRegexReplaceableByCompiledPatternVisitor extends BaseInspectionVisitor {
+
+ @Override
+ public void visitMethodCallExpression(PsiMethodCallExpression expression) {
+ super.visitMethodCallExpression(expression);
+ if (!isCallToRegexMethod(expression)) {
+ return;
+ }
+ registerMethodCallError(expression);
+ }
+
+
+ private static boolean isCallToRegexMethod(PsiMethodCallExpression expression) {
+ final PsiReferenceExpression methodExpression = expression.getMethodExpression();
+ final String name = methodExpression.getReferenceName();
+ if (!regexMethodNames.contains(name)) {
+ return false;
+ }
+ final PsiExpressionList argumentList = expression.getArgumentList();
+ final PsiExpression[] arguments = argumentList.getExpressions();
+ for (PsiExpression argument : arguments) {
+ if (!PsiUtil.isConstantExpression(argument)) {
+ return false;
+ }
+ }
+ final PsiMethod method = expression.resolveMethod();
+ if (method == null) {
+ return false;
+ }
+ final PsiClass containingClass = method.getContainingClass();
+ if (containingClass == null) {
+ return false;
+ }
+ final String className = containingClass.getQualifiedName();
+ return CommonClassNames.JAVA_LANG_STRING.equals(className);
+ }
+ }
+}