lambda: initial is assignable check; effectively final; acceptable context

This commit is contained in:
anna
2012-07-19 21:18:30 +02:00
parent 714b565acd
commit 812b9a72ea
15 changed files with 303 additions and 24 deletions
@@ -0,0 +1,22 @@
interface I {
int m(int i);
}
public class XXX {
static void foo() {
int l = 0;
int j = 0;
j = 2;
final int L = 0;
I i = (int h) -> { int k = 0; return h + <error descr="Variable used in lambda expression should be effectively final">j</error> + l + L; };
}
void bar() {
int l = 0;
int j = 0;
j = 2;
final int L = 0;
I i = (int h) -> { int k = 0; return h + k + <error descr="Variable used in lambda expression should be effectively final">j</error> + l + L; };
}
}
@@ -0,0 +1,18 @@
interface I {
void m(int x);
}
class Test {
void foo(Object x) {}
void bar() {
foo(<error descr="Operator '!' cannot be applied to '<lambda expression>'">!(int x)-> {}</error>);
foo(<error descr="Lambda expression is not expected here">(int x)-> { } instanceof Object</error> );
}
I bazz() {
foo((I)(int x)-> { });
I o = (I)(int x)-> { };
return (int x) -> {};
}
}
@@ -0,0 +1,9 @@
class XXX {
void foo() {
int k = 0;
int n = 2;
Runnable r = ()->{
<error descr="Variable used in lambda expression should be effectively final">k</error> = n;
};
}
}
@@ -0,0 +1,18 @@
interface I {
int m();
}
class XXX {
int var;
static int ourVar;
static void foo() {
I s = () -> <error descr="Non-static field 'var' cannot be referenced from a static context">var</error> + ourVar;
}
void bar() {
I s = ()->var + ourVar;
}
}
@@ -11,8 +11,9 @@ class Foo {
};
void bazz() {
bar((String s) -> {
System.out.println(s);});
bar<error descr="'bar(I)' in 'Foo' cannot be applied to '(<lambda expression>)'">((String s) -> {
System.out.println(s);})</error>;
bar((int i) -> {System.out.println(i);});
}
void bar(I i){}
}
@@ -0,0 +1,7 @@
class XXX {
Runnable bar() {
<error descr="Incompatible types. Found: '<lambda expression>', required: 'java.lang.Runnable'">return (o)->{
System.out.println();
};</error>
}
}
@@ -0,0 +1,89 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.lambda;
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
import com.intellij.psi.PsiClass;
import com.intellij.psi.LambdaUtil;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;
public class FunctionalInterfaceTest extends LightDaemonAnalyzerTestCase {
@NonNls static final String BASE_PATH = "/codeInsight/daemonCodeAnalyzer/lambda/functionalInterface";
private void doTestFunctionalInterface(@Nullable String expectedErrorMessage) throws Exception {
String filePath = BASE_PATH + "/" + getTestName(false) + ".java";
configureByFile(filePath);
final PsiClass psiClass = getJavaFacade().findClass("Foo", GlobalSearchScope.projectScope(getProject()));
assertNotNull("Class Foo not found", psiClass);
final String errorMessage = LambdaUtil.checkInterfaceFunctional(psiClass);
assertEquals(expectedErrorMessage, errorMessage);
}
public void testSimple() throws Exception {
doTestFunctionalInterface(null);
}
public void testNoMethods() throws Exception {
doTestFunctionalInterface("No target method found");
}
public void testMultipleMethods() throws Exception {
doTestFunctionalInterface(null);
}
public void testMultipleMethodsInOne() throws Exception {
doTestFunctionalInterface(null);
}
public void testClone() throws Exception {
doTestFunctionalInterface("Multiple non-overriding abstract methods found");
}
public void testTwoMethodsSameSignature() throws Exception {
doTestFunctionalInterface(null);
}
public void testTwoMethodsSubSignature() throws Exception {
doTestFunctionalInterface(null);
}
public void testTwoMethodsNoSubSignature() throws Exception {
doTestFunctionalInterface("Multiple non-overriding abstract methods found");
}
public void testTwoMethodsNoSubSignature1() throws Exception {
doTestFunctionalInterface("Multiple non-overriding abstract methods found");
}
public void testTwoMethodsSameSubstSignature() throws Exception {
doTestFunctionalInterface(null);
}
public void testMethodWithTypeParam() throws Exception {
doTestFunctionalInterface(null);
}
public void testTwoMethodsSameSignatureTypeParams() throws Exception {
doTestFunctionalInterface(null);
}
public void testAbstractClass() throws Exception {
doTestFunctionalInterface("Target type of a lambda conversion must be an interface");
}
}
@@ -0,0 +1,43 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.lambda;
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
import org.jetbrains.annotations.NonNls;
public class LambdaHighlightingTest extends LightDaemonAnalyzerTestCase {
@NonNls static final String BASE_PATH = "/codeInsight/daemonCodeAnalyzer/lambda/highlighting";
public void testStaticAccess() throws Exception {
doTest();
}
public void testEffectiveFinal() throws Exception {
doTest();
}
public void testReassignUsedVars() throws Exception {
doTest();
}
public void testLambdaContext() throws Exception {
doTest();
}
private void doTest() throws Exception {
doTest(BASE_PATH + "/" + getTestName(false) + ".java", false, false);
}
}
@@ -0,0 +1,43 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInsight.daemon.lambda;
import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase;
import org.jetbrains.annotations.NonNls;
public class LambdaParamsTest extends LightDaemonAnalyzerTestCase {
@NonNls static final String BASE_PATH = "/codeInsight/daemonCodeAnalyzer/lambda/params";
public void testFormalParams() throws Exception {
doTest();
}
public void testInferredParams() throws Exception {
doTest();
}
public void testMethodApplicability() throws Exception {
doTest();
}
public void testIncompatibleTypes() throws Exception {
doTest();
}
private void doTest() throws Exception {
doTest(BASE_PATH + "/" + getTestName(false) + ".java", false, false);
}
}