Intent.Modules.Common.Java module provides comprehensive support for generating Java code, including template base classes, code builders, and Maven/Gradle dependency management.
Template Base Class
All Java templates inherit fromJavaTemplateBase<TModel>, which provides:
- Automatic import management: Automatically adds and organizes
importstatements - Package resolution: Resolves fully qualified type names based on package structure
- Dependency management: Register Maven/Gradle dependencies
- Type resolution: Converts metadata types to Java types
Basic Template Structure
Key Properties and Methods
Java Code Builder (JavaFile)
TheJavaFile builder provides a fluent API for constructing Java code:
Building Classes
Interfaces
Annotations
Java annotations are first-class citizens in the builder API:- Class Annotations
- Field Annotations
- Method Annotations
- Parameter Annotations
Advanced Features
Generic Types
Try-Catch Blocks
Javadoc Comments
Maven/Gradle Dependencies
Intent can automatically register dependencies in your Maven or Gradle build files:Package Structure
Java templates follow standard package conventions:Import Resolution
The Java template base automatically handles imports:Best Practices
Follow Java Naming Conventions
Follow Java Naming Conventions
- Packages: lowercase (com.company.domain)
- Classes: PascalCase (UserService)
- Methods: camelCase (findById)
- Constants: UPPER_SNAKE_CASE (MAX_RETRY_COUNT)
Use Builder API for Complex Code
Use Builder API for Complex Code
The
JavaFile builder handles proper indentation, braces, and syntax. Use it instead of string concatenation for anything beyond trivial templates.Leverage Annotations
Leverage Annotations
Java’s annotation system is powerful. Use the builder’s annotation support to generate Spring, JPA, and custom annotations correctly.
Register Dependencies Early
Register Dependencies Early
Call
AddDependency() in your template constructor to ensure Maven/Gradle dependencies are available during the build process.Comparing with C#
- Template Base
- Builders
| Feature | C# (CSharpTemplateBase) | Java (JavaTemplateBase) |
|---|---|---|
| Namespace/Package | Namespace property | Package property |
| Dependencies | AddNugetDependency() | AddDependency(JavaDependency) |
| Imports/Usings | AddUsing() | AddImport() |
| Code Merging | Roslyn-based | Text-based with markers |
See Also
C# Support
Generate C# code with similar capabilities
Kotlin Support
Generate Kotlin code for JVM projects
Template Basics
Learn about Intent templates
Type Resolution
Understanding cross-template type references