diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java index 680ace7..f0ff6e8 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java @@ -13,6 +13,7 @@ * bug 295551 - Add option to automatically promote all warnings to errors * bug 185682 - Increment/decrement operators mark local variables as read * bug 349326 - [1.7] new warning for missing try-with-resources + * bug 359721 - [options] add command line option for new warning token "resource" *******************************************************************************/ package org.eclipse.jdt.core.tests.compiler.regression; @@ -1705,6 +1706,7 @@ " paramAssign assignment to a parameter\n" + " pkgDefaultMethod + attempt to override package-default method\n" + " raw + usage of raw type\n" + + " resource + (potential) leaks of resources of type Closeable\n" + " semicolon unnecessary semicolon, empty statement\n" + " serial + missing serialVersionUID\n" + " specialParamHiding constructor or setter parameter hiding a field\n" + @@ -12241,4 +12243,53 @@ "3 problems (1 error, 2 warnings)", true); } +//-warn option - regression tests +public void test0308_warn_options() { + // check the option introduced in bug 359721 + this.runConformTest( + new String[] { + "X.java", + "import java.io.FileReader;\n" + + "public class X {\n" + + " void foo() throws java.io.IOException {\n" + + " FileReader r = new FileReader(\"f1\");\n" + + " char[] cs = new char[1024];\n" + + " r.read(cs);\n" + + " }\n" + + "}\n" + }, + "\"" + OUTPUT_DIR + File.separator + "X.java\"" + + " -warn:-resource -1.7 -d \"" + OUTPUT_DIR + "\"", + "", + "", + true); +} +//-warn option - regression tests +public void test0309_warn_options() { + // check the option introduced in bug 359721 + this.runConformTest( + new String[] { + "X.java", + "import java.io.FileReader;\n" + + "public class X {\n" + + " void foo(boolean b) throws java.io.IOException {\n" + + " FileReader r = new FileReader(\"f1\");\n" + + " char[] cs = new char[1024];\n" + + " r.read(cs);\n" + + " if (b) r.close();\n" + + " }\n" + + "}\n" + }, + "\"" + OUTPUT_DIR + File.separator + "X.java\"" + + " -warn:+resource -1.7 -d \"" + OUTPUT_DIR + "\"", + "", + "----------\n" + + "1. WARNING in ---OUTPUT_DIR_PLACEHOLDER---/X.java (at line 4)\n" + + " FileReader r = new FileReader(\"f1\");\n" + + " ^\n" + + "Potential resource leak: \'r\' may not be closed\n" + + "----------\n" + + "1 problem (1 warning)", + true); +} } diff --git a/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/Main.java b/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/Main.java index 9523f55..3211979 100644 --- a/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/Main.java +++ b/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/Main.java @@ -10,8 +10,10 @@ * Tom Tromey - Contribution for bug 125961 * Tom Tromey - Contribution for bug 159641 * Benjamin Muskalla - Contribution for bug 239066 - * Stephan Herrmann - Contribution for bug 236385 - * Stephan Herrmann - Contribution for bug 295551 + * Stephan Herrmann - Contributions for + * bug 236385 - [compiler] Warn for potential programming problem if an object is created but not used + * bug 295551 - Add option to automatically promote all warnings to errors + * bug 359721 - [options] add command line option for new warning token "resource" *******************************************************************************/ package org.eclipse.jdt.internal.compiler.batch; @@ -3432,6 +3434,11 @@ } else if (/*token.equals("intfRedundant") ||*/ token.equals("redundantSuperinterface")) { //$NON-NLS-1$ setSeverity(CompilerOptions.OPTION_ReportRedundantSuperinterface, severity, isEnabling); return; + } else if (token.equals("resource")) { //$NON-NLS-1$ + setSeverity(CompilerOptions.OPTION_ReportUnclosedCloseable, severity, isEnabling); + setSeverity(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, severity, isEnabling); + setSeverity(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, severity, isEnabling); + return; } break; case 's' : diff --git a/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/messages.properties b/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/messages.properties index a762469..9f1204b 100644 --- a/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/messages.properties +++ b/org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/messages.properties @@ -8,6 +8,10 @@ # Contributors: # IBM Corporation - initial API and implementation # Benjamin Muskalla - Contribution for bug 239066 +# Stephan Herrmann - Contributions for +# bug 236385 - [compiler] Warn for potential programming problem if an object is created but not used +# bug 295551 - Add option to automatically promote all warnings to errors +# bug 359721 - [options] add command line option for new warning token "resource" ############################################################################### ### JavaBatchCompiler messages. @@ -307,6 +311,7 @@ \ paramAssign assignment to a parameter\n\ \ pkgDefaultMethod + attempt to override package-default method\n\ \ raw + usage of raw type\n\ +\ resource + (potential) leaks of resources of type Closeable\n\ \ semicolon unnecessary semicolon, empty statement\n\ \ serial + missing serialVersionUID\n\ \ specialParamHiding constructor or setter parameter hiding a field\n\