eclipse / lsp4mp

Technology lsp4mp
Eclipse Public License 2.0
22 stars 29 forks source link

CodeAction for `@Asynchronous` annotation diagnostic #169

Open AlexXuChen opened 3 years ago

AlexXuChen commented 3 years ago

Given a diagnostic for a Quarkus project containing a method tagged with the @Asynchronous annotation(see #168), a CodeAction should be displayed for fixing the method return type, replacing it with one of:

angelozerr commented 3 years ago

I think we should take care of:

For instance

import org.eclipse.microprofile.faulttolerance.Asynchronous;
...

public class Foo {

   @Asynchronous
   public String hello() {
        return "abcd";
    }
}

report an error on the String result type. The CodeAction java.util.concurrent.CompletionStage should update the method like this:

import java.util.concurrent.CompletionStage;

import org.eclipse.microprofile.faulttolerance.Asynchronous;
...

public class Foo {

   @Asynchronous
    public Future<String> hello() {
        return CompletableFuture.supplyAsync(() -> "abcd");
    }
}

or simplier:

import java.util.concurrent.CompletionStage;

import org.eclipse.microprofile.faulttolerance.Asynchronous;

public class Foo {

   @Asynchronous
    public Future<String> hello() {
        return CompletableFuture.completableFuture("abcd");
    }
}

I don't know if it will be easy to implement this quick fix. @rgrunber what do you think?

rgrunber commented 3 years ago

I think the simpler option is safer because we wouldn't have to additionally check the project source compliance level (source >= 1.8). As for implementing this, it should all be possible with ASTRewrite. I've written code in the past that generated new method references, and the return type in the method declaration is also possible with JDT API.

Some references : https://www.vogella.com/tutorials/EclipseJDT/article.html#adding-a-hello-world-statement-using-the-ast https://www.vogella.com/tutorials/EclipseJDT/article.html#adding-imports-with-astrewrite