DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Solving Comprehension Debt in AI-Generated Codebases
  • Running TypeSafe AI's Jev Inside Neo4j for Confidence-Aware Sentiment Decisions
  • Embabel vs LangGraph4j: Two Agentic Philosophies for Investment and Risk Analysis in BFSI
  • Beyond Token Intelligence: Why AI Code Review Needs Cognitive Architectures

Trending

  • Two Engines, One Query: DuckDB Inside Neo4j
  • Columnar Formats for AI Telemetry: Parquet vs Vortex With DuckDB
  • How Performance Engineers Find and Fix Hidden System Bottlenecks
  • A Reference Architecture for Constrained AI-Assisted Design

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Building an AI-Powered Code Review Assistant in Java

Building an AI-Powered Code Review Assistant in Java

Wire up LangChain4j to an LLM, submit Java code snippets, and get structured improvement suggestions back, all from plain Java.

By 
Akhil Sabu
·
Nov. 16, 26 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
2 Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Code review is the highest-leverage activity on most engineering teams, yet it is also the most expensive in terms of senior-developer time. What if you could offload a first pass to an LLM that understands Java idioms, common anti-patterns, SOLID principles, and security pitfalls — and get back structured, actionable feedback before a human even opens the PR?

In this post, we will build exactly that: a self-contained Java application that accepts a Java source snippet, sends it to an LLM (OpenAI GPT-4o or a locally running Ollama model — your choice), and returns a structured list of code-quality findings with severity levels and suggested fixes.

What you will build: A CodeReviewAssistant Java service backed by LangChain4j, which analyses Java code and returns structured improvement suggestions — runnable in under 10 minutes on your laptop.

Prerequisites

Tool Version Notes
Java JDK 17+ Records & sealed types used
Maven or Gradle Any recent Maven snippets shown here
LangChain4j 0.36.x Latest stable at time of writing
OpenAI API key or Ollama — We show both


No Spring, no frameworks — this is plain Java, so you can drop it into any stack.

Step 1: Add LangChain4j to Your Project

LangChain4j is the Java-native counterpart of the Python LangChain library. It gives you a clean, type-safe API over multiple LLM providers — OpenAI, Anthropic, Azure, Hugging Face, Ollama, and more — with zero vendor lock-in.

Add the BOM and the provider module you want:

XML
 
<!-- pom.xml -->
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>dev.langchain4j</groupId>
      <artifactId>langchain4j-bom</artifactId>
      <version>0.36.2</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <!-- Core -->
  <dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j</artifactId>
  </dependency>

  <!-- Choose ONE provider below -->

  <!-- Option A: OpenAI (GPT-4o, GPT-3.5-turbo, …) -->
  <dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-open-ai</artifactId>
  </dependency>

  <!-- Option B: Ollama (runs locally, no API key needed) -->
  <dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-ollama</artifactId>
  </dependency>
</dependencies>


Tip — local-first development: Start with Ollama + codellama or mistral so you can iterate without burning API credits. Switch to OpenAI GPT-4o for production quality by changing one line.

Step 2: Model the Review Output as a Java Record

LangChain4j's Structured Output feature can instruct the LLM to respond with JSON that maps directly onto a Java type. We define our domain model first:

Java
 
// Finding.java
public record Finding(
    String severity,      // "CRITICAL" | "HIGH" | "MEDIUM" | "LOW" | "INFO"
    String category,      // e.g. "Null Safety", "Performance", "Security"
    String description,   // human-readable explanation
    String suggestedFix   // concrete code-level recommendation
) {}

// CodeReviewResult.java
import java.util.List;

public record CodeReviewResult(
    String summary,
    List<Finding> findings
) {}


Using Java Records keeps the model immutable and removes boilerplate. LangChain4j will automatically build the JSON schema from these types and include it in the system prompt.

Step 3: Define the AI Service Interface

LangChain4j's AI Services abstraction is the cleanest part of the library. You declare an interface, annotate it with prompts, and the library generates a proxy at runtime — no boilerplate, no manual HTTP calls.

Java
 
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
import dev.langchain4j.service.V;

public interface CodeReviewService {

    @SystemMessage("""
        You are a senior Java engineer performing a thorough code review.
        Analyse the provided Java code for:
          - Correctness and potential bugs
          - Null safety issues
          - Performance anti-patterns
          - Security vulnerabilities (injection, insecure randomness, etc.)
          - SOLID principle violations
          - Readability and naming conventions
        
        Return ONLY a JSON object matching the CodeReviewResult schema.
        Be concise but actionable. Severity: CRITICAL, HIGH, MEDIUM, LOW, INFO.
        """)
    @UserMessage("Review this Java code:\n\n```java\n{{code}}\n```")
    CodeReviewResult review(@V("code") String javaCode);
}


How it works: The @SystemMessage sets the LLM's persona and instructions. The @UserMessage is the per-call prompt. @V("code") is a template variable — LangChain4j substitutes it at call time. Zero boilerplate.

Step 4: Wire It All Together

Now we create the LLM model, register our output type, and build the AI Service proxy. The entire wiring fits in one short class:

Java
 
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.ollama.OllamaChatModel;
import dev.langchain4j.service.AiServices;

public class CodeReviewAssistant {

    public static void main(String[] args) {
        // ── Choose your provider ──────────────────────────────────────────
        // Option A: OpenAI  (set env var OPENAI_API_KEY)
        var model = OpenAiChatModel.builder()
                .apiKey(System.getenv("OPENAI_API_KEY"))  // never hardcode keys!
                .modelName("gpt-4o")
                .responseFormat("json_object")            // enforce JSON mode
                .temperature(0.2)                         // low temp = consistent output
                .build();

        // Option B: Ollama running locally on port 11434
        // var model = OllamaChatModel.builder()
        //         .baseUrl("http://127.0.0.1:11434")
        //         .modelName("codellama")
        //         .build();
        // ─────────────────────────────────────────────────────────────────

        CodeReviewService reviewer = AiServices.builder(CodeReviewService.class)
                .chatLanguageModel(model)
                .build();

        // ── Sample code to review ─────────────────────────────────────────
        String javaCode = """
                public class UserService {
                    private static Map<String, String> cache = new HashMap<>();

                    public String getUser(String id) {
                        if (cache.containsKey(id)) return cache.get(id);
                        String sql = "SELECT * FROM users WHERE id = " + id;
                        // ... execute query
                        return null;
                    }
                }
                """;

        CodeReviewResult result = reviewer.review(javaCode);

        // ── Print results ─────────────────────────────────────────────────
        System.out.println("Summary: " + result.summary());
        System.out.println();
        for (Finding f : result.findings()) {
            System.out.printf("[%s] %s — %s%n", f.severity(), f.category(), f.description());
            System.out.printf("  Fix: %s%n%n", f.suggestedFix());
        }
    }
}


Step 5: Sample Output

Running the above against that deliberately flawed snippet produces output like:

Plain Text
 
Summary: The code has a critical SQL injection vulnerability, a thread-safety
issue with the shared HashMap cache, and returns null without documentation.

[CRITICAL] Security — String concatenation builds a raw SQL query, making the
  method vulnerable to SQL injection attacks.
  Fix: Use PreparedStatement with parameterised queries:
       "SELECT * FROM users WHERE id = ?" with stmt.setString(1, id)

[HIGH] Concurrency — HashMap is not thread-safe. Concurrent reads/writes will
  cause data corruption in multi-threaded environments.
  Fix: Replace with ConcurrentHashMap<>() or add explicit synchronisation.

[MEDIUM] Null Safety — getUser() returns null without documentation. Callers
  have no contract to guard against NPEs.
  Fix: Return Optional<String> and document the empty-case semantics.

[LOW] Performance — cache.containsKey() + cache.get() performs two lookups.
  Fix: Use cache.computeIfAbsent(id, k -> fetchFromDb(k)) instead.


Going Further: Production Hardening

1. Streaming Responses for Large Files

For large classes, swap the return type of your AI Service method to TokenStream and use onNext() / onComplete() to stream findings incrementally rather than waiting for the full response.

2. Memory and Conversation History

LangChain4j's MessageWindowChatMemory lets you maintain multi-turn conversations — useful if you want the developer to ask follow-up questions about a specific finding.

Java
 
CodeReviewService reviewer = AiServices.builder(CodeReviewService.class)
        .chatLanguageModel(model)
        .chatMemory(MessageWindowChatMemory.withMaxMessages(10))
        .build();


3. RAG: Ground Findings in Your Own Style Guide

Use EmbeddingStore + ContentRetriever to inject excerpts from your team's coding standards into every review. The LLM will then apply your own rules rather than falling back on generic advice.

4. CI/CD Integration

Wrap the service in a small CLI tool (using picocli), wire it into your GitHub Actions or Jenkins pipeline, and post findings as PR comments via the GitHub REST API. This enables a first-pass AI review before any human reviewer opens the PR.

Key Takeaways

  • LangChain4j AI Services let you express LLM interactions as plain Java interfaces — no HTTP plumbing, no JSON parsing.
  • Structured Output (records + JSON schema) eliminates fragile string parsing; you get strongly typed results directly.
  • Provider-agnostic design means you swap one builder line to move between OpenAI, Ollama, Anthropic, or Azure with zero business logic changes.
  • Keep API keys in environment variables — never in source code.
  • Start local (Ollama) to iterate fast, then promote to a hosted model for production quality.

Further Reading

  • LangChain4j official docs
  • AI Services tutorial
  • Ollama — CodeLlama model
  • OpenAI Structured Outputs guide
AI Java (programming language) code review

Opinions expressed by DZone contributors are their own.

Related

  • Solving Comprehension Debt in AI-Generated Codebases
  • Running TypeSafe AI's Jev Inside Neo4j for Confidence-Aware Sentiment Decisions
  • Embabel vs LangGraph4j: Two Agentic Philosophies for Investment and Risk Analysis in BFSI
  • Beyond Token Intelligence: Why AI Code Review Needs Cognitive Architectures

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook