/*
 * Copyright 2003-2010 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.
 * 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.security;

import com.intellij.psi.*;
import com.intellij.psi.util.ConstantExpressionUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

public class RuntimeExecWithNonConstantStringInspection
  extends BaseInspection {

  @Override
  @NotNull
  public String getID() {
    return "CallToRuntimeExecWithNonConstantString";
  }

  @Override
  @NotNull
  public String getDisplayName() {
    return InspectionGadgetsBundle.message(
      "runtime.exec.call.display.name");
  }

  @Override
  @NotNull
  protected String buildErrorString(Object... infos) {
    return InspectionGadgetsBundle.message(
      "runtime.exec.with.non.constant.string.problem.descriptor");
  }

  @Override
  public BaseInspectionVisitor buildVisitor() {
    return new RuntimeExecVisitor();
  }

  private static class RuntimeExecVisitor extends BaseInspectionVisitor {

    @Override
    public void visitMethodCallExpression(
      @NotNull PsiMethodCallExpression expression) {
      super.visitMethodCallExpression(expression);
      final PsiReferenceExpression methodExpression =
        expression.getMethodExpression();
      @NonNls final String methodName =
        methodExpression.getReferenceName();
      if (!"exec".equals(methodName)) {
        return;
      }
      final PsiMethod method = expression.resolveMethod();
      if (method == null) {
        return;
      }
      final PsiClass aClass = method.getContainingClass();
      if (aClass == null) {
        return;
      }
      final String className = aClass.getQualifiedName();
      if (!"java.lang.Runtime".equals(className)) {
        return;
      }
      final PsiExpressionList argumentList = expression.getArgumentList();
      final PsiExpression[] args = argumentList.getExpressions();
      if (args.length == 0) {
        return;
      }
      final PsiExpression arg = args[0];
      final PsiType type = arg.getType();
      if (type == null) {
        return;
      }
      final String typeText = type.getCanonicalText();
      if (!CommonClassNames.JAVA_LANG_STRING.equals(typeText)) {
        return;
      }
      final String stringValue =
        (String)ConstantExpressionUtil.computeCastTo(arg, type);
      if (stringValue != null) {
        return;
      }
      registerMethodCallError(expression);
    }
  }
}

@@ -17,17 +17,17 @@
 2003-201
-0
+2
  Dave Gr
@@ -696,34 +696,19 @@
 si.util.
-ConstantExpression
+Psi
 Util;%0Aim
@@ -965,18 +965,16 @@
 spection
-%0A 
  extends
@@ -1192,39 +1192,32 @@
 sBundle.message(
-%0A      
 %22runtime.exec.ca
@@ -1218,12 +1218,32 @@
 xec.
-call
+with.non.constant.string
 .dis
@@ -1381,23 +1381,16 @@
 message(
-%0A      
 %22runtime
@@ -1680,23 +1680,16 @@
 ression(
-%0A      
 @NotNull
@@ -1831,24 +1831,16 @@
 ession =
-%0A       
  express
@@ -1905,24 +1905,16 @@
 odName =
-%0A       
  methodE
@@ -2469,16 +2469,21 @@
 on%5B%5D arg
+ument
 s = argu
@@ -2522,16 +2522,21 @@
  if (arg
+ument
 s.length
@@ -2597,22 +2597,32 @@
 sion arg
+ument
  = arg
+ument
 s%5B0%5D;%0A  
@@ -2649,16 +2649,21 @@
 pe = arg
+ument
 .getType
@@ -2692,102 +2692,31 @@
 null
-) %7B%0A        return;%0A      %7D%0A      final String typeText = type.getCanonicalText();%0A      if (!
+ %7C%7C !type.equalsToText(
 Comm
@@ -2748,24 +2748,8 @@
 RING
-.equals(typeText
 )) %7B
@@ -2783,129 +2783,50 @@
     
-final String stringValue =%0A        (String)ConstantExpressionUtil.computeCastTo(arg, type);%0A      if (stringValue != null
+if (PsiUtil.isConstantExpression(argument)
 ) %7B%0A

Use the right name for "Call to 'Runtime.exec()' with non-constant string" inspection