question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Piranha Generic] Incomplete Enum cleanup compared to the behavior with PiranhaJava

See original GitHub issue

PiranhaJava 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:closed
  • Created a year ago
  • Comments:19 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ketkarameyacommented, Jun 22, 2022

Btw @krmahadevan binaries are now available 😃

1reaction
ketkarameyacommented, Jun 16, 2022

Awesome. I am happy it worked for you. I will close this issue once I add support for removing stale import statements.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[PiranhaJava] Does not remove Java enum constants ... - GitHub
Running Piranha with -XepOpt:Piranha:FlagName=stale.flag won't remove the STALE_FLAG constant from the enum (and therefore will also not ...
Read more >
Introducing Piranha: An Open Source Tool to Automatically ...
Seeking to automate this process, we developed Piranha, a tool that scans source code to delete code related to stale, or obsolete, ...
Read more >
Add support for Microhttp - piranhacloud/piranha - Devscope.io
piranha [Piranha Generic] Incomplete Enum cleanup compared to the behavior with PiranhaJava 19 closed 🗓️ 3 months ago. piranha [Piranha Generic]Need ...
Read more >
Uber Piranha Statistics & Issues - Codesti
[Piranha Generic] Incomplete Enum cleanup compared to the behavior with PiranhaJava, closed, 19, 2022-06-07, 2022-11-28, 2022-06-22.
Read more >
Datadownload toolbox error - QuantConnect/Lean - IssueHint
[Piranha Generic] Incomplete Enum cleanup compared to the behavior with PiranhaJava, 19, 2022-06-07, 2022-10-18. Open Roberta Lab Hangs, 10, 2018-05-28 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found