From 5a6fdedb9adc0605cb6b154d7c1dd832d3181565 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Thu, 13 Sep 2012 17:14:33 +0400 Subject: [PATCH 1/2] lambda: check parameters if they use free names (IDEA-91502) --- .../codeInsight/daemon/impl/analysis/HighlightUtil.java | 3 ++- .../lambda/highlighting/AlreadyUsedParamName.java | 6 ++++++ .../codeInsight/daemon/lambda/LambdaHighlightingTest.java | 4 ++++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/AlreadyUsedParamName.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java index aacc9176a037..27ca05a0d8f9 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightUtil.java @@ -616,7 +616,8 @@ public class HighlightUtil { boolean isIncorrect = false; if (variable instanceof PsiLocalVariable || variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiCatchSection || - variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiForeachStatement) { + variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiForeachStatement || + variable instanceof PsiParameter && ((PsiParameter)variable).getDeclarationScope() instanceof PsiLambdaExpression) { @SuppressWarnings("unchecked") PsiElement scope = PsiTreeUtil.getParentOfType(variable, PsiFile.class, PsiMethod.class, PsiClassInitializer.class, PsiResourceList.class); VariablesNotProcessor proc = new VariablesNotProcessor(variable, false) { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/AlreadyUsedParamName.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/AlreadyUsedParamName.java new file mode 100644 index 000000000000..b09badcba504 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/highlighting/AlreadyUsedParamName.java @@ -0,0 +1,6 @@ +class Test { + { + Object o = null; + Comparable c = o -> 42; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/LambdaHighlightingTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/LambdaHighlightingTest.java index 2aba17f529eb..1899a5c0d118 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/LambdaHighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/lambda/LambdaHighlightingTest.java @@ -141,6 +141,10 @@ public class LambdaHighlightingTest extends LightDaemonAnalyzerTestCase { doTest(); } + public void testAlreadyUsedParamName() throws Exception { + doTest(); + } + private void doTest() throws Exception { doTest(BASE_PATH + "/" + getTestName(false) + ".java", false, false); } From 87a2c4818df9345c589eaf00c22cdcbe47fd56b6 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Thu, 13 Sep 2012 17:33:31 +0400 Subject: [PATCH 2/2] class literal checks for parameterized type/type parameter (IDEA-89726) --- .../impl/analysis/GenericsHighlightUtil.java | 25 +++++++++++++++---- .../ReferenceTypeParams.java | 13 ++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java index 2f0c71494d01..e96d2c8f93df 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/analysis/GenericsHighlightUtil.java @@ -1005,17 +1005,32 @@ public class GenericsHighlightUtil { public static HighlightInfo checkClassObjectAccessExpression(PsiClassObjectAccessExpression expression) { PsiType type = expression.getOperand().getType(); if (type instanceof PsiClassType) { - PsiClass aClass = ((PsiClassType)type).resolve(); - if (aClass instanceof PsiTypeParameter) { - return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, - expression.getOperand(), - JavaErrorMessages.message("cannot.select.dot.class.from.type.variable")); + return canSelectFrom((PsiClassType)type, expression.getOperand()); + } else if (type instanceof PsiArrayType) { + final PsiType arrayComponentType = type.getDeepComponentType(); + if (arrayComponentType instanceof PsiClassType) { + return canSelectFrom((PsiClassType)arrayComponentType, expression.getOperand()); } } return null; } + @Nullable + private static HighlightInfo canSelectFrom(PsiClassType type, PsiTypeElement operand) { + PsiClass aClass = type.resolve(); + if (aClass instanceof PsiTypeParameter) { + return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, + operand, + JavaErrorMessages.message("cannot.select.dot.class.from.type.variable")); + } else if (type.getParameters().length > 0) { + return HighlightInfo.createHighlightInfo(HighlightInfoType.ERROR, + operand, + "Cannot select from parameterized type"); + } + return null; + } + @Nullable public static HighlightInfo checkOverrideAnnotation(PsiMethod method) { PsiModifierList list = method.getModifierList(); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/ReferenceTypeParams.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/ReferenceTypeParams.java index 9466c30afb77..7148d6e6cd1a 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/ReferenceTypeParams.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/genericsHighlighting/ReferenceTypeParams.java @@ -1,3 +1,4 @@ +import java.lang.Object; import java.util.*; class C { @@ -125,4 +126,16 @@ class IDontCompile { abstract class GenericTest99> { GenericTest99<Enum> local; +} + +class ClassLiteral { + { + Object c1 = T.class; + Object c2 = T[].class; + + Object c3 = List.class; + Object c4 = List[].class; + Object c5 = List[].class; + Object c6 = List.class; + } } \ No newline at end of file