Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Thomann2010-01-13 15:13:49 +0000
committerOlivier Thomann2010-01-13 15:13:49 +0000
commitc5e020e77f1e406c32c5394bb12c24f79a6e840b (patch)
tree5f8e472eed5022217056448d79a8457273d802b6
parentf348e270de7897a83e9fcfa09ba7634dab98219f (diff)
downloadeclipse.jdt.core-c5e020e77f1e406c32c5394bb12c24f79a6e840b.tar.gz
eclipse.jdt.core-c5e020e77f1e406c32c5394bb12c24f79a6e840b.tar.xz
eclipse.jdt.core-c5e020e77f1e406c32c5394bb12c24f79a6e840b.zip
HEAD - Fix for 236385
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java9
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java5
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/FlowAnalysisTest.java145
-rw-r--r--org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/Main.java4
-rw-r--r--org.eclipse.jdt.core/batch/org/eclipse/jdt/internal/compiler/batch/messages.properties9
-rw-r--r--org.eclipse.jdt.core/buildnotes_jdt-core.html15
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java7
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java8
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java10
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java5
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java14
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties4
-rw-r--r--org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java3
-rw-r--r--org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java15
14 files changed, 235 insertions, 18 deletions
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 bd218a09e6..c431241279 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
@@ -8,6 +8,7 @@
* Contributors:
* IBM Corporation - initial API and implementation
* Benjamin Muskalla - Contribution for bug 239066
+ * Stephan Herrmann - Contribution for bug 236385
*******************************************************************************/
package org.eclipse.jdt.core.tests.compiler.regression;
@@ -1703,9 +1704,10 @@ public void test012b(){
" unchecked + unchecked type operation\n" +
" unnecessaryElse unnecessary else clause\n" +
" unqualifiedField unqualified reference to field\n" +
- " unused macro for unusedArgument, unusedImport, unusedLabel,\n" +
- " unusedLocal, unusedPrivate, unusedThrown,\n" +
- " and unusedTypeArgs\n" +
+ " unused macro for unusedAllocation, unusedArgument,\n" +
+ " unusedImport, unusedLabel, unusedLocal,\n" +
+ " unusedPrivate, unusedThrown, and unusedTypeArgs\n" +
+ " unusedAllocation allocating an object that is not used\n" +
" unusedArgument unread method parameter\n" +
" unusedImport + unused import declaration\n" +
" unusedLabel + unused label\n" +
@@ -1858,6 +1860,7 @@ public void test012b(){
" <option key=\"org.eclipse.jdt.core.compiler.problem.unusedImport\" value=\"warning\"/>\n" +
" <option key=\"org.eclipse.jdt.core.compiler.problem.unusedLabel\" value=\"warning\"/>\n" +
" <option key=\"org.eclipse.jdt.core.compiler.problem.unusedLocal\" value=\"warning\"/>\n" +
+ " <option key=\"org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation\" value=\"ignore\"/>\n" +
" <option key=\"org.eclipse.jdt.core.compiler.problem.unusedParameter\" value=\"ignore\"/>\n" +
" <option key=\"org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference\" value=\"enabled\"/>\n" +
" <option key=\"org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract\" value=\"disabled\"/>\n" +
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java
index 5d913181a8..1b57c22c66 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006, 2009 IBM Corporation and others.
+ * Copyright (c) 2006, 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -8,6 +8,7 @@
* Contributors:
* IBM Corporation - initial API and implementation
* Benjamin Muskalla - Contribution for bug 239066
+ * Stephan Herrmann - Contribution for bug 236385
*******************************************************************************/
package org.eclipse.jdt.core.tests.compiler.regression;
@@ -843,6 +844,7 @@ public void test011_problem_categories() {
expectedProblemAttributes.put("UnusedImport", new ProblemAttributes(CategorizedProblem.CAT_UNNECESSARY_CODE));
expectedProblemAttributes.put("UnusedLabel", new ProblemAttributes(CategorizedProblem.CAT_UNNECESSARY_CODE));
expectedProblemAttributes.put("UnusedMethodDeclaredThrownException", new ProblemAttributes(CategorizedProblem.CAT_UNNECESSARY_CODE));
+ expectedProblemAttributes.put("UnusedObjectAllocation", new ProblemAttributes(CategorizedProblem.CAT_UNNECESSARY_CODE));
expectedProblemAttributes.put("UnusedPrivateConstructor", new ProblemAttributes(CategorizedProblem.CAT_UNNECESSARY_CODE));
expectedProblemAttributes.put("UnusedPrivateField", new ProblemAttributes(CategorizedProblem.CAT_UNNECESSARY_CODE));
expectedProblemAttributes.put("UnusedPrivateMethod", new ProblemAttributes(CategorizedProblem.CAT_UNNECESSARY_CODE));
@@ -1475,6 +1477,7 @@ public void test012_compiler_problems_tuning() {
expectedProblemAttributes.put("UnusedImport", new ProblemAttributes(JavaCore.COMPILER_PB_UNUSED_IMPORT));
expectedProblemAttributes.put("UnusedLabel", new ProblemAttributes(JavaCore.COMPILER_PB_UNUSED_LABEL));
expectedProblemAttributes.put("UnusedMethodDeclaredThrownException", new ProblemAttributes(JavaCore.COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_WHEN_OVERRIDING));
+ expectedProblemAttributes.put("UnusedObjectAllocation", new ProblemAttributes(JavaCore.COMPILER_PB_UNUSED_OBJECT_ALLOCATION));
expectedProblemAttributes.put("UnusedPrivateConstructor", new ProblemAttributes(JavaCore.COMPILER_PB_UNUSED_PRIVATE_MEMBER));
expectedProblemAttributes.put("UnusedPrivateField", new ProblemAttributes(JavaCore.COMPILER_PB_UNUSED_PRIVATE_MEMBER));
expectedProblemAttributes.put("UnusedPrivateMethod", new ProblemAttributes(JavaCore.COMPILER_PB_UNUSED_PRIVATE_MEMBER));
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/FlowAnalysisTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/FlowAnalysisTest.java
index a3d9b1438b..f7c4af4720 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/FlowAnalysisTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/FlowAnalysisTest.java
@@ -7,6 +7,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Stephan Herrmann - Contribution for bug 236385
*******************************************************************************/
package org.eclipse.jdt.core.tests.compiler.regression;
@@ -2016,6 +2017,150 @@ public void test062() {
"Unreachable code\n" +
"----------\n");
}
+
+//https://bugs.eclipse.org/bugs/show_bug.cgi?id=236385
+public void test063() {
+ Map compilerOptions = getCompilerOptions();
+ compilerOptions.put(CompilerOptions.OPTION_ReportUnusedObjectAllocation, CompilerOptions.ERROR);
+ runNegativeTest(
+ new String[] {
+ "X.java",
+ "public class X {\n" +
+ " boolean bar() { return false; } \n" +
+ " public void foo() {" +
+ " if (bar())\n" +
+ " new IllegalArgumentException(\"You must not bar!\");\n" +
+ " }\n" +
+ "}",
+ },
+ "----------\n" +
+ "1. ERROR in X.java (at line 4)\n" +
+ " new IllegalArgumentException(\"You must not bar!\");\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "The allocated object is never used\n" +
+ "----------\n",
+ null /* classLibraries */,
+ true /* shouldFlushOutputDirectory */,
+ compilerOptions);
+}
+//https://bugs.eclipse.org/bugs/show_bug.cgi?id=236385
+// non-throwable type
+public void test064() {
+ Map compilerOptions = getCompilerOptions();
+ compilerOptions.put(CompilerOptions.OPTION_ReportUnusedObjectAllocation, CompilerOptions.ERROR);
+ runNegativeTest(
+ new String[] {
+ "X.java",
+ "public class X {\n" +
+ " boolean bar() { return false; } \n" +
+ " public void foo() {" +
+ " if (bar())\n" +
+ " new String(\"You must not bar!\");\n" +
+ " }\n" +
+ "}",
+ },
+ "----------\n" +
+ "1. ERROR in X.java (at line 4)\n" +
+ " new String(\"You must not bar!\");\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "The allocated object is never used\n" +
+ "----------\n",
+ null /* classLibraries */,
+ true /* shouldFlushOutputDirectory */,
+ compilerOptions);
+}
+
+//https://bugs.eclipse.org/bugs/show_bug.cgi?id=236385
+// warning suppressed
+public void test065() {
+ if (this.complianceLevel < ClassFileConstants.JDK1_5) return;
+ Map compilerOptions = getCompilerOptions();
+ compilerOptions.put(CompilerOptions.OPTION_ReportUnusedObjectAllocation, CompilerOptions.WARNING);
+ runConformTest(
+ new String[] {
+ "X.java",
+ "public class X {\n" +
+ " boolean bar() { return false; }\n" +
+ " @SuppressWarnings(\"unused\")\n" +
+ " public void foo() {" +
+ " if (bar())\n" +
+ " new IllegalArgumentException(\"You must not bar!\");\n" +
+ " }\n" +
+ "}",
+ },
+ "" /* expectedOutputString */,
+ null /* classLib */,
+ true /* shouldFlushOutputDirectory */,
+ null /* vmArguments */,
+ compilerOptions /* customOptions */,
+ null /* clientRequestor */);
+}
+
+//https://bugs.eclipse.org/bugs/show_bug.cgi?id=236385
+// warning ignored (default)
+public void test066() {
+ runConformTest(
+ new String[] {
+ "X.java",
+ "public class X {\n" +
+ " boolean bar() { return false; }\n" +
+ " public void foo() {" +
+ " if (bar())\n" +
+ " new IllegalArgumentException(\"You must not bar!\");\n" +
+ " }\n" +
+ "}",
+ },
+ "" /* expectedOutputString */);
+}
+
+//https://bugs.eclipse.org/bugs/show_bug.cgi?id=236385
+// instance is assigned
+public void test067() {
+ Map compilerOptions = getCompilerOptions();
+ compilerOptions.put(CompilerOptions.OPTION_ReportUnusedObjectAllocation, CompilerOptions.ERROR);
+ runConformTest(
+ new String[] {
+ "X.java",
+ "public class X {\n" +
+ " boolean bar() { return false; }\n" +
+ " Throwable t;\n" +
+ " public void foo() {" +
+ " t = new IllegalArgumentException(\"You must not bar!\");\n" +
+ " }\n" +
+ "}",
+ },
+ "" /* expectedOutputString */,
+ null /* classLib */,
+ true /* shouldFlushOutputDirectory */,
+ null /* vmArguments */,
+ compilerOptions /* customOptions */,
+ null /* clientRequestor */);
+}
+
+//https://bugs.eclipse.org/bugs/show_bug.cgi?id=236385
+// method invoked
+public void test068() {
+ Map compilerOptions = getCompilerOptions();
+ compilerOptions.put(CompilerOptions.OPTION_ReportUnusedObjectAllocation, CompilerOptions.ERROR);
+ runConformTest(
+ new String[] {
+ "X.java",
+ "public class X {\n" +
+ " boolean bar() { return false; }\n" +
+ " public void foo() {" +
+ " if (bar())\n" +
+ " new IllegalArgumentException(\"You must not bar!\").printStackTrace();\n" +
+ " }\n" +
+ "}",
+ },
+ "" /* expectedOutputString */,
+ null /* classLib */,
+ true /* shouldFlushOutputDirectory */,
+ null /* vmArguments */,
+ compilerOptions /* customOptions */,
+ null /* clientRequestor */);
+}
+
public static Class testClass() {
return FlowAnalysisTest.class;
}
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 c4c1acd4f1..6a5dde693f 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,6 +10,7 @@
* Tom Tromey - Contribution for bug 125961
* Tom Tromey - Contribution for bug 159641
* Benjamin Muskalla - Contribution for bug 239066
+ * Stephan Herrmann - Contribution for bug 236385
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.batch;
@@ -3450,6 +3451,9 @@ private void handleErrorOrWarningToken(String token, boolean isEnabling, int sev
} else if (token.equals("unusedImport") || token.equals("unusedImports")/*backward compatible*/) { //$NON-NLS-1$ //$NON-NLS-2$
setSeverity(CompilerOptions.OPTION_ReportUnusedImport, severity, isEnabling);
return;
+ } else if (token.equals("unusedAllocation")) { //$NON-NLS-1$
+ setSeverity(CompilerOptions.OPTION_ReportUnusedObjectAllocation, severity, isEnabling);
+ return;
} else if (token.equals("unusedPrivate")) { //$NON-NLS-1$
setSeverity(CompilerOptions.OPTION_ReportUnusedPrivateMember, severity, isEnabling);
return;
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 3db67db307..e22c09cdf9 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
@@ -15,7 +15,7 @@
#Format: compiler.name = word1 word2 word3
compiler.name = Eclipse Compiler for Java(TM)
#Format: compiler.version = 0.XXX[, other words (don't forget the comma if adding other words)]
-compiler.version = 0.A30, 3.6.0 M5
+compiler.version = 0.A31, 3.6.0 M5
compiler.copyright = Copyright IBM Corp 2000, 2010. All rights reserved.
### progress
@@ -305,9 +305,10 @@ misc.usage.warn = {1} {2}\n\
\ unchecked + unchecked type operation\n\
\ unnecessaryElse unnecessary else clause\n\
\ unqualifiedField unqualified reference to field\n\
-\ unused macro for unusedArgument, unusedImport, unusedLabel,\n\
-\ unusedLocal, unusedPrivate, unusedThrown,\n\
-\ and unusedTypeArgs\n\
+\ unused macro for unusedAllocation, unusedArgument,\n\
+\ unusedImport, unusedLabel, unusedLocal,\n\
+\ unusedPrivate, unusedThrown, and unusedTypeArgs\n\
+\ unusedAllocation allocating an object that is not used\n\
\ unusedArgument unread method parameter\n\
\ unusedImport + unused import declaration\n\
\ unusedLabel + unused label\n\
diff --git a/org.eclipse.jdt.core/buildnotes_jdt-core.html b/org.eclipse.jdt.core/buildnotes_jdt-core.html
index e135b6f208..fe7f6d3737 100644
--- a/org.eclipse.jdt.core/buildnotes_jdt-core.html
+++ b/org.eclipse.jdt.core/buildnotes_jdt-core.html
@@ -40,11 +40,24 @@
</td>
</tr>
</table>
+<a name="v_A31"></a>
+<hr><h1>
+Eclipse Platform Build Notes<br>
+Java development tools core</h1>
+Eclipse SDK 3.6M5 - %date% - 3.6.0 M5
+<br>Project org.eclipse.jdt.core v_A31
+(<a href="http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jdt.core/?only_with_tag=v_A31">cvs</a>).
+<h2>What's new in this drop</h2>
+
+<h3>Problem Reports Fixed</h3>
+<a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=236385">236385</a>
+[compiler] Warn for potential programming problem if an object is created but not used
+
<a name="v_A30"></a>
<hr><h1>
Eclipse Platform Build Notes<br>
Java development tools core</h1>
-Eclipse SDK 3.6M5 - January 12, 2010 - 3.6.0 M5
+Eclipse SDK 3.6M5 - January 12, 2010
<br>Project org.eclipse.jdt.core v_A30
(<a href="http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jdt.core/?only_with_tag=v_A30">cvs</a>).
<h2>What's new in this drop</h2>
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java
index 99ad087cb1..d821fe5622 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2009 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -115,7 +115,8 @@
* RedundantSuperinterface
* Benjamin Muskalla - added the following constants
* MissingSynchronizedModifierInInheritedMethod
- *
+ * Stephan Herrmann - added the following constants
+ * UnusedObjectAllocation
*******************************************************************************/
package org.eclipse.jdt.core.compiler;
@@ -428,6 +429,8 @@ void setSourceStart(int sourceStart);
int UnhandledExceptionInImplicitConstructorCall = TypeRelated + 147;
// expressions
+ /** @since 3.6 */
+ int UnusedObjectAllocation = Internal + 148;
/** @since 3.5 */
int DeadCode = Internal + 149;
int ArrayReferenceRequired = Internal + 150;
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java
index 9c8db839b9..56d0b91696 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2009 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* IBM Corporation - initial API and implementation
+ * Stephan Herrmann - Contribution for bug 236385
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.ast;
@@ -82,8 +83,11 @@ public Expression enclosingInstance() {
}
public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
+ if (!valueRequired)
+ currentScope.problemReporter().unusedObjectAllocation(this);
+
int pc = codeStream.position;
- MethodBinding codegenBinding = this.binding.original();
+ MethodBinding codegenBinding = this.binding.original();
ReferenceBinding allocatedType = codegenBinding.declaringClass;
codeStream.new_(allocatedType);
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java
index cfc268b39b..3b6ae4730a 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java
@@ -8,6 +8,7 @@
* Contributors:
* IBM Corporation - initial API and implementation
* Benjamin Muskalla - Contribution for bug 239066
+ * Stephan Herrmann - Contribution for bug 236385
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.impl;
@@ -127,6 +128,7 @@ public class CompilerOptions {
public static final String OPTION_ReportDeadCode = "org.eclipse.jdt.core.compiler.problem.deadCode"; //$NON-NLS-1$
public static final String OPTION_ReportDeadCodeInTrivialIfStatement = "org.eclipse.jdt.core.compiler.problem.deadCodeInTrivialIfStatement"; //$NON-NLS-1$
public static final String OPTION_ReportTasks = "org.eclipse.jdt.core.compiler.problem.tasks"; //$NON-NLS-1$
+ public static final String OPTION_ReportUnusedObjectAllocation = "org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation"; //$NON-NLS-1$
// Backward compatibility
public static final String OPTION_ReportInvalidAnnotation = "org.eclipse.jdt.core.compiler.problem.invalidAnnotation"; //$NON-NLS-1$
@@ -232,6 +234,7 @@ public class CompilerOptions {
public static final int ShouldImplementHashcode = IrritantSet.GROUP2 | ASTNode.Bit1;
public static final int DeadCode = IrritantSet.GROUP2 | ASTNode.Bit2;
public static final int Tasks = IrritantSet.GROUP2 | ASTNode.Bit3;
+ public static final int UnusedObjectAllocation = IrritantSet.GROUP2 | ASTNode.Bit4;
// Severity level for handlers
/**
@@ -515,6 +518,8 @@ public class CompilerOptions {
return OPTION_ReportMissingHashCodeMethod;
case DeadCode :
return OPTION_ReportDeadCode;
+ case UnusedObjectAllocation:
+ return OPTION_ReportUnusedObjectAllocation;
}
return null;
}
@@ -641,6 +646,7 @@ public class CompilerOptions {
OPTION_ReportUnusedDeclaredThrownException,
OPTION_ReportUnusedImport,
OPTION_ReportUnusedLocal,
+ OPTION_ReportUnusedObjectAllocation,
OPTION_ReportUnusedParameter,
OPTION_ReportUnusedPrivateMember,
OPTION_ReportVarargsArgumentNeedCast,
@@ -701,6 +707,7 @@ public class CompilerOptions {
case UnusedPrivateMember :
case UnusedDeclaredThrownException :
case DeadCode :
+ case UnusedObjectAllocation :
return "unused"; //$NON-NLS-1$
case DiscouragedReference :
case ForbiddenReference :
@@ -891,6 +898,7 @@ public class CompilerOptions {
optionsMap.put(OPTION_ReportDeadCode, getSeverityString(DeadCode));
optionsMap.put(OPTION_ReportDeadCodeInTrivialIfStatement, this.reportDeadCodeInTrivialIfStatement ? ENABLED : DISABLED);
optionsMap.put(OPTION_ReportTasks, getSeverityString(Tasks));
+ optionsMap.put(OPTION_ReportUnusedObjectAllocation, getSeverityString(UnusedObjectAllocation));
return optionsMap;
}
@@ -1287,6 +1295,7 @@ public class CompilerOptions {
if ((optionValue = optionsMap.get(OPTION_ReportMissingHashCodeMethod)) != null) updateSeverity(ShouldImplementHashcode, optionValue);
if ((optionValue = optionsMap.get(OPTION_ReportDeadCode)) != null) updateSeverity(DeadCode, optionValue);
if ((optionValue = optionsMap.get(OPTION_ReportTasks)) != null) updateSeverity(Tasks, optionValue);
+ if ((optionValue = optionsMap.get(OPTION_ReportUnusedObjectAllocation)) != null) updateSeverity(UnusedObjectAllocation, optionValue);
// Javadoc options
if ((optionValue = optionsMap.get(OPTION_DocCommentSupport)) != null) {
@@ -1488,6 +1497,7 @@ public class CompilerOptions {
buf.append("\n\t- dead code: ").append(getSeverityString(DeadCode)); //$NON-NLS-1$
buf.append("\n\t- dead code in trivial if statement: ").append(this.reportDeadCodeInTrivialIfStatement ? ENABLED : DISABLED); //$NON-NLS-1$
buf.append("\n\t- tasks severity: ").append(getSeverityString(Tasks)); //$NON-NLS-1$
+ buf.append("\n\t- unused object allocation: ").append(getSeverityString(UnusedObjectAllocation)); //$NON-NLS-1$
return buf.toString();
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java
index b52acff3a2..2ef43e009f 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2008 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -117,7 +117,8 @@ public class IrritantSet {
.set(CompilerOptions.UnusedImport)
.set(CompilerOptions.UnusedTypeArguments)
.set(CompilerOptions.RedundantSuperinterface)
- .set(CompilerOptions.DeadCode);
+ .set(CompilerOptions.DeadCode)
+ .set(CompilerOptions.UnusedObjectAllocation);
String suppressRawWhenUnchecked = System.getProperty("suppressRawWhenUnchecked"); //$NON-NLS-1$
if (suppressRawWhenUnchecked != null && "true".equalsIgnoreCase(suppressRawWhenUnchecked)) { //$NON-NLS-1$
UNCHECKED.set(CompilerOptions.RawTypeReference);
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java
index 877bdce854..ca992950fa 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2009 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -414,6 +414,9 @@ public static int getIrritant(int problemID) {
case IProblem.Task :
return CompilerOptions.Tasks;
+
+ case IProblem.UnusedObjectAllocation:
+ return CompilerOptions.UnusedObjectAllocation;
}
return 0;
}
@@ -484,6 +487,7 @@ public static int getProblemCategory(int severity, int problemID) {
case CompilerOptions.UnusedWarningToken :
case CompilerOptions.UnusedLabel :
case CompilerOptions.RedundantSuperinterface :
+ case CompilerOptions.UnusedObjectAllocation :
return CategorizedProblem.CAT_UNNECESSARY_CODE;
case CompilerOptions.UsingDeprecatedAPI :
@@ -7124,6 +7128,14 @@ public void unusedLocalVariable(LocalDeclaration localDecl) {
localDecl.sourceStart,
localDecl.sourceEnd);
}
+public void unusedObjectAllocation(AllocationExpression allocationExpression) {
+ this.handle(
+ IProblem.UnusedObjectAllocation,
+ NoArgument,
+ NoArgument,
+ allocationExpression.sourceStart,
+ allocationExpression.sourceEnd);
+}
public void unusedPrivateConstructor(ConstructorDeclaration constructorDecl) {
int severity = computeSeverity(IProblem.UnusedPrivateConstructor);
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties
index a97865e1d2..0546516ffe 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties
@@ -1,5 +1,5 @@
###############################################################################
-# Copyright (c) 2000, 2009 IBM Corporation and others.
+# Copyright (c) 2000, 2010 IBM Corporation and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
@@ -123,6 +123,8 @@
146 = Default constructor cannot handle exception type {0} thrown by implicit super constructor. Must define an explicit constructor
147 = Unhandled exception type {0} thrown by implicit super constructor
+
+148 = The allocated object is never used
149 = Dead code
150 = The type of the expression must be an array type but it resolved to {0}
151 = Must explicitly convert the char[] to a String
diff --git a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java
index b3a2eadc98..d9a90fd7fd 100644
--- a/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java
+++ b/org.eclipse.jdt.core/formatter/org/eclipse/jdt/internal/formatter/DefaultCodeFormatter.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2008 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -387,6 +387,7 @@ public class DefaultCodeFormatter extends CodeFormatter {
optionsMap.put(CompilerOptions.OPTION_ReportDeprecationWhenOverridingDeprecatedMethod, CompilerOptions.DISABLED);
optionsMap.put(CompilerOptions.OPTION_ReportHiddenCatchBlock, CompilerOptions.IGNORE);
optionsMap.put(CompilerOptions.OPTION_ReportUnusedLocal, CompilerOptions.IGNORE);
+ optionsMap.put(CompilerOptions.OPTION_ReportUnusedObjectAllocation, CompilerOptions.IGNORE);
optionsMap.put(CompilerOptions.OPTION_ReportUnusedParameter, CompilerOptions.IGNORE);
optionsMap.put(CompilerOptions.OPTION_ReportUnusedImport, CompilerOptions.IGNORE);
optionsMap.put(CompilerOptions.OPTION_ReportSyntheticAccessEmulation, CompilerOptions.IGNORE);
diff --git a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java b/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java
index 75268d19ec..308de4472b 100644
--- a/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java
+++ b/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java
@@ -81,7 +81,9 @@
* COMPILER_PB_UNUSED_DECLARED_THROWN_EXCEPTION_EXEMPT_EXCEPTION_AND_THROWABLE
* IBM Corporation - added getOptionForConfigurableSeverity(int)
* Benjamin Muskalla - added COMPILER_PB_MISSING_SYNCHRONIZED_ON_INHERITED_METHOD
+ * Stephan Herrmann - added COMPILER_PB_UNUSED_OBJECT_ALLOCATION
*******************************************************************************/
+
package org.eclipse.jdt.core;
import java.util.ArrayList;
@@ -1542,6 +1544,19 @@ public final class JavaCore extends Plugin {
*/
public static final String COMPILER_PB_MISSING_SYNCHRONIZED_ON_INHERITED_METHOD = PLUGIN_ID + ".compiler.problem.missingSynchronizedOnInheritedMethod"; //$NON-NLS-1$
/**
+ * Compiler option ID: Reporting Allocation of an Unused Object.
+ * <p>When enabled, the compiler will issue an error or a warning if an object is allocated but never used,
+ * neither by holding a reference nor by invoking one of the object's methods.
+ * <dl>
+ * <dt>Option id:</dt><dd><code>"org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation"</code></dd>
+ * <dt>Possible values:</dt><dd><code>{ "error", "warning", "ignore" }</code></dd>
+ * <dt>Default:</dt><dd><code>"ignore"</code></dd>
+ * </dl>
+ * @since 3.6
+ * @category CompilerOptionID
+ */
+ public static final String COMPILER_PB_UNUSED_OBJECT_ALLOCATION = PLUGIN_ID + ".compiler.problem.unusedObjectAllocation"; //$NON-NLS-1$
+ /**
* Core option ID: Computing Project Build Order.
* <p>Indicate whether JavaCore should enforce the project build order to be based on
* the classpath prerequisite chain. When requesting to compute, this takes over

Back to the top