diff --git a/java/java-impl/src/META-INF/JavaPlugin.xml b/java/java-impl/src/META-INF/JavaPlugin.xml index 0e4bbe658265..8b0e8fabdc7f 100644 --- a/java/java-impl/src/META-INF/JavaPlugin.xml +++ b/java/java-impl/src/META-INF/JavaPlugin.xml @@ -1203,5 +1203,9 @@ + + com.intellij.codeInsight.intention.impl.ChangeUIDAction + Serialization + \ No newline at end of file diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/ChangeUIDAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/ChangeUIDAction.java new file mode 100644 index 000000000000..8c0f2565e922 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/ChangeUIDAction.java @@ -0,0 +1,55 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.codeInsight.intention.impl; + + +import com.intellij.codeInsight.CodeInsightBundle; +import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction; +import com.intellij.openapi.application.Application; +import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.*; +import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.util.IncorrectOperationException; +import com.siyeh.ig.psiutils.CommentTracker; +import org.jetbrains.annotations.Nls; +import org.jetbrains.annotations.NotNull; + +import java.util.Random; + + +public class ChangeUIDAction extends PsiElementBaseIntentionAction { + @Nls(capitalization = Nls.Capitalization.Sentence) + @NotNull + @Override + public String getFamilyName() { + return CodeInsightBundle.message("change.uid.action.name"); + } + + @NotNull + @Override + public String getText() { + return getFamilyName(); + } + + @Override + public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { + PsiField field = PsiTreeUtil.getParentOfType(element, PsiField.class); + if (field == null) return; + PsiExpression initializer = field.getInitializer(); + PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); + Application application = ApplicationManager.getApplication(); + Random random = application.isUnitTestMode() ? new Random(42) : new Random(); + PsiExpression newInitializer = factory.createExpressionFromText(Long.toString(random.nextLong()) + "L", null); + if (initializer == null) return; + new CommentTracker().replaceAndRestoreComments(initializer, newInitializer); + } + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { + PsiField field = PsiTreeUtil.getParentOfType(element, PsiField.class); + if (field == null) return false; + if (!field.getType().equals(PsiType.LONG)) return false; + return "serialVersionUID".equals(field.getName()); + } +} diff --git a/java/java-impl/src/intentionDescriptions/ChangeUIDAction/after.java.template b/java/java-impl/src/intentionDescriptions/ChangeUIDAction/after.java.template new file mode 100644 index 000000000000..86ba2d402a58 --- /dev/null +++ b/java/java-impl/src/intentionDescriptions/ChangeUIDAction/after.java.template @@ -0,0 +1 @@ +static final long serialVersionUID = -1234L; \ No newline at end of file diff --git a/java/java-impl/src/intentionDescriptions/ChangeUIDAction/before.java.template b/java/java-impl/src/intentionDescriptions/ChangeUIDAction/before.java.template new file mode 100644 index 000000000000..3d00eec41434 --- /dev/null +++ b/java/java-impl/src/intentionDescriptions/ChangeUIDAction/before.java.template @@ -0,0 +1 @@ +static final long serialVersionUID = 42L; \ No newline at end of file diff --git a/java/java-impl/src/intentionDescriptions/ChangeUIDAction/description.html b/java/java-impl/src/intentionDescriptions/ChangeUIDAction/description.html new file mode 100644 index 000000000000..974ff9cb160f --- /dev/null +++ b/java/java-impl/src/intentionDescriptions/ChangeUIDAction/description.html @@ -0,0 +1,5 @@ + + +

Intention to change serialVersionUID initializer

+ + \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/changeUid/afterChange.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/changeUid/afterChange.java new file mode 100644 index 000000000000..11a9148f7557 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/changeUid/afterChange.java @@ -0,0 +1,10 @@ +// "Change serial version UID" "true" + +import java.util.function.BinaryOperator; +import java.util.function.Function; +import java.util.function.UnaryOperator; + +public class Main { + // aasda + static final long serialVersionUID = -5025562857975149833L; +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/changeUid/beforeChange.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/changeUid/beforeChange.java new file mode 100644 index 000000000000..95ad31d6b4fd --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/changeUid/beforeChange.java @@ -0,0 +1,10 @@ +// "Change serial version UID" "true" + +import java.util.function.BinaryOperator; +import java.util.function.Function; +import java.util.function.UnaryOperator; + +public class Main { + static final long serialVersionUID = - // aasda + 42L; +} diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/ChangeUIDActionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/ChangeUIDActionTest.java new file mode 100644 index 000000000000..47cf386f8684 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/intention/ChangeUIDActionTest.java @@ -0,0 +1,19 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package com.intellij.java.codeInsight.intention; + +import com.intellij.codeInsight.daemon.LightIntentionActionTestCase; + +public class ChangeUIDActionTest extends LightIntentionActionTestCase { + + public void test() { doAllTests(); } + + @Override + protected boolean shouldBeAvailableAfterExecution() { + return true; + } + + @Override + protected String getBasePath() { + return "/codeInsight/daemonCodeAnalyzer/quickFix/changeUid"; + } +} diff --git a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties index 4d64d78d7216..fbcc84bda47b 100644 --- a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties +++ b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties @@ -566,4 +566,6 @@ collapse.selection.existing.autogenerated.region=Cannot remove auto-generated fo collapse.selection.overlapping.warning.title=Fold Selection collapse.selection.overlapping.warning.text=Overlapping fold region(s) exist collapse.selection.overlapping.warning.ok=Remove -collapse.selection.overlapping.warning.cancel=Cancel \ No newline at end of file +collapse.selection.overlapping.warning.cancel=Cancel + +change.uid.action.name=Change serial version UID \ No newline at end of file