From 7157fcb5e8b6dbebe33bfdb5c2dac5e9e144b068 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Fri, 22 Dec 2023 14:07:12 +0100 Subject: [PATCH] Java: Introduce field - don't offer initializing in constructor for static fields (IDEA-341676) GitOrigin-RevId: 761d6740ac1302a8bf1615225a6757b678d92f37 --- .../introduceField/IntroduceFieldDialogPanel.java | 12 +++++------- .../introduceField/IntroduceFieldPopupPanel.java | 8 ++++++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/java/java-impl-refactorings/src/com/intellij/refactoring/introduceField/IntroduceFieldDialogPanel.java b/java/java-impl-refactorings/src/com/intellij/refactoring/introduceField/IntroduceFieldDialogPanel.java index 9d287b11feec..fae5eb3793e6 100644 --- a/java/java-impl-refactorings/src/com/intellij/refactoring/introduceField/IntroduceFieldDialogPanel.java +++ b/java/java-impl-refactorings/src/com/intellij/refactoring/introduceField/IntroduceFieldDialogPanel.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. +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.refactoring.introduceField; import com.intellij.codeInsight.TestFrameworks; @@ -143,8 +143,8 @@ public class IntroduceFieldDialogPanel extends IntroduceFieldCentralPanel { myRbInConstructor = new JRadioButton(); myRbInConstructor.setFocusable(false); myRbInConstructor.setText(RefactoringBundle.message("class.constructors.radio")); - - + boolean inOnlyConstructor = myIsCurrentMethodConstructor && myParentClass.getConstructors().length == 1; + myRbInConstructor.setEnabled(!myWillBeDeclaredStatic && !inOnlyConstructor); initializationPanel.add(myRbInCurrentMethod); initializationPanel.add(myRbInFieldDeclaration); @@ -171,9 +171,6 @@ public class IntroduceFieldDialogPanel extends IntroduceFieldCentralPanel { myRbInFieldDeclaration.addItemListener(finalUpdater); if (myRbInSetUp != null) myRbInSetUp.addItemListener(finalUpdater); -// modifiersPanel.add(myCbFinal); -// modifiersPanel.add(myCbStatic); - JPanel groupPanel = new JPanel(new GridLayout(1, 2)); groupPanel.add(initializationPanel); @@ -190,7 +187,8 @@ public class IntroduceFieldDialogPanel extends IntroduceFieldCentralPanel { if (locals) { myRbInFieldDeclaration.setEnabled(false); } - myRbInConstructor.setEnabled(initializedInConstructor); + boolean inOnlyConstructor = myIsCurrentMethodConstructor && myParentClass.getConstructors().length == 1; + myRbInConstructor.setEnabled(initializedInConstructor && !myWillBeDeclaredStatic && !inOnlyConstructor); enableFinal(false); if (myRbInSetUp != null){ if (!initializedInSetup) { diff --git a/java/java-impl-refactorings/src/com/intellij/refactoring/introduceField/IntroduceFieldPopupPanel.java b/java/java-impl-refactorings/src/com/intellij/refactoring/introduceField/IntroduceFieldPopupPanel.java index dea52e875cdb..59419c3e3184 100644 --- a/java/java-impl-refactorings/src/com/intellij/refactoring/introduceField/IntroduceFieldPopupPanel.java +++ b/java/java-impl-refactorings/src/com/intellij/refactoring/introduceField/IntroduceFieldPopupPanel.java @@ -1,4 +1,4 @@ -// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.refactoring.introduceField; import com.intellij.codeInsight.TestFrameworks; @@ -25,7 +25,7 @@ import java.awt.event.ActionListener; import java.awt.event.ItemListener; public class IntroduceFieldPopupPanel extends IntroduceFieldCentralPanel { - private @Nullable JComboBox myInitializerCombo; + private @Nullable JComboBox myInitializerCombo; private DefaultComboBoxModel myInitialisersPlaceModel; public IntroduceFieldPopupPanel(PsiClass parentClass, @@ -50,6 +50,10 @@ public class IntroduceFieldPopupPanel extends IntroduceFieldCentralPanel { if (!myAllowInitInMethod) { myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_CURRENT_METHOD); } + boolean inOnlyConstructor = myIsCurrentMethodConstructor && myParentClass.getConstructors().length == 1; + if (myWillBeDeclaredStatic || inOnlyConstructor) { + myInitialisersPlaceModel.removeElement(BaseExpressionToFieldHandler.InitializationPlace.IN_CONSTRUCTOR); + } } else { myInitialisersPlaceModel.removeAllElements(); }