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

This commit is contained in:
anna
2012-07-19 21:16:19 +02:00
parent 714b565acd
commit 812b9a72ea
15 changed files with 303 additions and 24 deletions

View File

@@ -692,6 +692,27 @@ public class HighlightControlFlowUtil {
final HighlightInfo highlightInfo = HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, context, description);
QuickFixAction.registerQuickFixAction(highlightInfo, new VariableAccessFromInnerClassFix(variable, innerClass));
return highlightInfo;
} else {
final PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(context, PsiLambdaExpression.class);
if (lambdaExpression != null) {
if (variable instanceof PsiParameter) {
final PsiElement parent = variable.getParent();
if (parent instanceof PsiParameterList && parent.getParent() == lambdaExpression) {
return null;
}
}
boolean effectivelyFinal = true;
for (PsiReference reference : ReferencesSearch.search(variable)) {
final PsiElement element = reference.getElement();
if (element instanceof PsiExpression && PsiUtil.isOnAssignmentLeftHand((PsiExpression)element)) {
effectivelyFinal = false;
break;
}
}
if (!effectivelyFinal ) {
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, context, "Variable used in lambda expression should be effectively final");
}
}
}
return null;
}

View File

@@ -331,6 +331,9 @@ public class HighlightUtil {
PsiType checkType = typeElement.getType();
PsiType operandType = operand.getType();
if (operandType == null) return null;
if (operandType instanceof PsiLambdaExpressionType) {
return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, expression, "Lambda expression is not expected here");
}
if (TypeConversionUtil.isPrimitiveAndNotNull(operandType)
|| TypeConversionUtil.isPrimitiveAndNotNull(checkType)
|| !TypeConversionUtil.areTypesConvertible(operandType, checkType)) {

View File

@@ -13,9 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.psi.impl.source.tree.java;
package com.intellij.psi;
import com.intellij.psi.*;
import com.intellij.psi.util.*;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
@@ -171,4 +170,23 @@ public class LambdaUtil {
}
return null;
}
public static boolean isAcceptable(PsiLambdaExpression lambdaExpression, final PsiType leftType) {
final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(leftType);
final MethodSignature methodSignature = getFunction(resolveResult.getElement());
if (methodSignature == null) return false;
final PsiParameter[] lambdaParameters = lambdaExpression.getParameterList().getParameters();
final PsiType[] parameterTypes = methodSignature.getParameterTypes();
if (lambdaParameters.length != parameterTypes.length) return false;
for (int lambdaParamIdx = 0, length = lambdaParameters.length; lambdaParamIdx < length; lambdaParamIdx++) {
PsiParameter parameter = lambdaParameters[lambdaParamIdx];
final PsiTypeElement typeElement = parameter.getTypeElement();
if (typeElement != null) {
if (!typeElement.getType().equals(resolveResult.getSubstitutor().substitute(parameterTypes[lambdaParamIdx]))) {
return false;
}
}
}
return true;
}
}

View File

@@ -644,7 +644,9 @@ public class TypeConversionUtil {
// todo[r.sh] implement
if (right instanceof PsiMethodReferenceType && left instanceof PsiClassType) return true;
if (right instanceof PsiLambdaExpressionType && left instanceof PsiClassType) return true;
if (right instanceof PsiLambdaExpressionType && left instanceof PsiClassType) {
return LambdaUtil.isAcceptable(((PsiLambdaExpressionType)right).getExpression(), left);
}
if (left instanceof PsiIntersectionType) {
PsiType[] conjuncts = ((PsiIntersectionType)left).getConjuncts();

View File

@@ -29,7 +29,7 @@ import com.intellij.psi.impl.java.stubs.PsiParameterStub;
import com.intellij.psi.impl.source.tree.ChildRole;
import com.intellij.psi.impl.source.tree.CompositeElement;
import com.intellij.psi.impl.source.tree.JavaSharedImplUtil;
import com.intellij.psi.impl.source.tree.java.LambdaUtil;
import com.intellij.psi.LambdaUtil;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.SearchScope;
import com.intellij.ui.RowIcon;

View File

@@ -22,7 +22,7 @@ import com.intellij.openapi.util.Comparing;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.impl.PsiSuperMethodImplUtil;
import com.intellij.psi.impl.source.tree.java.LambdaUtil;
import com.intellij.psi.LambdaUtil;
import com.intellij.psi.infos.CandidateInfo;
import com.intellij.psi.infos.MethodCandidateInfo;
import com.intellij.psi.scope.PsiConflictResolver;
@@ -100,30 +100,15 @@ public class JavaMethodsConflictResolver implements PsiConflictResolver{
PsiType parameterType = myActualParameterTypes[i];
if (parameterType instanceof PsiLambdaExpressionType) {
final PsiLambdaExpression lambdaExpression = ((PsiLambdaExpressionType)parameterType).getExpression();
final int parametersCount = lambdaExpression.getParameterList().getParametersCount();
for (Iterator<CandidateInfo> iterator = conflicts.iterator(); iterator.hasNext(); ) {
final CandidateInfo conflict = iterator.next();
final PsiMethod method = (PsiMethod)conflict.getElement();
if (method != null) {
final PsiParameter[] methodParameters = method.getParameterList().getParameters();
final PsiParameter param = i < methodParameters.length ? methodParameters[i] : methodParameters[methodParameters.length - 1];
final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(param.getType());
final MethodSignature function = LambdaUtil.getFunction(resolveResult.getElement());
if (function != null && function.getParameterTypes().length == parametersCount) {
boolean correctArgs = true;
final PsiParameter[] lambdaParameters = lambdaExpression.getParameterList().getParameters();
for (int lambdaParamIdx = 0, length = lambdaParameters.length; lambdaParamIdx < length; lambdaParamIdx++) {
PsiParameter parameter = lambdaParameters[lambdaParamIdx];
final PsiTypeElement typeElement = parameter.getTypeElement();
if (typeElement != null) {
if (!typeElement.getType().equals(resolveResult.getSubstitutor().substitute(function.getParameterTypes()[lambdaParamIdx]))) {
correctArgs = false;
}
}
}
if (correctArgs) continue;
if (!LambdaUtil.isAcceptable(lambdaExpression, param.getType())) {
iterator.remove();
}
iterator.remove();
}
}
}

View File

@@ -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; };
}
}

View File

@@ -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) -> {};
}
}

View File

@@ -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;
};
}
}

View File

@@ -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;
}
}

View File

@@ -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){}
}

View File

@@ -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>
}
}

View File

@@ -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");
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}