Skip to content

Commit 4bc7a3d

Browse files
authored
Merge pull request #47 from IBM/support-slim-jsons-from-codeanalyzer
Version 0.2.0—support slim jsons from codeanalyzer
2 parents c1fe46f + c2f55aa commit 4bc7a3d

File tree

8 files changed

+20085
-52
lines changed

8 files changed

+20085
-52
lines changed

cldk/analysis/java/treesitter/javasitter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from ipdb import set_trace
12
from itertools import groupby
23
from typing import List, Set, Dict
34
from tree_sitter import Language, Node, Parser, Query

cldk/models/java/models.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import re
21
import json
3-
from ipdb import set_trace
4-
from contextvars import ContextVar
2+
import re
53
from typing import Dict, List, Optional
6-
from .constants_namespace import ConstantsNamespace
74
from pydantic import BaseModel, field_validator, model_validator
5+
from contextvars import ContextVar
6+
from .constants_namespace import ConstantsNamespace
87

98
constants = ConstantsNamespace()
109
context_concrete_class = ContextVar("context_concrete_class") # context var to store class concreteness
10+
_CALLABLES_LOOKUP_TABLE = dict()
1111

1212

1313
class JField(BaseModel):
@@ -340,12 +340,13 @@ class JGraphEdges(BaseModel):
340340
@field_validator("source", "target", mode="before")
341341
@classmethod
342342
def validate_source(cls, value) -> JMethodDetail:
343-
callable_dict = json.loads(value)
344-
j_callable = JCallable(**json.loads(callable_dict["callable"])) # parse the value which is a quoted string
345-
class_name = callable_dict["class_interface_declarations"]
343+
file_path, type_declaration, callable_declaration = value["file_path"], value["type_declaration"], value["callable_declaration"]
344+
j_callable = _CALLABLES_LOOKUP_TABLE.get((file_path, type_declaration, callable_declaration), None)
345+
if j_callable is None:
346+
raise ValueError(f"Callable not found in lookup table: {file_path}, {type_declaration}, {callable_declaration}")
347+
class_name = type_declaration
346348
method_decl = j_callable.declaration
347-
mc = JMethodDetail(method_declaration=method_decl, klass=class_name, method=j_callable)
348-
return mc
349+
return JMethodDetail(method_declaration=method_decl, klass=class_name, method=j_callable)
349350

350351
def __hash__(self):
351352
return hash(tuple(self))
@@ -365,3 +366,14 @@ class JApplication(BaseModel):
365366

366367
symbol_table: Dict[str, JCompilationUnit]
367368
system_dependency_graph: List[JGraphEdges] = None
369+
370+
@field_validator("symbol_table", mode="after")
371+
@classmethod
372+
def validate_source(cls, symbol_table):
373+
from ipdb import set_trace
374+
375+
# Populate the lookup table for callables
376+
for file_path, j_compulation_unit in symbol_table.items():
377+
for type_declaration, jtype in j_compulation_unit.type_declarations.items():
378+
for callable_declaration, j_callable in jtype.callable_declarations.items():
379+
_CALLABLES_LOOKUP_TABLE[(file_path, type_declaration, callable_declaration)] = j_callable

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "cldk"
3-
version = "0.1.1"
3+
version = "0.2.0"
44
description = "codellm-devkit: A python library for seamless integration with LLMs."
55
authors = ["Rahul Krishna <[email protected]>", "Rangeet Pan <[email protected]>", "Saurabh Sinhas <[email protected]>",
66
"Raju Pavuluri <[email protected]>"]
@@ -64,5 +64,5 @@ line-length = 180
6464

6565
[tool.cldk.testing]
6666
sample-application = "tests/resources/java/application/"
67-
sample-application-analysis-json = "tests/resources/java/analysis_db"
67+
sample-application-analysis-json = "tests/resources/java/analysis_json/slim"
6868
codeanalyzer-jar-path = "tests/resources/java/codeanalyzer/build/libs/"

tests/conftest.py

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,43 @@ def analysis_json_fixture():
1717
return config["tool"]["cldk"]["testing"]["sample-application-analysis-json"]
1818

1919

20-
@pytest.fixture(scope="session", autouse=True)
21-
def test_fixture():
22-
"""
23-
Returns the path to the test data directory.
24-
25-
Yields:
26-
Path : The path to the test data directory.
27-
"""
28-
# ----------------------------------[ SETUP ]----------------------------------
29-
# Path to your pyproject.toml
30-
pyproject_path = Path(__file__).parent.parent / "pyproject.toml"
31-
32-
# Load the configuration
33-
config = toml.load(pyproject_path)
34-
35-
# Access the test data path
36-
test_data_path = config["tool"]["cldk"]["testing"]["sample-application"]
37-
38-
if not Path(test_data_path).exists():
39-
Path(test_data_path).mkdir(parents=True)
40-
url = "https://github.com/OpenLiberty/sample.daytrader8/archive/refs/tags/v1.2.zip"
41-
filename = Path(test_data_path).absolute() / "v1.2.zip"
42-
urlretrieve(url, filename)
43-
44-
# Extract the zip file to the test data path
45-
with zipfile.ZipFile(filename, "r") as zip_ref:
46-
zip_ref.extractall(test_data_path)
47-
48-
# Remove the zip file
49-
filename.unlink()
50-
# --------------------------------------------------------------------------------
51-
# Daytrader8 sample application path
52-
yield Path(test_data_path) / "sample.daytrader8-1.2"
53-
54-
# -----------------------------------[ TEARDOWN ]----------------------------------
55-
# Remove the daytrader8 sample application that was downloaded for testing
56-
for directory in Path(test_data_path).iterdir():
57-
if directory.exists() and directory.is_dir():
58-
shutil.rmtree(directory)
59-
# ---------------------------------------------------------------------------------
20+
# @pytest.fixture(scope="session", autouse=True)
21+
# def test_fixture():
22+
# """
23+
# Returns the path to the test data directory.
24+
25+
# Yields:
26+
# Path : The path to the test data directory.
27+
# """
28+
# # ----------------------------------[ SETUP ]----------------------------------
29+
# # Path to your pyproject.toml
30+
# pyproject_path = Path(__file__).parent.parent / "pyproject.toml"
31+
32+
# # Load the configuration
33+
# config = toml.load(pyproject_path)
34+
35+
# # Access the test data path
36+
# test_data_path = config["tool"]["cldk"]["testing"]["sample-application"]
37+
38+
# if not Path(test_data_path).exists():
39+
# Path(test_data_path).mkdir(parents=True)
40+
# url = "https://github.com/OpenLiberty/sample.daytrader8/archive/refs/tags/v1.2.zip"
41+
# filename = Path(test_data_path).absolute() / "v1.2.zip"
42+
# urlretrieve(url, filename)
43+
44+
# # Extract the zip file to the test data path
45+
# with zipfile.ZipFile(filename, "r") as zip_ref:
46+
# zip_ref.extractall(test_data_path)
47+
48+
# # Remove the zip file
49+
# filename.unlink()
50+
# # --------------------------------------------------------------------------------
51+
# # Daytrader8 sample application path
52+
# yield Path(test_data_path) / "sample.daytrader8-1.2"
53+
54+
# # -----------------------------------[ TEARDOWN ]----------------------------------
55+
# # Remove the daytrader8 sample application that was downloaded for testing
56+
# for directory in Path(test_data_path).iterdir():
57+
# if directory.exists() and directory.is_dir():
58+
# shutil.rmtree(directory)
59+
# # ---------------------------------------------------------------------------------
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
from ipdb import set_trace
12
from typing import List, Tuple
23
from cldk import CLDK
34

45

56
def test_get_class_call_graph(analysis_json_fixture):
67
# Initialize the CLDK object with the project directory, language, and analysis_backend.
78
cldk = CLDK(language="java")
8-
99
analysis = cldk.analysis(
1010
project_path=analysis_json_fixture, analysis_backend="codeanalyzer", analysis_json_path=analysis_json_fixture, eager=False, analysis_level="call-graph"
1111
)
12+
assert analysis.get_call_graph_json() is not None
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!*.json

tests/resources/java/analysis_json/fat/analysis.json

Lines changed: 20017 additions & 0 deletions
Large diffs are not rendered by default.

tests/resources/java/analysis_json/slim/analysis.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)