mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-04 17:20:55 +07:00
[lombok] IDEA-309775 Add replace with lombok intention for synchronized methods
GitOrigin-RevId: 0c56c2af16d57425184a33eaa64fc5ae950d35a4
This commit is contained in:
committed by
intellij-monorepo-bot
parent
09695a8b0c
commit
db68ce26f8
@@ -0,0 +1,54 @@
|
||||
package de.plushnikov.intellij.plugin.intention;
|
||||
|
||||
import com.intellij.modcommand.ActionContext;
|
||||
import com.intellij.modcommand.ModPsiUpdater;
|
||||
import com.intellij.modcommand.Presentation;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import de.plushnikov.intellij.plugin.LombokBundle;
|
||||
import de.plushnikov.intellij.plugin.LombokClassNames;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class ReplaceSynchronizedWithLombokAction extends AbstractLombokIntentionAction {
|
||||
@Override
|
||||
public @NotNull String getFamilyName() {
|
||||
return LombokBundle.message("replace.synchronized.lombok.intention");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @Nullable Presentation getPresentation(@NotNull ActionContext context, @NotNull PsiElement element) {
|
||||
final Presentation presentation = super.getPresentation(context, element);
|
||||
if (presentation == null) return null;
|
||||
|
||||
final PsiModifierList psiModifierList = getElementToReplace(element);
|
||||
if (null == psiModifierList) return null;
|
||||
|
||||
return presentation;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void invoke(@NotNull ActionContext context, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) {
|
||||
final PsiModifierList psiModifierList = getElementToReplace(element);
|
||||
if (null != psiModifierList) {
|
||||
psiModifierList.setModifierProperty(PsiModifier.SYNCHRONIZED, false);
|
||||
|
||||
final PsiAnnotation addedAnnotation = psiModifierList.addAnnotation(LombokClassNames.SYNCHRONIZED);
|
||||
JavaCodeStyleManager.getInstance(context.project()).shortenClassReferences(Objects.requireNonNull(addedAnnotation));
|
||||
}
|
||||
}
|
||||
|
||||
private static @Nullable PsiModifierList getElementToReplace(@NotNull PsiElement element) {
|
||||
final PsiElement parent = PsiTreeUtil.getParentOfType(element, PsiMethod.class);
|
||||
if (parent instanceof PsiMethod psiMethod && !psiMethod.isConstructor()) {
|
||||
final PsiModifierList psiModifierList = psiMethod.getModifierList();
|
||||
if (psiModifierList.hasModifierProperty(PsiModifier.SYNCHRONIZED)) {
|
||||
return psiModifierList;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -214,6 +214,12 @@
|
||||
<bundleName>messages.LombokBundle</bundleName>
|
||||
<categoryKey>intention.category.lombok</categoryKey>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<language>JAVA</language>
|
||||
<className>de.plushnikov.intellij.plugin.intention.ReplaceSynchronizedWithLombokAction</className>
|
||||
<bundleName>messages.LombokBundle</bundleName>
|
||||
<categoryKey>intention.category.lombok</categoryKey>
|
||||
</intentionAction>
|
||||
|
||||
<codeInsight.template.postfixTemplateProvider language="JAVA"
|
||||
implementationClass="de.plushnikov.intellij.plugin.extension.postfix.LombokPostfixTemplateProvider"/>
|
||||
|
||||
@@ -6,6 +6,7 @@ config.warn.dependency.outdated.message=<br>\
|
||||
<a href="https://projectlombok.org/download">Maybe you want to update?</a> <br>
|
||||
config.warn.annotation-processing.disabled.title=Lombok requires enabled annotation processing
|
||||
notification.enable.annotation.processing=Enable annotation processing
|
||||
replace.synchronized.lombok.intention=Replace with Lombok @Synchronized
|
||||
replace.with.annotations.lombok=Replace with annotations (Lombok)
|
||||
replace.0.with.explicit.type.lombok=Replace ''{0}'' with explicit type (Lombok)
|
||||
replace.explicit.type.with.0.lombok=Replace explicit type with ''{0}'' (Lombok)
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package de.plushnikov.intellij.plugin.intention;
|
||||
|
||||
import com.intellij.modcommand.ModCommandAction;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiMethod;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import de.plushnikov.intellij.plugin.LombokClassNames;
|
||||
|
||||
public class ReplaceSynchronizedWithLombokActionTest extends LombokIntentionActionTest {
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return super.getBasePath() + "/synchronized";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModCommandAction getAction() {
|
||||
return new ReplaceSynchronizedWithLombokAction();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean wasInvocationSuccessful() {
|
||||
PsiElement elementAtCaret = myFixture.getFile().findElementAt(myFixture.getCaretOffset());
|
||||
PsiMethod psiMethod = PsiTreeUtil.getParentOfType(elementAtCaret, PsiMethod.class);
|
||||
if (psiMethod == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return psiMethod.hasAnnotation(LombokClassNames.SYNCHRONIZED) && !psiMethod.hasModifierProperty(PsiModifier.SYNCHRONIZED);
|
||||
}
|
||||
|
||||
public void testJavaSynchronizedMethod() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
public void testJavaSynchronizedStaticMethod() {
|
||||
doTest();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
public class JavaSynchronizedMethod {
|
||||
public synchronized void <caret> doSomething() {
|
||||
System.out.println("doSomething");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
public class JavaSynchronizedStaticMethod {
|
||||
public synchronized static void <caret> doSomethingStatic() {
|
||||
System.out.println("doSomethingStatic");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user