Skip to content

Commit

Permalink
refactor: Update comments, remove unused rule
Browse files Browse the repository at this point in the history
  • Loading branch information
lyxell committed Jul 24, 2022
1 parent 4a7d14a commit 831a273
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 83 deletions.
1 change: 0 additions & 1 deletion src/program.dl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "rules/simplify_calls_to_string_substring/implementation.dl"
#include "rules/simplify_calls_to_substring_and_startswith/implementation.dl"
#include "rules/simplify_code_using_collection_isempty/implementation.dl"
//#include "rules/simplify_code_using_lambda_expressions/implementation.dl"
#include "rules/simplify_code_using_map_computeifabsent/implementation.dl"
#include "rules/simplify_code_using_method_references/implementation.dl"
#include "rules/simplify_code_using_streams/implementation.dl"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
- [:stream].forEach([:coll]::add)
+ [:coll].addAll([:stream].collect(java.util.stream.Collectors.toList()))
*/
replace_node_with_string("fix_inefficient_calls_to_foreach_list_add", inv, cat(
@node_to_string(code, ref_subject), ".addAll(", @node_to_string(code, inv_subject), ".collect(java.util.stream.Collectors.toList()))"
)) :-
Expand Down
23 changes: 14 additions & 9 deletions src/rules/fix_inefficient_map_access/implementation.dl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* Introduce entry by iterating over entrySet instead of keySet.
*/
/*
- for ([:param] : [:map_var].keySet()) {
+ for (java.util.Map.Entry[:type_args] entry : [:map_var].entrySet()) {
+ [:param] = entry.getKey();
*/
rewrite("fix_inefficient_map_access", filename, start, end + 1,
cat("for (java.util.Map.Entry", @type_args_to_string(type_args), " entry : ", @node_to_string(code, map_reference), ".entrySet()) {\n",
@node_to_string(code, param), " = entry.getKey();")
Expand Down Expand Up @@ -32,9 +34,11 @@ rewrite("fix_inefficient_map_access", filename, start, end + 1,
map_get_end <= body_end,
point_of_declaration(key, param).

/**
* Use entry.getValue() instead of map.get(key) if available.
*/
/*
- [:map_var].get([:key])
+ entry.getValue()
where key is assigned with the value entry.getKey()
*/
replace_node_with_string("fix_inefficient_map_access", map_get, cat(@node_to_string(code, formal_param_id), ".getValue()")) :-

enhanced_for_statement(_, formal_param, expression, body),
Expand Down Expand Up @@ -67,9 +71,10 @@ replace_node_with_string("fix_inefficient_map_access", map_get, cat(@node_to_str
point_of_declaration(map_get_subject, decl),
point_of_declaration(key, declaration).

/**
* Use entry.getValue() instead of map.get(entry.getKey()) if available.
*/
/*
- [:map_var].get([:entry].getKey())
+ [:entry].getValue()
*/
replace_node_with_string("fix_inefficient_map_access", map_get, cat(@node_to_string(code, get_key_subject), ".getValue()")) :-

enhanced_for_statement(_, formal_param, expression, body),
Expand Down
14 changes: 14 additions & 0 deletions src/rules/fix_raw_use_of_empty_collections/implementation.dl
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
/*
- Collections.EMPTY_LIST
+ Collections.emptyList()
*/
replace_node_with_string("fix_raw_use_of_empty_collections", id, "Collections.emptyList()") :-
expression_name(id, [id1, [id2, nil]]),
identifier(id1, "Collections"),
identifier(id2, "EMPTY_LIST").

/*
- Collections.EMPTY_MAP
+ Collections.emptyMap()
*/
replace_node_with_string("fix_raw_use_of_empty_collections", id, "Collections.emptyMap()") :-
expression_name(id, [id1, [id2, nil]]),
identifier(id1, "Collections"),
identifier(id2, "EMPTY_MAP").

/*
- Collections.EMPTY_SET
+ Collections.emptySet()
*/
replace_node_with_string("fix_raw_use_of_empty_collections", id, "Collections.emptySet()") :-
expression_name(id, [id1, [id2, nil]]),
identifier(id1, "Collections"),
Expand Down
12 changes: 8 additions & 4 deletions src/rules/fix_raw_use_of_generic_class/implementation.dl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ field_or_local_variable_declaration(id, mods, type, declarators) :-
field_or_local_variable_declaration(id, mods, type, declarators) :-
local_variable_declaration(id, mods, type, declarators).

/* Pre [:left_type]<[:left_type_parameters]> [:left] = new [:right_type](); */
/* Post [:left_type]<[:left_type_parameters]> [:left] = new [:right_type]<>(); */
/*
- [:left_type]<[:left_type_parameters]> [:left] = new [:right_type]();
+ [:left_type]<[:left_type_parameters]> [:left] = new [:right_type]<>();
*/
replace_node_with_string("fix_raw_use_of_generic_class", right_type, cat(@node_to_string(code, right_type), "<>")) :-
field_or_local_variable_declaration(_, _, left_type, declarators),
ast_type_to_type(left_type, [left_package, left_class, left_type_args]),
Expand All @@ -18,8 +20,10 @@ replace_node_with_string("fix_raw_use_of_generic_class", right_type, cat(@node_t
ast_type_to_type(right_type, [right_package, right_class, nil]),
(collection_type(right_package, right_class); map_type(right_package, right_class)).

/* Pre Type [:var] = [:it]; */
/* Post Type<[:type]> [:var] = [:it]; */
/*
- [:type] [:var] = [:it]; */
+ [:type]<[:type_args]> [:var] = [:it];
*/
replace_node_with_string("fix_raw_use_of_generic_class", left_type, @type_to_string([package, class, type_args])) :-
local_variable_declaration(_, _, left_type, [declarator, nil]),
ast_type_to_type(left_type, [package, class, nil]),
Expand Down
12 changes: 8 additions & 4 deletions src/rules/remove_empty_finally_blocks/implementation.dl
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
/* Pre try [:body] [:catches] finally {} */
/* Post try [:body] [:catches] */
/*
- try [:body] [:catches] finally {}
+ try [:body] [:catches]
*/
replace_node_with_string("remove_empty_finally_blocks", finally, "") :-
try_statement(_, _, catches, finally),
catches != nil,
finally_block(finally, block),
block(block, nil).

/* Pre try ([:resources]) [:body] [:catches] finally {} */
/* Post try ([:resources]) [:body] [:catches] */
/*
- try ([:resources]) [:body] [:catches] finally {}
+ try ([:resources]) [:body] [:catches]
*/
replace_node_with_string("remove_empty_finally_blocks", finally, "") :-
try_with_resources_statement(_, _, _, catches, finally),
catches != nil,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* Pre [:lhs] [:id] = [:initializer] */
/* return [:id]; */

/* Post return [:initializer]; */
/*
- [:type] [:id] = [:initializer];
- return [:id];
+ return [:initializer];
*/
rewrite("remove_unnecessary_declarations_above_return_statements", filename, start, end,
cat("return ", @node_to_string(code, initializer), ";")
) :-
Expand Down
7 changes: 3 additions & 4 deletions src/rules/remove_unused_local_variables/implementation.dl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

/* Pre [:local_var_decl] */
/* Post */
/*
- [:local_var_decl];
*/
replace_node_with_string("remove_unused_local_variables", id, "") :-
local_variable_declaration_statement(id, declaration),
local_variable_declaration(declaration, _, _, [declarator, nil]),
variable_declarator(declarator, _, initializer),
is_side_effect_free_expression(initializer),
! point_of_declaration(_, declaration).

20 changes: 18 additions & 2 deletions src/rules/remove_use_of_fully_qualified_names/implementation.dl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ imports_start_at(start) :-
ordinary_compilation_unit(_, _, nil, [t_decl, _]),
starts_at(t_decl, start).

/* Auto-import java.util.stream.Collectors */
/* Used by simplify_code_using_streams */

/*
+ import java.util.stream.Collectors;
*/
rewrite("remove_use_of_fully_qualified_names", filename, start, start, "import java.util.stream.Collectors;\n") :-
expression_name(expr_name, _),
filename_of(expr_name, filename),
Expand All @@ -16,15 +19,23 @@ rewrite("remove_use_of_fully_qualified_names", filename, start, start, "import j
!import_specification(_, "java.util.stream", "Collectors"),
!import_specification(_, "java.util.stream", "*"),
@node_to_string(code, expr_name) = "java.util.stream.Collectors".

/*
- java.util.stream.Collectors
+ Collectors
*/
replace_node_with_string("remove_use_of_fully_qualified_names", expr_name, "Collectors") :-
expression_name(expr_name, _),
source_code(_, code),
(import_specification(_, "java.util.stream", "Collectors")
;import_specification(_, "java.util.stream", "*")),
@node_to_string(code, expr_name) = "java.util.stream.Collectors".

/* Auto-import java.util.Map */
/* Used by fix_inefficient_map_access */

/*
+ import java.util.Map;
*/
rewrite("remove_use_of_fully_qualified_names", filename, start, start, "import java.util.Map;\n") :-
class_type(class_type, _, _, _, _),
filename_of(class_type, filename),
Expand All @@ -33,6 +44,11 @@ rewrite("remove_use_of_fully_qualified_names", filename, start, start, "import j
!import_specification(_, "java.util", "Map"),
!import_specification(_, "java.util", "*"),
@node_to_string(code, class_type) = "java.util.Map".

/*
- java.util.Map
+ Map
*/
replace_node_with_string("remove_use_of_fully_qualified_names", class_type, "Map") :-
class_type(class_type, _, _, _, _),
source_code(_, code),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* Pre [:collection].removeAll([:collection]) */
/* Post [:collection].clear() */
/*
- [:collection].removeAll([:collection])
+ [:collection].clear()
*/
replace_node_with_string("simplify_calls_to_collection_removeall", id,
cat(@node_to_string(code, collection), ".clear()")
) :-
Expand Down
10 changes: 0 additions & 10 deletions src/rules/simplify_code_using_lambda_expressions/data.json

This file was deleted.

25 changes: 0 additions & 25 deletions src/rules/simplify_code_using_lambda_expressions/implementation.dl

This file was deleted.

34 changes: 16 additions & 18 deletions src/rules/simplify_code_using_map_computeifabsent/implementation.dl
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/*

- if (!map.containsKey(key)) {
- map.put(key, value);
- }
+ map.computeIfAbsent(key, k -> value);

- if (![:map_var].containsKey([:key_var])) {
- [:map_var].put([:key_var], [:value_var]);
- }
+ [:map_var].computeIfAbsent([:key_var], k -> [:value_var]);
*/
replace_node_with_string("simplify_code_using_map_computeifabsent", id,
cat(@node_to_string(code, contains_key_object), ".computeIfAbsent(", @node_to_string(code, contains_key_arg), ", k -> ", @node_to_string(code, put_arg2), ");")
Expand All @@ -21,18 +19,18 @@ replace_node_with_string("simplify_code_using_map_computeifabsent", id,
point_of_declaration(contains_key_arg, key_decl),
point_of_declaration(put_arg1, key_decl).

/* Pre [:value_type] [:value_var] = [:map_var].get([:key_var]);
if ([:value_var] == null) {
[:value_var] = [:value_initializer];
[:map_var].put([:key_var], [:value_var]);
[:trailing_statements]
}

Post [:value_type] [:value_var] = [:map_var].computeIfAbsent([:key_var], k -> {
[:value_type] [:value_var] = [:value_initializer];
[:trailing_statements]
return [:value_var];
});
/*
- [:value_type] [:value_var] = [:map_var].get([:key_var]);
- if ([:value_var] == null) {
- [:value_var] = [:value_initializer];
- [:map_var].put([:key_var], [:value_var]);
- [:trailing_statements]
- }
+ [:value_type] [:value_var] = [:map_var].computeIfAbsent([:key_var], k -> {
+ [:value_type] [:value_var] = [:value_initializer];
+ [:trailing_statements]
+ return [:value_var];
+ });
*/

rewrite("simplify_code_using_map_computeifabsent", filename, start, end, cat(
Expand Down

0 comments on commit 831a273

Please sign in to comment.