Show HN:JavaFactory——IntelliJ插件,用于生成Java代码
Show HN: JavaFactory – IntelliJ plugin to generate Java code

原始链接: https://github.com/JavaFactoryPluginDev/javafactory-plugin

JavaFactory 是一款利用大型语言模型 (LLM) 生成可预测且稳定的 Java 代码的工具,解决了传统 AI 代码生成器不一致的问题。它通过模式定义和基于注解的引用收集来运作。 模式使用自然语言定义工作单元(例如,测试生成),概述目标、规则、输出格式和示例。此模式包括指定系统提示和用户提示。 类使用 `@JavaFactoryData` 进行显式作用域限定,用于递归收集与数据相关的类(如领域模型),并使用 `@JavaFactoryApi` 用于单层收集 API 接口。这允许在代码生成过程中精确控制类的包含。 JavaFactory 适用于重复性、结构化的环境,例如分层架构,可自动执行诸如生成 DAO、存储库、实现、测试和夹具等任务。JavaFactory 并不会依赖 AI 完成整个过程,而是允许开发者手动设置模式参数,并由工具根据设定的参数生成大部分代码。

Hacker News 上的一篇文章展示了 JavaFactory,一款新的 IntelliJ 插件,旨在使用大型语言模型 (LLM) 生成重复的 Java 代码(实现、测试、fixture)。与 Copilot 等通用工具不同,JavaFactory 专注于创建与特定项目结构一致并通过测试的代码,它使用自定义的自然语言模式和基于注解的引用。创建者 javafactory 强调了一个演示,该插件在 20 秒内生成了 400 行 Java 代码并通过了测试。 一些评论者讨论了替代方法,例如 IntelliJ 中的实时模板和结构重构。讨论还围绕该插件使用 OpenAI 的 GPT-4 模型展开,一些用户表达了对将代码发送到外部服务的担忧,并希望支持本地或替代的 LLM,例如 Claude Sonnet 或 LLaMA。也有一些评论指出该插件生成的测试能够通过,但 LLM 生成的测试是否真正有效地验证了代码。

原文
short20s.mp4

JavaFactory is a tool that uses LLMs to automatically generate repetitive Java code. It produces results that are more predictable and stable than those of traditional AI code generators. JavaFactory operates based on two core components:

  • Pattern Definition: Define each unit of work in natural language (e.g., test generation, implementation generation, etc.)

  • Annotation-Based Reference Collection: Explicitly specify the required classes using annotations.

Once defined, these patterns can be reused repeatedly to generate various types of code — such as implementations, tests, and fixtures.

Demo: 400 Lines in 20s. All tests passed

The following demo shows an example where 400 lines of code were generated, and all tests passed:


1. Define units of work as patterns

Repetitive tasks can be defined as a single pattern. A pattern clearly specifies what to generate, how to generate it, and which classes to reference.

A pattern consists of two main parts:

## Goal
{{ user_defined_goal }}

## Rules
{{ user_defined_rules }}

## Output
{{ expected_format }}

## Example
{{ typical_example_code }}
<< {role_name1 } >>
{{ class source }}

<< {role_name1 } >>
{{ class source }}

....

You can configure the System Prompt and User Prompt for each task, and JavaFactory provides a dedicated UI for this purpose.

  1. Edit system prompt configuration

Goal, Rules, Output, Example can be customized

  1. Edit user prompt configuration

Determine which classes should be included in the user prompt.


2. Annotation-Based Reference Collection

The classes to be used in a pattern are explicitly scoped using annotations. When a class is annotated with a specific role, it will be automatically included if it matches the reference targets defined in the pattern.

Annotations are categorized into two main types:

  • @JavaFactoryData

    • Recursively collects the classes specified in referencedData.
    • Example: Domain models, entities, and other data-related classes.
  • @JavaFactoryApi

    • Collects only one level of referencedApi.
    • Optionally allows specifying the implementation, test, and fixture classes for the API.
    • Example: API interfaces such as Reader, Writer, Validator.

You can explicitly control which classes are needed for code generation and how far the reference collection should go.



Those who have been disappointed with traditional AI code generators

  • If you've experienced unpredictable results or maintainability issues on former code generation
  • If you want more than just simple generation — you want to explicitly control the task unit and reference structure

Developers working in repetitive, structured environments

  • For example: Environments with repeating patterns like layered architecture
  • Tasks like the following can be automated repeatedly after defining the rule once:
  1. dao-repository-implementation
  2. dao-repository-test
  3. dao-repository-fixture
  4. domain-api-implementation
  5. domain-api-test
  6. domain-api-fixture

In such structures, I recommend designing the red boxes manually, and letting JavaFactory automate the blue boxes.


联系我们 contact @ memedata.com