From Code Completion to Task Delivery: Fixing Java Defects with AI-Powered Engineering Tools

    This article examines the shift from AI-assisted code generation to full engineering task delivery, using a duplicate refund fix in a Java order service as a concrete example. It discusses how AI tools now analyze multi-file call chains, enforce constraints, and produce testable changes—while highlighting the technical boundaries, such as static analysis limits and the need for database-level guarantees. It also provides a practical checklist for teams adopting this workflow in production environments.

    From Local Code Generation to Full Task Delivery

    Early AI coding tools focused on local, scoped work. A developer would type a method signature and the tool would complete the body, describe an interface, and the tool would generate Controller, DTO, and Service skeletons. When an error occurred, the developer pasted the log and asked the tool to explain it. In this mode, the developer still handled task decomposition, file navigation, and line-by-line review. The tool was essentially a fast-writing assistant.

    Task delivery mode changes the unit of work. The developer hands over a well-defined engineering task, including goals, modification scope, prohibited changes, and acceptance criteria. The tool must read the relevant code, understand call relationships across files, apply changes within constraints, run tests, and prepare a reviewable diff.

    The following table summarizes the key differences between the two modes:

    Dimension Local Code Generation Full Task Delivery
    Input A code snippet or a single question A bounded engineering task
    Context scope Current file or selected code Entire repository, related services, configuration, and database schema
    Constraint enforcement Developer verifies manually Tool follows each constraint and explains compliance in the deliverable
    Deliverable A code snippet Code changes, tests, and an impact analysis
    Human review focus Each generated piece of code Modification plan, diff, test results, and rollout approach

    This shift changes the developer’s role. Review focus moves from “is this piece of code correct?” to “is this change within scope, are the risks acceptable, and can it be rolled back safely?”

    A Concrete Example: Duplicate Refund Records

    Suppose a developer gives the tool the following instruction:

    1. Fix the duplicate refund record creation issue in order-service.
    2. Requirements:
    3. 1. First analyze the Controller, Service, Mapper, and database constraints.
    4. 2. Identify the real root cause of duplicate refunds.
    5. 3. Do not modify payment-service or production configuration.
    6. 4. Make only minimal-scope changes.

    The tool reads the Controller, Service, Mapper, and the refund table DDL in order-service. It identifies two problems. First, the refund table has no unique constraint on the order number. Second, the Service layer first checks whether a refund record already exists and then performs an insert—these two steps are not atomic. When two identical refund requests arrive almost simultaneously, both threads can pass the existence check and then insert a record each. This is the direct cause of duplicate refunds.

    The tool then addresses the constraints. “Do not modify payment-service” means it cannot adjust the cross-service call. “Do not modify production configuration” means it cannot directly execute a DDL to add a unique index. Adding a unique index to a large table may also cause lock contention and requires cleaning up existing duplicates first. Therefore, the tool implements protection at the application layer and lists the database unique constraint as a recommended follow-up that requires human approval.

    Below is an example of an application-layer modification the tool might provide. It does not replace a database-level constraint, and the code should be reviewed and tested before use:

    1. @Service
    2. public class RefundService {
    3. private final RefundMapper refundMapper;
    4. private final RefundTxService refundTxService;
    5. private final RedissonClient redissonClient;
    6. public RefundService(RefundMapper refundMapper,
    7. RefundTxService refundTxService,
    8. RedissonClient redissonClient) {
    9. this.refundMapper = refundMapper;
    10. this.refundTxService = refundTxService;
    11. this.redissonClient = redissonClient;
    12. }
    13. public RefundResult createRefund(String orderId, BigDecimal amount) {
    14. RLock lock = redissonClient.getLock("refund:lock:" + orderId);
    15. boolean locked;
    16. try {
    17. // No leaseTime is set; Redisson auto-renews the lock to avoid premature expiry during business execution.
    18. locked = lock.tryLock(3, TimeUnit.SECONDS);
    19. } catch (InterruptedException e) {
    20. Thread.currentThread().interrupt();
    21. return RefundResult.fail("Refund processing interrupted");
    22. } catch (RedisException e) {
    23. // If Redis is unavailable, reject the request. Refund is a money operation, so fail rather than risk a duplicate.
    24. log.error("Failed to acquire refund lock, orderId={}", orderId, e);
    25. return RefundResult.fail("System busy, please try again later");
    26. }
    27. if (!locked) {
    28. return RefundResult.fail("System busy, please try again later");
    29. }
    30. try {
    31. // The transaction commits when the transactional method returns; commit before releasing the lock.
    32. return refundTxService.createIfAbsent(orderId, amount);
    33. } finally {
    34. if (lock.isHeldByCurrentThread()) {
    35. lock.unlock();
    36. }
    37. }
    38. }
    39. }
    40. @Service
    41. public class RefundTxService {
    42. private final RefundMapper refundMapper;
    43. public RefundTxService(RefundMapper refundMapper) {
    44. this.refundMapper = refundMapper;
    45. }
    46. @Transactional(rollbackFor = Exception.class)
    47. public RefundResult createIfAbsent(String orderId, BigDecimal amount) {
    48. // Another thread may have completed creation during the lock wait, so verify again inside the transaction.
    49. if (refundMapper.selectByOrderId(orderId) != null) {
    50. return RefundResult.fail("A refund record already exists for this order. Do not duplicate.");
    51. }
    52. RefundRecord record = new RefundRecord();
    53. record.setOrderId(orderId);
    54. record.setAmount(amount);
    55. record.setStatus(RefundStatus.PROCESSING);
    56. refundMapper.insert(record);
    57. return RefundResult.success(record);
    58. }
    59. }

    Several points to note about this code:

    1. The lock only reduces the probability of concurrent conflicts. During Redis primary-secondary failover, two nodes could believe they hold the same lock.
    2. The transaction must commit before the lock is released. If the lock is released first, another thread might read the old state before the transaction commits and still insert a duplicate.
    3. The code adds a RedissonClient dependency and Redis access. If the project did not originally use Redis, this is a configuration and dependency change that needs separate review.
    4. The code rejects the refund request when Redis is unavailable. This conservative choice needs business confirmation.

    The truly reliable final defense is a database unique constraint, or an independent idempotency key table. For example, adding a unique index on order_id would cause the database to reject a second record even if the application layer fails. Before adding the index, existing duplicate records must be identified and cleaned, otherwise index creation will fail.

    Boundaries of Tool Capabilities

    Full task delivery relies on two types of code understanding techniques, each with different applicability.

    AST parsing is based on syntax structure and can precisely locate method calls, class inheritance, and annotation relationships. It works well for questions like “who calls this method?”. However, Spring’s dynamic proxies, reflection calls, and beans assembled via configuration files are difficult to trace through static syntax analysis.

    Vector retrieval is based on semantic similarity and can quickly find code, comments, and documentation related to a requirement, making it useful for locating entry points in large repositories. But it returns “possibly relevant” results; it cannot prove that the code compiles or that the call chain is complete.

    Neither technique can replace compilation, unit tests, or integration tests. The tool’s delivery conclusions must be validated by these verification steps before they can be trusted.

    A Checklist for Production Adoption

    Before introducing task delivery mode into a team, verify the following:

    • The tool only accesses authorized code repositories, with permissions identical to the developer’s account.
    • The task description explicitly lists services, files, and configurations that are off-limits for modification.
    • The CI pipeline checks whether the code diff goes beyond the allowed scope.
    • Dependency, configuration, and database schema changes are listed separately and require approval.
    • The tool generates unit tests and concurrency scenario tests, and the test results are included in the deliverable.
    • The change description includes impact scope, potential risks, rollback steps, and monitoring metrics.
    • Pre-deployment validation is performed in a test or staging environment, observing metrics such as the number of duplicate records.
    • Backups are completed before any production data operation, and execution time and lock table risks are assessed.

    Conclusion

    AI programming tools are evolving from code generators to engineering collaborators. They can read context, follow constraints, and produce complete change proposals. The actual effectiveness of these capabilities depends on codebase quality, test coverage, access control, and the rigor of human review. In a poorly structured repository with few tests, the quality of the tool’s deliverable will drop significantly.

    When evaluating a tool, model scores on generic benchmarks should not be the sole criterion. A more practical approach is to run a pilot on your team’s own real defects, record whether the modification scope was compliant, whether tests passed, and how many issues the human reviewer found. Use that data to decide how widely to roll out the workflow.