Skip to content

Commit 1ac3356

Browse files
committed
Add API to get the array of method parameters given the callable object.
Signed-off-by: Rahul Krishna <[email protected]>
1 parent ea064d8 commit 1ac3356

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@
3838
"JSONStream": "^1.3.5",
3939
"bun": "^1.2.10",
4040
"c8": "^10.1.3",
41+
"chalk": "^5.4.1",
4142
"extract-zip": "^2.0.1",
4243
"fast-glob": "^3.3.3",
4344
"graphology": "^0.26.0",
4445
"loglevel": "^1.9.2",
46+
"signale": "^1.4.0",
4547
"zod": "^3.24.3"
4648
},
4749
"testing": {

src/analysis/java/JavaAnalysis.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,36 @@ export class JavaAnalysis {
262262
public async getMethodParameters(qualifiedName: string, methodName: string): Promise<Array<types.JCallableParameterType>> {
263263
return (await this.getMethodByQualifiedName(qualifiedName, methodName)).parameters ?? [];
264264
}
265+
266+
/**
267+
* Get all the method parameters in a specific method within a specific class by its callable object.
268+
* @param {types.JCallableType} callable - The callable object representing the method to retrieve
269+
* @returns {Promise<Array<types.JCallableParameterType>>} A promise that resolves to an array of {@link JCallableParameterType} objects
270+
*
271+
* @notes This method retrieves all the parameters of a specific method from the application by its callable object.
272+
* If the method is found, it returns an array of {@link JCallableParameter} objects representing
273+
* the parameters of the method. Otherwise, it returns an empty array.
274+
*/
275+
public async getMethodParametersFromCallable(callable: types.JCallableType): Promise<Array<types.JCallableParameterType>> {
276+
return callable.parameters ?? [];
277+
}
278+
279+
/**
280+
* Get the java file path given the qualified name of the class.
281+
* @param {string} qualifiedName - The qualified name of the class to retrieve
282+
* @returns {Promise<string>} A promise that resolves to the file path of the Java file containing the class
283+
* @throws {Error} If the class is not found in the application.
284+
*
285+
* @notes This method retrieves the file path of the Java file containing the class with the specified qualified name.
286+
* If the class is found, it returns the file path as a string. If the class is not found, it throws an error.
287+
*/
288+
public async getJavaFilePathByQualifiedName(qualifiedName: string): Promise<string> {
289+
const symbolTable = await this.getSymbolTable();
290+
for (const [filePath, compilationUnit] of Object.entries(symbolTable)) {
291+
if (Object.keys(compilationUnit.type_declarations).includes(qualifiedName)) {
292+
return filePath;
293+
}
294+
}
295+
throw new Error(`Class ${qualifiedName} not found in the application.`);
296+
}
265297
}

test/conftest.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020

2121
import * as fs from 'fs';
2222
import * as path from 'path';
23-
import * as https from 'https';
2423
import extract from 'extract-zip';
25-
import {beforeAll, afterAll } from "bun:test";
24+
import { beforeAll, afterAll } from "bun:test";
2625
import { CLDK } from "../src/CLDK";
2726

2827
/*
@@ -36,7 +35,7 @@ beforeAll(async () => {
3635
const javaSampleAppsDir = path.join(__dirname, "test-applications", "java");
3736
const appZipFile = path.join(javaSampleAppsDir, "sample.daytrader8-1.2.zip");
3837
const extractedDir = path.join(javaSampleAppsDir, "sample.daytrader8-1.2");
39-
await extract(appZipFile, {dir: javaSampleAppsDir});
38+
await extract(appZipFile, { dir: javaSampleAppsDir });
4039

4140
/**
4241
* I am just hardcoding the extracted directory name for now. The extracted directory name would follow GitHub's
@@ -63,7 +62,7 @@ beforeAll(async () => {
6362
*/
6463
afterAll(async () => {
6564
if (dayTraderApp) {
66-
fs.rmSync(dayTraderApp, {recursive: true, force: true});
65+
fs.rmSync(dayTraderApp, { recursive: true, force: true });
6766
}
6867
})
6968

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { JCallable, JCallableParameter, JType } from "../../../../src/models/java/";
22
import { daytraderJavaAnalysis } from "../../../conftest";
33
import { expect, test } from "bun:test";
4+
import chalk from "chalk";
5+
import { Signale } from "signale";
6+
7+
const logger = new Signale();
48

59
test("Must get analysis object from JavaAnalysis object", () => {
610
expect(daytraderJavaAnalysis).toBeDefined();
@@ -60,8 +64,27 @@ test("Must get parameters of a specific method in a specific class in the applic
6064
"com.ibm.websphere.samples.daytrader.impl.direct.TradeDirect", "publishQuotePriceChange(QuoteDataBean, BigDecimal, BigDecimal, double)");
6165

6266
expect(parameters).toBeDefined();
63-
expect(parameters.length).toBeGreaterThan(0);
67+
logger.success(chalk.green("parameters are defined"));
68+
expect(parameters.length).toBe(4);
69+
logger.success(chalk.green("there are 4 parameters"));
6470
parameters.forEach(param => {
6571
expect(async () => JCallableParameter.parse(param)).not.toThrow();
6672
});
67-
});
73+
logger.success(chalk.green("All parameters are valid JCallableParameter instances"));
74+
});
75+
76+
test("Must get parameters of a specific method in a specific class in the application given the callable object", async () => {
77+
const method = await daytraderJavaAnalysis.getMethodByQualifiedName(
78+
"com.ibm.websphere.samples.daytrader.impl.direct.TradeDirect", "publishQuotePriceChange(QuoteDataBean, BigDecimal, BigDecimal, double)");
79+
const parameters = await daytraderJavaAnalysis.getMethodParametersFromCallable(method);
80+
expect(parameters).toBeDefined();
81+
logger.success(chalk.green("parameters are defined"));
82+
expect(parameters.length).toBe(4);
83+
logger.success(chalk.green("there are 4 parameters"));
84+
parameters.forEach(param => {
85+
expect(async () => JCallableParameter.parse(param)).not.toThrow();
86+
}
87+
);
88+
logger.success(chalk.green("All parameters are valid JCallableParameter instances"));
89+
});
90+

0 commit comments

Comments
 (0)