Skip to content

Commit dcbbbbe

Browse files
committed
Add API to get all methods within a specific class in the application.
Signed-off-by: Rahul Krishna <[email protected]>
1 parent da60a60 commit dcbbbbe

File tree

4 files changed

+52
-20
lines changed

4 files changed

+52
-20
lines changed

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
run: bun install
2121

2222
- name: Run tests with coverage
23-
run: bun run test
23+
run: bun run test:withCoverage
2424

2525
- name: Upload coverage to Coveralls
2626
uses: coverallsapp/github-action@v2

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"homepage": "https://github.com/codellm-devkit/typescript-sdk#readme",
1414
"scripts": {
1515
"build": "bun build ./src/index.ts --outdir ./dist",
16-
"test": "bun test --verbose --coverage --coverage-reporter=lcov --preload ./test/conftest.ts --timeout=600000",
16+
"test": "bun test --verbose --coverage --preload ./test/conftest.ts --timeout=600000",
17+
"test:withCoverage": "bun test --verbose --coverage --coverage-reporter=lcov --preload ./test/conftest.ts --timeout=600000",
1718
"clean": "rm -rf dist coverage *.lock"
1819
},
1920
"files": [
@@ -36,6 +37,7 @@
3637
"@types/jsonstream": "^0.8.33",
3738
"JSONStream": "^1.3.5",
3839
"bun": "^1.2.10",
40+
"c8": "^10.1.3",
3941
"extract-zip": "^2.0.1",
4042
"fast-glob": "^3.3.3",
4143
"graphology": "^0.26.0",

src/analysis/java/JavaAnalysis.ts

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ export class JavaAnalysis {
8080
const tmpFilePath = path.join(os.tmpdir(), `${Date.now()}-${crypto.randomUUID()}`);
8181
const command = [
8282
...this.getCodeAnalyzerExec(),
83-
"-i",
83+
"--input",
8484
projectPath,
85-
"-o",
85+
"--output",
8686
tmpFilePath,
8787
`--analysis-level=${this.analysisLevel}`,
8888
"--verbose",
@@ -174,22 +174,14 @@ export class JavaAnalysis {
174174
}
175175

176176
/**
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
177+
* Get a specific class by its qualified name.
178+
* @param {string} qualifiedName - The qualified name of the class to retrieve
179+
* @returns {Promise<types.JTypeType>} A promise that resolves to the {@link JTypeType} object representing the class
180+
* @throws {Error} If the class is not found in the application
180181
*
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.
182+
* @notes This method retrieves a specific class from the application by its qualified name. If the class is found,
183+
* it returns the corresponding {@link JType} object. If the class is not found, it throws an error.
185184
*/
186-
public async getAllMethods(): Promise<Record<string, Record<string, types.JCallableType>>> {
187-
return Object.entries(await this.getAllClasses()).reduce((allMethods, [key, value]) => {
188-
allMethods[key] = value.callable_declarations;
189-
return allMethods;
190-
}, {} as Record<string, Record<string, types.JCallableType>>);
191-
}
192-
193185
public async getClassByQualifiedName(qualifiedName: string): Promise<types.JTypeType> {
194186
const allClasses = await this.getAllClasses();
195187
if (allClasses[qualifiedName]) {
@@ -198,4 +190,36 @@ export class JavaAnalysis {
198190
else
199191
throw new Error(`Class ${qualifiedName} not found in the application.`);
200192
}
193+
194+
/**
195+
* Get all methods in the application.
196+
* @returns {Promise<Record<string, Record<string, types.JCallableType>>>} A promise that resolves to a record of
197+
* method names and their corresponding {@link JCallableType} objects
198+
*
199+
* @notes This method retrieves all methods from the symbol table and returns them as a record. The returned
200+
* record contains class names as keys and their corresponding {@link JCallableType} objects as values.
201+
* Each {@link JCallableType} object contains information about the method's parameters, return type, and
202+
* other relevant details.
203+
*/
204+
public async getAllMethods(): Promise<Record<string, Record<string, types.JCallableType>>> {
205+
return Object.entries(await this.getAllClasses()).reduce((allMethods, [key, value]) => {
206+
allMethods[key] = value.callable_declarations;
207+
return allMethods;
208+
}, {} as Record<string, Record<string, types.JCallableType>>);
209+
}
210+
211+
/**
212+
* Get all methods in a specific class in the application.
213+
* @returns {Promise<Record<string, Record<string, types.JCallableType>>>} A promise that resolves to a record of
214+
* method names and their corresponding {@link JCallableType} objects
215+
*
216+
* @notes This method retrieves all methods from the symbol table and returns them as a record. The returned
217+
* record contains class names as keys and their corresponding {@link JCallableType} objects as values.
218+
* Each {@link JCallableType} object contains information about the method's parameters, return type, and
219+
* other relevant details.
220+
*/
221+
public async getAllMethodsByClass(qualifiedName: string): Promise<Array<types.JCallableType>> {
222+
const classForWhichMethodsAreRequested = await this.getClassByQualifiedName(qualifiedName);
223+
return classForWhichMethodsAreRequested ? Object.values(classForWhichMethodsAreRequested.callable_declarations ?? {}) : [];
224+
}
201225
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test("Must get all classes in a Java application", async () => {
2222

2323
test("Must get a specific class the application", async () => {
2424
const tradeDirectObject = await daytraderJavaAnalysis.getClassByQualifiedName("com.ibm.websphere.samples.daytrader.impl.direct.TradeDirect");
25-
expect(async () => JType.parse(tradeDirectObject)).not.toThrow();
25+
expect(async () => JType.parse(tradeDirectObject)).not.toThrow();
2626
});
2727

2828
test("Must throw error when a requested class in the application does not exist", async () => {
@@ -32,7 +32,7 @@ test("Must throw error when a requested class in the application does not exist"
3232
* by saying "Hey, I expect this promise to be rejected with this error ..."
3333
*/
3434
await expect(daytraderJavaAnalysis.getClassByQualifiedName("this.class.does.not.Exist")).rejects.toThrow(
35-
"Class this.class.does.not.Exist not found in the application.");
35+
"Class this.class.does.not.Exist not found in the application.");
3636
});
3737

3838
test("Must get all methods in the application", () => {
@@ -41,3 +41,9 @@ test("Must get all methods in the application", () => {
4141
});
4242
});
4343

44+
test("Must get all methods in a specific class in the application", async () => {
45+
expect(
46+
(
47+
await daytraderJavaAnalysis.getAllMethodsByClass("com.ibm.websphere.samples.daytrader.impl.direct.TradeDirect")).length
48+
).toBeGreaterThan(0)
49+
});

0 commit comments

Comments
 (0)