diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/ExplicitTypeCanBeDiamondInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/ExplicitTypeCanBeDiamondInspection.java index 9f49666a4c3e..208cada7d9a7 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/ExplicitTypeCanBeDiamondInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/ExplicitTypeCanBeDiamondInspection.java @@ -1,4 +1,4 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInspection; import com.intellij.codeInsight.intention.HighPriorityAction; @@ -51,10 +51,8 @@ public final class ExplicitTypeCanBeDiamondInspection extends AbstractBaseJavaLo LOG.assertTrue(classReference != null); final PsiReferenceParameterList parameterList = classReference.getParameterList(); LOG.assertTrue(parameterList != null); - for (PsiTypeElement typeElement : parameterList.getTypeParameterElements()) { - if (typeElement.getAnnotations().length > 0) { - return; - } + if (PsiTreeUtil.findChildOfType(parameterList, PsiAnnotation.class) != null) { + return; } final PsiElement firstChild = parameterList.getFirstChild(); final PsiElement lastChild = parameterList.getLastChild(); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/explicit2diamond/beforeAnnotated.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/explicit2diamond/beforeAnnotated.java index c2997ed61336..40924530748f 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/explicit2diamond/beforeAnnotated.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/explicit2diamond/beforeAnnotated.java @@ -18,7 +18,7 @@ class XYZ { public static void genericConsumer(T item) {} public static void main(String[] args) { - genericConsumer(new Wrapper<@Nullables String>(getString())); + genericConsumer(new Wrapper<@Nullable String>(getString())); // below one works great Wrapper<@Nullable String> wrapper = new Wrapper<>(getString()); } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/explicit2diamond/beforeNestedAnnotated.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/explicit2diamond/beforeNestedAnnotated.java new file mode 100644 index 000000000000..5944679a6c0e --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/explicit2diamond/beforeNestedAnnotated.java @@ -0,0 +1,30 @@ +// "Replace with <>" "false" + +import java.util.List; + +class XYZ { + @Target({ElementType.TYPE_USE, ElementType.METHOD}) + public @interface Nullable {} + + public final static class Wrapper { + private final T value; + + public Wrapper(T value) { + this.value = value; + } + } + + public static List<@Nullable String> getStrings() { + String element = ThreadLocalRandom.current().nextBoolean() ? "hello" : null; + return Collections.singletonList(element); + } + + public static void genericConsumer(T item) {} + + public static void main(String[] args) { + // suggests to replace the explicit type below with <> + genericConsumer(new Wrapperist<@Nullable String>>(getStrings())); + // below one works great + Wrapper> wrapper = new Wrapper<>(getStrings()); + } +} \ No newline at end of file