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.
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:
<!-- 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:
// 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.
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:
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:
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.
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
Opinions expressed by DZone contributors are their own.