From a9a1a18b947feeff06501c9b678c91a9fb0a2ece Mon Sep 17 00:00:00 2001 From: Mikhail Pyltsin Date: Mon, 7 Oct 2024 18:01:24 +0200 Subject: [PATCH] [java-inspections] IDEA-357275 "Convert class to record" quickfix throws IOE when inherit compiled class GitOrigin-RevId: 3fef0a37b006d12b2728b98454071d0fd7d3b24f --- .../classCanBeRecord/ConvertToRecordFix.java | 6 ++-- .../afterInheritCompiledClass.java | 23 ++++++++++++++ .../beforeInheritCompiledClass.java | 31 +++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 java/java-tests/testData/inspection/classCanBeRecord/afterInheritCompiledClass.java create mode 100644 java/java-tests/testData/inspection/classCanBeRecord/beforeInheritCompiledClass.java diff --git a/java/java-impl-inspections/src/com/intellij/codeInspection/classCanBeRecord/ConvertToRecordFix.java b/java/java-impl-inspections/src/com/intellij/codeInspection/classCanBeRecord/ConvertToRecordFix.java index 37ad7609f897..f9b18960f9ca 100644 --- a/java/java-impl-inspections/src/com/intellij/codeInspection/classCanBeRecord/ConvertToRecordFix.java +++ b/java/java-impl-inspections/src/com/intellij/codeInspection/classCanBeRecord/ConvertToRecordFix.java @@ -1,4 +1,4 @@ -// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInspection.classCanBeRecord; import com.intellij.codeInsight.AnnotationTargetUtil; @@ -294,7 +294,9 @@ public class ConvertToRecordFix extends InspectionGadgetsFix { recordStyleNaming = true; break; } - if (mySuggestAccessorsRenaming && fieldName.equals(PropertyUtilBase.getPropertyNameByGetter(psiMethod))) { + if (mySuggestAccessorsRenaming && fieldName.equals(PropertyUtilBase.getPropertyNameByGetter(psiMethod)) && + !ContainerUtil.exists(psiMethod.findDeepestSuperMethods(), + superMethod -> superMethod instanceof PsiCompiledElement || superMethod instanceof SyntheticElement)) { backingField = field; break; } diff --git a/java/java-tests/testData/inspection/classCanBeRecord/afterInheritCompiledClass.java b/java/java-tests/testData/inspection/classCanBeRecord/afterInheritCompiledClass.java new file mode 100644 index 000000000000..dbbe9207c565 --- /dev/null +++ b/java/java-tests/testData/inspection/classCanBeRecord/afterInheritCompiledClass.java @@ -0,0 +1,23 @@ +// "Convert to record class" "true-preview" +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +class Outer { + private record ParameterizedTypeImpl(Type rawType, Type... typeArguments) implements ParameterizedType { + + @Override + public Type[] getActualTypeArguments() { + return typeArguments; + } + + @Override + public Type getRawType() { + return rawType; + } + + @Override + public Type getOwnerType() { + return null; + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/classCanBeRecord/beforeInheritCompiledClass.java b/java/java-tests/testData/inspection/classCanBeRecord/beforeInheritCompiledClass.java new file mode 100644 index 000000000000..138fe0ec2d1f --- /dev/null +++ b/java/java-tests/testData/inspection/classCanBeRecord/beforeInheritCompiledClass.java @@ -0,0 +1,31 @@ +// "Convert to record class" "true-preview" +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +class Outer { + private static class ParameterizedTypeImpl implements ParameterizedType { + private final Type rawType; + + private final Type[] typeArguments; + + public ParameterizedTypeImpl(Type rawType, Type... typeArguments) { + this.rawType = rawType; + this.typeArguments = typeArguments; + } + + @Override + public Type[] getActualTypeArguments() { + return typeArguments; + } + + @Override + public Type getRawType() { + return rawType; + } + + @Override + public Type getOwnerType() { + return null; + } + } +} \ No newline at end of file