Skip to content

Commit

Permalink
feat: Improved performance in analyzer_java_spring.cr by caching file…
Browse files Browse the repository at this point in the history
… read data

Signed-off-by: ksg <ksg97031@gmail.com>
  • Loading branch information
ksg97031 committed May 2, 2024
1 parent bb2055e commit 7033f0f
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/analyzer/analyzers/analyzer_java_spring.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require "../../miniparsers/java"
class AnalyzerJavaSpring < Analyzer
REGEX_ROUTER_CODE_BLOCK = /route\(\)?.*?\);/m
REGEX_ROUTE_CODE_LINE = /((?:andRoute|route)\s*\(|\.)\s*(GET|POST|DELETE|PUT)\(\s*"([^"]*)/
FILE_CONTENT_CACHE = Hash(String, String).new

def analyze
parser_map = Hash(String, JavaParser).new
Expand All @@ -31,6 +32,7 @@ class AnalyzerJavaSpring < Analyzer
if File.exists?(path) && path.ends_with?(".java")
webflux_base_path = find_base_path(path, webflux_base_path_map)
content = File.read(path, encoding: "utf-8", invalid: :skip)
FILE_CONTENT_CACHE[path] = content

# Spring MVC Router (Controller)
spring_web_bind_package = "org.springframework.web.bind.annotation."
Expand Down Expand Up @@ -75,8 +77,7 @@ class AnalyzerJavaSpring < Analyzer
source_path = root_source_directory.join(import_path + ".java")
next if source_path.dirname == package_directory || !File.exists?(source_path)
if !parser_map.has_key?(source_path.to_s)
_content = File.read(source_path.to_s, encoding: "utf-8", invalid: :skip)
_parser = get_parser(source_path, _content)
_parser = get_parser(source_path)
parser_map[source_path.to_s] = _parser
_parser.classes.each do |package_class|
import_map[package_class.name] = package_class
Expand Down Expand Up @@ -243,6 +244,21 @@ class AnalyzerJavaSpring < Analyzer
@result
end

def get_parser(path : Path, content : String = "")
if content == ""
if FILE_CONTENT_CACHE.has_key?(path.to_s)
content = FILE_CONTENT_CACHE[path.to_s]
else
content = File.read(path, encoding: "utf-8", invalid: :skip)
end
end

lexer = JavaLexer.new
tokens = lexer.tokenize(content)
parser = JavaParser.new(path.to_s, tokens)
parser
end

def find_base_path(current_path : String, base_paths : Hash(String, String))
base_paths.keys.sort_by!(&.size).reverse!.each do |path|
if current_path.starts_with?(path)
Expand Down Expand Up @@ -407,16 +423,6 @@ class AnalyzerJavaSpring < Analyzer
end
end

def get_parser(path : Path, content : String = "")
if content == ""
content = File.read(path, encoding: "utf-8", invalid: :skip)
end
lexer = JavaLexer.new
tokens = lexer.tokenize(content)
parser = JavaParser.new(path.to_s, tokens)
parser
end

def analyzer_java_spring(options : Hash(Symbol, String))
instance = AnalyzerJavaSpring.new(options)
instance.analyze
Expand Down

0 comments on commit 7033f0f

Please sign in to comment.