23
23
from pdb import set_trace
24
24
from pydantic import BaseModel , field_validator , model_validator
25
25
26
- from .constants_namespace import ConstantsNamespace
27
-
28
- constants = ConstantsNamespace ()
29
- context_concrete_class = ContextVar ("context_concrete_class" ) # context var to store class concreteness
30
26
_CALLABLES_LOOKUP_TABLE = dict ()
31
27
32
28
@@ -179,6 +175,7 @@ class JCallable(BaseModel):
179
175
referenced_types : List [str ]
180
176
accessed_fields : List [str ]
181
177
call_sites : List [JCallSite ]
178
+ is_entrypoint : bool = False
182
179
variable_declarations : List [JVariableDeclaration ]
183
180
cyclomatic_complexity : int | None
184
181
@@ -188,33 +185,6 @@ def __hash__(self):
188
185
"""
189
186
return hash (self .declaration )
190
187
191
- @model_validator (mode = "after" )
192
- def detect_entrypoint_method (self ):
193
- # check first if the class in which this method exists is concrete or not, by looking at the context var
194
- if context_concrete_class .get ():
195
- # convert annotations to the form GET, POST even if they are @GET or @GET('/ID') etc.
196
- annotations_cleaned = [match for annotation in self .annotations for match in re .findall (r"@(.*?)(?:\(|$)" , annotation )]
197
-
198
- param_type_list = [val .type for val in self .parameters ]
199
- # check the param types against known servlet param types
200
- if any (substring in string for substring in param_type_list for string in constants .ENTRY_POINT_METHOD_SERVLET_PARAM_TYPES ):
201
- # check if this method is over-riding (only methods that override doGet / doPost etc. will be flagged as first level entry points)
202
- if "Override" in annotations_cleaned :
203
- self .is_entry_point = True
204
- return self
205
-
206
- # now check the cleaned annotations against known javax ws annotations
207
- if any (substring in string for substring in annotations_cleaned for string in constants .ENTRY_POINT_METHOD_JAVAX_WS_ANNOTATIONS ):
208
- self .is_entry_point = True
209
- return self
210
-
211
- # check the cleaned annotations against known spring rest method annotations
212
- if any (substring in string for substring in annotations_cleaned for string in constants .ENTRY_POINT_METHOD_SPRING_ANNOTATIONS ):
213
- self .is_entry_point = True
214
- return self
215
- return self
216
-
217
-
218
188
class JType (BaseModel ):
219
189
"""Represents a Java class or interface.
220
190
@@ -257,44 +227,12 @@ class JType(BaseModel):
257
227
modifiers : List [str ] = []
258
228
annotations : List [str ] = []
259
229
parent_type : str
230
+ is_entrypoint_class : bool = False
260
231
nested_type_declerations : List [str ] = []
261
232
callable_declarations : Dict [str , JCallable ] = {}
262
233
field_declarations : List [JField ] = []
263
234
enum_constants : List [JEnumConstant ] = []
264
235
265
- # first get the data in raw form and check if the class is concrete or not, before any model validation is done
266
- # for this we assume if a class is not an interface or abstract it is concrete
267
- # for abstract classes we will check the modifiers
268
- @model_validator (mode = "before" )
269
- def check_concrete_class (cls , values ):
270
- """Detects if the class is concrete based on its properties."""
271
- values ["is_concrete_class" ] = False
272
- if values .get ("is_class_or_interface_declaration" ) and not values .get ("is_interface" ):
273
- if "abstract" not in values .get ("modifiers" ):
274
- values ["is_concrete_class" ] = True
275
- # since the methods in this class need access to the concrete class flag,
276
- # we will store this in a context var - this is a hack
277
- token = context_concrete_class .set (values ["is_concrete_class" ])
278
- return values
279
-
280
- # after model validation is done we populate the is_entry_point flag by checking
281
- # if the class extends or implements known servlet classes
282
- @model_validator (mode = "after" )
283
- def check_concrete_entry_point (self ):
284
- """Detects if the class is entry point based on its properties."""
285
- if self .is_concrete_class :
286
- if any (substring in string for substring in (self .extends_list + self .implements_list ) for string in constants .ENTRY_POINT_SERVLET_CLASSES ):
287
- self .is_entry_point = True
288
- return self
289
- # Handle spring classes
290
- # clean annotations - take out @ and any paranehesis along with info in them.
291
- annotations_cleaned = [match for annotation in self .annotations for match in re .findall (r"@(.*?)(?:\(|$)" , annotation )]
292
- if any (substring in string for substring in annotations_cleaned for string in constants .ENTRY_POINT_CLASS_SPRING_ANNOTATIONS ):
293
- self .is_entry_point = True
294
- return self
295
- # context_concrete.reset()
296
- return self
297
-
298
236
299
237
class JCompilationUnit (BaseModel ):
300
238
"""Represents a compilation unit in Java.
0 commit comments