Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@
import com.intellij.java.impl.ig.psiutils.SerializationUtils;
import com.intellij.java.language.psi.PsiClass;
import com.intellij.java.language.psi.PsiMethod;
import com.intellij.java.language.psi.PsiModifier;
import com.intellij.java.language.psi.PsiParameterList;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.localize.InspectionGadgetsLocalize;
import consulo.annotation.access.RequiredReadAction;
import consulo.annotation.access.RequiredWriteAction;
import consulo.annotation.component.ExtensionImpl;
import consulo.dataContext.DataContext;
import consulo.dataContext.DataManager;
Expand Down Expand Up @@ -77,6 +78,7 @@ public LocalizeValue getName() {
}

@Override
@RequiredWriteAction
protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
PsiElement element = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PsiClass.class, PsiMethod.class);
AsyncResult<DataContext> context = DataManager.getInstance().getDataContextFromFocus();
Expand All @@ -94,18 +96,18 @@ public BaseInspectionVisitor buildVisitor() {
}

private static class PublicConstructorVisitor extends BaseInspectionVisitor {

@Override
public void visitMethod(PsiMethod method) {
@RequiredReadAction
public void visitMethod(@Nonnull PsiMethod method) {
super.visitMethod(method);
if (!method.isConstructor()) {
return;
}
if (!method.hasModifierProperty(PsiModifier.PUBLIC)) {
if (!method.isPublic()) {
return;
}
PsiClass aClass = method.getContainingClass();
if (aClass == null || aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
if (aClass == null || aClass.isAbstract()) {
return;
}
if (SerializationUtils.isExternalizable(aClass)) {
Expand All @@ -118,12 +120,13 @@ public void visitMethod(PsiMethod method) {
}

@Override
public void visitClass(PsiClass aClass) {
@RequiredReadAction
public void visitClass(@Nonnull PsiClass aClass) {
super.visitClass(aClass);
if (aClass.isInterface() || aClass.isEnum()) {
return;
}
if (!aClass.hasModifierProperty(PsiModifier.PUBLIC) || aClass.hasModifierProperty(PsiModifier.ABSTRACT)) {
if (!aClass.isPublic() || aClass.isAbstract()) {
return;
}
PsiMethod[] constructors = aClass.getConstructors();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import consulo.util.collection.ContainerUtil;
import consulo.util.lang.Comparing;
import consulo.xml.psi.xml.XmlElement;
import jakarta.annotation.Nonnull;

import java.util.ArrayList;
import java.util.HashSet;
Expand All @@ -64,9 +65,7 @@ class JavaChangeSignatureUsageSearcher {
public UsageInfo[] findUsages() {
List<UsageInfo> result = new ArrayList<>();
PsiElement element = myChangeInfo.getMethod();
if (element instanceof PsiMethod) {
PsiMethod method = (PsiMethod) element;

if (element instanceof PsiMethod method) {
findSimpleUsages(method, result);

UsageInfo[] usageInfos = result.toArray(new UsageInfo[result.size()]);
Expand Down Expand Up @@ -248,11 +247,8 @@ else if (element instanceof PsiDocTagValue) {
}
else if (element instanceof PsiMethod refMethod && refMethod.isConstructor()) {
if (JavaLanguage.INSTANCE.equals(element.getLanguage())) {
DefaultConstructorImplicitUsageInfo implicitUsageInfo = new DefaultConstructorImplicitUsageInfo(
(PsiMethod) element,
((PsiMethod) element).getContainingClass(),
method
);
DefaultConstructorImplicitUsageInfo implicitUsageInfo =
new DefaultConstructorImplicitUsageInfo(refMethod, refMethod.getContainingClass(), method);
result.add(implicitUsageInfo);
}
}
Expand All @@ -270,17 +266,19 @@ else if (element instanceof PsiClass psiClass) {
result.add(new NoConstructorClassUsageInfo(psiClass));
}
}
else if (ref instanceof PsiCallReference) {
result.add(new CallReferenceUsageInfo((PsiCallReference) ref));
else if (ref instanceof PsiCallReference callRef) {
result.add(new CallReferenceUsageInfo(callRef));
}
else {
result.add(new MoveRenameUsageInfo(element, ref, method));
}
}

//if (method.isConstructor() && parameterCount == 0) {
// RefactoringUtil.visitImplicitConstructorUsages(method.getContainingClass(),
// new DefaultConstructorUsageCollector(result));
// RefactoringUtil.visitImplicitConstructorUsages(
// method.getContainingClass(),
// new DefaultConstructorUsageCollector(result)
// );
//}
}
else if (myChangeInfo.isParameterTypesChanged()) {
Expand Down Expand Up @@ -338,6 +336,7 @@ public RenamedParameterCollidesWithLocalUsageInfo(PsiParameter parameter, PsiEle
myMethod = method;
}

@Nonnull
@Override
public LocalizeValue getDescription() {
return RefactoringLocalize.thereIsAlreadyA0InThe1ItWillConflictWithTheRenamedParameter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import consulo.language.editor.refactoring.localize.RefactoringLocalize;
import consulo.language.editor.refactoring.rename.UnresolvableCollisionUsageInfo;
import consulo.language.editor.refactoring.ui.RefactoringUIUtil;
import consulo.language.editor.refactoring.util.CommonRefactoringUtil;
import consulo.language.psi.PsiElement;
import consulo.localize.LocalizeValue;
import jakarta.annotation.Nonnull;

/**
* @author dsl
Expand All @@ -39,6 +39,7 @@ public NewParameterCollidesWithLocalUsageInfo(
myMethod = method;
}

@Nonnull
@Override
public LocalizeValue getDescription() {
return RefactoringLocalize.thereIsAlreadyA0In1ItWillConflictWithTheNewParameter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
*/
package com.intellij.java.impl.refactoring.convertToInstanceMethod;

import consulo.application.HelpManager;
import consulo.language.editor.refactoring.localize.RefactoringLocalize;
import consulo.logging.Logger;
import com.intellij.java.impl.refactoring.HelpID;
import com.intellij.java.impl.refactoring.move.moveInstanceMethod.MoveInstanceMethodDialogBase;
import com.intellij.java.language.psi.PsiMethod;
import com.intellij.java.language.psi.PsiParameter;
import com.intellij.java.language.psi.PsiVariable;
import com.intellij.java.impl.refactoring.HelpID;
import consulo.language.editor.refactoring.RefactoringBundle;
import com.intellij.java.impl.refactoring.move.moveInstanceMethod.MoveInstanceMethodDialogBase;
import consulo.ui.ex.awt.event.DoubleClickListener;
import consulo.application.HelpManager;
import consulo.language.editor.refactoring.localize.RefactoringLocalize;
import consulo.logging.Logger;
import consulo.ui.annotation.RequiredUIAccess;
import consulo.ui.ex.awt.UIUtil;
import consulo.ui.ex.awt.event.DoubleClickListener;

import javax.swing.*;
import java.awt.*;
Expand All @@ -34,49 +34,62 @@
/**
* @author ven
*/
public class ConvertToInstanceMethodDialog extends MoveInstanceMethodDialogBase {
private static final Logger LOG = Logger.getInstance(ConvertToInstanceMethodDialog.class);
public ConvertToInstanceMethodDialog(PsiMethod method, PsiParameter[] variables) {
super(method, variables, ConvertToInstanceMethodHandler.REFACTORING_NAME);
init();
}
public class ConvertToInstanceMethodDialog extends MoveInstanceMethodDialogBase {
private static final Logger LOG = Logger.getInstance(ConvertToInstanceMethodDialog.class);

public ConvertToInstanceMethodDialog(PsiMethod method, PsiParameter[] variables) {
super(method, variables, ConvertToInstanceMethodHandler.REFACTORING_NAME.get());
init();
}

protected void doAction() {
PsiVariable targetVariable = (PsiVariable)myList.getSelectedValue();
LOG.assertTrue(targetVariable instanceof PsiParameter);
ConvertToInstanceMethodProcessor processor = new ConvertToInstanceMethodProcessor(myMethod.getProject(),
myMethod, (PsiParameter)targetVariable,
myVisibilityPanel.getVisibility());
if (!verifyTargetClass(processor.getTargetClass())) return;
invokeRefactoring(processor);
}
@Override
@RequiredUIAccess
protected void doAction() {
PsiVariable targetVariable = (PsiVariable) myList.getSelectedValue();
LOG.assertTrue(targetVariable instanceof PsiParameter);
ConvertToInstanceMethodProcessor processor = new ConvertToInstanceMethodProcessor(myMethod.getProject(),
myMethod, (PsiParameter) targetVariable,
myVisibilityPanel.getVisibility()
);
if (!verifyTargetClass(processor.getTargetClass())) {
return;
}
invokeRefactoring(processor);
}

protected void doHelpAction() {
HelpManager.getInstance().invokeHelp(HelpID.CONVERT_TO_INSTANCE_METHOD);
}
@Override
@RequiredUIAccess
protected void doHelpAction() {
HelpManager.getInstance().invokeHelp(HelpID.CONVERT_TO_INSTANCE_METHOD);
}

protected JComponent createCenterPanel() {
JPanel panel = new JPanel(new BorderLayout(UIUtil.DEFAULT_HGAP, UIUtil.DEFAULT_VGAP));
JLabel label = new JLabel(RefactoringLocalize.moveinstancemethodSelectAnInstanceParameter().get());
panel.add(label, BorderLayout.NORTH);
panel.add(createListAndVisibilityPanels(), BorderLayout.CENTER);
return panel;
}
@Override
protected JComponent createCenterPanel() {
JPanel panel = new JPanel(new BorderLayout(UIUtil.DEFAULT_HGAP, UIUtil.DEFAULT_VGAP));
JLabel label = new JLabel(RefactoringLocalize.moveinstancemethodSelectAnInstanceParameter().get());
panel.add(label, BorderLayout.NORTH);
panel.add(createListAndVisibilityPanels(), BorderLayout.CENTER);
return panel;
}

@Override
protected JList createTargetVariableChooser() {
final JList variableChooser = super.createTargetVariableChooser();
new DoubleClickListener() {
@Override
protected boolean onDoubleClick(MouseEvent e) {
Point point = e.getPoint();
int index = variableChooser.locationToIndex(point);
if (index == -1) return false;
if (!variableChooser.getCellBounds(index, index).contains(point)) return false;
doRefactorAction();
return true;
}
}.installOn(variableChooser);
return variableChooser;
}
@Override
protected JList createTargetVariableChooser() {
final JList variableChooser = super.createTargetVariableChooser();
new DoubleClickListener() {
@Override
protected boolean onDoubleClick(MouseEvent e) {
Point point = e.getPoint();
int index = variableChooser.locationToIndex(point);
if (index == -1) {
return false;
}
if (!variableChooser.getCellBounds(index, index).contains(point)) {
return false;
}
doRefactorAction();
return true;
}
}.installOn(variableChooser);
return variableChooser;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,19 @@ public static ExtractedData extractLightMethodObject(
if (elementsCopy.length == 0) {
return null;
}
if (elementsCopy[elementsCopy.length - 1] instanceof PsiExpressionStatement) {
PsiExpression expr = ((PsiExpressionStatement) elementsCopy[elementsCopy.length - 1]).getExpression();
if (elementsCopy[elementsCopy.length - 1] instanceof PsiExpressionStatement expressionStmt) {
PsiExpression expr = expressionStmt.getExpression();
if (!(expr instanceof PsiAssignmentExpression)) {
PsiType expressionType = GenericsUtil.getVariableTypeByExpressionType(expr.getType());
if (expressionType instanceof PsiDisjunctionType disjunctionType) {
expressionType = ((PsiDisjunctionType) disjunctionType).getLeastUpperBound();
expressionType = disjunctionType.getLeastUpperBound();
}
if (isValidVariableType(expressionType)) {
String uniqueResultName =
JavaCodeStyleManager.getInstance(project).suggestUniqueVariableName("result", elementsCopy[0], true);
String statementText = expressionType.getCanonicalText() + " " + uniqueResultName + " = " + expr.getText() + ";";
elementsCopy[elementsCopy.length - 1] = elementsCopy[elementsCopy.length - 1]
.replace(elementFactory.createStatementFromText(statementText, elementsCopy[elementsCopy.length - 1]));
elementsCopy[elementsCopy.length - 1] =
expressionStmt.replace(elementFactory.createStatementFromText(statementText, expressionStmt));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,8 @@ private String inferTypeArguments(PsiMethodCallExpression methodCallExpression)

@Nonnull
@Override
protected String getCommandName() {
return REFACTORING_NAME.get();
protected LocalizeValue getCommandName() {
return REFACTORING_NAME;
}

@RequiredReadAction
Expand Down Expand Up @@ -736,7 +736,7 @@ protected boolean isNeedToChangeCallContext() {
@Override
protected void apply(AbstractExtractDialog dialog) {
super.apply(dialog);
myCreateInnerClass = !(dialog instanceof ExtractMethodObjectDialog) || ((ExtractMethodObjectDialog) dialog).createInnerClass();
myCreateInnerClass = !(dialog instanceof ExtractMethodObjectDialog extractDialog && !extractDialog.createInnerClass());
myInnerClassName = myCreateInnerClass ? StringUtil.capitalize(dialog.getChosenMethodName()) : dialog.getChosenMethodName();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public InlineParameterExpressionProcessor(

@Nonnull
@Override
protected String getCommandName() {
return InlineParameterHandler.REFACTORING_NAME.get();
protected LocalizeValue getCommandName() {
return InlineParameterHandler.REFACTORING_NAME;
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import consulo.language.editor.refactoring.rename.NonCodeUsageInfoFactory;
import consulo.language.editor.refactoring.util.TextOccurrencesUtil;
import consulo.language.psi.PsiElement;
import consulo.language.psi.PsiFile;
import consulo.language.psi.PsiReference;
import consulo.language.psi.scope.GlobalSearchScope;
import consulo.language.psi.search.ReferencesSearch;
Expand Down Expand Up @@ -346,7 +345,7 @@ public static PsiClassType getSuperType(PsiClass aClass) {

@Nonnull
@Override
protected String getCommandName() {
return RefactoringLocalize.inlineToAnonymousCommandName(myClass.getQualifiedName()).get();
protected LocalizeValue getCommandName() {
return RefactoringLocalize.inlineToAnonymousCommandName(myClass.getQualifiedName());
}
}
Loading
Loading