IDEA-107776 (Inspection on System.exit() should skip executable classes)

This commit is contained in:
Bas Leijdekkers
2013-05-25 12:43:43 +02:00
parent 8d923ad33c
commit efbeb6ea99
6 changed files with 67 additions and 20 deletions
@@ -446,6 +446,7 @@ runtime.exec.call.display.name=Call to 'Runtime.exec()'
runtime.exec.call.problem.descriptor=Call to <code>Runtime.#ref()</code> is non-portable #loc
system.exit.call.display.name=Call to 'System.exit()' or related methods
system.exit.call.problem.descriptor=Call to <code>{0}.#ref()</code> is non-portable #loc
system.exit.call.ignore.option=Ignore in main method
system.getenv.call.display.name=Call to 'System.getenv()'
system.getenv.call.problem.descriptor=Call to <code>System.#ref()</code> is non-portable #loc
use.of.awt.peer.class.display.name=Use of AWT peer class
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2007 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2013 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.
@@ -15,15 +15,24 @@
*/
package com.siyeh.ig.portability;
import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiMethodUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
public class SystemExitInspection extends BaseInspection {
@SuppressWarnings("PublicField")
public boolean ignoreInMainMethod = false;
@NotNull
public String getID() {
return "CallToSystemExit";
@@ -37,33 +46,37 @@ public class SystemExitInspection extends BaseInspection {
@NotNull
public String buildErrorString(Object... infos) {
final String className = (String)infos[0];
return InspectionGadgetsBundle.message(
"system.exit.call.problem.descriptor", className);
return InspectionGadgetsBundle.message("system.exit.call.problem.descriptor", className);
}
@Nullable
@Override
public JComponent createOptionsPanel() {
return new SingleCheckboxOptionsPanel(InspectionGadgetsBundle.message("system.exit.call.ignore.option"), this, "ignoreInMainMethod");
}
public BaseInspectionVisitor buildVisitor() {
return new SystemExitVisitor();
}
private static class SystemExitVisitor extends BaseInspectionVisitor {
private class SystemExitVisitor extends BaseInspectionVisitor {
@Override
public void visitMethodCallExpression(
@NotNull PsiMethodCallExpression expression) {
public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
super.visitMethodCallExpression(expression);
final PsiReferenceExpression methodExpression =
expression.getMethodExpression();
final String methodName = methodExpression.getReferenceName();
@NonNls final String exit = "exit";
@NonNls final String halt = "halt";
if (!exit.equals(methodName) && !halt.equals(methodName)) {
final PsiReferenceExpression methodExpression = expression.getMethodExpression();
@NonNls final String methodName = methodExpression.getReferenceName();
if (!"exit".equals(methodName) && !"halt".equals(methodName)) {
return;
}
final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(expression, PsiMethod.class, true, PsiClass.class);
if (ignoreInMainMethod && PsiMethodUtil.isMainMethod(containingMethod)) {
return;
}
final PsiMethod method = expression.resolveMethod();
if (method == null) {
return;
}
final PsiParameterList parameterList = method.getParameterList();
if (parameterList.getParametersCount() != 1) {
return;
@@ -78,15 +91,10 @@ public class SystemExitInspection extends BaseInspection {
return;
}
final String className = aClass.getQualifiedName();
if (className == null) {
if (!"java.lang.System".equals(className) && !"java.lang.Runtime".equals(className)) {
return;
}
if ("java.lang.System".equals(className)) {
registerMethodCallError(expression, "System");
}
else if ("java.lang.Runtime".equals(className)) {
registerMethodCallError(expression, "Runtime");
}
registerMethodCallError(expression, "System");
}
}
}
@@ -5,6 +5,8 @@ Reports the calls to <b>System.exit()</b>,
Calls to these methods make the calling code unportable to most application servers.
<!-- tooltip end -->
<p>
Use the checkbox below to ignore calls in main methods.
<p>
<small>Powered by InspectionGadgets</small>
</body>
</html>
@@ -0,0 +1,14 @@
package com.siyeh.igtest.portability.system_exit;
class SystemExit {
void foo() {
System.exit(0);
}
public static void main(String[] args) {
System.exit(1);
}
}
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<problems>
<problem>
<file>SystemExit.java</file>
<line>8</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Call to 'System.exit()' or related methods</problem_class>
<description>Call to &lt;code&gt;System.exit()&lt;/code&gt; is non-portable #loc</description>
</problem>
</problems>
@@ -0,0 +1,12 @@
package com.siyeh.ig.portability;
import com.siyeh.ig.IGInspectionTestCase;
public class SystemExitInspectionTest extends IGInspectionTestCase {
public void test() throws Exception {
final SystemExitInspection tool = new SystemExitInspection();
tool.ignoreInMainMethod = true;
doTest("com/siyeh/igtest/portability/system_exit", tool);
}
}