Skip to content

Commit cefedaa

Browse files
committed
Add API to get a specific method given the qualified class name and the method signature.
Signed-off-by: Rahul Krishna <[email protected]>
1 parent dcbbbbe commit cefedaa

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/analysis/java/JavaAnalysis.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import fg from "fast-glob";
1919
import fs from "fs";
2020
import log from "loglevel";
2121
import { spawnSync } from "node:child_process";
22-
import { JApplication } from "../../models/java";
22+
import { JApplication, JCompilationUnit } from "../../models/java";
2323
import * as types from "../../models/java/types";
2424
import { JType } from "../../models/java";
2525
import os from "os";
@@ -152,6 +152,15 @@ export class JavaAnalysis {
152152
return this.application;
153153
}
154154

155+
/**
156+
* Get the symbol table from the application.
157+
* @returns {Promise<Record<string, types.JCompilationUnitType>>} A promise that resolves to a record of file paths and their
158+
* corresponding {@link JCompilationUnitType} objects
159+
*
160+
* @notes This method retrieves the symbol table from the application, which contains information about all
161+
* compilation units in the Java application. The returned record contains file paths as keys and their
162+
* corresponding {@link JCompilationUnit} objects as values.
163+
*/
155164
public async getSymbolTable(): Promise<Record<string, types.JCompilationUnitType>> {
156165
return (await this.getApplication()).symbol_table;
157166
}
@@ -222,4 +231,21 @@ export class JavaAnalysis {
222231
const classForWhichMethodsAreRequested = await this.getClassByQualifiedName(qualifiedName);
223232
return classForWhichMethodsAreRequested ? Object.values(classForWhichMethodsAreRequested.callable_declarations ?? {}) : [];
224233
}
234+
235+
/**
236+
* Get a specific methods within a specific class by its qualified name.
237+
* @param {string} qualifiedName - The qualified name of the class to retrieve
238+
* @param {string} methodName - The name of the method to retrieve
239+
* @returns {Promise<types.JCallableType>} A promise that resolves to the {@link JCallable} object representing the method.
240+
* @throws {Error} If the class or method is not found in the application.
241+
*
242+
* @notes This method retrieves a specific method from the application by its qualified name and method name.
243+
* If the method is found, it returns the corresponding {@link JCallableType} object. If the method is not found,
244+
* it throws an error.
245+
*/
246+
public async getMethodByQualifiedName(qualifiedName: string, methodName: string): Promise<types.JCallableType> {
247+
return (await this.getAllMethodsByClass(qualifiedName)).find(
248+
(method) => method.signature === methodName
249+
) ?? (() => { throw new Error(`Method ${methodName} not found in class ${qualifiedName}.`); })();
250+
}
225251
}

test/unit/analysis/java/JavaAnalysis.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { JType } from "../../../../src/models/java/";
1+
import { JCallable, JType } from "../../../../src/models/java/";
22
import { daytraderJavaAnalysis } from "../../../conftest";
33
import { expect, test } from "bun:test";
44

@@ -46,4 +46,11 @@ test("Must get all methods in a specific class in the application", async () =>
4646
(
4747
await daytraderJavaAnalysis.getAllMethodsByClass("com.ibm.websphere.samples.daytrader.impl.direct.TradeDirect")).length
4848
).toBeGreaterThan(0)
49+
});
50+
51+
test("Must get a specific method in a specific class in the application", async () => {
52+
const method = await daytraderJavaAnalysis.getMethodByQualifiedName(
53+
"com.ibm.websphere.samples.daytrader.impl.direct.TradeDirect", "publishQuotePriceChange(QuoteDataBean, BigDecimal, BigDecimal, double)");
54+
55+
expect(async () => JCallable.parse(method)).not.toThrow();
4956
});

0 commit comments

Comments
 (0)