[Piranha Generic] Incomplete Enum cleanup compared to the behavior with PiranhaJava
See original GitHub issuePiranhaJava basically removes not only the treated part, but it also removes the enum values which are now going to be obsolete and have no usages.
But I don’t see the same behaviour when working with Piranha Generic
.
Here’s an example
Original Java class
class SampleJava {
public void sampleMethod(ExperimentInterface exp) {
if (exp.isToggleEnabled(MyEnum.SAMPLE_STALE_FLAG)) {
System.out.println("SAMPLE_STALE_FLAG is enabled");
} else {
System.out.println("SAMPLE_STALE_FLAG is disabled");
}
}
public void sampleMethod1(ExperimentInterface exp) {
if (exp.isToggleDisabled(ExpEnum.SAMPLE_STALE_FLAG)) {
System.out.println("SAMPLE_STALE_FLAG is disabled");
} else {
System.out.println("SAMPLE_STALE_FLAG is enabled");
}
}
public enum MyEnum{
SAMPLE_STALE_FLAG,
DRAGON_WARRIOR
}
}
Now with the below arguments and configuration file, when I run the piranha cli, I notice the output class looks like below
Altered Java class by Piranha
class SampleJava {
public void sampleMethod(ExperimentInterface exp) {
System.out.println("SAMPLE_STALE_FLAG is enabled");
}
public void sampleMethod1(ExperimentInterface exp) {
if (exp.isToggleDisabled(ExpEnum.SAMPLE_STALE_FLAG)) {
System.out.println("SAMPLE_STALE_FLAG is disabled");
} else {
System.out.println("SAMPLE_STALE_FLAG is enabled");
}
}
public enum MyEnum{
SAMPLE_STALE_FLAG,
DRAGON_WARRIOR
}
}
The java based piranha was removing the stale entry in my enum MyEnum
, but piranha generic doesn’t do that. It only removes the enum reference from the method, but not from the actual enum as well.
piranha_arguments.toml
language = ["java"]
substitutions = [
["stale_flag_name", "MyEnum.SAMPLE_STALE_FLAG"],
["treated", "true"],
["treated_complement", "false"],
]
rules.toml
[[rules]]
name = "replace_isToggleEnabled_with_boolean_literal"
query = """((
(method_invocation
name : (_) @name
arguments: ((argument_list
([
(field_access field: (_)@argument)
(_) @argument
])) )
) @method_invocation
)
(#eq? @name "isToggleEnabled")
(#eq? @argument "@stale_flag_name")
)"""
replace_node = "method_invocation"
replace = "@treated"
groups = [ "replace_expression_with_boolean_literal"]
holes = ["treated", "stale_flag_name"]
#
# For @stale_flag_name = STALE_FLAG and @treated = true
# Before :
# exp.isToggleDisabled(STALE_FLAG)
# After :
# false
#
[[rules]]
name = "replace_isToggleDisabled_with_boolean_literal"
query = """((
(method_invocation
name : (_) @name
arguments: ((argument_list
([
(field_access field: (_)@argument)
(_) @argument
])) )
) @method_invocation
)
(#eq? @name "isToggleDisabled")
(#eq? @argument "@stale_flag_name")
)"""
replace_node = "method_invocation"
replace = "@treated_complement"
groups = [ "replace_expression_with_boolean_literal"]
holes = ["treated_complement", "stale_flag_name"]
The other difference I noticed was that I now had to give the qualified name of the enum
["stale_flag_name", "MyEnum.SAMPLE_STALE_FLAG"],
in my configuration, which wasn’t the case when dealing with PiranhaJava
Issue Analytics
- State:
- Created a year ago
- Comments:19 (1 by maintainers)
Top GitHub Comments
Btw @krmahadevan binaries are now available 😃
Awesome. I am happy it worked for you. I will close this issue once I add support for removing stale import statements.