diff options
author | Jesper S Moller | 2013-02-10 23:37:17 +0000 |
---|---|---|
committer | ssankaran | 2013-02-10 23:37:17 +0000 |
commit | 1994e2cb10dd55292b9800d1b02a2532fa0d87dc (patch) | |
tree | 08aee493e4b6d3c36cdb8eb59552c9d79f2531e3 | |
parent | 683ea9e225586815608aba47b7144305522e9131 (diff) | |
download | eclipse.jdt.core-1994e2cb10dd55292b9800d1b02a2532fa0d87dc.tar.gz eclipse.jdt.core-1994e2cb10dd55292b9800d1b02a2532fa0d87dc.tar.xz eclipse.jdt.core-1994e2cb10dd55292b9800d1b02a2532fa0d87dc.zip |
Tests for bug 382721: [1.8][compiler] Effectively final variables needs
special treatment
2 files changed, 163 insertions, 1 deletions
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 8f168a97b7..86110f66a5 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 @@ -28,6 +28,7 @@ * bug 382789 - [compiler][null] warn when syntactically-nonnull expression is compared against null * Jesper S Moller - Contributions for * bug 382701 - [1.8][compiler] Implement semantic analysis of Lambda expressions & Reference expression + * bug 382721 - [1.8][compiler] Effectively final variables needs special treatment *******************************************************************************/ package org.eclipse.jdt.core.tests.compiler.regression; @@ -805,6 +806,8 @@ public void test011_problem_categories() { expectedProblemAttributes.put("ObjectCannotHaveSuperTypes", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); expectedProblemAttributes.put("ObjectHasNoSuperclass", new ProblemAttributes(CategorizedProblem.CAT_TYPE)); expectedProblemAttributes.put("ObjectMustBeClass", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); + expectedProblemAttributes.put("OuterLocalMustBeEffectivelyFinal", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); + expectedProblemAttributes.put("OuterLocalUnderLambdaMustBeEffectivelyFinal", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); expectedProblemAttributes.put("OuterLocalMustBeFinal", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); expectedProblemAttributes.put("OverridingDeprecatedMethod", new ProblemAttributes(CategorizedProblem.CAT_DEPRECATION)); expectedProblemAttributes.put("OverridingMethodWithoutSuperInvocation", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM)); @@ -1560,7 +1563,9 @@ public void test012_compiler_problems_tuning() { expectedProblemAttributes.put("ObjectCannotHaveSuperTypes", SKIP); expectedProblemAttributes.put("ObjectHasNoSuperclass", SKIP); expectedProblemAttributes.put("ObjectMustBeClass", SKIP); + expectedProblemAttributes.put("OuterLocalMustBeEffectivelyFinal", SKIP); expectedProblemAttributes.put("OuterLocalMustBeFinal", SKIP); + expectedProblemAttributes.put("OuterLocalUnderLambdaMustBeEffectivelyFinal", SKIP); expectedProblemAttributes.put("OverridingDeprecatedMethod", new ProblemAttributes(JavaCore.COMPILER_PB_DEPRECATION)); expectedProblemAttributes.put("OverridingMethodWithoutSuperInvocation", new ProblemAttributes(JavaCore.COMPILER_PB_OVERRIDING_METHOD_WITHOUT_SUPER_INVOCATION)); expectedProblemAttributes.put("OverridingNonVisibleMethod", new ProblemAttributes(JavaCore.COMPILER_PB_OVERRIDING_PACKAGE_DEFAULT_METHOD)); 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 15746f0c2c..390762c8d5 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 @@ -13,7 +13,7 @@ * IBM Corporation - initial API and implementation * Jesper S Moller - Contributions for * bug 382701 - [1.8][compiler] Implement semantic analysis of Lambda expressions & Reference expression - + * bug 382721 - [1.8][compiler] Effectively final variables needs special treatment *******************************************************************************/ package org.eclipse.jdt.core.tests.compiler.regression; @@ -1758,6 +1758,163 @@ public void test046() { "Name clash: The method f(List<Integer>) of type B has the same erasure as f(List<String>) of type A but does not override it\n" + "----------\n"); } + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=382721, [1.8][compiler] Effectively final variables needs special treatment +public void test047() { + // This test checks that the simple cases are OK + this.runNegativeTest( + new String[] { + "X.java", + "interface I {\n" + + " void doit();\n" + + "}\n" + + "public class X {\n" + + " public static void main(String[] args) {\n" + + " int var = 2;\n" + + " I x = new I() {\n" + + " public void doit() {\n" + + " System.out.println(args); // OK: args is not re-assignment since declaration/first assignment\n" + + " System.out.println(var); // Error: var is not effectively final\n" + + " }\n" + + " };\n" + + " I x2 = () -> {\n" + + " System.out.println(args); // OK: args is not re-assignment since declaration/first assignment\n" + + " System.out.println(var); // Error: var is not effectively final\n" + + " };\n" + + " var=2;\n" + + " }\n" + + "}" , + }, + "----------\n" + + "1. ERROR in X.java (at line 10)\n" + + " System.out.println(var); // Error: var is not effectively final\n" + + " ^^^\n" + + "The variable var must be must be final or effectively final if used in a lambda expression\n" + + "----------\n" + + "2. ERROR in X.java (at line 15)\n" + + " System.out.println(var); // Error: var is not effectively final\n" + + " ^^^\n" + + "The variable var must be must be final or effectively final if used in an inner class\n" + + "----------\n" + ); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=382721, [1.8][compiler] Effectively final variables needs special treatment +public void test048() { + // This test checks that common semantic checks are indeed run + this.runNegativeTest( + new String[] { + "X.java", + "interface I {\n" + + " void doit();\n" + + "}\n" + + "public class X {\n" + + " public static void main(String[] args) {\n" + + " try {\n" + + " new java.io.File(\"dweep\").getCanonicalPath();\n" + + " } catch (java.io.IOException ioe) {\n" + + " I x2 = () -> {\n" + + " System.out.println(ioe.getMessage()); // OK: args is not re-assignment since declaration/first assignment\n" + + " };\n" + + " };\n" + + " java.util.List<String> list = new java.util.ArrayList<>();\n" + + " for (String s : list) {\n" + + " I x2 = () -> {\n" + + " System.out.println(s); // OK: args is not re-assignment since declaration/first assignment\n" + + " };\n" + + " };\n" + + " for (String s2 : list) {\n" + + " s2 = \"Nice!\";\n" + + " I x2 = () -> {\n" + + " System.out.println(s2); // Error: var is not effectively final\n" + + " };\n" + + " };\n" + + " }\n" + + "\n" + + " void foo() {\n" + + " try {\n" + + " \n" + + " } catch (NullPointerException | ArrayIndexOutOfBoundsException e) {\n" + + " I i = () -> {\n" + + " System.out.println(e);\n" + + " };\n" + + " }\n" + + " }\n" + + "}\n" , + }, + "----------\n" + + "1. ERROR in X.java (at line 22)\n" + + " System.out.println(s2); // Error: var is not effectively final\n" + + " ^^\n" + + "The variable s2 must be must be final or effectively final if used in an inner class\n" + + "----------\n" + ); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=382721, [1.8][compiler] Effectively final variables needs special treatment +public void test049() { + // This test checks that common semantic checks are indeed run + this.runNegativeTest( + new String[] { + "X.java", + "interface I {\n" + + " void doit();\n" + + "}\n" + + "public class X {\n" + + " public static void main(String[] args) {\n" + + " try {\n" + + " new java.io.File(\"dweep\").getCanonicalPath();\n" + + " } catch (java.io.IOException ioe) {\n" + + " I x2 = () -> {\n" + + " System.out.println(ioe.getMessage()); // OK: args is not re-assignment since declaration/first assignment\n" + + " };\n" + + " };\n" + + " java.util.List<String> list = new java.util.ArrayList<>();\n" + + " for (String s : list) {\n" + + " I x2 = () -> {\n" + + " System.out.println(s); // OK: args is not re-assignment since declaration/first assignment\n" + + " };\n" + + " };\n" + + " for (String s2 : list) {\n" + + " s2 = \"Nice!\";\n" + + " I x2 = () -> {\n" + + " System.out.println(s2); // Error: var is not effectively final\n" + + " };\n" + + " };\n" + + " }\n" + + "\n" + + " void foo() {\n" + + " try {\n" + + " \n" + + " } catch (NullPointerException | ArrayIndexOutOfBoundsException e) {\n" + + " I i = () -> {\n" + + " System.out.println(e);\n" + + " };\n" + + " }\n" + + " }\n" + + " void foo2(String[] args) {\n" + + " int var;\n" + + " if (args != null)\n" + + " var = args.length;\n" + + " else\n" + + " var = 2;\n" + + " I x = new I() {\n" + + " public void doit() {\n" + + " System.out.println(var);\n" + + " }\n" + + " };\n" + + " }\n" + + "}\n" , + }, + "----------\n" + + "1. ERROR in X.java (at line 22)\n" + + " System.out.println(s2); // Error: var is not effectively final\n" + + " ^^\n" + + "The variable s2 must be must be final or effectively final if used in an inner class\n" + + "----------\n" + ); +} + public static Class testClass() { return NegativeLambdaExpressionsTest.class; } |