Skip to content

Commit

Permalink
fix: Refine Java Tokenization and Enhance Spring Controller Logic
Browse files Browse the repository at this point in the history
- Improve Java tokenization in `minilexers` for relational operators and tuples.
- Fix parsing issues in `miniparsers` for better handling of formal parameters.
- Extend `analyzer_java_spring` for improved support of multiple HTTP methods.
- Adjust Java Spring tests and controller logic for updated request handling.
  • Loading branch information
ksg97031 committed Sep 14, 2024
1 parent d06f9a0 commit 26355a6
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*.dwarf
.DS_Store
.vscode
ameba.sh

# Ignore the public directory for Jekyll
/docs/_site/
/docs/.jekyll-cache/
/docs/.jekyll-cache/
12 changes: 12 additions & 0 deletions spec/functional_test/fixtures/java_spring/src/ItemController.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,21 @@ public void deleteItem(@PathVariable Long id) {
public void getItemJson(){
}

@RequestMapping("/requestmap/put", method = RequestMethod.PUT)
public void requestGet(){
}

@RequestMapping("/requestmap/delete",method={RequestMethod.DELETE})
public void requestDelete(){
}

@RequestMapping("/multiple/methods", method = {RequestMethod.GET, RequestMethod.POST})
public void multipleMethods(){
}

@RequestMapping("/multiple/methods2", method = [RequestMethod.GET, RequestMethod.POST])
public void multipleMethods2(){
}
}

class Item {
Expand Down
6 changes: 5 additions & 1 deletion spec/functional_test/testers/java_spring_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ extected_endpoints = [
Endpoint.new("/items", "POST", [Param.new("id", "", "form"), Param.new("name", "", "form")]),
Endpoint.new("/items/update/{id}", "PUT", [Param.new("id", "", "json"), Param.new("name", "", "json")]),
Endpoint.new("/items/delete/{id}", "DELETE"),
Endpoint.new("/items/requestmap/put", "PUT"),
Endpoint.new("/items/requestmap/delete", "DELETE"),
Endpoint.new("/items/multiple/methods", "GET"),
Endpoint.new("/items/multiple/methods", "POST"),
Endpoint.new("/items/multiple/methods2", "GET"),
Endpoint.new("/items/multiple/methods2", "POST"),
Endpoint.new("/greet", "GET", [
Param.new("name", "", "query"),
Param.new("header", "", "header"),
Expand All @@ -34,5 +38,5 @@ extected_endpoints = [

FunctionalTester.new("fixtures/java_spring/", {
:techs => 1,
:endpoints => 19,
:endpoints => extected_endpoints.size,
}, extected_endpoints).test_all
5 changes: 2 additions & 3 deletions src/analyzer/analyzers/analyzer_java_spring.cr
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,11 @@ class AnalyzerJavaSpring < Analyzer
annotation_parameter_key = annotation_parameter_tokens[0].value
annotation_parameter_value = annotation_parameter_tokens[-1].value
if annotation_parameter_key == "method"
if annotation_parameter_value == "}"
if ["}", "]"].includes?(annotation_parameter_value)
# Handle methods declared with multiple HTTP verbs
annotation_parameter_tokens.reverse_each do |token|
break if token.value == "method"
next if token.type == :LBRACE || token.type == :RBRACE
next if token.type == :DOT
next if [:LBRACE, :RBRACE, :LBRACK, :RBRACK, :COMMA, :DOT].includes?(token.type)
http_methods = ["GET", "POST", "PUT", "DELETE", "PATCH"]
if http_methods.includes?(token.value)
request_methods.push(token.value)
Expand Down
8 changes: 6 additions & 2 deletions src/minilexers/java.cr
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,17 @@ class JavaLexer < MiniLexer
when '@' then self << Tuple.new(:AT, "@")
when '{' then self << Tuple.new(:LBRACE, "{")
when '}' then self << Tuple.new(:RBRACE, "}")
when '[' then self << Tuple.new(:LBRACK, "[")
when ']' then self << Tuple.new(:RBRACK, "]")
when '<' then self << Tuple.new(:LT, "<")
when '>' then self << Tuple.new(:GT, ">")
when ';' then self << Tuple.new(:SEMI, ";")
when '='
if @input[@position + 1] == '='
@position += 1
Tuple.new(:EQUAL, "==")
self << Tuple.new(:EQUAL, "==")
else
Tuple.new(:ASSIGN, "=")
self << Tuple.new(:ASSIGN, "=")
end
when '\t' then self << Tuple.new(:TAB, "\t")
when '\n'
Expand Down
52 changes: 26 additions & 26 deletions src/miniparsers/java.cr
Original file line number Diff line number Diff line change
Expand Up @@ -96,56 +96,56 @@ class JavaParser
end

def parse_formal_parameters(tokens : Array(Token), param_start_index : Int32)
lparen_count = 0
rparen_count = 0
lbrace_count = 0
rbrace_count = 0
parameters = Array(Array(Token)).new
parameter_token = Array(Token).new
return parameters if tokens.size <= param_start_index

while param_start_index < tokens.size
if tokens[param_start_index].type == :TAB
param_start_index += 1
elsif tokens[param_start_index].type == :NEWLINE
param_start_index += 1
elsif tokens[param_start_index].type == :LPAREN
lparen_index = param_start_index
while lparen_index < tokens.size
if tokens[lparen_index].type == :TAB
lparen_index += 1
elsif tokens[lparen_index].type == :NEWLINE
lparen_index += 1
elsif tokens[lparen_index].type == :LPAREN
break
else
# No parameters or wrong index was given
return parameters
end
end

cursor = param_start_index
# Parse the formal parameters between ( and )
lparen_count = 0
other_open_count = 0
cursor = lparen_index
parameter_token = Array(Token).new # Add this line to declare the parameter_token variable
while cursor < tokens.size
token = tokens[cursor]
if token.type == :LPAREN
lparen_count += 1
if lparen_count > 1
parameter_token << token
end
elsif token.type == :LBRACE
lbrace_count += 1
elsif token.type == :LBRACE || token.type == :LBRACK || token.type == :LT
other_open_count += 1
parameter_token << token
elsif token.type == :RBRACE
rbrace_count += 1
elsif token.type == :RBRACE || token.type == :RBRACK || token.type == :GT
other_open_count -= 1
parameter_token << token
elsif lbrace_count == rbrace_count && lparen_count - 1 == rparen_count && token.type == :COMMA
elsif token.type == :COMMA && other_open_count == 0 && lparen_count == 1
parameters << parameter_token
parameter_token = Array(Token).new
elsif lparen_count > 0
if token.type == :RPAREN
rparen_count += 1
if lparen_count == rparen_count
else
if token.type != :RPAREN
next if token.type == :TAB || token.type == :NEWLINE # Skip TAB and NEWLINE tokens
parameter_token << token # Add token to the parameter token list
else
lparen_count -= 1
if lparen_count == 0
parameters << parameter_token
break
break # End of the formal parameters
else
parameter_token << token
end
else
unless token.type == :TAB || token.type == :NEWLINE
parameter_token << token
end
end
end

Expand Down

0 comments on commit 26355a6

Please sign in to comment.