/plugins/InspectionGadgets/src/com/siyeh/ig/resources/DriverManagerGetConnectionInspection.java

https://bitbucket.org/nbargnesi/idea · Java · 88 lines · 63 code · 10 blank · 15 comment · 8 complexity · fcebe82de57135b3c5e4ce6d9aeadf7f MD5 · raw file

  1. /*
  2. * Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. package com.siyeh.ig.resources;
  17. import com.intellij.psi.PsiClass;
  18. import com.intellij.psi.PsiMethod;
  19. import com.intellij.psi.PsiMethodCallExpression;
  20. import com.intellij.psi.PsiReferenceExpression;
  21. import com.siyeh.HardcodedMethodConstants;
  22. import com.siyeh.InspectionGadgetsBundle;
  23. import com.siyeh.ig.BaseInspection;
  24. import com.siyeh.ig.BaseInspectionVisitor;
  25. import org.jetbrains.annotations.NotNull;
  26. public class DriverManagerGetConnectionInspection extends BaseInspection {
  27. @NotNull
  28. public String getID() {
  29. return "CallToDriverManagerGetConnection";
  30. }
  31. @NotNull
  32. public String getDisplayName() {
  33. return InspectionGadgetsBundle.message(
  34. "drivermanager.call.display.name");
  35. }
  36. @NotNull
  37. public String buildErrorString(Object... infos) {
  38. return InspectionGadgetsBundle.message(
  39. "drivermanager.call.problem.descriptor");
  40. }
  41. public BaseInspectionVisitor buildVisitor() {
  42. return new DriverManagerGetConnectionVisitor();
  43. }
  44. private static class DriverManagerGetConnectionVisitor
  45. extends BaseInspectionVisitor {
  46. @Override
  47. public void visitMethodCallExpression(
  48. @NotNull PsiMethodCallExpression expression) {
  49. super.visitMethodCallExpression(expression);
  50. if (!isDriverManagerGetConnection(expression)) {
  51. return;
  52. }
  53. registerMethodCallError(expression);
  54. }
  55. private static boolean isDriverManagerGetConnection(
  56. PsiMethodCallExpression expression) {
  57. final PsiReferenceExpression methodExpression =
  58. expression.getMethodExpression();
  59. final String methodName = methodExpression.getReferenceName();
  60. if (!HardcodedMethodConstants.GET_CONNECTION.equals(methodName)) {
  61. return false;
  62. }
  63. final PsiMethod method = expression.resolveMethod();
  64. if (method == null) {
  65. return false;
  66. }
  67. final PsiClass aClass = method.getContainingClass();
  68. if (aClass == null) {
  69. return false;
  70. }
  71. final String className = aClass.getQualifiedName();
  72. if (className == null) {
  73. return false;
  74. }
  75. return "java.sql.DriverManager".equals(className);
  76. }
  77. }
  78. }