Closes three gaps that broke `trace(controller, mapper-xml)` on real Spring +
MyBatis projects:
1. **Field-injected concrete-bean trace.** Java `this.<field>.method()` is
unwrapped at extraction (was surfaced as `this.<field>.method` and dropped
through every name-matcher strategy). The receiver name is then looked up
in the enclosing class's field declarations to get the declared type and
resolve the method on it. Closes the controller→bean hop when the field
name doesn't capitalize to the type (`userbo` → `UserBO`). General Java
fix, not Spring-specific.
2. **MyBatis XML mapper as a first-class language.** New extractor parses
`<mapper namespace="..."><select|insert|update|delete|sql id="X">` and
emits method-shaped nodes qualified as `<namespace>::<id>`, plus
`<include refid="X"/>` references to `<sql>` fragments. Non-mapper XML
(pom, log4j, web.xml) → file node only. A new synthesizer
(`mybatisJavaXmlEdges`) joins Java mapper methods to XML statements by
suffix-matching qualified names. Ambiguous simple-name collisions dropped
for precision.
3. **Spring `@Value`/`@ConfigurationProperties` → application config.**
`application.{yml,yaml,properties}` + profile variants parse on the
framework path; each leaf key becomes a `constant` node qualified by its
dotted path. `@Value("${k}")` / `@Value("${k:default}")` and
`@ConfigurationProperties(prefix="X")` emit binding nodes that resolve
with Spring's relaxed binding (kebab↔camel↔snake).
Validated on macrozheng/mall-tiny: full chain
`UmsRoleController.listResource → UmsRoleService.listResource → impl →
UmsResourceMapper.getResourceListByRoleId → XML <select>` connects across 5
hops via static + synthesized edges. 11/11 @Value annotations resolved
(incl. `@ConfigurationProperties(prefix="secure.ignored")`); 6/6 custom-SQL
mapper methods bridge to XML.
Tests: 4 new integration tests in frameworks-integration.test.ts. Full
suite: 1005 passed.
Docs: CHANGELOG `[Unreleased]` entry + dynamic-dispatch-coverage-playbook
narrative + matrix row.
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
55839edd8f
commit
2543ae565a
@@ -302,3 +302,307 @@ describe('C++ end-to-end — virtual override synthesis', () => {
|
||||
cg.close();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Java end-to-end — field-injected bean trace (issue #389)', () => {
|
||||
let tmpDir: string | undefined;
|
||||
afterEach(() => {
|
||||
if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
tmpDir = undefined;
|
||||
});
|
||||
|
||||
// Mirrors the issue's Spring MVC pattern:
|
||||
// UserAction(@Resource UserBO userbo).toLogin2() -> this.userbo.toLogin2()
|
||||
// -> UserBO.toLogin2() -> userService.toLogin() -> UserService.toLogin (iface)
|
||||
// -> UserServiceImpl.toLogin() via interface→impl synthesis.
|
||||
// Without the extractor `this.` strip + field-typed receiver lookup, the very
|
||||
// first hop (controller -> bean) was missing entirely, breaking trace.
|
||||
it('connects controller -> @Resource bean -> interface -> impl end-to-end', async () => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-spring-bean-'));
|
||||
const javaDir = path.join(tmpDir, 'src/main/java/com/example/user');
|
||||
fs.mkdirSync(path.join(javaDir, 'action'), { recursive: true });
|
||||
fs.mkdirSync(path.join(javaDir, 'bo'), { recursive: true });
|
||||
fs.mkdirSync(path.join(javaDir, 'service'), { recursive: true });
|
||||
fs.mkdirSync(path.join(javaDir, 'service/impl'), { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, 'pom.xml'),
|
||||
'<project><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>\n'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(javaDir, 'action/UserAction.java'),
|
||||
'package com.example.user.action;\n' +
|
||||
'import com.example.user.bo.UserBO;\n' +
|
||||
'import javax.annotation.Resource;\n' +
|
||||
'@org.springframework.stereotype.Controller\n' +
|
||||
'public class UserAction {\n' +
|
||||
' @Resource(name = "userBO") private UserBO userbo;\n' +
|
||||
' public void toLogin2() { this.userbo.toLogin2(); }\n' +
|
||||
'}\n'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(javaDir, 'bo/UserBO.java'),
|
||||
'package com.example.user.bo;\n' +
|
||||
'import com.example.user.service.UserService;\n' +
|
||||
'import javax.annotation.Resource;\n' +
|
||||
'@org.springframework.stereotype.Component("userBO")\n' +
|
||||
'public class UserBO {\n' +
|
||||
' @Resource private UserService userService;\n' +
|
||||
' public void toLogin2() { userService.toLogin(); }\n' +
|
||||
'}\n'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(javaDir, 'service/UserService.java'),
|
||||
'package com.example.user.service;\n' +
|
||||
'public interface UserService { void toLogin(); }\n'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(javaDir, 'service/impl/UserServiceImpl.java'),
|
||||
'package com.example.user.service.impl;\n' +
|
||||
'import com.example.user.service.UserService;\n' +
|
||||
'@org.springframework.stereotype.Service("userService")\n' +
|
||||
'public class UserServiceImpl implements UserService {\n' +
|
||||
' public void toLogin() { }\n' +
|
||||
'}\n'
|
||||
);
|
||||
|
||||
const cg = CodeGraph.initSync(tmpDir);
|
||||
await cg.indexAll();
|
||||
|
||||
const methods = cg.getNodesByKind('method');
|
||||
const find = (cls: string, name: string) =>
|
||||
methods.find((m) => m.name === name && m.filePath.endsWith(`${cls}.java`));
|
||||
|
||||
const action = find('UserAction', 'toLogin2');
|
||||
const bo = find('UserBO', 'toLogin2');
|
||||
const svc = find('UserService', 'toLogin');
|
||||
const impl = find('UserServiceImpl', 'toLogin');
|
||||
expect(action).toBeDefined();
|
||||
expect(bo).toBeDefined();
|
||||
expect(svc).toBeDefined();
|
||||
expect(impl).toBeDefined();
|
||||
|
||||
// UserAction.toLogin2 -> UserBO.toLogin2 (the regressed hop — `this.userbo`
|
||||
// receiver was emitted verbatim and the field-type lookup didn't exist).
|
||||
const actionToBo = cg.getOutgoingEdges(action!.id).find((e) => e.target === bo!.id);
|
||||
expect(actionToBo, 'controller `this.userbo.toLogin2()` should reach UserBO.toLogin2').toBeDefined();
|
||||
expect(actionToBo!.kind).toBe('calls');
|
||||
|
||||
// UserBO.toLogin2 -> UserService.toLogin (plain identifier receiver, works pre-fix).
|
||||
const boToSvc = cg.getOutgoingEdges(bo!.id).find((e) => e.target === svc!.id);
|
||||
expect(boToSvc).toBeDefined();
|
||||
|
||||
// UserService.toLogin -> UserServiceImpl.toLogin (interface->impl synth).
|
||||
const svcToImpl = cg.getOutgoingEdges(svc!.id).find((e) => e.target === impl!.id);
|
||||
expect(svcToImpl).toBeDefined();
|
||||
|
||||
cg.close();
|
||||
});
|
||||
|
||||
it('bridges a Java mapper interface method to its MyBatis XML statement (incl. SQL fragments)', async () => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-mybatis-'));
|
||||
const javaDir = path.join(tmpDir, 'src/main/java/com/example/dao');
|
||||
const xmlDir = path.join(tmpDir, 'src/main/resources/mappers');
|
||||
fs.mkdirSync(javaDir, { recursive: true });
|
||||
fs.mkdirSync(xmlDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, 'pom.xml'),
|
||||
'<project><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId></dependency></dependencies></project>\n'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(javaDir, 'UserDAOMapper.java'),
|
||||
'package com.example.dao;\n' +
|
||||
'public interface UserDAOMapper {\n' +
|
||||
' Object getById(int id);\n' +
|
||||
' int updateUser(Object u);\n' +
|
||||
'}\n'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(xmlDir, 'UserDAOMapper.xml'),
|
||||
'<?xml version="1.0" encoding="UTF-8"?>\n' +
|
||||
'<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">\n' +
|
||||
'<mapper namespace="com.example.dao.UserDAOMapper">\n' +
|
||||
' <sql id="userCols">id, name, email</sql>\n' +
|
||||
' <select id="getById" parameterType="int" resultType="User">\n' +
|
||||
' SELECT <include refid="userCols"/> FROM users WHERE id = #{id}\n' +
|
||||
' </select>\n' +
|
||||
' <update id="updateUser" parameterType="User">\n' +
|
||||
' UPDATE users SET name=#{name}, email=#{email} WHERE id=#{id}\n' +
|
||||
' </update>\n' +
|
||||
'</mapper>\n'
|
||||
);
|
||||
|
||||
const cg = CodeGraph.initSync(tmpDir);
|
||||
await cg.indexAll();
|
||||
|
||||
const methods = cg.getNodesByKind('method');
|
||||
const getByIdJava = methods.find((m) => m.name === 'getById' && m.language === 'java');
|
||||
const getByIdXml = methods.find((m) => m.name === 'getById' && m.language === 'xml');
|
||||
const updateJava = methods.find((m) => m.name === 'updateUser' && m.language === 'java');
|
||||
const updateXml = methods.find((m) => m.name === 'updateUser' && m.language === 'xml');
|
||||
const sqlFrag = methods.find((m) => m.name === 'userCols' && m.language === 'xml');
|
||||
expect(getByIdJava).toBeDefined();
|
||||
expect(getByIdXml).toBeDefined();
|
||||
expect(updateJava).toBeDefined();
|
||||
expect(updateXml).toBeDefined();
|
||||
expect(sqlFrag).toBeDefined();
|
||||
|
||||
// XML statement qualified name must be `<namespace>::<id>` so the
|
||||
// synthesizer can match against the Java method's `<Class>::<method>`
|
||||
// suffix — this is the load-bearing contract between extractor + synthesis.
|
||||
expect(getByIdXml!.qualifiedName).toBe('com.example.dao.UserDAOMapper::getById');
|
||||
|
||||
// Bridge: Java mapper method -> XML statement, kind 'calls'.
|
||||
const j2xGet = cg.getOutgoingEdges(getByIdJava!.id).find((e) => e.target === getByIdXml!.id);
|
||||
expect(j2xGet, 'Java getById should reach the XML <select id="getById">').toBeDefined();
|
||||
expect(j2xGet!.kind).toBe('calls');
|
||||
const j2xUpd = cg.getOutgoingEdges(updateJava!.id).find((e) => e.target === updateXml!.id);
|
||||
expect(j2xUpd, 'Java updateUser should reach the XML <update id="updateUser">').toBeDefined();
|
||||
|
||||
// <include refid="userCols"/> inside <select> -> <sql id="userCols"> in same mapper.
|
||||
const incEdge = cg.getOutgoingEdges(getByIdXml!.id).find((e) => e.target === sqlFrag!.id);
|
||||
expect(incEdge, '<include refid="userCols"/> should reach the <sql> fragment').toBeDefined();
|
||||
|
||||
cg.close();
|
||||
});
|
||||
|
||||
it('binds @Value / @ConfigurationProperties to YAML + .properties keys (incl. relaxed binding)', async () => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-spring-config-'));
|
||||
const javaDir = path.join(tmpDir, 'src/main/java/com/example');
|
||||
const resDir = path.join(tmpDir, 'src/main/resources');
|
||||
fs.mkdirSync(javaDir, { recursive: true });
|
||||
fs.mkdirSync(resDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, 'pom.xml'),
|
||||
'<project><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency></dependencies></project>\n'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(resDir, 'application.yml'),
|
||||
'app:\n' +
|
||||
' cache:\n' +
|
||||
' name:\n' +
|
||||
' user-token: "example-service:auth:token"\n' +
|
||||
' enabled: true\n' +
|
||||
'db:\n' +
|
||||
' url: "jdbc:mysql://localhost/x"\n'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(resDir, 'application.properties'),
|
||||
'app.retry-count=3\n'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(javaDir, 'CacheConfig.java'),
|
||||
'package com.example;\n' +
|
||||
'import org.springframework.beans.factory.annotation.Value;\n' +
|
||||
'public class CacheConfig {\n' +
|
||||
' @Value("${app.cache.name.user-token}") private String tokenCacheName;\n' +
|
||||
' @Value("${app.cache.enabled:true}") private boolean enabled;\n' +
|
||||
' // relaxed binding: java camelCase, properties kebab-case\n' +
|
||||
' @Value("${app.retryCount}") private int retry;\n' +
|
||||
'}\n'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(javaDir, 'CacheProperties.java'),
|
||||
'package com.example;\n' +
|
||||
'import org.springframework.boot.context.properties.ConfigurationProperties;\n' +
|
||||
'@ConfigurationProperties(prefix = "app.cache")\n' +
|
||||
'public class CacheProperties { private boolean enabled; }\n'
|
||||
);
|
||||
|
||||
const cg = CodeGraph.initSync(tmpDir);
|
||||
await cg.indexAll();
|
||||
|
||||
// YAML/properties leaf keys: one constant node per dotted path.
|
||||
const cfgKeys = cg
|
||||
.getNodesByKind('constant')
|
||||
.filter((n) => n.language === 'yaml' || n.language === 'properties');
|
||||
const cfgByQn = (qn: string) => cfgKeys.find((n) => n.qualifiedName === qn);
|
||||
expect(cfgByQn('app.cache.name.user-token')).toBeDefined();
|
||||
expect(cfgByQn('app.cache.enabled')).toBeDefined();
|
||||
expect(cfgByQn('db.url')).toBeDefined();
|
||||
expect(cfgByQn('app.retry-count')).toBeDefined();
|
||||
|
||||
// @Value("${app.cache.name.user-token}") -> the YAML leaf key.
|
||||
const valueBindings = cg
|
||||
.getNodesByKind('constant')
|
||||
.filter((n) => n.id.startsWith('spring-value:'));
|
||||
const userToken = valueBindings.find((n) => n.name === 'app.cache.name.user-token');
|
||||
expect(userToken).toBeDefined();
|
||||
const userTokenEdges = cg.getOutgoingEdges(userToken!.id);
|
||||
const userTokenTarget = userTokenEdges.find((e) =>
|
||||
cfgKeys.some((c) => c.id === e.target && c.qualifiedName === 'app.cache.name.user-token'),
|
||||
);
|
||||
expect(userTokenTarget, '@Value should reference the YAML leaf key').toBeDefined();
|
||||
|
||||
// Default-value form `${k:default}` — strip the `:default` and bind the key.
|
||||
const enabledBind = valueBindings.find((n) => n.name === 'app.cache.enabled');
|
||||
expect(enabledBind).toBeDefined();
|
||||
expect(cg.getOutgoingEdges(enabledBind!.id).some((e) => {
|
||||
const t = cfgByQn('app.cache.enabled');
|
||||
return t && e.target === t.id;
|
||||
})).toBe(true);
|
||||
|
||||
// Relaxed binding: `app.retryCount` (camel) -> `app.retry-count` (kebab).
|
||||
const retryBind = valueBindings.find((n) => n.name === 'app.retryCount');
|
||||
expect(retryBind).toBeDefined();
|
||||
expect(cg.getOutgoingEdges(retryBind!.id).some((e) => {
|
||||
const t = cfgByQn('app.retry-count');
|
||||
return t && e.target === t.id;
|
||||
})).toBe(true);
|
||||
|
||||
// @ConfigurationProperties(prefix="app.cache") -> a key under that prefix.
|
||||
const cpBindings = cg
|
||||
.getNodesByKind('constant')
|
||||
.filter((n) => n.id.startsWith('spring-cp:'));
|
||||
const cpAppCache = cpBindings.find((n) => n.name === 'app.cache');
|
||||
expect(cpAppCache).toBeDefined();
|
||||
const cpEdges = cg.getOutgoingEdges(cpAppCache!.id);
|
||||
expect(cpEdges.length).toBeGreaterThan(0);
|
||||
|
||||
cg.close();
|
||||
});
|
||||
|
||||
it('emits only a file node for non-MyBatis XML (pom.xml, beans.xml, log4j.xml)', async () => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-xml-non-mybatis-'));
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, 'pom.xml'),
|
||||
'<project><groupId>x</groupId><artifactId>y</artifactId></project>\n'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, 'log4j.xml'),
|
||||
'<?xml version="1.0"?><Configuration><Loggers><Root level="info"/></Loggers></Configuration>\n'
|
||||
);
|
||||
|
||||
const cg = CodeGraph.initSync(tmpDir);
|
||||
await cg.indexAll();
|
||||
// No method nodes — non-mapper XML produces no symbols (just file rows).
|
||||
expect(cg.getNodesByKind('method').filter((n) => n.language === 'xml').length).toBe(0);
|
||||
cg.close();
|
||||
});
|
||||
|
||||
it('resolves a `this.field.method()` call to a unique implementation class', async () => {
|
||||
// Standalone test of the extractor `this.` strip: even without Spring annotations,
|
||||
// `this.svc.run()` where `svc` is typed as a concrete class should route to that
|
||||
// class's method. This is the general Java fix, Spring is only one consumer.
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-java-this-field-'));
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, 'App.java'),
|
||||
'class Svc { public void run() { } }\n' +
|
||||
'class App {\n' +
|
||||
' private Svc svc;\n' +
|
||||
' public void go() { this.svc.run(); }\n' +
|
||||
'}\n'
|
||||
);
|
||||
|
||||
const cg = CodeGraph.initSync(tmpDir);
|
||||
await cg.indexAll();
|
||||
|
||||
const methods = cg.getNodesByKind('method');
|
||||
const go = methods.find((m) => m.name === 'go');
|
||||
const run = methods.find((m) => m.name === 'run');
|
||||
expect(go && run).toBeTruthy();
|
||||
|
||||
const edge = cg.getOutgoingEdges(go!.id).find((e) => e.target === run!.id);
|
||||
expect(edge, '`this.svc.run()` should resolve to Svc.run').toBeDefined();
|
||||
|
||||
cg.close();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user