[java] create annotation value() method from usage (IDEA-350345)

GitOrigin-RevId: 3596e1f35ce3f603789c69f90b28aacaf448866a
This commit is contained in:
Bas Leijdekkers
2024-04-01 10:40:16 +02:00
committed by intellij-monorepo-bot
parent 1fd9cf454d
commit 9414c123b8
9 changed files with 18 additions and 15 deletions

View File

@@ -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-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.ExpectedTypeInfo;
@@ -8,6 +8,7 @@ import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.codeInsight.daemon.impl.analysis.AnnotationsHighlightUtil;
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo;
import com.intellij.ide.highlighter.JavaFileType;
import com.intellij.java.JavaBundle;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
@@ -15,6 +16,7 @@ import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ObjectUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -36,10 +38,11 @@ public class CreateAnnotationMethodFromUsageFix extends CreateFromUsageBaseFix {
final PsiNameValuePair call = getNameValuePair();
if (call == null || !call.isValid()) return false;
String name = call.getName();
if (name == null) name = PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME;
if (name == null || !PsiNameHelper.getInstance(call.getProject()).isIdentifier(name)) return false;
if (!PsiNameHelper.getInstance(call.getProject()).isIdentifier(name)) return false;
if (getAnnotationValueType(call.getValue()) == null) return false;
setText(QuickFixBundle.message("create.method.from.usage.text", name));
setText(JavaBundle.message("intention.create.annotation.method.from.usage", name));
return true;
}
@@ -62,10 +65,9 @@ public class CreateAnnotationMethodFromUsageFix extends CreateFromUsageBaseFix {
if (nameValuePair == null || classes.isEmpty()) return IntentionPreviewInfo.EMPTY;
final PsiType type = getAnnotationValueType(nameValuePair.getValue());
PsiClass targetClass = classes.get(0);
String methodName = Objects.requireNonNull(nameValuePair.getName());
return new IntentionPreviewInfo.CustomDiff(JavaFileType.INSTANCE, targetClass.getName(),
"",
Objects.requireNonNull(type).getPresentableText() + " " + methodName + "()");
String methodName = ObjectUtils.notNull(nameValuePair.getName(), PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME);
return new IntentionPreviewInfo.CustomDiff(JavaFileType.INSTANCE, targetClass.getName(), "",
Objects.requireNonNull(type).getPresentableText() + " " + methodName + "();");
}
private void invokeImpl(@NotNull PsiClass targetClass) {
@@ -74,7 +76,7 @@ public class CreateAnnotationMethodFromUsageFix extends CreateFromUsageBaseFix {
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(nameValuePair.getProject());
final String methodName = nameValuePair.getName();
final String methodName = ObjectUtils.notNull(nameValuePair.getName(), PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME);
LOG.assertTrue(methodName != null);
PsiMethod method = factory.createMethod(methodName, PsiTypes.voidType());

View File

@@ -1,4 +1,4 @@
// "Create method 'test'" "true-preview"
// "Create @interface method 'test()'" "true-preview"
public class Test {
@Attr(test= "")
public Test() {

View File

@@ -1,4 +1,4 @@
// "Create method 'test'" "true"
// "Create @interface method 'test()'" "true"
public class Test {
@Attr(test= {""})
public Test() {

View File

@@ -1,4 +1,4 @@
// "Create method 'test'" "true-preview"
// "Create @interface method 'test()'" "true-preview"
public class Test {
@Attr(te<caret>st= "")
public Test() {

View File

@@ -1,4 +1,4 @@
// "Create method 'test'" "true"
// "Create @interface method 'test()'" "true"
public class Test {
@Attr(te<caret>st= {""})
public Test() {

View File

@@ -1,4 +1,4 @@
// "Create method 'test'" "false"
// "Create @interface method 'test()'" "false"
public class Test {
@Attr(te<caret>st= {"", 1})
public Test() {

View File

@@ -1,4 +1,4 @@
// "Create method 'test'" "false"
// "Create @interface method 'test()'" "false"
public class Test {
@Attr(te<caret>st= {new String[]{""}})
public Test() {

View File

@@ -826,6 +826,7 @@ intention.convert.compact.constructor.to.canonical=Convert compact constructor t
intention.convert.to.single.return.name=Transform body to single exit-point form
intention.convert.to.single.return.progress.title=Transform body to single exit-point form
intention.convert.to.single.return.command.text=Transform Body To Single Exit-Point Form
intention.create.annotation.method.from.usage=Create @interface method ''{0}()''
intention.create.field.from.parameter.family=Create field for parameter
intention.create.field.from.parameter.text=Create field for parameter ''{0}''
intention.create.test.dialog.choose.super.class=Choose Superclass