diff --git a/java/java-psi-impl/src/com/intellij/psi/PsiDiamondTypeImpl.java b/java/java-psi-impl/src/com/intellij/psi/PsiDiamondTypeImpl.java index d88896bc3ac9..5e8dac37ca07 100644 --- a/java/java-psi-impl/src/com/intellij/psi/PsiDiamondTypeImpl.java +++ b/java/java-psi-impl/src/com/intellij/psi/PsiDiamondTypeImpl.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 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. - */ +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.psi; import com.intellij.lang.java.JavaLanguage; @@ -109,7 +95,15 @@ public class PsiDiamondTypeImpl extends PsiDiamondType { private PsiNewExpression getNewExpression() { PsiElement typeElementWithDiamondTypeArgument = myTypeElement.getParent(); - return PsiTreeUtil.getParentOfType(typeElementWithDiamondTypeArgument, PsiNewExpression.class, true, PsiTypeElement.class); + PsiNewExpression newExpression = + PsiTreeUtil.getParentOfType(typeElementWithDiamondTypeArgument, PsiNewExpression.class, true, PsiTypeElement.class, PsiStatement.class); + if (newExpression != null) { + PsiJavaCodeReferenceElement classReference = newExpression.getClassOrAnonymousClassReference(); + if (classReference != null && classReference.getParameterList() == typeElementWithDiamondTypeArgument) { + return newExpression; + } + } + return null; } @Nullable diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/diamond/NestedClassArgConstructor.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/diamond/NestedClassArgConstructor.java new file mode 100644 index 000000000000..b4bf738416d0 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/lambda/diamond/NestedClassArgConstructor.java @@ -0,0 +1,11 @@ +class R { + static class O { + public O(T t) { + } + } + public static void main(String[] args) { + test(new R<>.O()); + } + + private static void test(R.O o) {} +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/lambda/Diamond8HighlightingTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/lambda/Diamond8HighlightingTest.java index 6d4c4162e33b..08eb7da7cbe0 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/lambda/Diamond8HighlightingTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/lambda/Diamond8HighlightingTest.java @@ -1,4 +1,4 @@ -// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.java.codeInsight.daemon.lambda; import com.intellij.codeInsight.daemon.LightDaemonAnalyzerTestCase; @@ -101,6 +101,9 @@ public class Diamond8HighlightingTest extends LightDaemonAnalyzerTestCase { public void testDiamondsUncheckedWarning() { doTest();} public void testWrongNumberOfArguments() { doTest();} + public void testNestedClassArgConstructor() { + doTest(); + } private void doTest() { doTest(BASE_PATH + "/" + getTestName(false) + ".java", false, false);