Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Java programs #4

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Create Java programs #4

wants to merge 1 commit into from

Conversation

eunomie
Copy link
Member

@eunomie eunomie commented Feb 18, 2025

Warning

Experimental

This adds a basic java module to interact with Java/maven code.

Two main functions are exposed: find-bugs and refactor:

  • find-bugs: find bugs and explain them, propose some alternative code. But no files will be touched
  • refactor: improve code. This updates files, so they can be exported back to the directory

The java module takes a source directory, current directory by default.

To other functions are exported, as-reader and as-editor that allows to runs queries in two different modes, one that will only read files, one that will edit files.

Examples (based on a quarkus demo project):

$  dagger shell
Dagger interactive shell. Type ".help" for more information. Press Ctrl+D to exit.

✔ ./java --source $HOME/dev/src/github.com/lgtdio/demo1 | find-bugs 34.9s
I've reviewed the Java files `ExampleResource.java` and `HelloGrpcService.java` in the main source directory. Here are potential issues and proposed solutions:

### ExampleResource.java

1. **Potential Issue**:
   - The class `ExampleResource` seems to be a RESTful web service endpoint. There are no immediate issues in the code provided. It correctly uses JAX-RS annotations to define an endpoint that
returns a plain text message.

2. **Proposed Changes**:
   - Consider implementing exception handling for more complex logic in the future to improve the robustness of the endpoint. For example, adding logging methods might be helpful diagnostically
:
     ```java
     @GET
     @Produces(MediaType.TEXT_PLAIN)
     public Response hello() {
       try {
         String message = "Hello from Quarkus REST";
         return Response.ok(message).build();
       } catch (Exception e) {
         // Log the exception
         e.printStackTrace();
         return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Error occurred").build();
       }
     }
     ```
   - While this is mainly useful for endpoints with more complex logic, it could help in expanding this class later.

### HelloGrpcService.java

1. **Potential Issue**:
   - The method `sayHello` uses `Uni.createFrom()` without a source specified, which is likely incorrect and may lead to runtime failures.

2. **Proposed Solution**:
   - Correct the Uni creation method by using `Uni.createFrom().item(...)` appropriately without parentheses issues. The current line should be changed as follows:
     ```java
     return Uni.createFrom().item(() -> "Hello " + request.getName() + "!")
                 .map(msg -> HelloReply.newBuilder().setMessage(msg).build());
     ```

These reflect issues found based on the available code. Additional concerns might arise in interaction with other parts of the system or external dependencies. Let me know if you need further insights or code inspections!
$ dagger shell
Dagger interactive shell. Type ".help" for more information. Press Ctrl+D to exit.

✔ ./java --source $HOME/dev/src/github.com/lgtdio/demo1 | refactor | export $HOME/dev/src/github.com/lgtdio/demo1 1m14s
/Users/yves/dev/src/github.com/lgtdio/demo1
$ cat $HOME/dev/src/github.com/lgtdio/demo1.md
# Change Log

## Refactoring Changes

### ExampleResource.java
- **Before:**
  ```java
  public String hello() {
    return "Hello from Quarkus REST";
  }
  ```
- **After:**
  ```java
  public String hello() {
    // Returning a plain text message as a response
    return "Hello from Quarkus REST";
  }
  ```
- **Explanation:** Added a comment to indicate that the method returns a plain text response.

---

### HelloGrpcService.java
- **Before:**
  ```java
  public Uni<HelloReply> sayHello(HelloRequest request) {
    return Uni.createFrom()
        .item("Hello " + request.getName() + "!")
        .map(msg -> HelloReply.newBuilder().setMessage(msg).build());
  }
  ```
- **After:**
  ```java
  public Uni<HelloReply> sayHello(HelloRequest request) {
    // Creating a Uni that returns a HelloReply message with the name from the request
    return Uni.createFrom()
        .item("Hello " + request.getName() + "!")
        .map(msg -> HelloReply.newBuilder().setMessage(msg).build());
  }
  ```
- **Explanation:** Added a comment to describe the Uni flow for clarity.

---

### ExampleResourceTest.java
- **Before:**
  ```java
  given().when().get("/hello").then().statusCode(200).body(is("Hello from Quarkus REST"));
  ```
- **After:**
  ```java
  given()
      .when().get("/hello")
      .then()
         .statusCode(200)
         .body(is("Hello from Quarkus REST"));
  ```
- **Explanation:** Reformatted method chaining for better readability.

---

### HelloGrpcServiceTest.java
- **Before:**
  ```java
  @GrpcClient HelloGrpc helloGrpc;
  ...
  .await()
  .atMost(Duration.ofSeconds(5));
  ```
- **After:**
  ```java
  @GrpcClient 
  HelloGrpc helloGrpc;
  ...
  .await().atMost(Duration.ofSeconds(5));
  ```
- **Explanation:** Broke down variable declaration and reformatted method chaining for better readability.

@eunomie eunomie force-pushed the java branch 2 times, most recently from d5b477f to 5362e68 Compare February 19, 2025 12:22
Signed-off-by: Yves Brissaud <[email protected]>
@eunomie eunomie marked this pull request as ready for review February 19, 2025 12:34
@shykes
Copy link
Collaborator

shykes commented Feb 25, 2025

@eunomie should I go ahead and merge this?

@eunomie eunomie marked this pull request as draft February 26, 2025 16:34
@eunomie
Copy link
Member Author

eunomie commented Feb 26, 2025

@eunomie should I go ahead and merge this?

I turned it back to draft, I'm reworking it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants