diff options
| author | Shankha Banerjee | 2013-05-20 12:09:39 +0000 |
|---|---|---|
| committer | ssankaran | 2013-05-20 12:10:38 +0000 |
| commit | 4d3e86023b52d983561e88d8a94124c1505fe619 (patch) | |
| tree | 182801bbe90c1a5d3bbb301141b631aea7d6cc0a | |
| parent | 654143a38aec753fd98b7be6f2d8a5edee7245f1 (diff) | |
| download | eclipse.jdt.core-4d3e86023b52d983561e88d8a94124c1505fe619.tar.gz eclipse.jdt.core-4d3e86023b52d983561e88d8a94124c1505fe619.tar.xz eclipse.jdt.core-4d3e86023b52d983561e88d8a94124c1505fe619.zip | |
Fixed Bug 406859 - [1.8][compiler] Bad hint that method could be
declared static
3 files changed, 138 insertions, 4 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NegativeLambdaExpressionsTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NegativeLambdaExpressionsTest.java index d00f07a158..0b00f6203e 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NegativeLambdaExpressionsTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NegativeLambdaExpressionsTest.java @@ -6534,6 +6534,134 @@ public void test406773() { compilerOptions /* custom options */ ); } +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=406859, [1.8][compiler] Bad hint that method could be declared static +public void test406859a() { + Map compilerOptions = getCompilerOptions(); + compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); + compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR); + this.runNegativeTest( + new String[] { + "X.java", + "interface I {\n" + + " int foo(int i);\n" + + "}\n" + + "public class X {\n" + + " public static void main(String[] args) {\n" + + " X x = new X();\n" + + " I i = x::foo;\n" + + " i.foo(3);\n" + + " }\n" + + " int foo(int x) {\n" + + " return x;\n" + + " } \n" + + "}\n" + }, + "", + null /* no extra class libraries */, + true /* flush output directory */, + compilerOptions /* custom options */ + ); +} +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=406859, [1.8][compiler] Bad hint that method could be declared static +public void test406859b() { + Map compilerOptions = getCompilerOptions(); + compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); + compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR); + this.runNegativeTest( + new String[] { + "X.java", + "interface I {\n" + + " void doit (Y y);\n" + + "}\n" + + "\n" + + "class Y {\n" + + " void foo() {\n" + + " return;\n" + + " }\n" + + "}\n" + + "\n" + + "public class X {\n" + + " public static void main(String[] args) {\n" + + " I i = Y::foo; \n" + + " Y y = new Y();\n" + + " i.doit(y);\n" + + " }\n" + + "}\n" + }, + "", + null /* no extra class libraries */, + true /* flush output directory */, + compilerOptions /* custom options */ + ); +} +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=406859, [1.8][compiler] Bad hint that method could be declared static +public void test406859c() { + Map compilerOptions = getCompilerOptions(); + compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); + compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.ERROR); + this.runNegativeTest( + new String[] { + "X.java", + "interface I {\n" + + " void doit ();\n" + + "}\n" + + "\n" + + "class Y {\n" + + " void foo() { \n" + + " return;\n" + + " }\n" + + "}\n" + + "\n" + + "public class X {\n" + + " public static void main(String[] args) {\n" + + " I i = new Y()::foo;\n" + + " i.doit();\n" + + " }\n" + + "}\n" + }, + "", + null /* no extra class libraries */, + true /* flush output directory */, + compilerOptions /* custom options */ + ); +} +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=406859, [1.8][compiler] Bad hint that method could be declared static +// A case where we can't help but report the wrong hint due to separate compilation. +public void test406859d() { + Map compilerOptions = getCompilerOptions(); + compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBeStatic, CompilerOptions.ERROR); + compilerOptions.put(CompilerOptions.OPTION_ReportMethodCanBePotentiallyStatic, CompilerOptions.WARNING); + this.runNegativeTest( + new String[] { + "Y.java", + "public class Y {\n" + + " void foo() {\n" + + " return;\n" + + " }\n" + + "}", + "X.java", + "interface I {\n" + + " void doit ();\n" + + "}\n" + + "\n" + + "public class X {\n" + + " public static void main(String[] args) {\n" + + " I i = new Y()::foo;\n" + + " i.doit();\n" + + " }\n" + + "}\n" + }, + "----------\n" + + "1. WARNING in Y.java (at line 2)\n" + + " void foo() {\n" + + " ^^^^^\n" + + "The method foo() from the type Y can potentially be declared as static\n" + + "----------\n", + null /* no extra class libraries */, + true /* flush output directory */, + compilerOptions /* custom options */ + ); +} public static Class testClass() { return NegativeLambdaExpressionsTest.class; } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java index 4a4a314a2c..a9134aa1c8 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java @@ -399,9 +399,15 @@ public class ReferenceExpression extends FunctionalExpression implements Invocat if (this.binding.isAbstract() && this.lhs.isSuper()) scope.problemReporter().cannotDireclyInvokeAbstractMethod(this, this.binding); - if (this.binding.isStatic() && this.binding.declaringClass != this.receiverType) - scope.problemReporter().indirectAccessToStaticMethod(this, this.binding); - + if (this.binding.isStatic()) { + if (this.binding.declaringClass != this.receiverType) + scope.problemReporter().indirectAccessToStaticMethod(this, this.binding); + } else { + AbstractMethodDeclaration srcMethod = this.binding.sourceMethod(); + if (srcMethod != null && srcMethod.isMethod()) + srcMethod.bits &= ~ASTNode.CanBeStatic; + } + if (isMethodUseDeprecated(this.binding, scope, true)) scope.problemReporter().deprecatedMethod(this.binding, this); diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java index a3acc6961d..025b07804d 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodBinding.java @@ -1129,7 +1129,7 @@ public AbstractMethodDeclaration sourceMethod() { return null; } - AbstractMethodDeclaration[] methods = sourceType.scope.referenceContext.methods; + AbstractMethodDeclaration[] methods = sourceType.scope != null ? sourceType.scope.referenceContext.methods : null; if (methods != null) { for (int i = methods.length; --i >= 0;) if (this == methods[i].binding) |
