Skip to content

Commit 00ca701

Browse files
committed
Add API to get a specific class in the application.
Signed-off-by: Rahul Krishna <[email protected]>
1 parent 314185d commit 00ca701

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/analysis/java/JavaAnalysis.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,29 @@ export class JavaAnalysis {
173173
}, {} as Record<string, types.JTypeType>);
174174
}
175175

176+
/**
177+
* Get all methods in the application.
178+
* @returns {Promise<Record<string, Record<string, types.JCallableType>>>} A promise that resolves to a record of
179+
* method names and their corresponding {@link JCallableType} objects
180+
*
181+
* @notes This method retrieves all methods from the symbol table and returns them as a record. The returned
182+
* record contains class names as keys and their corresponding {@link JCallableType} objects as values.
183+
* Each {@link JCallableType} object contains information about the method's parameters, return type, and
184+
* other relevant details.
185+
*/
176186
public async getAllMethods(): Promise<Record<string, Record<string, types.JCallableType>>> {
177187
return Object.entries(await this.getAllClasses()).reduce((allMethods, [key, value]) => {
178188
allMethods[key] = value.callable_declarations;
179189
return allMethods;
180190
}, {} as Record<string, Record<string, types.JCallableType>>);
181191
}
192+
193+
public async getClassByQualifiedName(qualifiedName: string): Promise<types.JTypeType> {
194+
const allClasses = await this.getAllClasses();
195+
if (allClasses[qualifiedName]) {
196+
return allClasses[qualifiedName];
197+
}
198+
else
199+
throw new Error(`Class ${qualifiedName} not found in the application.`);
200+
}
182201
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { JType } from "../../../../src/models/java/";
12
import { daytraderJavaAnalysis } from "../../../conftest";
23
import { expect, test } from "bun:test";
34

@@ -19,7 +20,15 @@ test("Must get all classes in a Java application", async () => {
1920
expect(await daytraderJavaAnalysis.getAllClasses()).toBeDefined();
2021
});
2122

22-
test("Must get all methods in the application", async () => {
23-
const allMethods = await daytraderJavaAnalysis.getAllMethods();
24-
expect(allMethods).toBeDefined();
23+
test("Must get a specific class the application", async () => {
24+
const tradeDirectObject = await daytraderJavaAnalysis.getClassByQualifiedName("com.ibm.websphere.samples.daytrader.impl.direct.TradeDirect");
25+
console.log(tradeDirectObject);
26+
expect(async () => JType.parse(tradeDirectObject)).not.toThrow();
2527
});
28+
29+
test("Must get all methods in the application", () => {
30+
return daytraderJavaAnalysis.getAllMethods().then((methods) => {
31+
expect(methods).toBeDefined()
32+
});
33+
});
34+

0 commit comments

Comments
 (0)