/plugins/InspectionGadgets/src/com/siyeh/ig/security/LoadLibraryWithNonConstantStringInspection.java

https://bitbucket.org/nbargnesi/idea · Java · 95 lines · 73 code · 7 blank · 15 comment · 13 complexity · b8e7af400f3886beda0f8ef6c32c9682 MD5 · raw file

  1. /*
  2. * Copyright 2003-2011 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.security;
  17. import com.intellij.psi.*;
  18. import com.intellij.psi.util.ConstantExpressionUtil;
  19. import com.intellij.psi.util.InheritanceUtil;
  20. import com.siyeh.InspectionGadgetsBundle;
  21. import com.siyeh.ig.BaseInspection;
  22. import com.siyeh.ig.BaseInspectionVisitor;
  23. import org.jetbrains.annotations.NonNls;
  24. import org.jetbrains.annotations.NotNull;
  25. public class LoadLibraryWithNonConstantStringInspection
  26. extends BaseInspection {
  27. @Override
  28. @NotNull
  29. public String getDisplayName() {
  30. return InspectionGadgetsBundle.message(
  31. "load.library.with.non.constant.string.display.name");
  32. }
  33. @Override
  34. @NotNull
  35. protected String buildErrorString(Object... infos) {
  36. return InspectionGadgetsBundle.message(
  37. "load.library.with.non.constant.string.problem.descriptor");
  38. }
  39. @Override
  40. public BaseInspectionVisitor buildVisitor() {
  41. return new RuntimeExecVisitor();
  42. }
  43. private static class RuntimeExecVisitor extends BaseInspectionVisitor {
  44. @Override
  45. public void visitMethodCallExpression(
  46. @NotNull PsiMethodCallExpression expression) {
  47. super.visitMethodCallExpression(expression);
  48. final PsiReferenceExpression methodExpression =
  49. expression.getMethodExpression();
  50. @NonNls final String methodName =
  51. methodExpression.getReferenceName();
  52. if (!"loadLibrary".equals(methodName)) {
  53. return;
  54. }
  55. final PsiMethod method = expression.resolveMethod();
  56. if (method == null) {
  57. return;
  58. }
  59. final PsiClass aClass = method.getContainingClass();
  60. if (aClass == null) {
  61. return;
  62. }
  63. if (!InheritanceUtil.isInheritor(aClass, "java.lang.System")) {
  64. return;
  65. }
  66. final PsiExpressionList argumentList = expression.getArgumentList();
  67. final PsiExpression[] args = argumentList.getExpressions();
  68. if (args.length == 0) {
  69. return;
  70. }
  71. final PsiExpression arg = args[0];
  72. final PsiType type = arg.getType();
  73. if (type == null) {
  74. return;
  75. }
  76. final String typeText = type.getCanonicalText();
  77. if (!CommonClassNames.JAVA_LANG_STRING.equals(typeText)) {
  78. return;
  79. }
  80. final String stringValue =
  81. (String)ConstantExpressionUtil.computeCastTo(arg, type);
  82. if (stringValue != null) {
  83. return;
  84. }
  85. registerMethodCallError(expression);
  86. }
  87. }
  88. }